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

« back to all changes in this revision

Viewing changes to heartbeat/postfix

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2011-06-10 16:26:35 UTC
  • Revision ID: james.westby@ubuntu.com-20110610162635-yiy0vfopqw4trzgx
Tags: upstream-3.9.0
ImportĀ upstreamĀ versionĀ 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Resource script for Postfix
 
4
#
 
5
# Description:  Manages Postfix as an OCF resource in
 
6
#               an high-availability setup.
 
7
#
 
8
#               Tested with postfix 2.5.5 on Debian 5.0.
 
9
#               Based on the mysql-proxy and mysql OCF resource agents.
 
10
#
 
11
# Author:       Raoul Bhatia <r.bhatia@ipax.at> : Original Author
 
12
# License:      GNU General Public License (GPL)
 
13
# Note:         if you want to run multiple postfix instances, please see
 
14
#               http://amd.co.at/adminwiki/Postfix#Adding_a_Second_Postfix_Instance_on_one_Server
 
15
#               http://www.postfix.org/postconf.5.html
 
16
#
 
17
#
 
18
#       usage: $0 {start|stop|reload|status|monitor|validate-all|meta-data}
 
19
#
 
20
#       The "start" arg starts a Postfix instance
 
21
#
 
22
#       The "stop" arg stops it.
 
23
#
 
24
#
 
25
# Test via
 
26
# * /usr/sbin/ocf-tester -n post1 /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
 
27
# * /usr/sbin/ocf-tester -n post1 -o binary="/usr/sbin/postfix" \
 
28
#       -o config_dir="" /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
 
29
# * /usr/sbin/ocf-tester -n post1 -o binary="/usr/sbin/postfix" \
 
30
#       -o config_dir="/root/postfix/" /usr/lib/ocf/resource.d/heartbeat/postfix; echo $?
 
31
#
 
32
#
 
33
# OCF parameters:
 
34
#  OCF_RESKEY_binary
 
35
#  OCF_RESKEY_config_dir
 
36
#  OCF_RESKEY_parameters
 
37
#
 
38
##########################################################################
 
39
 
 
40
# Initialization:
 
41
 
 
42
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
 
43
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
 
44
 
 
45
: ${OCF_RESKEY_binary="/usr/sbin/postfix"}
 
46
: ${OCF_RESKEY_config_dir=""}
 
47
: ${OCF_RESKEY_parameters=""}
 
48
USAGE="Usage: $0 {start|stop|reload|status|monitor|validate-all|meta-data}";
 
49
 
 
50
##########################################################################
 
51
 
 
52
usage() {
 
53
    echo $USAGE >&2
 
54
}
 
55
 
 
56
meta_data() {
 
57
        cat <<END
 
58
<?xml version="1.0"?>
 
59
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
 
60
<resource-agent name="postfix">
 
61
<version>0.1</version>
 
62
<longdesc lang="en">
 
63
This script manages Postfix as an OCF resource in a high-availability setup.
 
64
Tested with Postfix 2.5.5 on Debian 5.0.
 
65
</longdesc>
 
66
<shortdesc lang="en">Manages a highly available Postfix mail server instance</shortdesc>
 
67
 
 
68
<parameters>
 
69
 
 
70
<parameter name="binary" unique="0" required="0">
 
71
<longdesc lang="en">
 
72
Full path to the Postfix binary.
 
73
For example, "/usr/sbin/postfix".
 
74
</longdesc>
 
75
<shortdesc lang="en">Full path to Postfix binary</shortdesc>
 
76
<content type="string" default="/usr/sbin/postfix" />
 
77
</parameter>
 
78
 
 
79
<parameter name="config_dir" unique="1" required="0">
 
80
<longdesc lang="en">
 
81
Full path to a Postfix configuration directory.
 
82
For example, "/etc/postfix".
 
83
</longdesc>
 
84
<shortdesc lang="en">Full path to configuration directory</shortdesc>
 
85
<content type="string" default="" />
 
86
</parameter>
 
87
 
 
88
<parameter name="parameters" unique="0" required="0">
 
89
<longdesc lang="en">
 
90
The Postfix daemon may be called with additional parameters.
 
91
Specify any of them here.
 
92
</longdesc>
 
93
<shortdesc lang="en"></shortdesc>
 
94
<content type="string" default="" />
 
95
</parameter>
 
96
 
 
97
</parameters>
 
98
 
 
99
<actions>
 
100
<action name="start"   timeout="20s" />
 
101
<action name="stop"    timeout="20s" />
 
102
<action name="reload"  timeout="20s" />
 
103
<action name="monitor" depth="0"  timeout="20s" interval="60s" />
 
104
<action name="validate-all"  timeout="20s" />
 
105
<action name="meta-data"  timeout="5s" />
 
106
</actions>
 
107
</resource-agent>
 
108
END
 
109
}
 
