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

« back to all changes in this revision

Viewing changes to heartbeat/slapd

  • 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/bash
 
2
#
 
3
# Stand-alone LDAP Daemon (slapd)
 
4
#
 
5
# Description:  Manages Stand-alone LDAP Daemon (slapd) as an OCF resource in
 
6
#               an high-availability setup.
 
7
#
 
8
# Authors:      Jeroen Koekkoek
 
9
#               nozawat@gmail.com
 
10
#               John Keith Hohm
 
11
#
 
12
# License:      GNU General Public License (GPL)
 
13
# Copyright:    (C) 2011 Pagelink B.V.
 
14
#
 
15
#       The OCF code was inspired by the Postfix resource script written by
 
16
#       Raoul Bhatia <r.bhatia@ipax.at>.
 
17
#
 
18
#       The code for managing the slapd instance is based on the the slapd init
 
19
#       script found in Debian GNU/Linux 6.0.
 
20
#
 
21
# OCF parameters:
 
22
#   OCF_RESKEY_slapd
 
23
#   OCF_RESKEY_ldapsearch
 
24
#   OCF_RESKEY_config
 
25
#   OCF_RESKEY_pidfile
 
26
#   OCF_RESKEY_user
 
27
#   OCF_RESKEY_group
 
28
#   OCF_RESKEY_services
 
29
#   OCF_RESKEY_watch_suffix
 
30
#   OCF_RESKEY_ignore_suffix
 
31
#   OCF_RESKEY_bind_dn
 
32
#   OCF_RESKEY_password
 
33
#   OCF_RESKEY_parameters
 
34
#   OCF_RESKEY_stop_escalate
 
35
#
 
36
################################################################################
 
37
 
 
38
# Initialization:
 
39
 
 
40
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
 
41
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
 
42
 
 
43
: ${OCF_RESKEY_slapd="/usr/sbin/slapd"}
 
44
: ${OCF_RESKEY_ldapsearch="ldapsearch"}
 
45
: ${OCF_RESKEY_config=""}
 
46
: ${OCF_RESKEY_pidfile=""}
 
47
: ${OCF_RESKEY_user=""}
 
48
: ${OCF_RESKEY_group=""}
 
49
: ${OCF_RESKEY_services="ldap:///"}
 
50
: ${OCF_RESKEY_watch_suffix=""}
 
51
: ${OCF_RESKEY_ignore_suffix=""}
 
52
: ${OCF_RESKEY_bind_dn=""}
 
53
: ${OCF_RESKEY_password=""}
 
54
: ${OCF_RESKEY_parameters=""}
 
55
: ${OCF_RESKEY_stop_escalate=15}
 
56
 
 
57
USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
 
58
ORIG_IFS=$IFS
 
59
NEWLINE='
 
60
'
 
61
 
 
62
################################################################################
 
63
 
 
64
usage() {
 
65
    echo $USAGE >&2
 
66
}
 
67
 
 
68
meta_data()
 
