6
# Description: Manage varnish instances as a HA resource
8
# Author: Léon Keijser <keijser@stone-it.com>
10
# License: GNU General Public License (GPL)
12
# See usage() for more details
14
# OCF instance parameters:
17
# OCF_RESKEY_client_binary
20
# OCF_RESKEY_listen_address
21
# OCF_RESKEY_mgmt_address
23
# OCF_RESKEY_varnish_user
24
# OCF_RESKEY_varnish_group
25
# OCF_RESKEY_backend_type
26
# OCF_RESKEY_backend_size
27
# OCF_RESKEY_backend_file
28
# OCF_RESKEY_worker_threads
30
#######################################################################
32
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
33
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
35
#######################################################################
36
# Set default paramenter values
38
# Set these two first, as other defaults depend on it
39
OCF_RESKEY_name_default=${OCF_RESOURCE_INSTANCE}
40
: ${OCF_RESKEY_name=${OCF_RESKEY_name_default}}
42
OCF_RESKEY_binary_default=varnishd
43
OCF_RESKEY_client_binary_default=varnishadm
44
OCF_RESKEY_pid_default=/var/run/varnishd_${OCF_RESKEY_name}.pid
45
OCF_RESKEY_listen_address_default=0.0.0.0:80
46
OCF_RESKEY_ttl_default=600
47
OCF_RESKEY_varnish_user_default=varnish
48
OCF_RESKEY_varnish_group_default=varnish
49
OCF_RESKEY_backend_type_default=malloc
50
OCF_RESKEY_backend_size_default=1G
51
OCF_RESKEY_backend_file_default=/var/lib/varnish/${OCF_RESKEY_name}.bin
52
OCF_RESKEY_worker_threads_default=100,3000,120
54
: ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
55
: ${OCF_RESKEY_client_binary=${OCF_RESKEY_client_binary_default}}
56
: ${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}
57
: ${OCF_RESKEY_listen_address=${OCF_RESKEY_listen_address_default}}
58
: ${OCF_RESKEY_ttl=${OCF_RESKEY_ttl_default}}
59
: ${OCF_RESKEY_varnish_user=${OCF_RESKEY_varnish_user_default}}
60
: ${OCF_RESKEY_varnish_group=${OCF_RESKEY_varnish_group_default}}
61
: ${OCF_RESKEY_backend_type=${OCF_RESKEY_backend_type_default}}
62
: ${OCF_RESKEY_backend_size=${OCF_RESKEY_backend_size_default}}
63
: ${OCF_RESKEY_backend_file=${OCF_RESKEY_backend_file_default}}
64
: ${OCF_RESKEY_worker_threads=${OCF_RESKEY_worker_threads_default}}
70
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
71
<resource-agent name="varnish" version="0.1">
72
<version>1.0</version>
75
The Varnish Resource Agent can manage several varnishd
76
instances throughout the cluster. It does so by creating
77
a unique PID file and requires a unique listen address
78
and name for each instance.
80
<shortdesc lang="en">Manage a Varnish instance</shortdesc>
84
<parameter name="config" unique="1" required="1">
86
The VCL configuration file that varnish should manage, for example
87
"/etc/varnish/default.vcl".
89
<shortdesc lang="en">VCL file</shortdesc>
90
<content type="string" default="" />
93
<parameter name="name" unique="1">
95
Override the name of the instance that should be given to varnish
96
(defaults to the resource identifier).
98
<shortdesc lang="en">Instance name</shortdesc>
99
<content type="string" default="${OCF_RESKEY_name_default}" />
102
<parameter name="pid" unique="1">
104
Write the process's PID to the specified file.
105
The default will include the specified name, ie.:
106
"/var/run/varnish_production.pid". Unlike what this help message shows.
107
It is most likely not necessary to change this parameter.
109
<shortdesc lang="en">Listen address</shortdesc>
110
<content type="string" default="${OCF_RESKEY_pid_default}" />
113
<parameter name="listen_address" unique="1">
115
Listen on this address:port, for example "192.168.1.1:80"
117
<shortdesc lang="en">Listen address</shortdesc>
118
<content type="string" default="${OCF_RESKEY_listen_address_default}" />
121
<parameter name="mgmt_address" unique="1" required="1">
123
Provide a management interface, for example "127.0.0.1:2222"
125
<shortdesc lang="en">Management interface</shortdesc>
126
<content type="string" />
129
<parameter name="ttl">
131
Specifies a hard minimum time to live for cached documents.
133
<shortdesc lang="en">TTL</shortdesc>
134
<content type="integer" default="${OCF_RESKEY_ttl_default}" />
137
<parameter name="varnish_user">
139
Specifies the name of an unprivileged user to which the
140
child process should switch before it starts accepting
143
<shortdesc lang="en">Unprivileged user</shortdesc>
144
<content type="string" default="${OCF_RESKEY_varnish_user_default}" />
147
<parameter name="varnish_group">
149
Specifies the name of an unprivileged group to which
150
the child process should switch before it starts accepting
153
<shortdesc lang="en">Unprivileged group</shortdesc>
154
<content type="string" default="${OCF_RESKEY_varnish_group_default}" />
157
<parameter name="backend_type">
159
Use the specified storage backend. Valid options are
160
'malloc' for memory and 'file' for a file backend.
162
<shortdesc lang="en">Backend type</shortdesc>
163
<content type="string" default="${OCF_RESKEY_backend_type_default}" />
166
<parameter name="backend_size">
168
Specify the size of the backend. For example "1G".
170
<shortdesc lang="en">Backend size</shortdesc>
171
<content type="string" default="${OCF_RESKEY_backend_size_default}" />
174
<parameter name="backend_file" unique="1">
176
Specify the backend filename if you use backend_type file.
177
For example /var/lib/varnish/mybackend.bin
179
<shortdesc lang="en">Backend file</shortdesc>
180
<content type="string" default="${OCF_RESKEY_backend_file_default}" />
183
<parameter name="worker_threads">
185
Start at least min but no more than max worker
186
threads with the specified idle timeout.
188
Syntax: min[,max[,timeout]]
189
For example: 100,3000,120
191
<shortdesc lang="en">Worker threads</shortdesc>
192
<content type="string" default="${OCF_RESKEY_worker_threads_default}" />
195
<parameter name="client_binary">
197
This is used to control varnish via a CLI. It's currently
198
only used to check the status of the running child process.
200
<shortdesc lang="en">Varnish admin utility</shortdesc>
201
<content type="string" default="${OCF_RESKEY_client_binary_default}" />
207
<action name="start" timeout="20" />
208
<action name="stop" timeout="20" />
209
<action name="monitor" timeout="20" interval="10" depth="0" />
210
<action name="status" timeout="20" />
211
<action name="meta-data" timeout="5" />
212
<action name="validate-all" timeout="20" />
218
#######################################################################
223
usage: $0 {start|stop|monitor|validate-all|meta-data}
225
Expects to have a fully populated OCF RA-compliant environment set.
233
# FAILED = pidfile exist, but no running proc (or mismatch pid)
234
# SUCCES = contents of pidfile == running process id
235
# NOTRUN = no pidfile, no running process
237
# check if pidfile exists and larger than 0 bytes
238
if [ -s $OCF_RESKEY_pid ]; then
239
# it does, now check if the pid exists
240
pid=$(cat $OCF_RESKEY_pid)
241
ocf_run kill -s 0 $pid
243
if [ $rc -eq 0 ]; then
244
ocf_log info "Varnish is running"
245
# check if the child process is started and varnish is
246
# reporting child status as ok
247
ocf_run $OCF_RESKEY_client_binary -T $OCF_RESKEY_mgmt_address status
249
if [ "$v_rc" -eq 0 ]; then
250
ocf_log info "Varnish child reported running"
253
ocf_log err "Varnish child not running"
254
return $OCF_ERR_GENERIC
257
ocf_log err "Varnish PID file exists, but varnishd is not running"
258
return $OCF_ERR_GENERIC
262
return $OCF_NOT_RUNNING
267
local backend_options
271
if [ $rc -eq $OCF_SUCCESS ]; then
272
ocf_log info "Varnish already running"
276
# check which backend is to be used
277
case "$OCF_RESKEY_backend_type" in
279
backend_options="$OCF_RESKEY_backend_size"
282
backend_options="$OCF_RESKEY_backend_file,$OCF_RESKEY_backend_size"
285
# not implemented yet
286
return $OCF_ERR_CONFIGURED
289
ocf_run $OCF_RESKEY_binary \
291
-a $OCF_RESKEY_listen_address \
292
-f $OCF_RESKEY_config \
293
-T $OCF_RESKEY_mgmt_address \
295
-u $OCF_RESKEY_varnish_user \
296
-g $OCF_RESKEY_varnish_group \
297
-w $OCF_RESKEY_worker_threads \
298
-s $OCF_RESKEY_backend_type,$backend_options \
301
if [ $rc -ne 0 ]; then
302
ocf_log err "Varnish failed to start"
303
return $OCF_ERR_GENERIC
306
# Spin waiting for varnishd to come up.
307
# Let the CRM/LRM time us out if required
311
[ $rc -eq $OCF_SUCCESS ] && break
312
if [ $rc -ne $OCF_NOT_RUNNING ]; then
313
ocf_log err "Varnish start failed"
314
exit $OCF_ERR_GENERIC
319
ocf_log info "Varnish started succesfully"
329
if [ $rc -eq $OCF_NOT_RUNNING ]; then
330
ocf_log info "Varnish already stopped"
334
# kill the varnish process
335
pid=$(cat $OCF_RESKEY_pid)
336
ocf_run kill -s TERM $pid
339
if [ $rc -ne 0 ]; then
340
ocf_log err "Varnish failed to stop"
341
return $OCF_ERR_GENERIC
345
shutdown_timeout=$((($OCF_RESKEY_CRM_meta_timeout/1000)-5))
347
while [ $count -lt $shutdown_timeout ]; do
348
# check if process still exists
349
ocf_run kill -s 0 $pid
351
if [ $rc -ne 0 ]; then
352
# Varnish stopped succesfully, so let's delete the pidfile
353
rm -f $OCF_RESKEY_pid
356
count=$(expr $count + 1)
358
ocf_log info "Varnish still hasn't stopped yet. Waiting..."
363
if [ $rc -ne $OCF_NOT_RUNNING ]; then
364
# varnish didn't quit on a SIGTERM, try SIGKILL
365
ocf_log warn "Varnish failed to stop after ${shutdown_timeout}s using SIGTERM. Trying SIGKILL..."
366
ocf_run kill -s KILL $pid
368
rm -f $OCF_RESKEY_pid
371
ocf_log info "Varnish stopped"
377
if [ -f $OCF_RESKEY_config ]; then
380
return $OCF_ERR_INSTALLED
385
case $__OCF_ACTION in
408
exit $OCF_ERR_UNIMPLEMENTED
412
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"