From 757910a1bb2fb9865721cfd2c8db59b0dc11cd38 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sun, 30 Sep 2018 19:12:19 -0400 Subject: [PATCH] cli: Add --metadata os_name= and os_full_id= Allows changing the libosinfo metadata for an existing vm via virt-xml --- .../compare/virt-xml-edit-metadata-full-os.xml | 11 +++++++++++ .../compare/virt-xml-edit-simple-metadata.xml | 6 +++++- tests/clitest.py | 3 ++- tests/xmlparse.py | 3 +++ virtinst/cli.py | 8 ++++++++ virtinst/guest.py | 11 ++++++++++- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/cli-test-xml/compare/virt-xml-edit-metadata-full-os.xml diff --git a/tests/cli-test-xml/compare/virt-xml-edit-metadata-full-os.xml b/tests/cli-test-xml/compare/virt-xml-edit-metadata-full-os.xml new file mode 100644 index 00000000..9dd1701a --- /dev/null +++ b/tests/cli-test-xml/compare/virt-xml-edit-metadata-full-os.xml @@ -0,0 +1,11 @@ + + + +- ++ + + + 409600 + +Domain 'test-for-virtxml' defined successfully. +Changes will take effect after the domain is fully powered off. \ No newline at end of file diff --git a/tests/cli-test-xml/compare/virt-xml-edit-simple-metadata.xml b/tests/cli-test-xml/compare/virt-xml-edit-simple-metadata.xml index 2fb88606..7ddc1bf5 100644 --- a/tests/cli-test-xml/compare/virt-xml-edit-simple-metadata.xml +++ b/tests/cli-test-xml/compare/virt-xml-edit-simple-metadata.xml @@ -10,7 +10,11 @@ +very,very=new desc' - +- ++ + + + 409600 @@ diff --git a/tests/clitest.py b/tests/clitest.py index 04795e05..5d617cfd 100644 --- a/tests/clitest.py +++ b/tests/clitest.py @@ -852,9 +852,10 @@ c.add_compare("--connect %(URI-KVM)s test-hyperv-uefi --edit --boot uefi", "hype c = vixml.add_category("simple edit diff", "test-for-virtxml --edit --print-diff --define") -c.add_compare("""--metadata name=foo-my-new-name,uuid=12345678-12F4-1234-1234-123456789AFA,description="hey this is my +c.add_compare("""--metadata name=foo-my-new-name,os_name=fedora13,uuid=12345678-12F4-1234-1234-123456789AFA,description="hey this is my new very,very=new desc\\\'",title="This is my,funky=new title" """, "edit-simple-metadata") +c.add_compare("""--metadata os_full_id=http://fedoraproject.org/fedora/23""", "edit-metadata-full-os") c.add_compare("--events on_poweroff=destroy,on_reboot=restart,on_crash=preserve", "edit-simple-events") c.add_compare("--qemu-commandline='-foo bar,baz=\"wib wob\"'", "edit-simple-qemu-commandline") c.add_compare("--memory 500,maxmemory=1000,hugepages=off", "edit-simple-memory") diff --git a/tests/xmlparse.py b/tests/xmlparse.py index ef6479a2..da2487f0 100644 --- a/tests/xmlparse.py +++ b/tests/xmlparse.py @@ -117,6 +117,9 @@ class XMLParseTest(unittest.TestCase): check = self._make_checker(guest._metadata.libosinfo) # pylint: disable=protected-access check("os_id", "http://fedoraproject.org/fedora/17") + guest.set_os_full_id("http://fedoraproject.org/fedora/10") + check("os_id", "http://fedoraproject.org/fedora/10") + self.assertEqual(guest.osinfo.name, "fedora10") guest.set_os_name("generic") check("os_id", None, "frib") self.assertEqual(guest.osinfo.name, "generic") diff --git a/virtinst/cli.py b/virtinst/cli.py index 4435b49d..1d5c8a71 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -1299,11 +1299,19 @@ def parse_check(checkstr): class ParserMetadata(VirtCLIParser): cli_arg_name = "metadata" + def set_os_name_cb(self, inst, val, virtarg): + inst.set_os_name(val) + + def set_os_full_id_cb(self, inst, val, virtarg): + inst.set_os_full_id(val) + _register_virt_parser(ParserMetadata) ParserMetadata.add_arg("name", "name", can_comma=True) ParserMetadata.add_arg("title", "title", can_comma=True) ParserMetadata.add_arg("uuid", "uuid") ParserMetadata.add_arg("description", "description", can_comma=True) +ParserMetadata.add_arg(None, "os_name", cb=ParserMetadata.set_os_name_cb) +ParserMetadata.add_arg(None, "os_full_id", cb=ParserMetadata.set_os_full_id_cb) #################### diff --git a/virtinst/guest.py b/virtinst/guest.py index bac94976..983dedbe 100644 --- a/virtinst/guest.py +++ b/virtinst/guest.py @@ -262,7 +262,16 @@ class Guest(XMLBuilder): raise ValueError( _("Distro '%s' does not exist in our dictionary") % name) - logging.debug("Setting Guest os_name=%s", name) + logging.debug("Setting Guest osinfo %s", obj) + self.__osinfo = obj + self._metadata.libosinfo.os_id = self.__osinfo.full_id + + def set_os_full_id(self, full_id): + obj = OSDB.lookup_os_by_full_id(full_id) + if obj is None: + raise ValueError(_("Unknown libosinfo ID '%s'") % full_id) + + logging.debug("Setting Guest osinfo %s", obj) self.__osinfo = obj self._metadata.libosinfo.os_id = self.__osinfo.full_id