4949# Class to handle XenServer patching
5050class 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."
0 commit comments