- merge from ronnie
[metze/ctdb/wip.git] / config / functions
1 # utility functions for ctdb event scripts
2
3 #######################################
4 # pull in a system config file, if any
5 loadconfig() {
6     name="$1"
7     if [ -f /etc/sysconfig/$name ]; then
8         . /etc/sysconfig/$name
9     elif [ -f /etc/default/$name ]; then
10         . /etc/default/$name
11     fi
12 }
13
14
15 ######################################################
16 # simulate /sbin/service on platforms that don't have it
17 service() { 
18   service_name="$1"
19   op="$2"
20   if [ -x /sbin/service ]; then
21       /sbin/service "$service_name" "$op"
22   elif [ -x /etc/init.d/$service_name ]; then
23       /etc/init.d/$service_name "$op"
24   elif [ -x /etc/rc.d/init.d/$service_name ]; then
25       /etc/init.d/$service_name "$op"
26   fi
27 }
28
29
30 ######################################################
31 # wait for a command to return a zero exit status
32 # usage: ctdb_wait_command SERVICE_NAME <command>
33 ######################################################
34 ctdb_wait_command() {
35   service_name="$1"
36   wait_cmd="$2"
37   [ -z "$wait_cmd" ] && return;
38   all_ok=0
39   echo "`/bin/date` Waiting for service $service_name to start"
40   while [ $all_ok -eq 0 ]; do
41           $wait_cmd > /dev/null 2>&1 && all_ok=1
42           ctdb status > /dev/null 2>&1 || {
43                 echo "ctdb daemon has died. Exiting wait for $service_name"
44                 exit 1
45           }
46           [ $all_ok -eq 1 ] || sleep 1
47   done
48   echo "`/bin/date` Local service $service_name is up"
49 }
50
51
52 ######################################################
53 # wait for a set of tcp ports
54 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
55 ######################################################
56 ctdb_wait_tcp_ports() {
57   service_name="$1"
58   shift
59   wait_ports="$*"
60   [ -z "$wait_ports" ] && return;
61   all_ok=0
62   echo "`/bin/date` Waiting for tcp service $service_name to start"
63   while [ $all_ok -eq 0 ]; do
64           all_ok=1
65           for p in $wait_ports; do
66               if [ -x /usr/bin/netcat ]; then
67                   /usr/bin/netcat -z 127.0.0.1 $p || all_ok=0
68               elif [ -x /usr/bin/nc ]; then
69                   /usr/bin/nc -z 127.0.0.1 $p || all_ok=0
70               else 
71                   echo "`date` netcat not found - cannot check tcp ports"
72                   return
73               fi
74           done
75           [ $all_ok -eq 1 ] || sleep 1
76           ctdb status > /dev/null 2>&1 || {
77                 echo "ctdb daemon has died. Exiting tcp wait $service_name"
78                 exit 1
79           }
80   done
81   echo "`/bin/date` Local tcp services for $service_name are up"
82 }
83
84
85
86 ######################################################
87 # wait for a set of directories
88 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
89 ######################################################
90 ctdb_wait_directories() {
91   service_name="$1"
92   shift
93   wait_dirs="$*"
94   [ -z "$wait_dirs" ] && return;
95   all_ok=0
96   echo "`/bin/date` Waiting for local directories for $service_name"
97   while [ $all_ok -eq 0 ]; do
98           all_ok=1
99           for d in $wait_dirs; do
100               [ -d $d ] || all_ok=0
101           done
102           [ $all_ok -eq 1 ] || sleep 1
103           ctdb status > /dev/null 2>&1 || {
104                 echo "ctdb daemon has died. Exiting directory wait for $service_name"
105                 exit 1
106           }
107   done
108   echo "`/bin/date` Local directories for $service_name are available"
109 }
110
111
112 ######################################################
113 # check that a rpc server is registered with portmap
114 # and responding to requests
115 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
116 ######################################################
117 ctdb_check_rpc() {
118     service_name="$1"
119     prognum="$2"
120     version="$3"
121     rpcinfo -u localhost $prognum $version > /dev/null || {
122             echo "`date` ERROR: $service_name not responding to rpc requests"
123             exit 1
124     }
125 }
126
127 ######################################################
128 # check a set of directories is available
129 # usage: ctdb_check_directories SERVICE_NAME <directories...>
130 ######################################################
131 ctdb_check_directories() {
132   service_name="$1"
133   shift
134   wait_dirs="$*"
135   [ -z "$wait_dirs" ] && return;
136   for d in $wait_dirs; do
137       [ -d $d ] || {
138           echo "`date` ERROR: $service_name directory $d not available"
139           exit 1
140       }
141   done
142 }
143
144 ######################################################
145 # check a set of tcp ports
146 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
147 ######################################################
148 ctdb_check_tcp_ports() {
149   service_name="$1"
150   shift
151   wait_ports="$*"
152   [ -z "$wait_ports" ] && return;
153   for p in $wait_ports; do
154       all_ok=1
155       if [ -x /usr/bin/netcat ]; then
156           /usr/bin/netcat -z 127.0.0.1 $p || all_ok=0
157       elif [ -x /usr/bin/nc ]; then
158           /usr/bin/nc -z 127.0.0.1 $p || all_ok=0
159       fi
160       [ $all_ok -eq 1 ] || {
161           echo "`date` ERROR: $service_name tcp port $p is not responding"
162           exit 1
163       }
164   done
165 }
166
167 ######################################################
168 # check a command returns zero status
169 # usage: ctdb_check_command SERVICE_NAME <command>
170 ######################################################
171 ctdb_check_command() {
172   service_name="$1"
173   wait_cmd="$2"
174   [ -z "$wait_cmd" ] && return;
175   $wait_cmd > /dev/null 2>&1 || {
176       echo "`date` ERROR: $service_name - $wait_cmd returned error"
177       exit 1
178   }
179 }