Debian Codegraph @

debian/molly-guard-helper

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # molly-guard helper script to detect running Ganeti instances.
4 #
5 # Copyright (c) 2014-2020 Apollon Oikonomopoulos <[email protected]>
6 #
7 # This program is free software; you can redistribute it
8 # and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # This program is distributed in the hope that it will be
14 # useful, but WITHOUT ANY WARRANTY; without even the implied
15 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 # PURPOSE. See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public
20 # License along with this package; if not, write to the Free
21 # Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 # Boston, MA 02110-1301 USA
23 #
24
25 import os
26 import sys
27 import signal
28 import socket
29
30
31 try:
32 from ganeti.ssconf import SimpleStore
33 from ganeti.hypervisor import GetHypervisor
34 from ganeti.errors import HypervisorError, ConfigurationError
35 except ImportError:
36 sys.stderr.write("W: unable to import Ganeti code\n")
37 sys.exit(0)
38
39
40 def sigh(action, hostname):
41 """ Exit with an error code to prevent system shutdown/reboot """
42 sys.stdout.write("Good thing I asked; I won't %s %s ...\n" %
43 (action, hostname))
44 sys.exit(1)
45
46
47 def main():
48 action = os.environ.get("MOLLYGUARD_CMD", "?")
49 hostname = socket.gethostname()
50
51 for sig in (signal.SIGHUP, signal.SIGTERM, signal.SIGQUIT):
52 signal.signal(sig, lambda frame, stack: sigh(action, hostname))
53
54 conf = SimpleStore()
55
56 instances = []
57 try:
58 enabled_hvs = conf.GetHypervisorList()
59 except ConfigurationError:
60 # Cluster is probably not initialized
61 sys.stderr.write("W: failed to get hypervisor list"
62 " (not part of a cluster?)\n")
63 sys.exit(0)
64
65 for hv_type in enabled_hvs:
66 hv = GetHypervisor(hv_type)
67
68 try:
69 instances += hv.ListInstances()
70 except HypervisorError as err:
71 sys.stderr.write("W: unable to list %s instances: %s\n" %
72 (hv_type, str(err)))
73
74 current_master = conf.GetMasterNode()
75
76 node_is_current_master = False
77 if hostname == current_master:
78 node_is_current_master = True
79
80 if instances or node_is_current_master:
81 if instances:
82 sys.stdout.write("W: The following Ganeti instances will be"
83 " terminated abnormally if you %s this system:\n" %
84 action)
85 for instance in sorted(instances):
86 sys.stdout.write(" - %s\n" % instance)
87
88 if node_is_current_master:
89 sys.stdout.write("W: This system is the current Ganeti master"
90 " for this cluster\n")
91
92 try:
93 response = input("Type YES to %s the system: " % action)
94 except KeyboardInterrupt:
95 sigh(action, hostname)
96
97 if response.lower().strip() != "yes":
98 sigh(action, hostname)
99
100 sys.exit(0)
101
102
103 if __name__ == "__main__":
104 if not sys.stdin.isatty():
105 sys.exit(0)
106
107 main()