From 12440b00c1bf4638281b762e7952ae7ecf95e0c0 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sat, 11 May 2019 18:05:59 -0400 Subject: [PATCH] osdict: return _OsResources to callers Use the class helper routines to save some tedious dict management --- tests/osdict.py | 7 ++++--- virt-install | 15 +++++++++------ virtManager/create.py | 23 ++++++++++++----------- virtinst/guest.py | 11 +++++++---- virtinst/osdict.py | 10 ++-------- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/tests/osdict.py b/tests/osdict.py index 329da07c7..95bce194a 100644 --- a/tests/osdict.py +++ b/tests/osdict.py @@ -27,10 +27,11 @@ class TestOSDB(unittest.TestCase): def test_recommended_resources(self): conn = utils.URIs.open_testdefault_cached() guest = Guest(conn) - assert not OSDB.lookup_os("generic").get_recommended_resources(guest) + res = OSDB.lookup_os("generic").get_recommended_resources() + self.assertEqual(res.get_recommended_ram(guest.os.arch), None) - res = OSDB.lookup_os("fedora21").get_recommended_resources(guest) - assert res["n-cpus"] == 2 + res = OSDB.lookup_os("fedora21").get_recommended_resources() + self.assertEqual(res.get_recommended_ncpus(guest.os.arch), 2) def test_urldetct_matching_distros(self): allstores = urldetect.ALLSTORES diff --git a/virt-install b/virt-install index b52a23697..e5647131b 100755 --- a/virt-install +++ b/virt-install @@ -526,12 +526,15 @@ def set_resources_from_osinfo(options, guest): if options.disk: return - res = guest.osinfo.get_recommended_resources(guest) - if res and res.get('storage') > 0: - diskstr = 'size=%d' % (res.get('storage') // (1024 ** 3)) - logging.debug("Generated default libosinfo '--disk %s'", diskstr) - options.disk = [diskstr] - cli.ParserDisk(diskstr, guest=guest).parse(None) + res = guest.osinfo.get_recommended_resources() + storage = res.get_recommended_storage(guest.os.arch) + if not storage: + return + + diskstr = 'size=%d' % (storage // (1024 ** 3)) + logging.debug("Generated default libosinfo '--disk %s'", diskstr) + options.disk = [diskstr] + cli.ParserDisk(diskstr, guest=guest).parse(None) def build_guest_instance(conn, options): diff --git a/virtManager/create.py b/virtManager/create.py index da1c921e3..2424ae7fa 100644 --- a/virtManager/create.py +++ b/virtManager/create.py @@ -1674,23 +1674,24 @@ class vmmCreate(vmmGObjectUI): self._addstorage.check_path_search( self, self.conn, path) - res = self._guest.osinfo.get_recommended_resources(self._guest) - logging.debug("Recommended resources for os=%s: %s", - self._guest.osinfo.name, res) + res = self._guest.osinfo.get_recommended_resources() + ram = res.get_recommended_ram(self._guest.os.arch) + n_cpus = res.get_recommended_ncpus(self._guest.os.arch) + storage = res.get_recommended_storage(self._guest.os.arch) + logging.debug("Recommended resources for os=%s: " + "ram=%s ncpus=%s storage=%s", + self._guest.osinfo.name, ram, n_cpus, storage) # Change the default values suggested to the user. ram_size = DEFAULT_MEM - if res and res.get("ram") > 0: - ram_size = res["ram"] // (1024 ** 2) + if ram: + ram_size = ram // (1024 ** 2) self.widget("mem").set_value(ram_size) - n_cpus = 1 - if res and res.get("n-cpus") > 0: - n_cpus = res["n-cpus"] - self.widget("cpus").set_value(n_cpus) + self.widget("cpus").set_value(n_cpus or 1) - if res and res.get("storage"): - storage_size = int(res["storage"]) // (1024 ** 3) + if storage: + storage_size = storage // (1024 ** 3) self._addstorage.widget("storage-size").set_value(storage_size) # Stash the installer in the _guest instance so we don't need diff --git a/virtinst/guest.py b/virtinst/guest.py index 15fd1eafa..34fcabf7e 100644 --- a/virtinst/guest.py +++ b/virtinst/guest.py @@ -656,13 +656,16 @@ class Guest(XMLBuilder): ######################## def _set_default_resources(self): - res = self.osinfo.get_recommended_resources(self) + res = self.osinfo.get_recommended_resources() - if not self.currentMemory and res and res.get('ram') > 0: - self.currentMemory = res['ram'] // 1024 + if not self.currentMemory: + ram = res.get_recommended_ram(self.os.arch) + if ram: + self.currentMemory = ram // 1024 if not self.vcpus: - self.vcpus = res.get('n-cpus') if res and res.get('n-cpus') > 0 else 1 + ncpus = res.get_recommended_ncpus(self.os.arch) + self.vcpus = ncpus or 1 def _set_default_machine(self): diff --git a/virtinst/osdict.py b/virtinst/osdict.py index a8b1c818c..215c3198d 100644 --- a/virtinst/osdict.py +++ b/virtinst/osdict.py @@ -515,16 +515,10 @@ class _OsVariant(object): devids = ["http://qemu.org/chipset/x86/q35"] return bool(self._device_filter(devids=devids)) - def get_recommended_resources(self, guest): + def get_recommended_resources(self): minimum = self._os and self._os.get_minimum_resources() or None recommended = self._os and self._os.get_recommended_resources() or None - osresources = _OsResources(minimum, recommended) - - ret = {} - ret["ram"] = osresources.get_recommended_ram(guest.os.arch) - ret["n-cpus"] = osresources.get_recommended_ncpus(guest.os.arch) - ret["storage"] = osresources.get_recommended_storage(guest.os.arch) - return {k: v for k, v in ret.items() if v} + return _OsResources(minimum, recommended) def get_network_install_resources(self, guest): ret = {}