69
{
 
70
  cat <<END
 
71
<?xml version="1.0"?>
 
72
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
 
73
<resource-agent name="slapd">
 
74
<version>0.1</version>
 
75
 
 
76
<longdesc lang="en">
 
77
Resource script for Stand-alone LDAP Daemon (slapd). It manages a slapd instance as an OCF resource.
 
78
</longdesc>
 
79
<shortdesc lang="en">Manages a Stand-alone LDAP Daemon (slapd) instance</shortdesc>
 
80
 
 
81
<parameters>
 
82
 
 
83
<parameter name="slapd" unique="0" required="0">
 
84
<longdesc lang="en">
 
85
Full path to the slapd binary.
 
86
For example, "/usr/sbin/slapd".
 
87
</longdesc>
 
88
<shortdesc lang="en">Full path to slapd binary</shortdesc>
 
89
<content type="string" default="/usr/sbin/slapd" />
 
90
</parameter>
 
91
 
 
92
<parameter name="ldapsearch" unique="0" required="0">
 
93
<longdesc lang="en">
 
94
Full path to the ldapsearch binary.
 
95
For example, "/usr/bin/ldapsearch".
 
96
</longdesc>
 
97
<shortdesc lang="en">Full path to ldapsearch binary</shortdesc>
 
98
<content type="string" default="ldapsearch" />
 
99
</parameter>
 
100
 
 
101
<parameter name="config" required="0" unique="1">
 
102
<longdesc lang="en">
 
103
Full path to a slapd configuration directory or a slapd configuration file.
 
104
For example, "/etc/ldap/slapd.d" or "/etc/ldap/slapd.conf".
 
105
</longdesc>
 
106
<shortdesc>Full path to configuration directory or file</shortdesc>
 
107
<content type="string" default=""/>
 
108
</parameter>
 
109
 
 
110
<parameter name="pidfile" required="0" unique="0">
 
111
<longdesc lang="en">
 
112
File to read the PID from; read from olcPidFile/pidfile in config if not set.
 
113
</longdesc>
 
114
<shortdesc lang="en">File to read PID from</shortdesc>
 
115
<content type="string" default="" />
 
116
</parameter>
 
117
 
 
118
<parameter name="user" unique="0" required="0">
 
119
<longdesc lang="en">
 
120
User name or id slapd will run with. The group id is also changed to this
 
121
user's gid, unless the group parameter is used to override.
 
122
</longdesc>
 
123
<shortdesc lang="en">User name or id slapd will run with</shortdesc>
 
124
<content type="string" default="" />
 
125
</parameter>
 
126
 
 
127
<parameter name="group" unique="0" required="0">
 
128
<longdesc lang="en">
 
129
Group name or id slapd will run with.
 
130
</longdesc>
 
131
<shortdesc lang="en">Group name or id slapd will run with</shortdesc>
 
132
<content type="string" default="" />
 
133
</parameter>
 
134
 
 
135
<parameter name="services" required="0" unique="1">
 
136
<longdesc lang="en">
 
137
LDAP (and other scheme) URLs slapd will serve.
 
138
For example, "ldap://127.0.0.1:389 ldaps:/// ldapi:///"
 
139
</longdesc>
 
140
<shortdesc>LDAP (and other scheme) URLs to serve</shortdesc>
 
141
<content type="string" default="ldap:///"/>
 
142
</parameter>
 
143
 
 
144
<parameter name="watch_suffix" required="0" unique="0">
 
145
<longdesc lang="en">
 
146
Suffix (database backend) that will be monitored for availability. Multiple
 
147
suffixes can be specified by providing a space seperated list. By providing one
 
148
or more suffixes here, the ignore_suffix parameter is discarded. All suffixes
 
149
will be monitored if left blank.
 
150
</longdesc>
 
151
<shortdesc>Suffix that will be monitored for availability.</shortdesc>
 
152
<content type="string" default=""/>
 
153
</parameter>
 
154
 
 
155
<parameter name="ignore_suffix" required="0" unique="0">
 
156
<longdesc lang="en">
 
157
Suffix (database backend) that will not be monitored for availability. Multiple
 
158
suffixes can be specified by providing a space seperated list. No suffix will
 
159
be excluded if left blank.
 
160
</longdesc>
 
161
<shortdesc>Suffix that will not be monitored for availability.</shortdesc>
 
162
<content type="string" default=""/>
 
163
</parameter>
 
164
 
 
165
<parameter name="bind_dn" required="0" unique="0">
 
166
<longdesc lang="en">
 
167
Distinguished Name used to bind to the LDAP directory for testing. Leave blank
 
168
to bind to the LDAP directory anonymously.
 
169
</longdesc>
 
170
<shortdesc>Distinguished Name used to bind to the LDAP directory for testing.</shortdesc>
 
171
<content type="string" default=""/>
 
172
</parameter>
 
173
 
 
174
<parameter name="password" required="0" unique="0">
 
175
<longdesc lang="en">
 
176
Password used to bind to the LDAP directory for testing.
 
177
</longdesc>
 
178
<shortdesc>Password used to bind to the LDAP directory for testing.</shortdesc>
 
179
<content type="string" default=""/>
 
180
</parameter>
 
181
 
 
182
<parameter name="parameters" unique="0" required="0">
 
183
<longdesc lang="en">
 
184
slapd may be called with additional parameters.
 
185
Specify any of them here.
 
186
</longdesc>
 
187
<shortdesc lang="en">Any additional parameters to slapd.</shortdesc>
 
188
<content type="string" default="" />
 
189
</parameter>
 
190
 
 
191
<parameter name="stop_escalate" unique="0" required="0">
 
192
<longdesc lang="en">
 
193
Number of seconds to wait for shutdown (using SIGTERM) before resorting to
 
194
SIGKILL
 
195
</longdesc>
 
196
<shortdesc lang="en">Seconds before stop escalation to KILL</shortdesc>
 
197
<content type="integer" default="15" />
 
198
</parameter>
 
199
</parameters>
 
200
 
 
201
<actions>
 
202
<action name="start"   timeout="20s" />
 
203
<action name="stop"    timeout="20s" />
 
204
<action name="monitor" depth="0"  timeout="20s" interval="60s" />
 
205
<action name="validate-all"  timeout="20s" />
 
206
<action name="meta-data"  timeout="5s" />
 
207
</actions>
 
208
</resource-agent>
 
209
END
 
210
}
 
