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:
Joanna Rutkowska 2010-05-08 16:28:13 +02:00
parent 56350e07e9
commit cd20eeb8a4

View File

@ -106,6 +106,7 @@ class XendSession(object):
print "get_xend_session_new_api(): cannot open session!" print "get_xend_session_new_api(): cannot open session!"
if not dry_run:
xend_session = XendSession() xend_session = XendSession()
class QubesException (Exception) : pass class QubesException (Exception) : pass
@ -178,12 +179,7 @@ class QubesVm(object):
self.icon_path = self.dir_path + "/icon.png" self.icon_path = self.dir_path + "/icon.png"
if not dry_run and xend_session.session is not None: if not dry_run and xend_session.session is not None:
uuids = xend_session.session.xenapi.VM.get_by_name_label (self.name) self.refresh_xend_session()
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
@property @property
def qid(self): def qid(self):
@ -273,11 +269,7 @@ class QubesVm(object):
self.in_xen_storage = False self.in_xen_storage = False
def update_xen_storage(self): def refresh_xend_session(self):
self.remove_from_xen_storage()
self.add_to_xen_storage()
if not dry_run and xend_session.session is not None:
uuids = xend_session.session.xenapi.VM.get_by_name_label (self.name) uuids = xend_session.session.xenapi.VM.get_by_name_label (self.name)
self.session_uuid = uuids[0] if len (uuids) > 0 else None self.session_uuid = uuids[0] if len (uuids) > 0 else None
if self.session_uuid is not None: if self.session_uuid is not None:
@ -285,27 +277,46 @@ class QubesVm(object):
else: else:
self.session_metrics = None 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): def get_xid(self):
if dry_run: if dry_run:
return 666 return 666
try:
xid = int (xend_session.session.xenapi.VM.get_domid (self.session_uuid)) 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 return xid
def get_mem(self): def get_mem(self):
if dry_run: if dry_run:
return 666 return 666
try:
mem = int (xend_session.session.xenapi.VM_metrics.get_memory_actual (self.session_metrics)) 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 return mem
def get_mem_static_max(self): def get_mem_static_max(self):
if dry_run: if dry_run:
return 666 return 666
try:
mem = int(xend_session.session.xenapi.VM.get_memory_static_max(self.session_uuid)) 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 return mem
@ -314,7 +325,12 @@ class QubesVm(object):
import random import random
return random.random() * 100 return random.random() * 100
try:
cpus_util = xend_session.session.xenapi.VM_metrics.get_VCPUs_utilisation (self.session_metrics) 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: if len (cpus_util) == 0:
return 0 return 0
@ -331,10 +347,14 @@ class QubesVm(object):
if dry_run: if dry_run:
return "NA" 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: if self.session_uuid is None:
return "NA" return "NA"
power_state = xend_session.session.xenapi.VM.get_power_state (self.session_uuid) power_state = xend_session.session.xenapi.VM.get_power_state (self.session_uuid)
return power_state return power_state
def is_running(self): def is_running(self):
@ -343,7 +363,6 @@ class QubesVm(object):
else: else:
return False return False
def get_disk_usage(self, file_or_dir): def get_disk_usage(self, file_or_dir):
if not os.path.exists(file_or_dir): if not os.path.exists(file_or_dir):
return 0 return 0
@ -442,7 +461,12 @@ class QubesVm(object):
if dom0_mem_new < dom0_min_memory: 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)) 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 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)) xid = int (xend_session.session.xenapi.VM.get_domid (self.session_uuid))
if verbose: if verbose:
@ -487,6 +511,10 @@ class QubesVm(object):
if dry_run: if dry_run:
return 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) xend_session.session.xenapi.VM.hard_shutdown (self.session_uuid)
def remove_from_disk(self): def remove_from_disk(self):