merge from tridge
[vlendec/samba-autobuild/.git] / ctdb / 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   done
47   echo "`/bin/date` Local service $service_name is up"
48 }
49
50
51 ######################################################
52 # wait for a set of tcp ports
53 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
54 ######################################################
55 ctdb_wait_tcp_ports() {
56   service_name="$1"
57   shift
58   wait_ports="$*"
59   [ -z "$wait_ports" ] && return;
60   all_ok=0
61   echo "`/bin/date` Waiting for tcp service $service_name to start"
62   while [ $all_ok -eq 0 ]; do
63           all_ok=1
64           for p in $wait_ports; do
65               if [ -x /usr/bin/netcat ]; then
66                   /usr/bin/netcat -z 127.0.0.1 $p || all_ok=0
67               elif [ -x /usr/bin/nc ]; then
68                   /usr/bin/nc -z 127.0.0.1 $p || all_ok=0
69               else 
70                   echo "`date` netcat not found - cannot check tcp ports"
71                   return
72               fi
73           done
74           [ $all_ok -eq 1 ] || sleep 1
75           ctdb status > /dev/null 2>&1 || {
76                 echo "ctdb daemon has died. Exiting tcp wait $service_name"
77                 exit 1
78           }
79   done
80   echo "`/bin/date` Local tcp services for $service_name are up"
81 }
82
83
84
85 ######################################################
86 # wait for a set of directories
87 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
88 ######################################################
89 ctdb_wait_directories() {
90   service_name="$1"
91   shift
92   wait_dirs="$*"
93   [ -z "$wait_dirs" ] && return;
94   all_ok=0
95   echo "`/bin/date` Waiting for local directories for $service_name"
96   while [ $all_ok -eq 0 ]; do
97           all_ok=1
98           for d in $wait_dirs; do
99               [ -d $d ] || all_ok=0
100           done
101           [ $all_ok -eq 1 ] || sleep 1
102           ctdb status > /dev/null 2>&1 || {
103                 echo "ctdb daemon has died. Exiting directory wait for $service_name"
104                 exit 1
105           }
106   done
107   echo "`/bin/date` Local directories for $service_name are available"
108 }
109
110
111 ######################################################
112 # check that a rpc server is registered with portmap
113 # and responding to requests
114 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
115 ######################################################
116 ctdb_check_rpc() {
117     service_name="$1"
118     prognum="$2"
119     version="$3"
120     rpcinfo -u localhost $prognum $version > /dev/null || {
121             echo "`date` ERROR: $service_name not responding to rpc requests"
122             exit 1
123     }
124 }
125
126 ######################################################
127 # check a set of directories is available
128 # usage: ctdb_check_directories SERVICE_NAME <directories...>
129 ######################################################
130 ctdb_check_directories() {
131   service_name="$1"
132   shift
133   wait_dirs="$*"
134   [ -z "$wait_dirs" ] && return;
135   for d in $wait_dirs; do
136       [ -d $d ] || {
137           echo "`date` ERROR: $service_name directory $d not available"
138           exit 1
139       }
140   done
141 }
142
143 ######################################################
144 # check a set of tcp ports
145 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
146 ######################################################
147 ctdb_check_tcp_ports() {
148   service_name="$1"
149   shift
150   wait_ports="$*"
151   [ -z "$wait_ports" ] && return;
152   for p in $wait_ports; do
153       all_ok=1
154       if [ -x /usr/bin/netcat ]; then
155           /usr/bin/netcat -z 127.0.0.1 $p || all_ok=0
156       elif [ -x /usr/bin/nc ]; then
157           /usr/bin/nc -z 127.0.0.1 $p || all_ok=0
158       fi
159       [ $all_ok -eq 1 ] || {
160           echo "`date` ERROR: $service_name tcp port $p is not responding"
161           exit 1
162       }
163   done
164 }
165
166 ######################################################
167 # check a command returns zero status
168 # usage: ctdb_check_command SERVICE_NAME <command>
169 ######################################################
170 ctdb_check_command() {
171   service_name="$1"
172   wait_cmd="$2"
173   [ -z "$wait_cmd" ] && return;
174   $wait_cmd > /dev/null 2>&1 || {
175       echo "`date` ERROR: $service_name - $wait_cmd returned error"
176       exit 1
177   }
178 }