summaryrefslogtreecommitdiffstats
path: root/tools/ocft/caselib.in
blob: 44165d6f1b4639abb964e8f656e2a9004828c65b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#
# Copyright (c) 2010-2011 Novell Inc, John Shi
#           All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.


quit()
{
  local ret
  ret="$1"

  while [ $__OCFT__atexit_num -gt 0 ]; do
    atexit$__OCFT__atexit_num
    let __OCFT__atexit_num--
  done

  rm -rf $__OCFT__fakebin

  exit $ret
}

agent_install()
{
  local pkg

  if [ $# -eq 0 ]; then
    return 0
  fi

  for pkg in "$@"; do
    if [ -e /etc/SuSE-release ]; then
      if ! zypper -q install -y "$pkg" >/dev/null 2>&1; then
        echo
        echo "${__OCFT__showhost}ERROR: Install '$pkg' failed."
        quit 2
      fi
    elif [ -e /etc/debian_version ]; then
      if ! apt-get -y install "$pkg" >/dev/null 2>&1; then
        echo
        echo "${__OCFT__showhost}ERROR: Install '$pkg' failed."
        quit 2
      fi
    elif [ -e /etc/redhat-release ]; then
      if ! yum -y install "$pkg" >/dev/null 2>&1; then
        echo
        echo "${__OCFT__showhost}ERROR: Install '$pkg' failed."
        quit 2
      fi
    else
      echo "${__OCFT__showhost}ERROR: Cannot detect your OS type."
      quit 2
    fi
  done
}

set_ocf_env()
{
  export OCF_RA_VERSION_MAJOR=1
  export OCF_RA_VERSION_MINOR=0
  export OCF_RESOURCE_TYPE=$1
  export OCF_RESOURCE_INSTANCE=${OCF_RESOURCE_INSTANCE:-"ocft"}
}

agent_run()
{
  local agent cmd timeout pid i ret aroot
  agent="$1"
  cmd="$2"
  timeout="$3"

  set_ocf_env $agent

  export OCF_RESKEY_CRM_meta_timeout
  : ${OCF_RESKEY_CRM_meta_timeout:=$((timeout*1000))}

  aroot=${__OCFT__MYROOT:-$__OCFT__AGENT_ROOT}

  setsid $aroot/$agent $cmd >${HA_RSCTMP}/.ocft_runlog 2>&1 &
  pid=$!

  i=0
  while [ $i -lt $timeout ]; do
    if [ ! -e /proc/$pid ]; then
      break
    fi
    sleep 1
    let i++
  done

  if [ $i -ge $timeout ]; then
    kill -SIGTERM -$pid >/dev/null 2>&1
    sleep 3
    kill -SIGKILL -$pid >/dev/null 2>&1
    echo -n "${__OCFT__showhost}ERROR: The agent was hanging, killed it, "
    echo "maybe you damaged the agent or system's environment, see details below:"
    cat ${HA_RSCTMP}/.ocft_runlog
    echo
    quit 1
  fi

  wait $pid
}

check_success()
{
  local ret msg
  ret="$1"
  msg="$2"

  if [ $ret -ne 0 ]; then
    echo "${__OCFT__showhost}ERROR: '${msg}' failed, the return code is ${ret}."
    quit 1
  fi
}

__maxfd()
{
  (echo 0; ls -1 /proc/$$/fd) | sort -rn | head -1
}

__getfd()
{
  local host rw fd file
  host="$1"
  rw="$2"

  for fd in /proc/$$/fd/*; do
    file=$(basename "$(readlink $fd)")
    if [ "$file" = "${host}_$rw" ]; then
      basename $fd
      break
    fi
  done
}

backbash_start()
{
  local host fd rfd wfd
  host="$1"

  if [ ! -d "$__OCFT__CASES_DIR" ]; then
    echo "${__OCFT__showhost}ERROR: Could not found Directory: ${__OCFT__CASES_DIR}."
    quit 1
  fi

  if lsof $__OCFT__CASES_DIR/${host}_r $__OCFT__CASES_DIR/${host}_w >/dev/null 2>&1; then
    echo "${__OCFT__showhost}ERROR: Connection exist with $host."
    quit 1
  fi
  if [ ! -p "$__OCFT__CASES_DIR/${host}_r" ] || [ ! -p "$__OCFT__CASES_DIR/${host}_w" ]; then
    rm -f $__OCFT__CASES_DIR/${host}_r $__OCFT__CASES_DIR/${host}_w
    if ! mkfifo $__OCFT__CASES_DIR/${host}_r $__OCFT__CASES_DIR/${host}_w >/dev/null 2>&1; then
      echo "${__OCFT__showhost}ERROR: Could not create pipe file: $__OCFT__CASES_DIR/${host}_*."
      quit 1
    fi
  fi

  ssh root@$host '@BASH_SHELL@ 2>&1
                  sed "s/00/001/g" ${HA_RSCTMP}/.backbash-log
                  echo 000
                  echo 1' >$__OCFT__CASES_DIR/${host}_r <$__OCFT__CASES_DIR/${host}_w &

  fd=$(__maxfd)
  rfd=$(expr $fd + 1)
  wfd=$(expr $fd + 2)
  eval "exec ${rfd}<$__OCFT__CASES_DIR/${host}_r ${wfd}>$__OCFT__CASES_DIR/${host}_w"
}

backbash()
{
  local host rfd wfd ret
  host="$1"

  rfd=$(__getfd $host r)
  wfd=$(__getfd $host w)

  if [ -z "$rfd" -o -z "$wfd" ]; then
    echo "${__OCFT__showhost}ERROR: Could not found connection with $host."
  fi

  cat >&$wfd <<EOF
{
true
EOF
  cat >&$wfd
  cat >&$wfd <<EOF

} >&${HA_RSCTMP}/.backbash-log
sed 's/00/001/g' ${HA_RSCTMP}/.backbash-log
echo 000
echo 0
EOF
  if [ $? -ne 0 ]; then
    echo "${__OCFT__showhost}ERROR: Broken connection with $host."
    quit 1
  fi

  awk -vlive=2 '{
    if (sub(/000$/, "")) {
      if ($0 != "") {
        gsub("001", "00");
        printf("%s", $0);
      }
      getline live;
      exit;
    }
    gsub("001", "00");
    print;
  } END {
    exit(live);
  }' <&$rfd
  case $? in
      1)
        quit 1
        ;;
      2)
        echo "${__OCFT__showhost}ERROR: Broken connection with $host."
        quit 1
        ;;
  esac
}

backbash_stop()
{
  local host rfd wfd
  host="$1"

  wfd=$(__getfd $host w)
  if [ -n "$wfd" ]; then
    cat >&$wfd <<<'quit 0'
  fi
  rm -f $__OCFT__CASES_DIR/${host}_r $__OCFT__CASES_DIR/${host}_w
}


export OCF_ROOT=@OCF_ROOT_DIR@
export OCF_LIB=@OCF_LIB_DIR@/heartbeat
__OCFT__AGENT_ROOT=@OCF_RA_DIR@/heartbeat
__OCFT__CASES_DIR=/var/lib/@PACKAGE_NAME@/ocft/cases
OCFT_DIR=@datadir@/@PACKAGE_NAME@/ocft

. $OCFT_DIR/helpers.sh

__OCFT__atexit_num=0

if [ $EUID -ne 0 ]; then
  echo "${__OCFT__showhost}ERROR: '$0' needs to be run by root."
  quit 3
fi

__OCFT__fakebin=./fakebin

mkdir -p $__OCFT__fakebin >/dev/null 2>&1 &&
ln -sf /bin/true $__OCFT__fakebin/crm_master >/dev/null 2>&1 &&
ln -sf /bin/true $__OCFT__fakebin/crm_mon >/dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "${__OCFT__showhost}ERROR: initialize 'fakebin' failed."
  quit 3
fi
export HA_SBIN_DIR=$__OCFT__fakebin

. $OCF_LIB/ocf-returncodes || {
  echo "${__OCFT__showhost}ERROR: $OCF_LIB/ocf-returncodes not found."
  quit 3
}

. $OCF_LIB/ocf-directories || {
  echo "${__OCFT__showhost}ERROR: $OCF_LIB/ocf-directories not found."
  quit 3
}

while read __OCFT__line; do
  if [ -n "$__OCFT__line" ]; then
    __OCFT__retn=${__OCFT__line%%=*}
    __OCFT__reti=$(eval echo \$$__OCFT__retn)
    __OCFT__retval[__OCFT__reti]=$__OCFT__retn
  fi
done <<<"$(sed 's/#.*//' $OCF_LIB/ocf-returncodes)"


# vim:ts=2:sw=2:et: