~ubuntu-branches/debian/sid/resource-agents/sid

« back to all changes in this revision

Viewing changes to heartbeat/varnish

  • Committer: Package Import Robot
  • Author(s): Martin Loschwitz
  • Date: 2012-10-09 11:16:59 UTC
  • mfrom: (3.1.1)
  • Revision ID: package-import@ubuntu.com-20121009111659-jrcwfqskcbh0iuio
Tags: 1:3.9.3+git20121009-1
* New upstream version
* debian/patches/01_docbook_patch.patch: Refactored for new release
* debian/patches/02_spelling_fixes.patch.new: Refatored for new release
* debian/control: Bumped Standards-Version to 3.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
#
 
4
# Varnish
 
5
#
 
6
# Description:  Manage varnish instances as a HA resource
 
7
#
 
8
# Author:       Léon Keijser <keijser@stone-it.com>
 
9
#
 
10
# License:      GNU General Public License (GPL)
 
11
#
 
12
# See usage() for more details
 
13
#
 
14
# OCF instance parameters:
 
15
#   OCF_RESKEY_pid
 
16
#   OCF_RESKEY_binary
 
17
#   OCF_RESKEY_client_binary
 
18
#   OCF_RESKEY_config
 
19
#   OCF_RESKEY_name
 
20
#   OCF_RESKEY_listen_address
 
21
#   OCF_RESKEY_mgmt_address
 
22
#   OCF_RESKEY_ttl
 
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
 
29
#
 
30
#######################################################################
 
31
# Initialization:
 
32
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
 
33
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
 
34
 
 
35
#######################################################################
 
36
# Set default paramenter values
 
37
 
 
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}}
 
41
 
 
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
 
53
 
 
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}}
 
