Domain: Simplify xml fetching

Just have get_xml take arguments, rather than have get_xml, get_inactive...
This commit is contained in:
Cole Robinson 2010-09-03 12:10:26 -04:00
parent 2d35e75809
commit 965f68133f
2 changed files with 42 additions and 55 deletions

View File

@ -771,13 +771,7 @@ class vmmDomainBase(vmmLibvirtObject):
def parse_wrap_func(doc, ctx):
return parse_function(ctx)
if inactive:
xml = self._get_inactive_xml()
elif refresh_if_necc:
xml = self.get_xml()
else:
xml = self._get_xml_no_refresh()
xml = self.get_xml(inactive=inactive, refresh_if_necc=refresh_if_necc)
return util.xml_parse_wrapper(xml, parse_wrap_func)
def _get_device_xml(self, dev_type, dev_id_info):
@ -1210,6 +1204,7 @@ class vmmDomain(vmmDomainBase):
self.reboot_listener = None
self._guest = None
self._reparse_xml()
# Determine available XML flags (older libvirt versions will error
# out if passed SECURE_XML, INACTIVE_XML, etc)
@ -1390,7 +1385,7 @@ class vmmDomain(vmmDomainBase):
flags |= libvirt.VIR_MIGRATE_PEER2PEER
flags |= libvirt.VIR_MIGRATE_TUNNELLED
newxml = self._get_xml_to_define()
newxml = self.get_xml(inactive=True)
logging.debug("Migrating: conn=%s flags=%s dname=%s uri=%s rate=%s" %
(destconn.vmm, flags, newname, interface, rate))
@ -1444,22 +1439,26 @@ class vmmDomain(vmmDomainBase):
# XML/Config Altering API #
###########################
def _get_domain_xml(self):
return vmmLibvirtObject.get_xml(self)
def _get_domain_xml(self, inactive=False, refresh_if_necc=True):
return vmmLibvirtObject.get_xml(self, inactive, refresh_if_necc)
def get_xml(self):
# Do this to make sure we have latest XML
self._get_domain_xml()
return self._get_guest().get_xml_config()
def get_xml(self, inactive=False, refresh_if_necc=True):
return self._get_guest(inactive, refresh_if_necc).get_xml_config()
def _get_guest(self, inactive=False, refresh_if_necc=True):
xml = self._get_domain_xml(inactive, refresh_if_necc)
if not self.is_active() and inactive:
# We don't cache a guest for 'inactive' XML, so just return it
return self._build_guest(xml)
def _get_guest(self):
if not self._guest:
self._reparse_xml()
return self._guest
def _build_guest(self, xml):
return virtinst.Guest(connection=self.connection.vmm, parsexml=xml)
def _reparse_xml(self, ignore=None):
self._guest = virtinst.Guest(connection=self.connection.vmm,
parsexml=self._get_domain_xml())
self._guest = self._build_guest(self._get_domain_xml())
def _check_device_is_present(self, dev_type, dev_id_info):
"""
@ -1471,7 +1470,7 @@ class vmmDomain(vmmDomainBase):
of the inactive XML. If the device can't be found, make no change
and return success.
"""
vmxml = self._get_inactive_xml()
vmxml = self.get_xml(inactive=True)
def find_dev(doc, ctx, dev_type, dev_id_info):
ret = self._get_device_xml_nodes(ctx, dev_type, dev_id_info)
@ -2073,13 +2072,14 @@ class vmmDomainVirtinst(vmmDomainBase):
def status(self):
return libvirt.VIR_DOMAIN_SHUTOFF
def get_xml(self):
def get_xml(self, inactive=False, refresh_if_necc=True):
ignore = inactive
ignore = refresh_if_necc
xml = self._backend.get_config_xml()
if not xml:
xml = self._backend.get_config_xml(install=False)
return xml
def _get_inactive_xml(self):
return self.get_xml()
def refresh_xml(self):
# No caching, so no refresh needed

View File

@ -20,7 +20,6 @@
import gobject
import time
import difflib
import logging
@ -67,11 +66,27 @@ class vmmLibvirtObject(gobject.GObject):
# Public XML API #
##################
def get_xml(self):
def get_xml(self, inactive=False, refresh_if_necc=True):
"""
Get domain xml. If cached xml is invalid, update.
@param inactive: Return persistent XML, not the running config.
No effect if domain is not running. Use this flag
if the XML will be used for redefining a guest
@param refresh_if_necc: Check if XML is out of date, and if so,
refresh it (default behavior). Skipping a refresh is
useful to prevent updating xml in the tick loop when
it's not that important (disk/net stats)
"""
return self._xml_fetch_helper(refresh_if_necc=True)
if inactive:
return self._XMLDesc(self._inactive_xml_flags)
if self._xml is None:
self.refresh_xml()
elif refresh_if_necc and not self._is_xml_valid:
self.refresh_xml()
return self._xml
def refresh_xml(self):
# Force an xml update. Signal 'config-changed' if domain xml has
@ -82,44 +97,16 @@ class vmmLibvirtObject(gobject.GObject):
self._is_xml_valid = True
if origxml != self._xml:
# 'tick' to make sure we have the latest time
self.tick(time.time())
util.safe_idle_add(util.idle_emit, self, "config-changed")
######################################
# Internal XML cache/update routines #
######################################
def _get_xml_no_refresh(self):
"""
Fetch XML, but don't force a refresh. Useful to prevent updating
xml in the tick loop when it's not that important (disk/net stats)
"""
return self._xml_fetch_helper(refresh_if_necc=False)
def _get_xml_to_define(self):
if self.is_active():
return self._get_inactive_xml()
else:
self._invalidate_xml()
return self.get_xml()
def _invalidate_xml(self):
# Mark cached xml as invalid
self._is_xml_valid = False
def _xml_fetch_helper(self, refresh_if_necc):
# Helper to fetch xml with various options
if self._xml is None:
self.refresh_xml()
elif refresh_if_necc and not self._is_xml_valid:
self.refresh_xml()
return self._xml
def _get_inactive_xml(self):
return self._XMLDesc(self._inactive_xml_flags)
##########################
# Internal API functions #
##########################
@ -132,7 +119,7 @@ class vmmLibvirtObject(gobject.GObject):
original XML as its first argument.
@param args: Extra arguments to pass to xml_func
"""
origxml = self._get_xml_to_define()
origxml = self.get_xml(inactive=True)
# Sanitize origxml to be similar to what we will get back
origxml = util.xml_parse_wrapper(origxml, lambda d, c: d.serialize())