-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshutdown.py
279 lines (231 loc) · 9.74 KB
/
shutdown.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
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
278
279
#!/usr/bin/env python
# Copyright (c) Cloud Software Group, Inc.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Prepare to shutdown a host (assumed to exist) by:
# 1. attempting to evacuate running VMs to other hosts
# 2. remaining VMs are shutdown cleanly
# 3. remaining VMs are shutdown forcibly
#
# Step (1) and (2) can be skipped if the "force" option is specified.
# Clean shutdown attempts are run in parallel and are cancelled on
# timeout.
import sys, time
import signal
import syslog
import XenAPI
TIMEOUT_SECS=30
# class for determining whether xe is responsive
class TimeoutFunctionException(Exception):
"""Exception to raise on a timeout"""
pass
class TimeoutFunction:
def __init__(self, function, timeout):
self.timeout = timeout
self.function = function
def handle_timeout(self, signum, frame):
raise TimeoutFunctionException()
def __call__(self, *args):
old = signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.timeout)
try:
result = self.function(*args)
finally:
signal.signal(signal.SIGALRM, old)
signal.alarm(0)
return result
def task_is_pending(session, task):
try:
return session.xenapi.task.get_status(task) == "pending"
except:
return False
def wait_for_tasks(session, tasks, timeout):
"""returns true if all tasks are no longer pending (ie success/failure/cancelled)
and false if a timeout occurs"""
finished = False
start = time.time ()
while not(finished) and ((time.time () - start) < timeout):
finished = True
for task in tasks:
if task_is_pending(session, task):
finished = False
time.sleep(1)
return finished
def get_running_domains(session, host):
"""Return a list of (vm, record) pairs for all VMs running on the given host"""
vms = []
for vm in session.xenapi.host.get_resident_VMs(host):
record = session.xenapi.VM.get_record(vm)
if not record["is_control_domain"] and record["power_state"] == "Running":
if record['other_config'].has_key('auto_poweroff') and record['other_config'].get('auto_poweroff') == "false":
print "\n Skip running VM %s has self-managed power-off" % record["name_label"],
sys.stdout.flush()
continue
vms.append((vm,record))
return vms
def estimate_evacuate_timeout(session, host):
""" Rough estimation of the evacuate uplimit based on live VMs memory """
mref = session.xenapi.host.get_metrics(host)
metrics = session.xenapi.host_metrics.get_record(mref)
memory_used = int(metrics['memory_total']) - int(metrics['memory_free'])
# Conservative estimation based on 1000Mbps link, and the memory usage of
# Dom0 (which is not going to be transferred) is an intentional surplus
return (memory_used * 8. / (1000. * 1024 * 1024))
def host_evacuate(session, host):
"""Attempts a host evacuate. If the timeout expires then it attempts to cancel
any in-progress tasks it can find."""
rc = 0
print "\n Requesting evacuation of host",
sys.stdout.flush()
task = session.xenapi.Async.host.evacuate(host)
timeout = 240
try:
timeout = max(estimate_evacuate_timeout(session, host), timeout)
except Exception, e:
syslog.syslog(syslog.LOG_WARNING, "Evacuate timeout estimation error: %s, use default." % e)
try:
if not(wait_for_tasks(session, [ task ], timeout)):
print "\n Cancelling evacuation of host",
sys.stdout.flush()
session.xenapi.task.cancel(task)
for vm, record in get_running_domains(session, host):
current = record["current_operations"]
for t in current.keys():
try:
print "\n Cancelling operation on VM: %s" % record["name_label"],
sys.stdout.flush()
session.xenapi.task.cancel(t)
except:
print "Failed to cancel task: %s" % t,
sys.stdout.flush()
finally:
try:
session.xenapi.task.destroy(task)
except Exception:
# db gc thread in xapi may delete task from tasks table
print "\n Task %s has been destroyed" % task
sys.stdout.flush()
return rc
def parallel_clean_shutdown(session, vms):
"""Performs a parallel VM.clean_shutdown of all running VMs on a given host.
If the timeout expires then any in-progress tasks are cancelled."""
tasks = []
rc = 0
try:
for vm,record in vms:
if not "clean_shutdown" in record["allowed_operations"]:
continue
print "\n Requesting clean shutdown of VM: %s" % (record["name_label"]),
sys.stdout.flush()
task = session.xenapi.Async.VM.clean_shutdown(vm)
tasks.append((task,vm,record))
if not tasks:
return 0
if not(wait_for_tasks(session, map(lambda x:x[0], tasks), 60)):
# Cancel any remaining tasks.
for (task,_,record) in tasks:
try:
if task_is_pending(session, task):
print "\n Cancelling clean shutdown of VM: %s" % (record["name_label"]),
sys.stdout.flush()
session.xenapi.task.cancel(task)
except:
pass
if not(wait_for_tasks(session, map(lambda x:x[0], tasks), 60)):
for (_,vm,_) in tasks:
if session.xenapi.VM.get_power_state(vm) == "Running":
rc += 1
finally:
for (task,_,_) in tasks:
try:
session.xenapi.task.destroy(task)
except Exception:
# db gc thread in xapi may delete task from tasks table
print "\n Task %s has been destroyed" % task
sys.stdout.flush()
return rc
def serial_hard_shutdown(session, vms):
"""Performs a serial VM.hard_shutdown of all running VMs on a given host."""
rc = 0
try:
for (vm,record) in vms:
print "\n Requesting hard shutdown of VM: %s" % (record["name_label"]),
sys.stdout.flush()
try:
session.xenapi.VM.hard_shutdown(vm)
except:
print "\n Failure performing hard shutdown of VM: %s" % (record["name_label"]),
rc += 1
finally:
return rc
def main(session, host_uuid, force):
rc = 0
host = session.xenapi.host.get_by_uuid(host_uuid)
if not force:
# VMs which can't be evacuated should be shutdown first
vms = []
for vm in session.xenapi.host.get_vms_which_prevent_evacuation(host).keys():
r = session.xenapi.VM.get_record(vm)
# check for self-managed power off
if 'auto_poweroff' in r['other_config'] and r['other_config'].get('auto_poweroff') == "false":
print "\n VM %s has self-managed power-off" % r["name_label"],
sys.stdout.flush()
continue
print "\n VM %s cannot be evacuated" % r["name_label"],
sys.stdout.flush()
vms.append((vm, r))
rc += parallel_clean_shutdown(session, vms)
vms_f = filter(lambda (vm, _): session.xenapi.VM.get_power_state(vm) == "Running", vms)
# check for self-managed power off
vms = []
for (vm,record) in vms_f:
if 'auto_poweroff' in record['other_config'] and record['other_config'].get('auto_poweroff') == "false":
print "\n VM %s has self-managed power-off" % record["name_label"],
sys.stdout.flush()
continue
vms.append((vm, record))
rc += serial_hard_shutdown(session, vms)
# VMs which can be evacuated should be evacuated
rc += host_evacuate(session, host)
# Any remaining VMs should be shutdown
rc += parallel_clean_shutdown(session, get_running_domains(session, host))
else:
rc += serial_hard_shutdown(session, get_running_domains(session, host))
return rc
if __name__ == "__main__":
if len(sys.argv) != 2 and len(sys.argv) != 3:
print "Usage:"
print sys.argv[0], "<host-uuid> [--force]"
sys.exit(1)
uuid = sys.argv[1]
force = False
if len(sys.argv) == 3 and sys.argv[2] == "--force":
force = True
new_session = XenAPI.xapi_local()
connect_to_coordinator_with_timeout = TimeoutFunction(new_session.xenapi.login_with_password, TIMEOUT_SECS)
try:
connect_to_coordinator_with_timeout("root", "")
except TimeoutFunctionException:
print "Unable to connect to coordinator within %d seconds. Exiting." % TIMEOUT_SECS
sys.exit(1)
except Exception:
print 'Failed to connect to coordinator.'
sys.exit(2)
try:
rc = main(new_session, uuid, force)
sys.exit(rc)
except Exception, e:
print "Caught %s" % str(e)
sys.exit(1)
finally:
new_session.xenapi.session.logout()