File: anything

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (314 lines) | stat: -rwxr-xr-x 9,507 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/sh
#
#       OCF Resource Agent compliant resource script.
#
# Copyright (c) 2009 IN-telegence GmbH & Co. KG, Dominik Klein
#                    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.

# OCF instance parameters
#       OCF_RESKEY_binfile
#       OCF_RESKEY_cmdline_options
#	OCF_RESKEY_workdir
#	OCF_RESKEY_pidfile
#       OCF_RESKEY_logfile
#       OCF_RESKEY_errlogfile
#       OCF_RESKEY_user
#       OCF_RESKEY_monitor_hook
#       OCF_RESKEY_stop_timeout
#
# This RA starts $binfile with $cmdline_options as $user in $workdir and writes a $pidfile from that. 
# If you want it to, it logs:
# - stdout to $logfile, stderr to $errlogfile or 
# - stdout and stderr to $logfile
# - or to will be captured by lrmd if these options are omitted.
# Monitoring is done through $pidfile or your custom $monitor_hook script. 
# The RA expects the program to keep running "daemon-like" and
# not just quit and exit. So this is NOT (yet - feel free to
# enhance) a way to just run a single one-shot command which just
# does something and then exits.

# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

getpid() {
        grep -o '[0-9]*' $1
}

anything_status() {
	if test -f "$pidfile"
	then
		if pid=`getpid $pidfile` && [ "$pid" ] && kill -s 0 $pid
		then
			return $OCF_SUCCESS
		else
			# pidfile w/o process means the process died
			return $OCF_ERR_GENERIC
		fi
	else
		return $OCF_NOT_RUNNING
	fi
}

anything_start() {
	if ! anything_status
	then
		if [ -n "$logfile" -a -n "$errlogfile" ]
		then
			# We have logfile and errlogfile, so redirect STDOUT und STDERR to different files
			cmd="su - $user -c \"cd $workdir; nohup $binfile $cmdline_options >> $logfile 2>> $errlogfile & \"'echo \$!' "
		else
			# We only have logfile so redirect STDOUT and STDERR to the same file
			cmd="su - $user -c \"cd $workdir; nohup $binfile $cmdline_options >> $logfile 2>&1 & \"'echo \$!' "
		fi
		ocf_log debug "Starting $process: $cmd"
		# Execute the command as created above
		eval $cmd > $pidfile
		if anything_status
		then
			ocf_log debug "$process: $cmd started successfully"
			return $OCF_SUCCESS
		else 
			ocf_log err "$process: $cmd could not be started"
			return $OCF_ERR_GENERIC
		fi
	else
		# If already running, consider start successful
		ocf_log debug "$process: $cmd is already running"
		return $OCF_SUCCESS
	fi
}

