On 04/24/2014 02:19 PM, shaohef(a)linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef(a)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(a)linux.vnet.ibm.com>
> Signed-off-by: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
> ---
> src/kimchi/i18n.py | 1 +
> src/kimchi/model/utils.py | 41 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 42 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..be5e526 100644
> --- a/src/kimchi/model/utils.py
> +++ b/src/kimchi/model/utils.py
> @@ -18,6 +18,11 @@
> # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA
>
> from kimchi.exception import OperationFailed
> +import libvirt
> +from lxml import etree
> +
> +
> +KIMCHI_META_URL = "https://github.com/kimchi-project/kimchi/metadata/"
>
>
> def get_vm_name(vm_name, t_name, name_list):
> @@ -28,3 +33,39 @@ 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 set_vm_metadata_element(dom, meta_xml, mode="all"):
> + element = etree.fromstring(meta_xml)
> + # 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, meta_xml,
> + "kimchi", KIMCHI_META_URL + element.tag,
Why are you append element.tag to namespace? Use only
https://github.com/kimchi-project/kimchi/ as namespace
> + flags=get_vm_config_flag(dom, mode))
> +
> +
> +def get_vm_metadata_element(dom, element, mode="current"):
> + try:
> + return dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT,
> + KIMCHI_META_URL + element,
> + flags=get_vm_config_flag(dom, mode))
> + except libvirt.libvirtError as e:
> + if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN_METADATA:
> + return ""
> + else:
> + raise OperationFailed("KCHVM0030E", {'name': dom.name(),
> + 'err': e.message})
--
Thanks and best regards!
Sheldon Feng(冯少合)<shaohef(a)linux.vnet.ibm.com>
IBM Linux Technology Center