2424
2525import sys , time
2626import signal
27+ import syslog
2728
2829import XenAPI
2930
@@ -90,6 +91,14 @@ def get_running_domains(session, host):
9091 vms .append ((vm ,record ))
9192 return vms
9293
94+ def estimate_evacuate_timeout (session , host ):
95+ """ Rough estimation of the evacuate uplimit based on live VMs memory """
96+ mref = session .xenapi .host .get_metrics (host )
97+ metrics = session .xenapi .host_metrics .get_record (mref )
98+ memory_used = int (metrics ['memory_total' ]) - int (metrics ['memory_free' ])
99+ # Conservative estimation based on 1000Mbps link, and the memory usage of
100+ # Dom0 (which is not going to be transferred) is an intentional surplus
101+ return (memory_used * 8. / (1000. * 1024 * 1024 ))
93102
94103def host_evacuate (session , host ):
95104 """Attempts a host evacuate. If the timeout expires then it attempts to cancel
@@ -98,8 +107,13 @@ def host_evacuate(session, host):
98107 print "\n Requesting evacuation of host" ,
99108 sys .stdout .flush ()
100109 task = session .xenapi .Async .host .evacuate (host )
110+ timeout = 240
101111 try :
102- if not (wait_for_tasks (session , [ task ], 240 )):
112+ timeout = max (estimate_evacuate_timeout (session , host ), timeout )
113+ except Exception , e :
114+ syslog .syslog (syslog .LOG_WARNING , "Evacuate timeout estimation error: %s, use default." % e )
115+ try :
116+ if not (wait_for_tasks (session , [ task ], timeout )):
103117 print "\n Cancelling evacuation of host" ,
104118 sys .stdout .flush ()
105119 session .xenapi .task .cancel (task )
0 commit comments