File: molly-guard-helper

package info (click to toggle)
ganeti 2.16.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 16,660 kB
  • sloc: python: 105,115; haskell: 43,768; sh: 3,896; makefile: 2,763
file content (107 lines) | stat: -rwxr-xr-x 3,253 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/python
#
# molly-guard helper script to detect running Ganeti instances.
#
# Copyright (c) 2014 Apollon Oikonomopoulos <apoikos@debian.org>
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with this package; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA  02110-1301 USA
#

import os
import sys
import signal
import socket


try:
    from ganeti.ssconf import SimpleStore
    from ganeti.hypervisor import GetHypervisor
    from ganeti.errors import HypervisorError, ConfigurationError
except ImportError:
    sys.stderr.write("W: unable to import Ganeti code\n")
    sys.exit(0)


def sigh(action, hostname):
    """ Exit with an error code to prevent system shutdown/reboot """
    sys.stdout.write("Good thing I asked; I won't %s %s ...\n" %
                     (action, hostname))
    sys.exit(1)


def main():
    action = os.environ.get("MOLLYGUARD_CMD", "?")
    hostname = socket.gethostname()

    for sig in (signal.SIGHUP, signal.SIGTERM, signal.SIGQUIT):
        signal.signal(sig, lambda frame, stack: sigh(action, hostname))

    conf = SimpleStore()

    instances = []
    try:
        enabled_hvs = conf.GetHypervisorList()
    except ConfigurationError:
        # Cluster is probably not initialized
        sys.stderr.write("W: failed to get hypervisor list"
                         " (not part of a cluster?)\n")
        sys.exit(0)

    for hv_type in enabled_hvs:
        hv = GetHypervisor(hv_type)

        try:
            instances += hv.ListInstances()
        except HypervisorError as err:
            sys.stderr.write("W: unable to list %s instances: %s\n" %
                             (hv_type, str(err)))

    current_master = conf.GetMasterNode()

    node_is_current_master = False
    if hostname == current_master:
        node_is_current_master = True

    if instances or node_is_current_master:
        if instances:
            sys.stdout.write("W: The following Ganeti instances will be"
                             " terminated abnormally if you %s this system:\n" %
                             action)
            for instance in sorted(instances):
                sys.stdout.write("   - %s\n" % instance)

        if node_is_current_master:
            sys.stdout.write("W: This system is the current Ganeti master"
                             " for this cluster\n")

        try:
            response = raw_input("Type YES to %s the system: " % action)
        except KeyboardInterrupt:
            sigh(action, hostname)

        if response.lower().strip() != "yes":
            sigh(action, hostname)

    sys.exit(0)


if __name__ == "__main__":
    if not sys.stdin.isatty():
        sys.exit(0)

    main()