211
 
 
212
terminate()
 
213
{
 
214
  local pid=$1
 
215
  local signal=$2
 
216
  local recheck=${3-0}
 
217
  local rc
 
218
  local waited=0
 
219
 
 
220
  kill -$signal $pid >/dev/null 2>&1; rc=$?
 
221
 
 
222
  while [ \( $rc -eq 0 \) -a \( $recheck -eq 0 -o $waited -lt $recheck \) ]; do
 
223
    kill -0 $pid >/dev/null 2>&1; rc=$?
 
224
    let "waited += 1"
 
225
 
 
226
    if [ $rc -eq 0 ]; then
 
227
      sleep 1
 
228
    fi
 
229
  done
 
230
 
 
231
  if [ $rc -ne 0 ]; then
 
232
    return 0
 
233
  fi
 
234
 
 
235
  return 1
 
236
}
 
237
 
 
238
watch_suffix()
 
239
{
 
240
  local rc
 
241
 
 
242
  if [ -n "$OCF_RESKEY_watch_suffix" ]; then
 
243
    if echo "'$OCF_RESKEY_watch_suffix'" | grep "'$1'" >/dev/null 2>&1; then
 
244
      rc=0
 
245
    else
 
246
      rc=1
 
247
    fi
 
248
  else
 
249
    if echo "'$OCF_RESKEY_ignore_suffix'" | grep "'$1'" >/dev/null 2>&1; then
 
250
      rc=1
 
251
    else
 
252
      rc=0
 
253
    fi
 
254
  fi
 
255
 
 
256
  return $rc
 
257
}
 
258
 
 
259
slapd_pid()
 
260
{
 
261
  local pid
 
262
 
 
263
  if [ -f "$pid_file" ]; then
 
264
    pid=`head -n 1 "$pid_file" 2>/dev/null`
 
265
 
 
266
    if [ "X$pid" != "X" ]; then
 
267
      echo "$pid"
 
268
      return $OCF_SUCCESS
 
269
    fi
 
270
 
 
271
    ocf_log err "slapd pid file '$pid_file' empty."
 
272
    return $OCF_ERR_GENERIC
 
273
  fi
 
274
 
 
275
  ocf_log info "slapd pid file '$pid_file' does not exist."
 
276
  return $OCF_NOT_RUNNING
 
277
}
 
