summaryrefslogtreecommitdiffstats
path: root/agents/stonith/fence_legacy.in
blob: c0eeca1b926b7b3ad6656b11c19d261d9249176c (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
#!@PYTHON@

__copyright__ = "Copyright 2018-2023 the Pacemaker project contributors"
__license__ = "GNU General Public License version 2 or later (GPLv2+) WITHOUT ANY WARRANTY"

import os
import sys
import argparse
import subprocess

# These imports allow running from a source checkout after running `make`.
# Note that while this doesn't necessarily mean it will successfully run tests,
# but being able to see --help output can be useful.
if os.path.exists("@abs_top_srcdir@/python"):
    sys.path.insert(0, "@abs_top_srcdir@/python")

if os.path.exists("@abs_top_builddir@/python") and "@abs_top_builddir@" != "@abs_top_srcdir@":
    sys.path.insert(0, "@abs_top_builddir@/python")

from pacemaker.exitstatus import ExitStatus

VERSION = "1.1.0"

USAGE = """Helper that presents a Pacemaker-style interface for Linux-HA stonith plugins

Should never be invoked by the user directly


Usage: fence_legacy [options]

Options:
  -h               usage
  -t <sub agent>   sub agent
  -n <name>        nodename
  -o <string>      Action:  on | off | reset (default) | stat | hostlist
  -s <stonith>     stonith command
  -q               quiet mode
  -V               version"""

META_DATA = """<?xml version="1.0" ?>
<resource-agent name="fence_pcmk" shortdesc="Helper that presents a Pacemaker-style interface for Linux-HA stonith plugins">
<longdesc>
This agent should never be invoked by the user directly.
</longdesc>
<vendor-url>https://www.clusterlabs.org/</vendor-url>
<parameters>
        <parameter name="action" unique="1" required="1">
                <getopt mixed="-o &lt;action&gt;" />
                <content type="string" default="disable" />
                <shortdesc lang="en">Fencing Action</shortdesc>
        </parameter>
        <parameter name="port" unique="1" required="1">
                <getopt mixed="-n &lt;id&gt;" />
                <content type="string"  />
                <shortdesc lang="en">Physical plug number or name of virtual machine</shortdesc>
        </parameter>
        <parameter name="help" unique="1" required="0">
                <getopt mixed="-h" />
                <content type="string"  />
                <shortdesc lang="en">Display help and exit</shortdesc>
        </parameter>
</parameters>
<actions>
        <action name="enable" />
        <action name="disable" />
        <action name="reboot" />
        <action name="off" />
        <action name="on" />
        <action name="status" />
        <action name="list" />
        <action name="metadata" />
</actions>
</resource-agent>"""

ACTIONS = [
    "on",
    "off",
    "reset",
    "reboot",
    "stat",
    "status",
    "metadata",
    "monitor",
    "list",
    "hostlist",
    "poweroff",
    "poweron"
]


def parse_cli_options():
    """ Return parsed command-line options (as argparse namespace) """


    # Don't add standard help option, so we can format it how we want
    parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument("-t", metavar="SUBAGENT", dest="subagent",
                        nargs=1, default="none", help="sub-agent")

    parser.add_argument("-n", metavar="NODE", dest="node",
                        nargs=1, default="", help="name of target node")

    # The help text here is consistent with the original version, though
    # perhaps all actions should be listed.
    parser.add_argument("-o", metavar="ACTION", dest="action",
                        nargs=1, choices=ACTIONS, default="reset",
                        help="action: on | off | reset (default) | stat | hostlist")

    parser.add_argument("-s", metavar="COMMAND", dest="command",
                        nargs=1, default="stonith", help="stonith command")

    parser.add_argument("-q", dest="quiet", action="store_true",
                        help="quiet mode")

    parser.add_argument("-h", "--help", action="store_true",
                        help="show usage and exit")

    # Don't use action="version", because that printed to stderr before
    # Python 3.4, and help2man doesn't like that.
    parser.add_argument("-V", "--version", action="store_true",
                        help="show version and exit")

    return parser.parse_args()


def parse_stdin_options(options):
    """ Update options namespace with options parsed from stdin """

    nlines = 0
    for line in sys.stdin:
        # Remove leading and trailing whitespace
        line = line.strip()

        # Skip blank lines and comments
        if line == "" or line[0] == "#":
            continue

        nlines = nlines + 1

        # Parse option name and value (allow whitespace around equals sign)
        try:
            (name, value) = line.split("=", 1)
            name = name.rstrip()
            if name == "":
                raise ValueError
        except ValueError:
            print("parse error: illegal name in option %d" % nlines,
                  file=sys.stderr)
            sys.exit(ExitStatus.INVALID_PARAM)
        value = value.lstrip()

        if name == "plugin":
            options.subagent = value

        elif name in [ "option", "action" ]:
            options.action = value

        elif name == "nodename":
            options.node = value
            os.environ[name] = value

        elif name == "stonith":
            options.command = value

        elif name != "agent": # agent is used by fenced
            os.environ[name] = value


def normalize_options(options):
    """ Use string rather than list of one string """

    if not hasattr(options.subagent, "strip"):
        options.subagent = options.subagent[0]

    if not hasattr(options.node, "strip"):
        options.node = options.node[0]

    if not hasattr(options.action, "strip"):
        options.action = options.action[0]

    if not hasattr(options.command, "strip"):
        options.command = options.command[0]


def build_command(options):
    """ Return command to execute (as list of arguments) """

    if options.action in [ "hostlist", "list" ]:
        extra_args = [ "-l" ]

    elif options.action in [ "monitor", "stat", "status" ]:
        extra_args = [ "-S" ]

    else:
        if options.node == "":
            if not options.quiet:
                print("failed: no plug number")
            sys.exit(ExitStatus.ERROR)
        extra_args = [ "-T", options.action, options.node ]

    return [ options.command, "-t", options.subagent, "-E" ] + extra_args


def handle_local_options(options):
    """ Handle options that don't require the fence agent """

    if options.help:
        print(USAGE)
        sys.exit(ExitStatus.OK)

    if options.version:
        print(VERSION)
        sys.exit(ExitStatus.OK)


def remap_action(options):
    """ Pre-process requested action """

    options.action = options.action.lower()

    if options.action == "metadata":
        print(META_DATA)
        sys.exit(ExitStatus.OK)

    elif options.action in [ "hostlist", "list" ]:
        options.quiet = True

    # Remap accepted aliases to their actual commands

    elif options.action == "reboot":
        options.action = "reset"

    elif options.action == "poweron":
        options.action = "on"

    elif options.action == "poweroff":
        options.action = "off"


def execute_command(options, cmd):
    """ Execute command and return its exit status """

    if not options.quiet:
        print("Performing: " + " ".join(cmd))
    return subprocess.call(cmd)


def handle_result(options, status):
    """ Process fence agent result """

    if status == 0:
        message = "success"
        exitcode = ExitStatus.OK
    else:
        message = "failed"
        exitcode = ExitStatus.ERROR
    if not options.quiet:
        print("%s: %s %d" % (message, options.node, status))
    sys.exit(exitcode)


def main():
    """ Execute an LHA-style fence agent """

    options = parse_cli_options()
    handle_local_options(options)
    normalize_options(options)
    parse_stdin_options(options)
    remap_action(options)
    cmd = build_command(options)
    status = execute_command(options, cmd)
    handle_result(options, status)


if __name__ == "__main__":
    main()