110
 
 
111
running() {
 
112
    # run postfix status
 
113
    $binary $OPTION_CONFIG_DIR status >/dev/null 2>&1
 
114
}
 
115
 
 
116
 
 
117
postfix_status()
 
118
{
 
119
    running
 
120
}
 
121
 
 
122
postfix_start()
 
123
{
 
124
    # if Postfix is running return success
 
125
    if postfix_status; then
 
126
        ocf_log info "Postfix already running."
 
127
        return $OCF_SUCCESS
 
128
    fi
 
129
 
 
130
    # start Postfix
 
131
    $binary $OPTIONS start >/dev/null 2>&1
 
132
    ret=$?
 
133
 
 
134
    if [ $ret -ne 0 ]; then
 
135
        ocf_log err "Postfix returned error." $ret
 
136
        return $OCF_ERR_GENERIC
 
137
    fi
 
138
 
 
139
    return $OCF_SUCCESS
 
140
}
 
141
 
 
142
 
 
143
postfix_stop()
 
144
{
 
145
    # if Postfix is not running return success
 
146
    if ! postfix_status; then
 
147
        ocf_log info "Postfix already stopped."
 
148
        return $OCF_SUCCESS
 
149
    fi
 
150
 
 
151
    # stop Postfix
 
152
    $binary $OPTIONS stop >/dev/null 2>&1
 
153
    ret=$?
 
154
 
 
155
    if [ $ret -ne 0 ]; then
 
156
        ocf_log err "Postfix returned an error while stopping." $ret
 
157
        return $OCF_ERR_GENERIC
 
158
    fi
 
159
 
 
160
    # grant some time for shutdown and recheck 5 times
 
161
    for i in 1 2 3 4 5; do
 
162
        if postfix_status; then
 
163
            sleep 1
 
164
        fi
 
165
    done
 
166
 
 
167
    # escalate to abort if we did not stop by now
 
168
    # @TODO shall we loop here too?
 
169
    if postfix_status; then
 
170
        ocf_log err "Postfix failed to stop. Escalating to 'abort'"
 
171
 
 
172
        $binary $OPTIONS abort >/dev/null 2>&1; ret=$?
 
173
        sleep 5
 
174
        postfix_status && $OCF_ERR_GENERIC
 
175
    fi
 
176
 
 
177
    return $OCF_SUCCESS
 
178
}
 
179
 
 
180
postfix_reload()
 
181
{
 
182
    if postfix_status; then
 
183
        ocf_log info "Reloading Postfix."
 
184
        $binary $OPTIONS reload
 
185
    fi
 
186
}
 
187
 
 
188
postfix_monitor()
 
189
{
 
190
    if postfix_status; then
 
191
        return $OCF_SUCCESS
 
192
    fi
 
193
 
 
194
    return $OCF_NOT_RUNNING
 
195
}
 
196
 
 
197
postfix_validate_all()
 