278
 
 
279
slapd_status()
 
280
{
 
281
  local pid=$1
 
282
  local state=$?
 
283
 
 
284
  if [ $state -eq $OCF_SUCCESS ]; then
 
285
 
 
286
    if ! kill -0 $pid >/dev/null 2>&1; then
 
287
      return $OCF_NOT_RUNNING
 
288
    else
 
289
      return $OCF_SUCCESS
 
290
    fi
 
291
  fi
 
292
 
 
293
  return $state
 
294
}
 
295
 
 
296
slapd_start()
 
297
{
 
298
  local options
 
299
  local reason
 
300
  local rc
 
301
  local state
 
302
 
 
303
  slapd_status `slapd_pid`; state=$?
 
304
 
 
305
  if [ $state -eq $OCF_SUCCESS ]; then
 
306
    ocf_log info "slapd already running."
 
307
    return $state
 
308
  elif [ $state -eq $OCF_ERR_GENERIC ]; then
 
309
    return $state
 
310
  fi
 
311
 
 
312
  options="-u $user -g $group"
 
313
 
 
314
  if [ -d "$config" ]; then
 
315
    options="$options -F $config"
 
316
  elif [ -f "$config" ]; then
 
317
    options="$options -f $config"
 
318
  else
 
319
    ocf_log err "slapd configuration '$config' does not exist."
 
320
    return $OCF_ERR_INSTALLED
 
321
  fi
 
322
 
 
323
  if [ -n "$parameters" ]; then
 
324
    options="$options $parameters"
 
325
  fi
 
326
 
 
327
  if [ -n "$services" ]; then
 
328
    $slapd -h "$services" $options 2>&1; rc=$?
 
329
  else
 
330
    $slapd $options 2>&1; rc=$?
 
331
  fi
 
332
 
 
333
  if [ $rc -ne 0 ]; then
 
334
    ocf_log err "slapd returned error."
 
335
 
 
336
    return $OCF_ERR_GENERIC
 
337
  fi
 
338
 
 
339
  while true; do
 
340
    slapd_monitor start
 
341
    if [ $? = "$OCF_SUCCESS" ]; then
 
342
      break
 
343
    fi
 
344
    sleep 1
 
345
  done
 
346
 
 
347
  ocf_log info "slapd started."
 
348
 
 
349
  return $OCF_SUCCESS
 
350
}
 
351
 
 
352
slapd_stop()
 
353
{
 
354
  local pid
 
355
  local rc
 
356
  local state
 
357
 
 
358
  pid=`slapd_pid`; slapd_status $pid; state=$?
 
359
 
 
360
  if [ $state -eq $OCF_NOT_RUNNING ]; then
 
361
    ocf_log info "slapd already stopped."
 
362
    return $OCF_SUCCESS
 
363
  elif [ $state -eq $OCF_ERR_GENERIC ]; then
 
364
    return $state
 
365
  fi
 
366
 
 
367
  terminate $pid TERM $OCF_RESKEY_stop_escalate; rc=$?
 
368
  if [ $rc -ne 0  ]; then
 
369
    ocf_log err "slapd failed to stop. Escalating to KILL."
 
370
    terminate $pid KILL; rc=$?
 
371
  fi
 
372
 
 
373
  if [ -f "$pid_file" ]; then
 
374
    rm -f "$pid_file" >/dev/null 2>&1
 
375
  fi
 
376
 
 
377
  ocf_log info "slapd stopped."
 
378
  return $OCF_SUCCESS
 
379
}
 
380
 
 
381
slapd_monitor()
 
