Skip to content

Commit b214496

Browse files
committed
Ability to run script before/after live migrations
1 parent e2a5df4 commit b214496

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

cloudstackops/xenserver.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949
# Class to handle XenServer patching
5050
class xenserver():
5151

52-
def __init__(self, ssh_user='root', threads=5):
52+
def __init__(self, ssh_user='root', threads=5, pre_empty_script='xenserver_pre_empty_script.sh',
53+
post_empty_script='xenserver_post_empty_script.sh'):
5354
self.ssh_user = ssh_user
5455
self.threads = threads
56+
self.pre_empty_script = pre_empty_script
57+
self.post_empty_script = post_empty_script
5558

5659
# Wait for hypervisor to become alive again
5760
def check_connect(self, host):
@@ -169,11 +172,16 @@ def host_evacuate(self, host):
169172

170173
# Reboot a host when all conditions are met
171174
def host_reboot(self, host, halt_hypervisor=False):
172-
# Disbale host
175+
# Disable host
173176
if self.host_disable(host) is False:
174177
print "Error: Disabling host " + host.name + " failed."
175178
return False
176179

180+
# Execute pre-empty-script
181+
if self.exec_script_on_hypervisor(host, self.pre_empty_script) is False:
182+
print "Error: Executing script '" + self.pre_empty_script + "' on host " + host.name + " failed."
183+
return False
184+
177185
# Then evacuate it
178186
if self.host_evacuate(host) is False:
179187
print "Error: Evacuating host " + host.name + " failed."
@@ -185,6 +193,11 @@ def host_reboot(self, host, halt_hypervisor=False):
185193
return False
186194
print "Note: Host " + host.name + " has no VMs running, continuing"
187195

196+
# Execute post-empty-script
197+
if self.exec_script_on_hypervisor(host, self.post_empty_script) is False:
198+
print "Error: Executing script '" + self.post_empty_script + "' on host " + host.name + " failed."
199+
return False
200+
188201
# Finally reboot it
189202
try:
190203
with settings(host_string=self.ssh_user + "@" + host.ipaddress):
@@ -209,6 +222,15 @@ def host_reboot(self, host, halt_hypervisor=False):
209222
print "Error: Enabling host " + host.name + " failed."
210223
return False
211224

225+
# Execute script on hypervisor
226+
def exec_script_on_hypervisor(self, host, script):
227+
print "Note: Executing script %s on host %s.." % (script, host.name)
228+
try:
229+
with settings(show('output'), host_string=self.ssh_user + "@" + host.ipaddress):
230+
return fab.run("bash /tmp/" + script)
231+
except:
232+
return False
233+
212234
# Get VM count of a hypervisor
213235
def host_get_vms(self, host):
214236
try:
@@ -275,6 +297,12 @@ def put_scripts(self, host):
275297
'/tmp/xenserver_fake_pvtools.sh', mode=0755)
276298
put('xenserver_parallel_evacuate.py',
277299
'/tmp/xenserver_parallel_evacuate.py', mode=0755)
300+
if len(self.pre_empty_script) > 0:
301+
put(self.pre_empty_script,
302+
'/tmp/' + self.pre_empty_script, mode=0755)
303+
if len(self.post_empty_script) > 0:
304+
put(self.post_empty_script,
305+
'/tmp/' + self.post_empty_script, mode=0755)
278306
return True
279307
except:
280308
print "Warning: Could not upload check scripts to host " + host.name + ". Continuing anyway."

xenserver_post_empty_script.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo "This is the post_empty script you could customise."

xenserver_pre_empty_script.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo "This is the pre_empty script you could customise."

xenserver_rolling_reboot.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def handleArguments(argv):
5757
threads = 5
5858
global halt_hypervisor
5959
halt_hypervisor = False
60-
60+
global pre_empty_script
61+
pre_empty_script = 'xenserver_pre_empty_script.sh'
62+
global post_empty_script
63+
post_empty_script = 'xenserver_post_empty_script.sh'
6164
# Usage message
6265
help = "Usage: ./" + os.path.basename(__file__) + ' [options]' + \
6366
'\n --config-profile -c <profilename>\t\tSpecify the CloudMonkey profile name to ' \
@@ -68,14 +71,19 @@ def handleArguments(argv):
6871
'\n --threads <nr>\t\t\t\tUse this number or concurrent migration threads ' + \
6972
'\n --halt\t\t\t\t\tInstead of the default reboot, halt the hypervisor (useful in case of hardware ' \
7073
'upgrades) ' + \
74+
'\n --pre-empty-script\t\t\t\tBash script to run on hypervisor before starting the live migrations to empty ' \
75+
'hypervisor (expected in same folder as this script)' + \
76+
'\n --post-empty-script\t\t\t\tBash script to run on hypervisor after a hypervisor has no more VMs running' \
77+
'(expected in same folder as this script)' + \
7178
'\n --debug\t\t\t\t\tEnable debug mode' + \
7279
'\n --exec\t\t\t\t\tExecute for real' + \
7380
'\n --prepare\t\t\t\t\tExecute some prepare commands'
7481

7582
try:
7683
opts, args = getopt.getopt(
7784
argv, "hc:n:t:p", [
78-
"credentials-file=", "clustername=", "ignore-hosts=", "threads=", "halt", "debug", "exec", "prepare"])
85+
"credentials-file=", "clustername=", "ignore-hosts=", "threads=", "pre-empty-script=",
86+
"post-empty-script=", "halt", "debug", "exec", "prepare"])
7987
except getopt.GetoptError as e:
8088
print "Error: " + str(e)
8189
print help
@@ -95,6 +103,10 @@ def handleArguments(argv):
95103
ignoreHostList = arg
96104
elif opt in ("--halt"):
97105
halt_hypervisor = True
106+
elif opt in ("--pre-empty-script"):
107+
pre_empty_script = arg
108+
elif opt in ("--post-empty-script"):
109+
post_empty_script = arg
98110
elif opt in ("--debug"):
99111
DEBUG = 1
100112
elif opt in ("--exec"):

0 commit comments

Comments
 (0)