On 05/05/2015 10:02, Ramon Medeiros wrote:
Changes:
v2:
Improve coding to avoid code duplication
The function FeatureTests.disable_libvirt_error_logging only removes the
error message that will be displayed on the console. Libvirt also logs
errors on messages log. To avoid flood messages log with metadata
errors, it will be verified manually if the xml has the tag.
Signed-off-by: Ramon Medeiros <ramonn(a)linux.vnet.ibm.com>
---
src/kimchi/model/utils.py | 38 ++++++++++++++++++++------------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/kimchi/model/utils.py b/src/kimchi/model/utils.py
index 0c9d519..50b0578 100644
--- a/src/kimchi/model/utils.py
+++ b/src/kimchi/model/utils.py
@@ -84,7 +84,6 @@ def _kimchi_set_metadata_node(dom, node):
def libvirt_get_kimchi_metadata_node(dom, mode="current"):
- FeatureTests.disable_libvirt_error_logging()
try:
xml = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT,
KIMCHI_META_URL,
@@ -115,36 +114,39 @@ def set_metadata_node(dom, node, metadata_support,
mode="all"):
_kimchi_set_metadata_node(dom, node)
-def _kimchi_get_metadata_node(dom, tag):
+def _kimchi_get_metadata_node(xml, tag):
# some other tools will not let libvirt create a persistent
# configuration, just return empty
+ # remove the "kimchi" prefix of xml
+ for elem in xml.getiterator():
+ if not hasattr(elem.tag, 'find'):
+ continue
+ i = elem.tag.find('}')
+ if i >= 0:
+ elem.tag = elem.tag[i+1:]
+
+ objectify.deannotate(xml)
+ etree.cleanup_namespaces(xml)
+ return xml
+
+
+def get_metadata_node(dom, tag, metadata_support, mode="current"):
if not dom.isPersistent():
- return None
+ return ""
xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
root = etree.fromstring(xml)
kimchi = root.find("metadata/{%s}kimchi" %
KIMCHI_META_URL)
The tests are failing because of the above check.
When libvirt has .metadata() support the guest XML will not have the tag
"{%s}kimchi % KIMCHI_META_URL".
I suggest create a new function, _get_metadata(dom)
def _get_metadata(dom):
if not dom.isPersistent():
return None
xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
root = etree.fromstring(xml)
metadata = root.find("metadata")
if metadata is None:
return None
return metadata
Then in get_metadata_node():
metadata = _get_metadata(dom)
if metadata is None:
return ""
if metadata_support:
kimchi = libvirt_get_kimchi_metadata_node(dom, mode)
else:
kimchi = _kimchi_get_metadata_node(metadata, tag)
And in _kimchi_get_metadata_node(metadata, tag) add:
kimchi = metadata.find("{%s}kimchi" % KIMCHI_META_URL)
Note that libvirt_get_kimchi_metadata_node() is also called by
set_metadata_node() so we need to do the metadata check there too to
avoid the libvirt errors on logs.
- # remove the "kimchi" prefix of xml
- if kimchi is not None:
- for elem in kimchi.getiterator():
- if not hasattr(elem.tag, 'find'):
- continue
- i = elem.tag.find('}')
- if i >= 0:
- elem.tag = elem.tag[i+1:]
-
- objectify.deannotate(kimchi)
- etree.cleanup_namespaces(kimchi)
- return kimchi
- return None
+ # remove the "kimchi" prefix of xml
+ if kimchi is None:
+ return ""
-def get_metadata_node(dom, tag, metadata_support, mode="current"):
if metadata_support:
kimchi = libvirt_get_kimchi_metadata_node(dom, mode)
else:
# FIXME remove this code when all distro libvirt supports metadata
# element
- kimchi = _kimchi_get_metadata_node(dom, tag)
+ kimchi = _kimchi_get_metadata_node(kimchi, tag)
if kimchi is not None:
node = kimchi.find(tag)