-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
156 lines (134 loc) · 4.79 KB
/
controller.py
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
import libvirt
import sys
from xml.dom import minidom
CONNSTRING = "qemu:///system"
def _get_ro_conn():
"""Returns a read-only connection to the local qemu hypervisor"""
conn = libvirt.openReadOnly(CONNSTRING)
if conn == None:
print("Failed to open connection to qemu:///system", file=sys.stderr)
exit(1)
else:
return conn
def _get_conn():
"""Returns a read/write connection to the local qemu hypervisor"""
conn = libvirt.open(CONNSTRING)
if conn == None:
print("Failed to open connection to qemu:///system", file=sys.stderr)
exit(1)
else:
return conn
def list_all_domains():
"""Returns a list of all domains on a hypervisor, active or not"""
conn = _get_ro_conn()
domain_names = conn.listDefinedDomains()
domain_ids = conn.listDomainsID()
if domain_ids == None:
print("Failed to get a list of domain IDs", file=sys.stderr)
conn.close()
exit(1)
if len(domain_ids) != 0:
for domain_id in domain_ids:
domain = conn.lookupByID(domain_id)
domain_names.append(domain.name())
elif len(domain_ids) == 0:
conn.close()
return "No domains"
conn.close()
return domain_names
def list_active_domains():
"""Returns a list of active domains on this hypervisor"""
conn = _get_ro_conn()
domain_names = {}
domain_ids = conn.listDomainsID()
if domain_ids == None:
print("Failed to get a list of domain IDs", file=sys.stderr)
conn.close()
exit(1)
if len(domain_ids) == 0:
conn.close()
return "No active domains"
else:
for domain_id in domain_ids:
domain = conn.lookupByID(domain_id)
domain_names[domain_id] = domain.name()
conn.close()
return domain_names
def domain_state(domain_name):
"""Takes in the name of a domain; returns the state of the domain and the reason it is in that state"""
conn = _get_ro_conn()
domain = conn.lookupByName(domain_name)
if domain == None:
print("Failed to find the domain " + domain_name, file=sys.stderr)
conn.close()
exit(1)
state_code, reason_code = domain.state()
domain_states = {
0: "VIR_DOMAIN_NOSTATE",
1: "VIR_DOMAIN_RUNNING",
2: "VIR_DOMAIN_BLOCKED",
3: "VIR_DOMAIN_PAUSED",
4: "VIR_DOMAIN_SHUTDOWN",
5: "VIR_DOMAIN_SHUTOFF",
6: "VIR_DOMAIN_CRASHED",
7: "VIR_DOMAIN_PMSUSPENDED",
}
domain_reasons = {
"VIR_DOMAIN_NOSTATE": {0: "VIR_DOMAIN_NOSTATE_UNKNOWN"},
"VIR_DOMAIN_RUNNING": {
0: "VIR_DOMAIN_RUNNING_UNKNOWN",
1: "VIR_DOMAIN_RUNNING_BOOTED",
2: "VIR_DOMAIN_RUNNING_MIGRATED",
3: "VIR_DOMAIN_RUNNING_RESTORED",
4: "VIR_DOMAIN_RUNNING_FROM_SNAPSHOT",
5: "VIR_DOMAIN_RUNNING_UNPAUSED",
6: "VIR_DOMAIN_RUNNING_MIGRATION_CANCELED",
7: "VIR_DOMAIN_RUNNING_SAVE_CANCELED",
8: "VIR_DOMAIN_RUNNING_WAKEUP",
9: "VIR_DOMAIN_RUNNING_CRASHED",
10: "VIR_DOMAIN_RUNNING_POSTCOPY",
},
"VIR_DOMAIN_BLOCKED": {0: "VIR_DOMAIN_BLOCKED_UNKNOWN"},
"VIR_DOMAIN_PAUSED": {
0: "VIR_DOMAIN_PAUSED_UNKNOWN",
1: "VIR_DOMAIN_PAUSED_USER",
2: "VIR_DOMAIN_PAUSED_MIGRATION",
3: "VIR_DOMAIN_PAUSED_SAVE",
4: "VIR_DOMAIN_PAUSED_DUMP",
5: "VIR_DOMAIN_PAUSED_IOERROR",
6: "VIR_DOMAIN_PAUSED_WATCHDOG",
7: "VIR_DOMAIN_PAUSED_FROM_SNAPSHOT",
8: "VIR_DOMAIN_PAUSED_SHUTTING_DOWN",
9: "VIR_DOMAIN_PAUSED_SNAPSHOT",
10: "VIR_DOMAIN_PAUSED_CRASHED",
11: "VIR_DOMAIN_PAUSED_STARTING_UP",
12: "VIR_DOMAIN_PAUSED_POSTCOPY",
13: "VIR_DOMAIN_PAUSED_POSTCOPY_FAILED",
},
"VIR_DOMAIN_SHUTDOWN": {
0: "VIR_DOMAIN_SHUTDOWN_UNKNOWN",
1: "VIR_DOMAIN_SHUTDOWN_USER",
},
"VIR_DOMAIN_SHUTOFF": {
0: "VIR_DOMAIN_SHUTOFF_UNKNOWN",
1: "VIR_DOMAIN_SHUTOFF_SHUTDOWN",
2: "VIR_DOMAIN_SHUTOFF_DESTROYED",
3: "VIR_DOMAIN_SHUTOFF_CRASHED",
4: "VIR_DOMAIN_SHUTOFF_MIGRATED",
5: "VIR_DOMAIN_SHUTOFF_SAVED",
6: "VIR_DOMAIN_SHUTOFF_FAILED",
7: "VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT",
8: "VIR_DOMAIN_SHUTOFF_DAEMON",
},
"VIR_DOMAIN_CRASHED": {
0: "VIR_DOMAIN_CRASHED_UNKNOWN",
1: "VIR_DOMAIN_CRASHED_PANICKED",
},
"VIR_DOMAIN_PMSUSPENDED": {
0: "VIR_DOMAIN_PMSUSPENDED_DISK_UNKNOWN",
},
}
state = domain_states[state_code]
reason = domain_reasons[state][reason_code]
conn.close()
return state, reason