anything_stop() {
	local rc=$OCF_SUCCESS

        if [ -n "$OCF_RESKEY_stop_timeout" ]
        then
                stop_timeout=$OCF_RESKEY_stop_timeout
        elif [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
                # Allow 2/3 of the action timeout for the orderly shutdown
                # (The origin unit is ms, hence the conversion)
                stop_timeout=$((OCF_RESKEY_CRM_meta_timeout/1500))
        else
                stop_timeout=10
        fi
	if anything_status
	then
                pid=`getpid $pidfile`
                kill $pid
                i=0
                while [ $i -lt $stop_timeout ]
                do
                        if ! anything_status
                        then
                        	rm -f $pidfile
                                return $OCF_SUCCESS
                        fi
                        sleep 1 
                        i=$((i+1))
                done
                ocf_log warn "Stop with SIGTERM failed/timed out, now sending SIGKILL."
                kill -s 9 $pid
                while :
                do
                        if ! anything_status
                        then
                                ocf_log warn "SIGKILL did the job."
                                rc=$OCF_SUCCESS
                                break
                        fi
                        ocf_log info "The job still hasn't stopped yet. Waiting..."
                        sleep 1
                done
	fi
	rm -f $pidfile 
	return $rc
}

anything_monitor() {
	anything_status
	ret=$?
	if [ $ret -eq $OCF_SUCCESS ]
	then
		if [ -n "$OCF_RESKEY_monitor_hook" ]; then
			eval "$OCF_RESKEY_monitor_hook"
                        if [ $? -ne $OCF_SUCCESS ]; then
                                return ${OCF_ERR_GENERIC}
                        fi
			return $OCF_SUCCESS
		else
			true
		fi
	else
		return $ret
	fi
}

# FIXME: Attributes special meaning to the resource id
process="$OCF_RESOURCE_INSTANCE"
binfile="$OCF_RESKEY_binfile"
cmdline_options="$OCF_RESKEY_cmdline_options"
workdir="$OCF_RESKEY_workdir"
pidfile="$OCF_RESKEY_pidfile"
[ -z "$pidfile" ] && pidfile=${HA_VARRUN}/anything_${process}.pid
logfile="${OCF_RESKEY_logfile:-/dev/null}"
errlogfile="$OCF_RESKEY_errlogfile"
user="$OCF_RESKEY_user"
[ -z "$user" ] && user=root

anything_validate() {
	if ! su - $user -c "test -x $binfile"
	then
		ocf_log err "binfile $binfile does not exist or is not executable by $user."
		exit $OCF_ERR_INSTALLED
	fi
	if ! getent passwd $user >/dev/null 2>&1
	then
		ocf_log err "user $user does not exist."
		exit $OCF_ERR_INSTALLED
	fi
	for logfilename in "$logfile" "$errlogfile"
	do
		if [ -n "$logfilename" ]; then
			mkdir -p `dirname $logfilename` || {
				ocf_log err "cannot create $(dirname $logfilename)"
				exit $OCF_ERR_INSTALLED
			}
		fi
	done
	[ "x$workdir" != x -a ! -d "$workdir" ] && {
		ocf_log err "working directory $workdir doesn't exist"
		exit $OCF_ERR_INSTALLED
	}
	return $OCF_SUCCESS
}

anything_meta() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="anything">
<version>1.0</version>
<longdesc lang="en">
This is a generic OCF RA to manage almost anything.
</longdesc>
<shortdesc lang="en">Manages an arbitrary service</shortdesc>

<parameters>
<parameter name="binfile" required="1" unique="1">
<longdesc lang="en">
The full name of the binary to be executed. This is expected to keep running with the same pid and not just do something and exit.
</longdesc>
<shortdesc lang="en">Full path name of the binary to be executed</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="cmdline_options" required="0">
<longdesc lang="en">
Command line options to pass to the binary
</longdesc>
<shortdesc lang="en">Command line options</shortdesc>
<content type="string" />
</parameter>
<parameter name="workdir" required="0" unique="0">
<longdesc lang="en">
The path from where the binfile will be executed.
</longdesc>
<shortdesc lang="en">Full path name of the work directory</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="pidfile">
<longdesc lang="en">
File to read/write the PID from/to.
</longdesc>
<shortdesc lang="en">File to write STDOUT to</shortdesc>
<content type="string" default="${HA_VARRUN}/anything_${process}.pid"/>
</parameter>
<parameter name="logfile" required="0">
<longdesc lang="en">
File to write STDOUT to
</longdesc>
<shortdesc lang="en">File to write STDOUT to</shortdesc>
<content type="string" default="/dev/null" />
</parameter>
<parameter name="errlogfile" required="0">
<longdesc lang="en">
File to write STDERR to
</longdesc>
<shortdesc lang="en">File to write STDERR to</shortdesc>
<content type="string" />
</parameter>
<parameter name="user" required="0">
<longdesc lang="en">
User to run the command as
</longdesc>
<shortdesc lang="en">User to run the command as</shortdesc>
<content type="string" default="root"/>
</parameter>
<parameter name="monitor_hook">
<longdesc lang="en">
Command to run in monitor operation
</longdesc>
<shortdesc lang="en">Command to run in monitor operation</shortdesc>
<content type="string"/>
</parameter>
<parameter name="stop_timeout">
<longdesc lang="en">
In the stop operation: Seconds to wait for kill -TERM to succeed
before sending kill -SIGKILL. Defaults to 2/3 of the stop operation timeout.
</longdesc>
<shortdesc lang="en">Seconds to wait after having sent SIGTERM before sending SIGKILL in stop operation</shortdesc>
<content type="string" default=""/>
</parameter>
</parameters>
<actions>
<action name="start"   timeout="20s" />
<action name="stop"    timeout="20s" />
<action name="monitor" depth="0"  timeout="20s" interval="10" />
<action name="meta-data"  timeout="5" />
<action name="validate-all"  timeout="5" />
</actions>
</resource-agent>
END
exit 0
}

case "$1" in
	meta-data|metadata|meta_data)
		anything_meta
	;;
	start)
		anything_start
	;;
	stop)
		anything_stop
	;;
	monitor)
		anything_monitor
	;;
	validate-all)
		anything_validate
	;;
	*)
		ocf_log err "$0 was called with unsupported arguments: $*"
		exit $OCF_ERR_UNIMPLEMENTED
	;;
esac