Debian Codegraph @

heartbeat/gcp-ilb

raw
1 #!/bin/sh
2 # ---------------------------------------------------------------------
3 # # Copyright 2021 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # Authors: Fatima Silveira, Lucia Subatin
16 # ---------------------------------------------------------------------
17 # Description: Wrapper to respond to probe requests from health
18 # check agents in Google Cloud Platform. Nothing is specific to Google
19 # Cloud.
20 # ---------------------------------------------------------------------
21
22
23 # Initialization:
24 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
25 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
26
27 # Defaults
28 OCF_RESKEY_cat_default="socat"
29 OCF_RESKEY_port_default="60000"
30 OCF_RESKEY_log_enable_default="false"
31 OCF_RESKEY_log_cmd_default="gcloud"
32 OCF_RESKEY_log_params_default="logging write GCPILB"
33 OCF_RESKEY_log_end_params_default=""
34
35
36 if type "socat" > /dev/null 2>&1; then
37 OCF_RESKEY_cat_default="socat"
38 else
39 OCF_RESKEY_cat_default="nc"
40 fi
41
42
43 : ${OCF_RESKEY_cat=${OCF_RESKEY_cat_default}}
44 : ${OCF_RESKEY_port=${OCF_RESKEY_port_default}}
45 : ${OCF_RESKEY_log_enable=${OCF_RESKEY_log_enable_default}}
46 : ${OCF_RESKEY_log_cmd=${OCF_RESKEY_log_cmd_default}}
47 : ${OCF_RESKEY_log_params=${OCF_RESKEY_log_params_default}}
48 : ${OCF_RESKEY_log_end_params=${OCF_RESKEY_log_end_params_default}}
49
50
51 process="$OCF_RESOURCE_INSTANCE"
52 pidfile="/var/run/$OCF_RESOURCE_INSTANCE.pid"
53
54
55 #Validate command for logging
56 if ocf_is_true "$OCF_RESKEY_log_enable"; then
57 if type $OCF_RESKEY_log_cmd > /dev/null 2>&1; then
58 logging_cmd="$OCF_RESKEY_log_cmd $OCF_RESKEY_log_params"
59 ocf_log debug "Logging command is: \'$logging_cmd\' "
60 else
61 OCF_RESKEY_log_enable="false"
62 ocf_log err "\'$logging_cmd\' is invalid. External logging disabled."
63
64 fi
65 fi
66
67
68 #######################################################################
69 ilb_metadata() {
70 cat <<END
71 <?xml version="1.0"?>
72 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
73 <resource-agent name="gcp-ilb" version="1.0">
74 <version>1.0</version>
75 <longdesc lang="en">
76 Resource Agent that wraps /usr/bin/nc or /usr/bin/socat to reply to health checks in Google Cloud.
77 See https://cloud.google.com/load-balancing/docs/health-checks for more information.
78 </longdesc>
79
80 <shortdesc lang="en">Replies to health checks from Google Cloud</shortdesc>
81
82 <parameters>
83 <parameter name="port">
84 <longdesc lang="en">
85 Listening port for health-check probes. Default: ${OCF_RESKEY_port_default}
86 </longdesc>
87 <shortdesc lang="en">Listening port (def ${OCF_RESKEY_port_default}) </shortdesc>
88 <content type="string" default="${OCF_RESKEY_port_default}" />
89 </parameter>
90
91 <parameter name="cat">
92 <longdesc lang="en">
93 Location of netcat (default: /usr/bin/nc ) or socat (default: /usr/bin/socat ). If present, is used /usr/bin/socat.
94 The recommended binary is socat, present in the following minimum versions if the package resource-agents:
95 - SLES 12 SP4/SP5: resource-agents-4.3.018.a7fb5035-3.30.1.
96 - SLES 15/15 SP1: resource-agents-4.3.0184.6ee15eb2-4.13.1.
97 </longdesc>
98 <shortdesc lang="en">Path to nc / socat </shortdesc>
99 <content type="string" default="${OCF_RESKEY_cat_default}" />
100 </parameter>
101 <parameter name="log_enable">
102 <longdesc lang="en">
103 Logging with an external application is enabled (accepts "true" or "false"). The defaults are configured to call "gcloud logging" (see: https://cloud.google.com/sdk/gcloud/reference/logging).
104 </longdesc>
105 <shortdesc lang="en">Log enabled </shortdesc>
106 <content type="string" default="${OCF_RESKEY_log_enable_default}" />
107 </parameter>
108 <parameter name="log_cmd">
109 <longdesc lang="en">
110 External logging command. The defaults are configured to call "gcloud logging" (see: https://cloud.google.com/sdk/gcloud/reference/logging).
111 This parameter should only have the binary that can be validated (e.g., gcloud). The rest of the command is formed with the additional parameters
112 and the message being logged as follows:
113 - log_cmd + log_params + "The message being logged" + log_end_params
114
115 Using the gcloud command for Stackdriver logging, the parameters would be:
116 - log_cmd = gcloud
117 - log_params = logging write GCPILB
118 - "This is a message being sent by the app"
119 - log_end_params = (nothing in this case, this is reserved for use with other tools)
120 Which would result in this valid command (where GCPILB is the name of the log):
121 gcloud logging write GCPILB "This is a message being sent by the app"
122
123 NOTE: Only the binary is validated for existence and no further checks are performed. The assumption is that only administrators with root access can configure this tool.
124 </longdesc>
125 <shortdesc lang="en">External log command </shortdesc>
126 <content type="string" default="${OCF_RESKEY_log_cmd_default}" />
127 </parameter>
128 <parameter name="log_params">
129 <longdesc lang="en">
130 Additional input for the logging application. See explanation for log_cmd
131 </longdesc>
132 <shortdesc lang="en">Additional input 1 </shortdesc>
133 <content type="string" default="${OCF_RESKEY_log_params_default}" />
134 </parameter>
135 <parameter name="log_end_params">
136 <longdesc lang="en">
137 Additional input for the logging application. Placed after the message being logged.
138 </longdesc>
139 <shortdesc lang="en">Additional input 1 </shortdesc>
140 <content type="string" default="${OCF_RESKEY_log_end_params_default}" />
141 </parameter>
142
143 </parameters>
144
145 <actions>
146 <action name="start" timeout="10s" />
147 <action name="stop" timeout="30s" />
148 <action name="monitor" depth="0" timeout="30s" interval="30s" />
149 <action name="validate-all" timeout="5s" />
150 <action name="meta-data" timeout="5s" />
151 </actions>
152 </resource-agent>
153 END
154 exit 0
155 }
156
157 #######################################################################
158
159 log() {
160 lvl=$1
161 msg=$2
162 ocf_log $lvl "$0 - $process - $pid: $msg"
163 if ocf_is_true "${OCF_RESKEY_log_enable}" ; then
164 ( ${OCF_RESKEY_log_cmd} ${OCF_RESKEY_log_params} "L $lvl: $msg" ${OCF_RESKEY_log_end_params} )
165 fi
166 }
167
168 getpid() {
169 if ! [ -f "$pidfile" ] ; then
170 return
171 fi
172
173 cat $pidfile
174 }
175
176 ilb_usage() {
177 cat <<END
178 usage: $0 {start|stop|monitor|status|meta-data|validate-all}
179
180 Agent wrapping socat or nc to reply to health probes.
181 END
182 }
183
184
185
186
187 ilb_monitor() {
188
189 pid=`getpid`
190 log debug "pid is $pid"
191
192 if [ -z "$pid" ] ; then
193 return $OCF_NOT_RUNNING
194 fi
195
196 if [ -n "$pid" ] && kill -s 0 $pid ; then
197 log debug "Process is currently running"
198 return $OCF_SUCCESS
199 else
200 log warn "The process is not running but has a pidfile. Removing file"
201 rm -f $pidfile
202 return $OCF_NOT_RUNNING
203 fi
204
205 }
206
207 ilb_start() {
208
209 if ilb_monitor; then
210 log debug "Process is already running"
211 return $OCF_SUCCESS
212 fi
213
214 cmd="$OCF_RESKEY_cat -U TCP-LISTEN:$OCF_RESKEY_port,backlog=10,fork,reuseaddr /dev/null"
215
216 if [ $( basename $OCF_RESKEY_cat ) = 'nc' ]; then
217 cmd="$OCF_RESKEY_cat -l -k $OCF_RESKEY_port"
218 fi
219
220 log debug "Starting with \'$cmd\'"
221 ( ${cmd} ) & pid="$!"
222 disown
223
224 if [ -n "$pid" ] ; then
225 log debug "$pid is started"
226 echo "$pid" > $pidfile
227 return $OCF_SUCCESS
228 else
229 log err "\'$cmd\' could not be started"
230 return $OCF_ERR_GENERIC
231 fi
232
233 }
234
235 ilb_stop() {
236
237 if ! ilb_monitor; then
238 rm -f $pidfile
239 return $OCF_SUCCESS
240 fi
241
242 if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
243 # Allow 2/3 of the action timeout for the orderly shutdown
244 # (The origin unit is ms, hence the conversion)
245 stop_timeout=$((OCF_RESKEY_CRM_meta_timeout/1500))
246 else
247 stop_timeout=10
248 fi
249
250
251 pid=`getpid`
252 kill $pid
253
254 i=0
255 while [ $i -lt $stop_timeout ]; do
256 if ! ilb_monitor; then
257 rm -f $pidfile
258 return $OCF_SUCCESS
259 fi
260 sleep 1
261 i=$((i+1))
262 done
263
264 log warn "Stop with SIGTERM failed/timed out, now sending SIGKILL."
265
266 i=0
267 while [ $i -lt $stop_timeout ]; do
268
269 kill -s 9 $pid
270
271 if ! ilb_monitor; then
272 log warn "SIGKILL did the job."
273 rm -f $pidfile
274 return $OCF_SUCCESS
275 fi
276 log info "The job still hasn't stopped yet. Re-trying SIGKILL..."
277 sleep 2
278 i=$((i+2))
279 done
280
281 log err "The cat has more than 9 lives and could not be terminated."
282 return $OCF_ERR_GENERIC
283
284 }
285
286 ilb_validate() {
287 check_binary "$OCF_RESKEY_cat"
288
289 ocf_is_true "$OCF_RESKEY_log_enable" && check_binary "$OCF_RESKEY_log_cmd"
290
291 if ! ocf_is_decimal "$OCF_RESKEY_port"; then
292 ocf_exit_reason "$OCF_RESKEY_port is not a valid port"
293 exit $OCF_ERR_CONFIGURED
294 fi
295
296 return $OCF_SUCCESS
297 }
298
299 ###############################################################################
300 #
301 # MAIN
302 #
303 ###############################################################################
304
305 case $__OCF_ACTION in
306 meta-data)
307 ilb_metadata
308 exit $OCF_SUCCESS
309 ;;
310 usage|help)
311 ilb_usage
312 exit $OCF_SUCCESS
313 ;;
314 esac
315
316 if ! ocf_is_root; then
317 log err "You must be root for $__OCF_ACTION operation."
318 exit $OCF_ERR_PERM
319 fi
320
321 case $__OCF_ACTION in
322 start)
323 ilb_validate
324 ilb_start
325 ;;
326 stop)
327 ilb_stop
328 ;;
329 monitor)
330 ilb_monitor
331 ;;
332 validate-all)
333 ilb_validate
334 ;;
335 *)
336 ilb_usage
337 exit $OCF_ERR_UNIMPLEMENTED
338 ;;
339 esac
340
341 rc=$?
342 log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION returned $rc"
343
344 exit $rc