From 45a43031aba750ec1de0cf3f9fd733bc807409ba Mon Sep 17 00:00:00 2001 From: Erik Skultety Date: Thu, 18 May 2023 15:27:42 +0200 Subject: install: Extract logic from 'from_vendor_image' to '_from_image' Future patches will introduce support for instantiating custom template images which will share most of the logic with the vendor cloud image scenario so extract the common logic into a private helper. Signed-off-by: Erik Skultety --- lcitool/install/install.py | 73 +++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/lcitool/install/install.py b/lcitool/install/install.py index 16e4924..68449e6 100644 --- a/lcitool/install/install.py +++ b/lcitool/install/install.py @@ -57,10 +57,6 @@ class VirtInstall: def from_vendor_image(cls, name, config, facts, force_download=False): """ Shortcut constructor for a cloud-init image-based installation. """ - runner = cls(name, facts) - - conf_size = config.values["install"]["disk_size"] - conf_pool = config.values["install"]["storage_pool"] arch = config.values["install"]["arch"] target = facts["target"] @@ -73,36 +69,8 @@ class VirtInstall: if image.path is None or force_download: image.download() - # Dump the edited cloud-init template for virt-install to use - ssh_keypair = util.SSHKeyPair(config.values["install"]["ssh_key"]) - ssh_pubkey_str = str(ssh_keypair.public_key) - cloud_config = CloudConfig(ssh_authorized_keys=[ssh_pubkey_str]) - with NamedTemporaryFile("w", - prefix="cloud_init_", - suffix=".conf", - dir=util.get_temp_dir(), - delete=False) as fd: - - fd.write(cloud_config.dump()) - runner.args.extend(["--cloud-init", f"user-data={fd.name}"]) - - baseimg_path = image.path.as_posix() - disk_arg = (f"size={conf_size}," - f"pool={conf_pool}," - f"backing_store={baseimg_path}," - f"bus=virtio") - - runner.args.extend(["--import", - "--disk", disk_arg]) - runner.args.extend(runner._get_common_args(config)) - - # NOTE: URL installs wait by connecting to a serial console which kills - # any automation needs, so we don't want that here and instead always - # pass --noautoconsole and set an SSH wait callback. - runner.args.append("--noautoconsole") - runner._ssh_keypair = ssh_keypair - runner._wait_callback = runner._ssh_wait_cb - return runner + runner = cls(name, facts) + return cls._from_image(runner, config, image.path) def __init__(self, name, facts): """ @@ -137,6 +105,43 @@ class VirtInstall: return " ".join([self._cmd] + self.args) @staticmethod + def _from_image(runner, config, baseimg_path, **kwargs): + + conf_size = config.values["install"]["disk_size"] + conf_pool = config.values["install"]["storage_pool"] + + # Dump the edited cloud-init template for virt-install to use + ssh_keypair = util.SSHKeyPair(config.values["install"]["ssh_key"]) + ssh_pubkey_str = str(ssh_keypair.public_key) + cloud_config = CloudConfig(ssh_authorized_keys=[ssh_pubkey_str]) + with NamedTemporaryFile("w", + prefix="cloud_init_", + suffix=".conf", + dir=util.get_temp_dir(), + delete=False) as fd: + + fd.write(cloud_config.dump()) + runner.args.extend(["--cloud-init", f"user-data={fd.name}"]) + + baseimg_path_str = baseimg_path.as_posix() + disk_arg = (f"size={conf_size}," + f"pool={conf_pool}," + f"backing_store={baseimg_path_str}," + f"bus=virtio") + + runner.args.extend(["--import", + "--disk", disk_arg]) + runner.args.extend(runner._get_common_args(config)) + + # NOTE: URL installs wait by connecting to a serial console which kills + # any automation needs, so we don't want that here and instead always + # pass --noautoconsole and set an SSH wait callback. + runner.args.append("--noautoconsole") + runner._ssh_keypair = ssh_keypair + runner._wait_callback = runner._ssh_wait_cb + return runner + + @staticmethod def _get_common_args(config): # Both memory size and disk size are stored as GiB in the -- cgit v1.1