382
{
 
383
  local options
 
384
  local rc
 
385
  local state
 
386
  local suffix
 
387
  local suffixes
 
388
  local err_option="-info"
 
389
 
 
390
  slapd_status `slapd_pid`; state=$?
 
391
  if [ $state -eq $OCF_NOT_RUNNING ]; then
 
392
    if [ -z "$1" ];then
 
393
      if ! ocf_is_probe; then
 
394
        ocf_log err "slapd process not found."
 
395
      fi
 
396
    fi
 
397
    return $state
 
398
  elif [ $state -ne $OCF_SUCCESS ]; then
 
399
    ocf_log err "slapd returned error."
 
400
    return $state
 
401
  fi
 
402
 
 
403
  if [ -d "$config" ]; then
 
404
    for suffix in `find "$config"/'cn=config' -type f -name olcDatabase* -exec \
 
405
                   sed -ne 's/^[[:space:]]*olcSuffix:[[:space:]]\+\(.\+\)/\1/p' {} \;`
 
406
    do
 
407
      suffix=${suffix#\"*}
 
408
      suffix=${suffix%\"*}
 
409
 
 
410
      if watch_suffix $suffix; then
 
411
        suffixes="$suffixes $suffix"
 
412
      fi
 
413
    done
 
414
 
 
415
  elif [ -f "$config" ]; then
 
416
    for suffix in `sed -ne 's/^[[:space:]]*suffix[[:space:]]\+\(.\+\)/\1/p' "$config"`
 
417
    do
 
418
      suffix=${suffix#\"*}
 
419
      suffix=${suffix%\"*}
 
420
 
 
421
      if watch_suffix $suffix; then
 
422
        suffixes="$suffixes $suffix"
 
423
      fi
 
424
    done
 
425
 
 
426
  else
 
427
    if ocf_is_probe; then
 
428
      ocf_log info "slapd configuration '$config' does not exist during probe."
 
429
    else
 
430
      ocf_log err "slapd configuration '$config' does not exist."
 
431
      return $OCF_ERR_INSTALLED
 
432
    fi
 
433
  fi
 
434
 
 
435
  options="-LLL -s base -x"
 
436
 
 
437
  if [ -n "$bind_dn" ]; then
 
438
    options="$options -D $bind_dn -w $password"
 
439
  fi
 
440
 
 
441
  [ -z "$1" ] && err_option=""
 
442
  for suffix in $suffixes; do
 
443
    ocf_run -q $err_option "$ldapsearch" -H "$services" -b "$suffix" $options >/dev/null 2>&1; rc=$?
 
444
 
 
445
    case "$rc" in
 
446
      "0")
 
447
        ocf_log debug "slapd database with suffix '$suffix' reachable"
 
448
        ;;
 
449
      "49")
 
450
        ocf_log err "slapd database with suffix '$suffix' unreachable. Invalid credentials."
 
451
        return $OCF_ERR_CONFIGURED
 
452
        ;;
 
453
      *)
 
454
        if [ -z "$1" ] || [ -n "$1" -a $rc -ne 1 ]; then
 
455
          ocf_log err "slapd database with suffix '$suffix' unreachable. exit code ($rc)"
 
456
        fi
 
457
        state=$OCF_ERR_GENERIC
 
458
        ;;
 
459
    esac
 
460
  done
 
461
 
 
462
  return $state
 
463
}
 
464
 
 
465
slapd_validate_all()
 
466
{
 
467
  check_binary "$slapd"
 
468
  check_binary "$ldapsearch"
 
469
 
 
470
  if [ -z "$pid_file" ]; then
 
471
    if [ -d "$config" ]; then
 
472
      pid_file=`sed -ne \
 
473
               's/^olcPidFile:[[:space:]]\+\(.\+\)[[:space:]]*/\1/p' \
 
474
               "$config"/'cn=config.ldif' 2>/dev/null`
 
475
    elif [ -f "$config" ]; then
 
476
      pid_file=`sed -ne \
 
477
                's/^pidfile[[:space:]]\+\(.\+\)/\1/p' \
 
478
                "$config" 2>/dev/null`
 
479
    else
 
480
      if ocf_is_probe; then
 
481
        ocf_log info "slapd configuration '$config' does not exist during probe."
 
482
      else
 
483
        ocf_log err "slapd configuration '$config' does not exist."
 
484
        return $OCF_ERR_INSTALLED
 
485
      fi
 
486
    fi
 
487
  fi
 
488
 
 
489
  if [ -z "$user" ]; then
 
490
    user=`id -nu 2>/dev/null`
 
491
  elif ! id "$user" >/dev/null 2>&1; then
 
492
    ocf_log err "slapd user '$user' does not exist"
 
493
    return $OCF_ERR_INSTALLED
 
494
  fi
 
495
 
 
496
  if [ -z "$group" ]; then
 
497
    group=`id -ng 2>/dev/null`
 
498
  elif ! grep "^$group:" /etc/group >/dev/null 2>&1; then
 
499
    ocf_log err "slapd group '$group' does not exist"
 
500
    return $OCF_ERR_INSTALLED
 
501
  fi
 
502
 
 
503
  pid_dir=`dirname "$pid_file"`
 
504
  if [ ! -d "$pid_dir" ]; then
 
505
    mkdir -p "$pid_dir"
 
506
    chown -R "$user" "$pid_dir"
 
507
    chgrp -R "$group" "$pid_dir"
 
508
  fi
 
509
 
 
510
  return $OCF_SUCCESS
 
511
}
 
512
 
 
513
#
 
514
# Main
 
515
#
 
516
 
 
517
slapd=$OCF_RESKEY_slapd
 
518
ldapsearch=$OCF_RESKEY_ldapsearch
 
519
config=$OCF_RESKEY_config
 
520
user=$OCF_RESKEY_user
 
521
group=$OCF_RESKEY_group
 
522
services=$OCF_RESKEY_services
 
523
bind_dn=$OCF_RESKEY_bind_dn
 
524
password=$OCF_RESKEY_password
 
525
parameters=$OCF_RESKEY_parameters
 
526
pid_file=$OCF_RESKEY_pidfile
 
527
 
 
528
if [ -z "$config" ]; then
 
529
  if [ -e "/etc/ldap/slapd.d" ]; then
 
530
    config="/etc/ldap/slapd.d"
 
531
  else
 
532
    config="/etc/ldap/slapd.conf"
 
533
  fi
 
534
fi
 
535
 
 
536
if [ $# -ne 1 ]; then
 
537
  usage
 
538
  exit $OCF_ERR_ARGS
 
539
fi
 
540
 
 
541
case $1 in
 
542
  meta-data)
 
543
    meta_data
 
544
    exit $OCF_SUCCESS
 
545
    ;;
 
546
  usage|help)
 