198
{
 
199
    # check that the Postfix binary exists and can be executed
 
200
    if [ ! -x "$binary" ]; then
 
201
        ocf_log err "Postfix binary '$binary' does not exist or cannot be executed."
 
202
        return $OCF_ERR_GENERIC
 
203
    fi
 
204
 
 
205
    # check config_dir and alternate_config_directories parameter
 
206
    if [ "x$config_dir" != "x" ]; then
 
207
        if [ ! -d "$config_dir" ]; then
 
208
            ocf_log err "Postfix configuration directory '$config_dir' does not exist." $ret
 
209
            return $OCF_ERR_GENERIC
 
210
        fi
 
211
 
 
212
        alternate_config_directories=`postconf -h alternate_config_directories 2>/dev/null | grep $config_dir`
 
213
        if [ "x$alternate_config_directories" = "x" ]; then
 
214
            ocf_log err "Postfix main configuration must contain correct 'alternate_config_directories' parameter."
 
215
            return $OCF_ERR_GENERIC
 
216
        fi
 
217
    fi
 
218
 
 
219
    # check spool/queue directory
 
220
    queue=`postconf $OPTION_CONFIG_DIR -h queue_directory 2>/dev/null`
 
221
    if [ ! -d "$queue" ]; then
 
222
        ocf_log err "Postfix spool/queue directory '$queue' does not exist." $ret
 
223
        return $OCF_ERR_GENERIC
 
224
    fi
 
225
 
 
226
    # run postfix internal check
 
227
    $binary $OPTIONS check >/dev/null 2>&1
 
228
    ret=$?
 
229
    if [ $ret -ne 0 ]; then
 
230
        ocf_log err "Postfix 'check' failed." $ret
 
231
        return $OCF_ERR_GENERIC
 
232
    fi
 
233
 
 
234
    return $OCF_SUCCESS
 
235
}
 
236
 
 
237
#
 
238
# Main
 
239
#
 
240
 
 
241
if [ $# -ne 1 ]; then
 
242
    usage
 
243
    exit $OCF_ERR_ARGS
 
244
fi
 
245
 
 
246
binary=$OCF_RESKEY_binary
 
247
config_dir=$OCF_RESKEY_config_dir
 
248
parameters=$OCF_RESKEY_parameters
 
249
 
 
250
# debugging stuff
 
251
#echo OCF_RESKEY_binary=$OCF_RESKEY_binary >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
 
252
#echo OCF_RESKEY_config_dir=$OCF_RESKEY_config_dir >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
 
253
#echo OCF_RESKEY_parameters=$OCF_RESKEY_parameters >> /tmp/prox_conf_$OCF_RESOURCE_INSTANCE
 
254
 
 
255
 
 
256
# build Postfix options string *outside* to access from each method
 
257
OPTIONS=''
 
258
OPTION_CONFIG_DIR=''
 
259
 
 
260
# check if the Postfix config_dir exist
 
261
if [ "x$config_dir" != "x" ]; then
 
262
    # save OPTION_CONFIG_DIR seperatly
 
263
    OPTION_CONFIG_DIR="-c $config_dir"
 
264
    OPTIONS=$OPTION_CONFIG_DIR
 
265
fi
 
266
 
 
267
if [ "x$parameters" != "x" ]; then
 
268
    OPTIONS="$OPTIONS $parameters"
 
269
fi
 
270
 
 
271
case $1 in
 
272
    meta-data)  meta_data
 
273
                exit $OCF_SUCCESS
 
274
                ;;
 
275
 
 
276
    usage|help) usage
 
277
                exit $OCF_SUCCESS
 
278
                ;;
 
279
esac
 
280
 
 
281
postfix_validate_all
 
282
ret=$?
 
283
 
 
284
#echo "debug[$1:$ret]"
 
285
LSB_STATUS_STOPPED=3
 
286
if [ $ret -ne $OCF_SUCCESS ]; then
 
287
    case $1 in
 
288
    stop)       exit $OCF_SUCCESS ;;
 
289
    monitor)    exit $OCF_NOT_RUNNING;;
 
290
    status)     exit $LSB_STATUS_STOPPED;;
 
291
    *)          exit $ret;;
 
292
    esac
 
293
fi
 
294
 
 
295
case $1 in
 
296
    monitor)    postfix_monitor
 
297
                exit $?
 
298
                ;;
 
299
    start)      postfix_start
 
300
                exit $?
 
301
                ;;
 
302
 
 
303
    stop)       postfix_stop
 
304
                exit $?
 
305
                ;;
 
306
 
 
307
    reload)     postfix_reload
 
308
                exit $?
 
309
                ;;
 
310
 
 
311
    status)     if postfix_status; then
 
312
                    ocf_log info "Postfix is running."
 
313
                    exit $OCF_SUCCESS
 
314
                else
 
315
                    ocf_log info "Postfix is stopped."
 
316
                    exit $OCF_NOT_RUNNING
 
317
                fi
 
318
                ;;
 
319
 
 
320
    validate-all)   exit $OCF_SUCCESS
 
321
                    ;;
 
322
 
 
323
    *)          usage
 
324
                exit $OCF_ERR_UNIMPLEMENTED
 
325
                ;;
 
326
esac