Reload Xend session params when we got an XenAPI.Failure exception
Because we're caching e.g. uuid and metrics objects for the Xend session, we can get an exception when the VM changed the power state between since we cached the object. We now catch this exception and reload uuid and metrics object in the handler.
This commit is contained in:
parent
56350e07e9
commit
cd20eeb8a4
@ -106,7 +106,8 @@ class XendSession(object):
|
||||
print "get_xend_session_new_api(): cannot open session!"
|
||||
|
||||
|
||||
xend_session = XendSession()
|
||||
if not dry_run:
|
||||
xend_session = XendSession()
|
||||
|
||||
class QubesException (Exception) : pass
|
||||
|
||||
@ -178,12 +179,7 @@ class QubesVm(object):
|
||||
self.icon_path = self.dir_path + "/icon.png"
|
||||
|
||||
if not dry_run and xend_session.session is not None:
|
||||
uuids = xend_session.session.xenapi.VM.get_by_name_label (self.name)
|
||||
self.session_uuid = uuids[0] if len (uuids) > 0 else None
|
||||
if self.session_uuid is not None:
|
||||
self.session_metrics = xend_session.session.xenapi.VM.get_metrics(self.session_uuid)
|
||||
else:
|
||||
self.session_metrics = None
|
||||
self.refresh_xend_session()
|
||||
|
||||
@property
|
||||
def qid(self):
|
||||
@ -273,11 +269,7 @@ class QubesVm(object):
|
||||
|
||||
self.in_xen_storage = False
|
||||
|
||||
def update_xen_storage(self):
|
||||
self.remove_from_xen_storage()
|
||||
self.add_to_xen_storage()
|
||||
|
||||
if not dry_run and xend_session.session is not None:
|
||||
def refresh_xend_session(self):
|
||||
uuids = xend_session.session.xenapi.VM.get_by_name_label (self.name)
|
||||
self.session_uuid = uuids[0] if len (uuids) > 0 else None
|
||||
if self.session_uuid is not None:
|
||||
@ -285,27 +277,46 @@ class QubesVm(object):
|
||||
else:
|
||||
self.session_metrics = None
|
||||
|
||||
|
||||
def update_xen_storage(self):
|
||||
self.remove_from_xen_storage()
|
||||
self.add_to_xen_storage()
|
||||
if not dry_run and xend_session.session is not None:
|
||||
self.refresh_xend_session()
|
||||
|
||||
def get_xid(self):
|
||||
if dry_run:
|
||||
return 666
|
||||
|
||||
try:
|
||||
xid = int (xend_session.session.xenapi.VM.get_domid (self.session_uuid))
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
xid = int (xend_session.session.xenapi.VM.get_domid (self.session_uuid))
|
||||
|
||||
return xid
|
||||
|
||||
def get_mem(self):
|
||||
if dry_run:
|
||||
return 666
|
||||
|
||||
try:
|
||||
mem = int (xend_session.session.xenapi.VM_metrics.get_memory_actual (self.session_metrics))
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
mem = int (xend_session.session.xenapi.VM_metrics.get_memory_actual (self.session_metrics))
|
||||
|
||||
return mem
|
||||
|
||||
def get_mem_static_max(self):
|
||||
if dry_run:
|
||||
return 666
|
||||
|
||||
try:
|
||||
mem = int(xend_session.session.xenapi.VM.get_memory_static_max(self.session_uuid))
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
mem = int(xend_session.session.xenapi.VM.get_memory_static_max(self.session_uuid))
|
||||
|
||||
return mem
|
||||
|
||||
|
||||
@ -314,7 +325,12 @@ class QubesVm(object):
|
||||
import random
|
||||
return random.random() * 100
|
||||
|
||||
try:
|
||||
cpus_util = xend_session.session.xenapi.VM_metrics.get_VCPUs_utilisation (self.session_metrics)
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
cpus_util = xend_session.session.xenapi.VM_metrics.get_VCPUs_utilisation (self.session_metrics)
|
||||
|
||||
if len (cpus_util) == 0:
|
||||
return 0
|
||||
|
||||
@ -331,10 +347,14 @@ class QubesVm(object):
|
||||
if dry_run:
|
||||
return "NA"
|
||||
|
||||
try:
|
||||
power_state = xend_session.session.xenapi.VM.get_power_state (self.session_uuid)
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
if self.session_uuid is None:
|
||||
return "NA"
|
||||
|
||||
power_state = xend_session.session.xenapi.VM.get_power_state (self.session_uuid)
|
||||
|
||||
return power_state
|
||||
|
||||
def is_running(self):
|
||||
@ -343,7 +363,6 @@ class QubesVm(object):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def get_disk_usage(self, file_or_dir):
|
||||
if not os.path.exists(file_or_dir):
|
||||
return 0
|
||||
@ -442,7 +461,12 @@ class QubesVm(object):
|
||||
if dom0_mem_new < dom0_min_memory:
|
||||
raise MemoryError ("ERROR: starting this VM would cause Dom0 memory to go below {0}B".format(dom0_min_memory))
|
||||
|
||||
try:
|
||||
xend_session.session.xenapi.VM.start (self.session_uuid, True) # Starting a VM paused
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
xend_session.session.xenapi.VM.start (self.session_uuid, True) # Starting a VM paused
|
||||
|
||||
xid = int (xend_session.session.xenapi.VM.get_domid (self.session_uuid))
|
||||
|
||||
if verbose:
|
||||
@ -487,6 +511,10 @@ class QubesVm(object):
|
||||
if dry_run:
|
||||
return
|
||||
|
||||
try:
|
||||
xend_session.session.xenapi.VM.hard_shutdown (self.session_uuid)
|
||||
except XenAPI.Failure:
|
||||
self.refresh_xend_session()
|
||||
xend_session.session.xenapi.VM.hard_shutdown (self.session_uuid)
|
||||
|
||||
def remove_from_disk(self):
|
||||
|
Loading…
Reference in New Issue
Block a user