547
    usage
 
548
    exit $OCF_SUCCESS
 
549
    ;;
 
550
esac
 
551
 
 
552
slapd_validate_all
 
553
rc=$?
 
554
[ $rc -eq $OCF_SUCCESS ] || exit $rc
 
555
 
 
556
case $1 in
 
557
  status)
 
558
    slapd_status `slapd_pid`; state=$?
 
559
 
 
560
    if [ $state -eq $OCF_SUCCESS ]; then
 
561
      ocf_log debug "slapd is running."
 
562
    elif [ $state -eq $OCF_NOT_RUNNING ]; then
 
563
      ocf_log debug "slapd is stopped."
 
564
    fi
 
565
 
 
566
    exit $state
 
567
    ;;
 
568
  start)
 
569
    slapd_start
 
570
    exit $?
 
571
    ;;
 
572
  stop)
 
573
    slapd_stop
 
574
    exit $?
 
575
    ;;
 
576
  monitor)
 
577
    slapd_monitor; state=$?
 
578
    exit $state
 
579
    ;;
 
580
  validate-all)
 
581
    exit $OCF_SUCCESS
 
582
    ;;
 
583
  *)
 
584
    usage
 
585
    exit $OCF_ERR_UNIMPLEMENTED
 
586
    ;;
 
587
esac