| 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 |
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 |
|
| 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() |