65
 
 
66
 
 
67
meta_data() {
 
68
        cat <<END
 
69
<?xml version="1.0"?>
 
70
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
 
71
<resource-agent name="varnish" version="0.1">
 
72
<version>1.0</version>
 
73
 
 
74
<longdesc lang="en">
 
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.
 
79
</longdesc>
 
80
<shortdesc lang="en">Manage a Varnish instance</shortdesc>
 
81
 
 
82
<parameters>
 
83
 
 
84
<parameter name="config" unique="1" required="1">
 
85
<longdesc lang="en">
 
86
The VCL configuration file that varnish should manage, for example
 
87
"/etc/varnish/default.vcl".
 
88
</longdesc>
 
89
<shortdesc lang="en">VCL file</shortdesc>
 
90
<content type="string" default="" />
 
91
</parameter>
 
92
 
 
93
<parameter name="name" unique="1">
 
94
<longdesc lang="en">
 
95
Override the name of the instance that should be given to varnish
 
96
(defaults to the resource identifier).
 
97
</longdesc>
 
98
<shortdesc lang="en">Instance name</shortdesc>
 
99
<content type="string" default="${OCF_RESKEY_name_default}" />
 
100
</parameter>
 
101
 
 
102
<parameter name="pid" unique="1">
 
103
<longdesc lang="en">
 
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.
 
108
</longdesc>
 
109
<shortdesc lang="en">Listen address</shortdesc>
 
110
<content type="string" default="${OCF_RESKEY_pid_default}" />
 
111
</parameter>
 
112
 
 
113
<parameter name="listen_address" unique="1">
 
114
<longdesc lang="en">
 
115
Listen on this address:port, for example "192.168.1.1:80"
 
116
</longdesc>
 
117
<shortdesc lang="en">Listen address</shortdesc>
 
118
<content type="string" default="${OCF_RESKEY_listen_address_default}" />
 
119
</parameter>
 
120
 
 
121
<parameter name="mgmt_address" unique="1" required="1">
 
122
<longdesc lang="en">
 
123
Provide a management interface, for example "127.0.0.1:2222"
 
124
</longdesc>
 
125
<shortdesc lang="en">Management interface</shortdesc>
 
126
<content type="string" />
 
127
</parameter>
 
128
 
 
129
<parameter name="ttl">
 
130
<longdesc lang="en">
 
131
Specifies a hard minimum time to live for cached documents.
 
132
</longdesc>
 
133
<shortdesc lang="en">TTL</shortdesc>
 
134
<content type="integer" default="${OCF_RESKEY_ttl_default}" />
 
135
</parameter>
 
136
 
 
137
<parameter name="varnish_user">
 
138
<longdesc lang="en">
 
139
Specifies the name of an unprivileged user to which the 
 
140
child process should switch before it starts accepting 
 
141
connections.
 
142
</longdesc>
 
143
<shortdesc lang="en">Unprivileged user</shortdesc>
 
144
<content type="string" default="${OCF_RESKEY_varnish_user_default}" />
 
145
</parameter>
 
146
 
 
147
<parameter name="varnish_group">
 
148
<longdesc lang="en">
 
149
Specifies  the  name  of an unprivileged group to which 
 
150
the child process should switch before it starts accepting 
 
151
connections.
 
152
</longdesc>
 
153
<shortdesc lang="en">Unprivileged group</shortdesc>
 
154
<content type="string" default="${OCF_RESKEY_varnish_group_default}" />
 
155
</parameter>
 
156
 
 
157
<parameter name="backend_type">
 
158
<longdesc lang="en">
 
159
Use the specified storage backend. Valid options are
 
160
'malloc' for memory and 'file' for a file backend.
 
161
</longdesc>
 
162
<shortdesc lang="en">Backend type</shortdesc>
 
163
<content type="string" default="${OCF_RESKEY_backend_type_default}" />
 
164
</parameter>
 
165
 
 
166
<parameter name="backend_size">
 
167
<longdesc lang="en">
 
168
Specify the size of the backend. For example "1G".
 
169
</longdesc>
 
170
<shortdesc lang="en">Backend size</shortdesc>
 
171
<content type="string" default="${OCF_RESKEY_backend_size_default}" />
 
172
</parameter>
 
173
 
 
174
<parameter name="backend_file" unique="1">
 
175
<longdesc lang="en">
 
176
Specify the backend filename if you use backend_type file. 
 
177
For example /var/lib/varnish/mybackend.bin
 
178
</longdesc>
 
179
<shortdesc lang="en">Backend file</shortdesc>
 
180
<content type="string" default="${OCF_RESKEY_backend_file_default}" />
 
181
</parameter>
 
182
 
 
183
<parameter name="worker_threads">
 
184
<longdesc lang="en">
 
185
Start  at  least  min but no more than max worker 
 
186
threads with the specified idle timeout.
 
187
 
 
188
Syntax: min[,max[,timeout]]
 
189
For example: 100,3000,120
 
190
</longdesc>
 
191
<shortdesc lang="en">Worker threads</shortdesc>
 
192
<content type="string" default="${OCF_RESKEY_worker_threads_default}" />
 
193
</parameter>
 
194
 
 
195
<parameter name="client_binary">
 
196
<longdesc lang="en">
 
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.
 
199
</longdesc>
 
200
<shortdesc lang="en">Varnish admin utility</shortdesc>
 
201
<content type="string" default="${OCF_RESKEY_client_binary_default}" />
 
202
</parameter>
 
203
 
 
204
</parameters>
 
205
 
 
206
<actions>
 
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" />
 
213
</actions>
 
214
</resource-agent>
 
215
END
 
216
}
 
217
 
 
218
#######################################################################
 
219
 
 
220
 
 
221
varnish_usage() {
 
222
        cat <<END
 
223
usage: $0 {start|stop|monitor|validate-all|meta-data}
 
224
 
 
225
Expects to have a fully populated OCF RA-compliant environment set.
 
226
END
 
227
}
 
228
 
 
229
varnish_status() {
 
230
    local pid
 
231
    local rc
 
232
 
 
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
 
236
 
 
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
 
242
        rc=$?
 
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
 
248
            v_rc=$?
 
249
            if [ "$v_rc" -eq 0 ]; then
 
250
                ocf_log info "Varnish child reported running"
 
251
                return $OCF_SUCCESS
 
252
            else
 
253
                ocf_log err "Varnish child not running"
 
254
                return $OCF_ERR_GENERIC
 
255
            fi
 
256
        else
 
257
            ocf_log err "Varnish PID file exists, but varnishd is not running"
 
258
            return $OCF_ERR_GENERIC
 
259
        fi
 
260
    fi
 
261
    
 
262
    return $OCF_NOT_RUNNING
 
263
}
 
264
 
 
265
varnish_start() {
 
266
    local rc
 
267
    local backend_options
 
268
 
 
269
    varnish_status
 
270
    rc=$?
 
271
    if [ $rc -eq $OCF_SUCCESS ]; then
 
272
        ocf_log info "Varnish already running"
 
273
        return $OCF_SUCCESS
 
274
    fi
 
275
 
 
276
    # check which backend is to be used
 
277
    case "$OCF_RESKEY_backend_type" in
 
278
       malloc)  
 
