summaryrefslogtreecommitdiffstats
path: root/heartbeat/corosync-qnetd
blob: 6b9777711679cd2e5882f5a9968ff2db79c438bb (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/sh
#
# Copyright (C) 2022 Red Hat, Inc.  All rights reserved.
#
# Authors: Jan Friesse <jfriesse@redhat.com>
#
# 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.
#

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

# Use runuser if available for SELinux.
if [ -x "/sbin/runuser" ]; then
    SU="runuser"
else
    SU="su"
fi

# Attempt to detect a default binary
OCF_RESKEY_binary_default=$(which corosync-qnetd 2> /dev/null)
if [ "${OCF_RESKEY_binary_default}" = "" ]; then
    OCF_RESKEY_binary_default="/usr/bin/corosync-qnetd"
fi

# Defaults
OCF_RESKEY_qnetd_opts_default=""
OCF_RESKEY_qnetd_tool_binary_default="/usr/bin/corosync-qnetd-tool"
OCF_RESKEY_ip_default=""
OCF_RESKEY_port_default=""
OCF_RESKEY_nss_db_dir_default=""
OCF_RESKEY_pid_default="/var/run/corosync-qnetd/corosync-qnetd-${OCF_RESOURCE_INSTANCE}.pid"
OCF_RESKEY_ipc_sock_default="/var/run/corosync-qnetd/corosync-qnetd-${OCF_RESOURCE_INSTANCE}.sock"
OCF_RESKEY_user_default="coroqnetd"
OCF_RESKEY_group_default="${OCF_RESKEY_user_default}"

: "${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}"
: "${OCF_RESKEY_qnetd_opts=${OCF_RESKEY_qnetd_opts_default}}"
: "${OCF_RESKEY_qnetd_tool_binary=${OCF_RESKEY_qnetd_tool_binary_default}}"
: "${OCF_RESKEY_ip=${OCF_RESKEY_ip_default}}"
: "${OCF_RESKEY_port=${OCF_RESKEY_port_default}}"
: "${OCF_RESKEY_nss_db_dir=${OCF_RESKEY_nss_db_dir_default}}"
: "${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}"
: "${OCF_RESKEY_ipc_sock=${OCF_RESKEY_ipc_sock_default}}"
: "${OCF_RESKEY_user=${OCF_RESKEY_user_default}}"
: "${OCF_RESKEY_group=${OCF_RESKEY_group_default}}"

corosync_qnetd_usage() {
    cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

corosync_qnetd_meta_data() {
        cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="corosync-qnetd" version="1.0">
<version>1.0</version>

<longdesc lang="en">OCF Resource script for corosync-qnetd. It manages a corosync-qnetd
instance as a HA resource. It is required to copy nss db directory (usually /etc/corosync/qnetd/nssdb)
across all nodes (only once - after database is initialized).</longdesc>
<shortdesc lang="en">Corosync QNet daemon resource agent</shortdesc>

<parameters>

<parameter name="binary">
    <longdesc lang="en">Location of the corosync-qnetd binary</longdesc>
    <shortdesc lang="en">corosync-qnetd binary</shortdesc>
    <content type="string" default="${OCF_RESKEY_binary_default}" />
</parameter>

<parameter name="qnetd_opts">
    <longdesc lang="en">
        Additional options for corosync-qnetd binary. "-4" for example.
    </longdesc>
    <shortdesc lang="en">corosync-qnetd extra options</shortdesc>
    <content type="string" default="${OCF_RESKEY_qnetd_opts_default}" />
</parameter>

<parameter name="qnetd_tool_binary">
    <longdesc lang="en">
        The absolute path to the corosync-qnetd-tool for monitoring with OCF_CHECK_LEVEL greater zero.
    </longdesc>
    <shortdesc lang="en">The absolute path to the corosync-qnetd-tool binary</shortdesc>
    <content type="string" default="${OCF_RESKEY_qnetd_tool_binary_default}" />
</parameter>

<parameter name="ip">
    <longdesc lang="en">
        IP address to listen on. By default the daemon listens on all addresses (wildcard).
    </longdesc>
    <shortdesc lang="en">IP address to listen on</shortdesc>
    <content type="string" default="${OCF_RESKEY_ip_default}" />
</parameter>

<parameter name="port">
    <longdesc lang="en">
        TCP port to listen on. Default port is 5403.
    </longdesc>
    <shortdesc lang="en">TCP port to listen on</shortdesc>
    <content type="string" default="${OCF_RESKEY_port_default}" />
</parameter>

<parameter name="nss_db_dir">
    <longdesc lang="en">
        Location of the corosync-qnetd nss db directory (empty for default - usually /etc/corosync/qnetd/nssdb)
    </longdesc>
    <shortdesc lang="en">corosync-qnetd nss db directory</shortdesc>
    <content type="string" default="${OCF_RESKEY_nss_db_dir_default}" />
</parameter>

<parameter name="pid">
    <longdesc lang="en">
        Location of the corosync-qnetd pid/lock
    </longdesc>
    <shortdesc lang="en">corosync-qnetd pid file</shortdesc>
    <content type="string" default="${OCF_RESKEY_pid_default}" />
</parameter>

<parameter name="ipc_sock">
    <longdesc lang="en">
        Location of the corosync-qnetd ipc socket
    </longdesc>
    <shortdesc lang="en">corosync-qnetd ipc socket file</shortdesc>
    <content type="string" default="${OCF_RESKEY_ipc_sock_default}" />
</parameter>

<parameter name="user">
    <longdesc lang="en">User running corosync-qnetd</longdesc>
    <shortdesc lang="en">corosync-qnetd user</shortdesc>
    <content type="string" default="${OCF_RESKEY_user_default}" />
</parameter>

<parameter name="group">
    <longdesc lang="en">Group running corosync-qnetd</longdesc>
    <shortdesc lang="en">corosync-qnetd group</shortdesc>
    <content type="string" default="${OCF_RESKEY_group_default}" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="20s" />
<action name="stop" timeout="20s" />
<action name="status" timeout="20s" />
<action name="monitor" depth="0" timeout="20s" interval="10s" start-delay="10s" />
<action name="validate-all" timeout="20s" />
<action name="meta-data" timeout="20s" />
</actions>
</resource-agent>
END
}

corosync_qnetd_status() {
    ocf_pidfile_status "${OCF_RESKEY_pid}" > /dev/null 2>&1
    case "$?" in
        0)
            rc="$OCF_SUCCESS"
            ;;
        1|2)
            rc="$OCF_NOT_RUNNING"
            ;;
        *)
            rc="$OCF_ERR_GENERIC"
            ;;
    esac

    return "$rc"
}

