Debian Codegraph @

heartbeat/azure-lb

raw
1 #!/bin/sh
2 #
3
4 # License: GNU General Public License (GPL)
5 # (c) 2017 O. Albrigtsen
6 # and Linux-HA contributors
7 #
8 # -----------------------------------------------------------------------------
9 # O C F R E S O U R C E S C R I P T S P E C I F I C A T I O N
10 # -----------------------------------------------------------------------------
11 #
12 # NAME
13 # azure-lb : OCF resource agent script for Azure Load Balancer
14 #
15 # Initialization:
16 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
17 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
18
19 # Defaults
20 if is_suse_based; then
21 OCF_RESKEY_nc_default="/usr/bin/socat"
22 else
23 OCF_RESKEY_nc_default="/usr/bin/nc"
24 fi
25
26 OCF_RESKEY_port_default="61000"
27 OCF_RESKEY_listen_default="default"
28
29 [ "$(cat /sys/module/ipv6/parameters/disable)" = "1" ] && OCF_RESKEY_listen_default="ipv4only"
30
31 : ${OCF_RESKEY_nc=${OCF_RESKEY_nc_default}}
32 : ${OCF_RESKEY_port=${OCF_RESKEY_port_default}}
33 : ${OCF_RESKEY_listen=${OCF_RESKEY_listen_default}}
34
35 process="$OCF_RESOURCE_INSTANCE"
36 pidfile="/var/run/$OCF_RESOURCE_INSTANCE.pid"
37
38
39 lb_usage() {
40 cat <<END
41 usage: $0 (start|stop|validate-all|meta-data|help|usage|monitor)
42 $0 manages service that answers Azure Load Balancer health probe requests as a OCF HA resource.
43 The 'start' operation starts the instance.
44 The 'stop' operation stops the instance.
45 The 'monitor' operation reports whether the instance seems to be working
46 The 'validate-all' operation reports whether the parameters are valid
47 END
48 }
49
50 lb_metadata() {
51 cat <<END
52 <?xml version="1.0"?>
53 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
54 <resource-agent name="azure-lb" version="1.0">
55 <version>1.0</version>
56 <longdesc lang="en">
57 Resource agent to answer Azure Load Balancer health probe requests
58 </longdesc>
59 <shortdesc lang="en">Answers Azure Load Balancer health probe requests</shortdesc>
60
61 <parameters>
62
63 <parameter name="nc">
64 <longdesc lang="en">
65 The full path of the used binary. This can be nc or socat path.
66 The default is /usr/bin/nc and /usr/bin/socat for SUSE distributions.
67 </longdesc>
68 <shortdesc lang="en">Full path of the used binary (nc or socat are allowed)</shortdesc>
69 <content type="string" default="${OCF_RESKEY_nc_default}"/>
70 </parameter>
71
72 <parameter name="port">
73 <longdesc lang="en">
74 Port to listen to.
75 </longdesc>
76 <shortdesc lang="en">Listen to port</shortdesc>
77 <content type="string" default="${OCF_RESKEY_port_default}"/>
78 </parameter>
79
80 <parameter name="listen">
81 <longdesc lang="en">
82 This parameter can have following walues:
83 default: Neither -4 nor -6 will be used. The default behavior of socat and nc will be used.
84 socat: Listen only on IPv4 addresses
85 nc: If net.ipv6.bindv6only = 0 => Listen on both IPv4 and IP6 addresses
86 If net.ipv6.bindv6only = 1 => Listen only on IPv4 addresses
87 ipv4only: Listen only on IPv4 addresses.
88 ipv6enable: Enable TCP6 support.
89 nc: Listen only on IPv6 addresses independent of net.ipv6.bindv6only
90 socat: If net.ipv6.bindv6only = 0 => Listen on both IPv4 and IP6 addresses.
91 If net.ipv6.bindv6only = 1 => Listen only on IPv6 addresses.
92 </longdesc>
93 <shortdesc lang="en">Usage of IPv4 and IPv6 addresses.</shortdesc>
94 <content type="string" default="${OCF_RESKEY_listen_default}"/>
95 </parameter>
96
97
98
99 </parameters>
100
101 <actions>
102 <action name="start" timeout="20s" />
103 <action name="stop" timeout="20s" />
104 <action name="monitor" depth="0" timeout="20s" interval="10s" />
105 <action name="meta-data" timeout="5s" />
106 <action name="validate-all" timeout="5s" />
107 </actions>
108 </resource-agent>
109 END
110 exit 0
111 }
112
113 getpid() {
114 grep -o '[0-9]*' $1
115 }
116
117 lb_monitor() {
118 if test -f "$pidfile"; then
119 [ "$__OCF_ACTION" = "stop" ] && level="debug" || level="err"
120
121 if pid=$(getpid "$pidfile") && [ -n "$pid" ]; then
122 output=$(kill -s 0 "$pid" 2>&1)
123 mon_rc=$?
124
125 [ -n "$output" ] && ocf_log "$level" "$output"
126 [ "$mon_rc" -eq 0 ] && return $OCF_SUCCESS
127 fi
128
129 # pidfile w/o process means the process died
130 return $OCF_ERR_GENERIC
131 else
132 return $OCF_NOT_RUNNING
133 fi
134 }
135
136 lb_start() {
137 if [ "$( basename $OCF_RESKEY_nc )" = 'socat' ]; then
138 case "${OCF_RESKEY_listen,,}" in
139 ipv4only)
140 cmd="$OCF_RESKEY_nc -4 -U TCP4-LISTEN:$OCF_RESKEY_port,backlog=10,fork,reuseaddr /dev/null" ;;
141 ipv6enable)
142 cmd="$OCF_RESKEY_nc -U TCP6-LISTEN:$OCF_RESKEY_port,backlog=10,fork,reuseaddr /dev/null" ;;
143 *)
144 cmd="$OCF_RESKEY_nc -U TCP-LISTEN:$OCF_RESKEY_port,backlog=10,fork,reuseaddr /dev/null" ;;
145 esac
146 else
147 case "${OCF_RESKEY_listen,,}" in
148 ipv4only)
149 cmd="$OCF_RESKEY_nc -4 -l -k $OCF_RESKEY_port" ;;
150 ipv6enable)
151 cmd="$OCF_RESKEY_nc -6 -l -k $OCF_RESKEY_port" ;;
152 *)
153 cmd="$OCF_RESKEY_nc -l -k $OCF_RESKEY_port" ;;
154 esac
155 fi
156 if ! lb_monitor; then
157 ocf_log debug "Starting $process: $cmd"
158 # Execute the command as created above
159 $cmd >/dev/null 2>&1 &
160 echo $! > $pidfile
161 if lb_monitor; then
162 ocf_log debug "$process: $cmd started successfully, calling monitor"
163 lb_monitor
164 return $?
165 else
166 ocf_log err "$process: $cmd could not be started"
167 return $OCF_ERR_GENERIC
168 fi
169 else
170 # If already running, consider start successful
171 ocf_log debug "$process: $cmd is already running"
172 return $OCF_SUCCESS
173 fi
174 }
175
176 lb_stop() {
177 stop_rc=$OCF_SUCCESS
178
179 if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
180 # Allow 2/3 of the action timeout for the orderly shutdown
181 # (The origin unit is ms, hence the conversion)
182 stop_timeout=$((OCF_RESKEY_CRM_meta_timeout/1500))
183 else
184 stop_timeout=10
185 fi
186
187 if lb_monitor; then
188 pid=`getpid $pidfile`
189 kill $pid
190
191 i=0
192 while [ $i -lt $stop_timeout ]; do
193 if ! lb_monitor; then
194 rm -f $pidfile
195 return $OCF_SUCCESS
196 fi
197 sleep 1
198 i=$((i+1))
199 done
200
201 ocf_log warn "Stop with SIGTERM failed/timed out, now sending SIGKILL."
202 kill -s 9 $pid
203 while :; do
204 if ! lb_monitor; then
205 ocf_log warn "SIGKILL did the job."
206 stop_rc=$OCF_SUCCESS
207 break
208 fi
209 ocf_log info "The job still hasn't stopped yet. Waiting..."
210 sleep 1
211 done
212 fi
213 rm -f $pidfile
214 return $stop_rc
215 }
216
217 lb_validate() {
218 check_binary "$OCF_RESKEY_nc"
219
220 if ! ocf_is_decimal "$OCF_RESKEY_port"; then
221 ocf_exit_reason "$OCF_RESKEY_port is not a valid port"
222 exit $OCF_ERR_CONFIGURED
223 fi
224
225 return $OCF_SUCCESS
226 }
227
228 ###############################################################################
229 #
230 # MAIN
231 #
232 ###############################################################################
233
234 case $__OCF_ACTION in
235 meta-data)
236 lb_metadata
237 exit $OCF_SUCCESS
238 ;;
239 usage|help)
240 lb_usage
241 exit $OCF_SUCCESS
242 ;;
243 esac
244
245 if ! ocf_is_root; then
246 ocf_log err "You must be root for $__OCF_ACTION operation."
247 exit $OCF_ERR_PERM
248 fi
249
250 case $__OCF_ACTION in
251 start)
252 lb_validate
253 lb_start;;
254 stop)
255 lb_stop;;
256 monitor)
257 lb_monitor;;
258 validate-all)
259 lb_validate;;
260 *)
261 echo $USAGE
262 exit $OCF_ERR_UNIMPLEMENTED
263 ;;
264 esac
265
266 exit $?