279
                backend_options="$OCF_RESKEY_backend_size"
 
280
                ;;
 
281
        file)
 
282
                backend_options="$OCF_RESKEY_backend_file,$OCF_RESKEY_backend_size"
 
283
                ;;
 
284
        *)
 
285
                # not implemented yet
 
286
                return $OCF_ERR_CONFIGURED 
 
287
                ;;
 
288
    esac
 
289
    ocf_run $OCF_RESKEY_binary \
 
290
        -P $OCF_RESKEY_pid \
 
291
        -a $OCF_RESKEY_listen_address \
 
292
        -f $OCF_RESKEY_config \
 
293
        -T $OCF_RESKEY_mgmt_address \
 
294
        -t $OCF_RESKEY_ttl \
 
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 \
 
299
        -n $OCF_RESKEY_name
 
300
    rc=$?
 
301
    if [ $rc -ne 0 ]; then
 
302
        ocf_log err "Varnish failed to start"
 
303
        return $OCF_ERR_GENERIC
 
304
    fi
 
305
 
 
306
    # Spin waiting for varnishd to come up.
 
307
    # Let the CRM/LRM time us out if required
 
308
    while true; do
 
309
        varnish_status
 
310
        rc=$?
 
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
 
315
        fi
 
316
        sleep 2
 
317
    done
 
318
 
 
319
    ocf_log info "Varnish started succesfully"
 
320
    return $OCF_SUCCESS
 
321
}
 
322
 
 
323
varnish_stop() {
 
324
    local rc
 
325
    local pid
 
326
 
 
327
    varnish_status
 
328
    rc=$?
 
329
    if [ $rc -eq $OCF_NOT_RUNNING ]; then
 
330
        ocf_log info "Varnish already stopped"
 
331
        return $OCF_SUCCESS
 
332
    fi
 
333
 
 
334
    # kill the varnish process
 
335
    pid=$(cat $OCF_RESKEY_pid)
 
336
    ocf_run kill -s TERM $pid
 
337
    rc=$?
 
338
 
 
339
    if [ $rc -ne 0 ]; then
 
340
        ocf_log err "Varnish failed to stop"
 
341
        return $OCF_ERR_GENERIC
 
342
    fi
 
343
 
 
344
    # stop waiting
 
345
    shutdown_timeout=$((($OCF_RESKEY_CRM_meta_timeout/1000)-5))
 
346
    count=0
 
347
    while [ $count -lt $shutdown_timeout ]; do
 
348
        # check if process still exists
 
349
        ocf_run kill -s 0 $pid
 
350
        rc=$?
 
351
        if [ $rc -ne 0 ]; then
 
352
            # Varnish stopped succesfully, so let's delete the pidfile
 
353
            rm -f $OCF_RESKEY_pid
 
354
            break
 
355
        fi
 
356
        count=$(expr $count + 1)
 
357
        sleep 1
 
358
        ocf_log info "Varnish still hasn't stopped yet. Waiting..."
 
359
    done 
 
360
 
 
361
    varnish_status
 
362
    rc=$?
 
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
 
367
        # delete the pidfile
 
368
        rm -f $OCF_RESKEY_pid
 
369
    fi
 
370
 
 
371
    ocf_log info "Varnish stopped"
 
372
    return $OCF_SUCCESS
 
373
}
 
374
 
 
375
 
 
376
varnish_validate() {
 
377
    if [ -f $OCF_RESKEY_config ]; then
 
378
        return $OCF_SUCCESS
 
379
    else
 
380
        return $OCF_ERR_INSTALLED
 
381
    fi
 
382
}
 
383
 
 
384
 
 
385
case $__OCF_ACTION in
 
386
    meta-data)
 
387
        meta_data
 
388
        exit $OCF_SUCCESS
 
389
        ;;
 
390
    start)
 
391
        varnish_start
 
392
        ;;
 
393
    stop)
 
394
        varnish_stop
 
395
        ;;
 
396
    monitor|status)
 
397
        varnish_status
 
398
        ;;
 
399
    validate-all)
 
400
        varnish_validate
 
401
        ;;
 
402
    usage|help)
 
403
        varnish_usage
 
404
        exit $OCF_SUCCESS
 
405
        ;;
 
406
    *)
 
407
        varnish_usage
 
408
        exit $OCF_ERR_UNIMPLEMENTED
 
409
        ;;
 
410
esac
 
411
rc=$?
 
412
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
 
413
exit $rc
 
414