corosync_qnetd_start() {
    corosync_qnetd_validate_all
    rc="$?"

    if [ "$rc" -ne 0 ]; then
        return "$rc"
    fi

    # if resource is already running,no need to continue code after this.
    if corosync_qnetd_status; then
        ocf_log info "corosync-qnetd is already running"
        return "${OCF_SUCCESS}"
    fi

    pid_dir=$(dirname "${OCF_RESKEY_pid}")
    sock_dir=$(dirname "${OCF_RESKEY_ipc_sock}")

    for d in "$pid_dir" "$sock_dir";do
        if [ ! -d "$d" ];then
            mkdir -p "$d"
            chmod 0770 "$d"
            chown "${OCF_RESKEY_user}:${OCF_RESKEY_group}" "$d"
        fi
    done

    params="-S \"local_socket_file=${OCF_RESKEY_ipc_sock}\" -S \"lock_file=${OCF_RESKEY_pid}\""

    if [ -n "${OCF_RESKEY_nss_db_dir}" ];then
        params="$params -S \"nss_db_dir=${OCF_RESKEY_nss_db_dir}\""
    fi

    if [ -n "${OCF_RESKEY_ip}" ];then
        params="$params -l \"${OCF_RESKEY_ip}\""
    fi

    if [ -n "${OCF_RESKEY_port}" ];then
        params="$params -p \"${OCF_RESKEY_port}\""
    fi

    params="$params ${OCF_RESKEY_qnetd_opts}"

    ocf_run "$SU" -s "/bin/sh" "${OCF_RESKEY_user}" -c "${OCF_RESKEY_binary} $params"

    while :; do
        corosync_qnetd_monitor "debug"
        rc="$?"

        if [ "$rc" -eq "${OCF_SUCCESS}" ]; then
            break
        fi
        sleep 1

        ocf_log debug "corosync-qnetd still hasn't started yet. Waiting..."
    done

    ocf_log info "corosync-qnetd started"
    return "${OCF_SUCCESS}"
}

corosync_qnetd_stop() {
    corosync_qnetd_status

    if [ "$?" -ne "$OCF_SUCCESS" ]; then
        # Currently not running. Nothing to do.
        ocf_log info "corosync-qnetd is already stopped"

        return "$OCF_SUCCESS"
    fi

    pid=$(cat "${OCF_RESKEY_pid}")
    kill "$pid"

    # Wait for process to stop
    while corosync_qnetd_monitor "debug"; do
        sleep 1
    done

    ocf_log info "corosync-qnetd stopped"
    return "$OCF_SUCCESS"
}

corosync_qnetd_monitor() {
    loglevel=${1:-err}

    corosync_qnetd_status
    rc="$?"

    if [ "$rc" -ne "$OCF_SUCCESS" ];then
        return "$rc"
    fi

    out=$("${OCF_RESKEY_qnetd_tool_binary}" -s -p "${OCF_RESKEY_ipc_sock}" 2>&1 >/dev/null)
    rc="$?"

    if [ "$rc" != 0 ];then
        ocf_log "$loglevel" "$out"
    fi

    case "$rc" in
        "0") rc="$OCF_SUCCESS" ;;
        "3") rc="$OCF_NOT_RUNNING" ;;
        *) rc="$OCF_ERR_GENERIC" ;;
    esac

    return "$rc"
}

corosync_qnetd_validate_all() {
    check_binary "${OCF_RESKEY_binary}"

    check_binary "${OCF_RESKEY_qnetd_tool_binary}"
}


# **************************** MAIN SCRIPT ************************************

# Make sure meta-data and usage always succeed
case "$__OCF_ACTION" in
    meta-data)
        corosync_qnetd_meta_data
        exit "$OCF_SUCCESS"
        ;;
    usage|help)
        corosync_qnetd_usage
        exit "$OCF_SUCCESS"
        ;;
esac

# This OCF agent script need to be run as root user.
if ! ocf_is_root; then
        echo "$0 agent script need to be run as root user."
        ocf_log debug "$0 agent script need to be run as root user."
        exit "$OCF_ERR_GENERIC"
fi

# Translate each action into the appropriate function call
case "$__OCF_ACTION" in
    start)
        corosync_qnetd_start
        ;;
    stop)
        corosync_qnetd_stop
        ;;
    status)
        corosync_qnetd_status
        ;;
    monitor)
        corosync_qnetd_monitor
        ;;
    validate-all)
        corosync_qnetd_validate_all
        ;;
    *)
        corosync_qnetd_usage
        exit "$OCF_ERR_UNIMPLEMENTED"
        ;;
esac

rc="$?"
exit "$rc"
# End of this script