[Kimchi-devel] [PATCH V11 3/7] Add two function to set and get domain xml metadata
shaohef at linux.vnet.ibm.com
shaohef at linux.vnet.ibm.com
Mon Apr 28 15:44:24 UTC 2014
From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
Libvirt support two API to set and get domain xml metadata.
These two function wrap them.
Put them in model/utils.py, so vm_storage and vm_iface can make use of
them.
For set domain xml metadata, we set live and config xml by default.
For get domain xml metadata, we get the current xml by default.
Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
src/kimchi/i18n.py | 1 +
src/kimchi/model/utils.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index a1af1f7..e28f689 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -87,6 +87,7 @@ messages = {
"KCHVM0027E": _("User(s) '%(users)s' do not exist"),
"KCHVM0028E": _("Group(s) '%(groups)s' do not exist"),
"KCHVM0029E": _("Unable to shutdown virtual machine %(name)s. Details: %(err)s"),
+ "KCHVM0030E": _("Unable to get access metadata of virtual machine %(name)s. Details: %(err)s"),
"KCHVMIF0001E": _("Interface %(iface)s does not exist in virtual machine %(name)s"),
"KCHVMIF0002E": _("Network %(network)s specified for virtual machine %(name)s does not exist"),
diff --git a/src/kimchi/model/utils.py b/src/kimchi/model/utils.py
index 80e1801..3136402 100644
--- a/src/kimchi/model/utils.py
+++ b/src/kimchi/model/utils.py
@@ -18,6 +18,13 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from kimchi.exception import OperationFailed
+import libvirt
+from lxml import etree
+from lxml.builder import E
+
+
+KIMCHI_META_URL = "https://github.com/kimchi-project/kimchi"
+KIMCHI_NAMESPACE = "kimchi"
def get_vm_name(vm_name, t_name, name_list):
@@ -28,3 +35,55 @@ def get_vm_name(vm_name, t_name, name_list):
if vm_name not in name_list:
return vm_name
raise OperationFailed("KCHUTILS0003E")
+
+
+def get_vm_config_flag(dom, mode="persistent"):
+ # libvirt.VIR_DOMAIN_AFFECT_CURRENT is 0
+ # VIR_DOMAIN_AFFECT_LIVE is 1, VIR_DOMAIN_AFFECT_CONFIG is 2
+ flag = {"live": libvirt.VIR_DOMAIN_AFFECT_LIVE,
+ "persistent": libvirt.VIR_DOMAIN_AFFECT_CONFIG,
+ "current": libvirt.VIR_DOMAIN_AFFECT_CURRENT,
+ "all": libvirt.VIR_DOMAIN_AFFECT_CONFIG +
+ libvirt.VIR_DOMAIN_AFFECT_LIVE if dom.isActive() and
+ dom.isPersistent() else libvirt.VIR_DOMAIN_AFFECT_CURRENT}
+
+ return flag[mode]
+
+
+def libvirt_get_kimchi_metadata_node(dom, mode="current"):
+ try:
+ xml = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT,
+ KIMCHI_META_URL,
+ flags=get_vm_config_flag(dom, mode))
+ return etree.fromstring(xml)
+ except libvirt.libvirtError:
+ return None
+
+
+def set_metadata_node(dom, node_xml, mode="all"):
+ element = etree.fromstring(node_xml)
+ kimchi = libvirt_get_kimchi_metadata_node(dom, mode)
+ old_node = None
+ if kimchi is not None:
+ old_node = kimchi.find(element.tag)
+ else:
+ kimchi = E.kimchi()
+ (kimchi.replace(old_node, element) if old_node is not None
+ else kimchi.append(element))
+
+ node_xml = etree.tostring(kimchi)
+ # From libvirt doc, Passing None for @metadata says to remove that
+ # element from the domain XML (passing the empty string leaves the
+ # element present). Do not support remove the old metadata.
+ dom.setMetadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, node_xml,
+ KIMCHI_NAMESPACE, KIMCHI_META_URL,
+ flags=get_vm_config_flag(dom, mode))
+
+
+def get_metadata_node(dom, tag, mode="current"):
+ kimchi = libvirt_get_kimchi_metadata_node(dom, mode)
+ if kimchi is not None:
+ node = kimchi.find(tag)
+ if node is not None:
+ return etree.tostring(node)
+ return ""
--
1.9.0
More information about the Kimchi-devel
mailing list