Signed-off-by: Andrea Bolognani <abolo...@redhat.com>
---
 guests/lcitool | 62 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)

diff --git a/guests/lcitool b/guests/lcitool
index 1dd1ec8..cefce65 100755
--- a/guests/lcitool
+++ b/guests/lcitool
@@ -23,6 +23,7 @@ import fnmatch
 import os
 import random
 import string
+import subprocess
 import sys
 import textwrap
 import yaml
@@ -237,7 +238,8 @@ class Application:
             description = "libvirt CI guest management tool",
             epilog = textwrap.dedent("""
                 supported actions:
-                  list  list all known hosts
+                  list     list all known hosts
+                  install  perform unattended host installation
                 """),
         )
         self._parser.add_argument(
@@ -256,6 +258,64 @@ class Application:
         for host in self._inventory.expand_pattern("all"):
             print(host)
 
+    def _action_install(self, hosts):
+        flavor = self._config.get_flavor()
+
+        for host in self._inventory.expand_pattern(hosts):
+            facts = self._inventory.get_facts(host)
+
+            # Both memory size and disk size are stored as GiB in the
+            # inventory, but virt-install expects the disk size in GiB
+            # and the memory size in *MiB*, so perform conversion here
+            memory_arg = str(int(facts["install_memory_size"]) * 1024)
+
+            vcpus_arg = str(facts["install_vcpus"])
+
+            disk_arg = "size={},pool={},bus=virtio".format(
+                facts["install_disk_size"],
+                facts["install_storage_pool"],
+            )
+            network_arg = "network={},model=virtio".format(
+                facts["install_network"],
+            )
+
+            # preseed files must use a well-known name to be picked up by
+            # d-i; for kickstart files, we can use whatever name we please
+            # but we need to point anaconda in the right direction through
+            # a kernel argument
+            extra_arg = "console=ttyS0 ks=file:/{}".format(
+                facts["install_config"],
+            )
+
+            cmd = [
+                "virt-install",
+                "--name", host,
+                "--location", facts["install_url"],
+                "--virt-type", facts["install_virt_type"],
+                "--arch", facts["install_arch"],
+                "--machine", facts["install_machine"],
+                "--cpu", facts["install_cpu_model"],
+                "--vcpus", vcpus_arg,
+                "--memory", memory_arg,
+                "--disk", disk_arg,
+                "--network", network_arg,
+                "--graphics", "none",
+                "--console", "pty",
+                "--sound", "none",
+                "--initrd-inject", facts["install_config"],
+                "--extra-args", extra_arg,
+                "--wait", "0",
+            ]
+
+            # Only configure autostart for the guest for the jenkins flavor
+            if flavor == "jenkins":
+                cmd += [ "--autostart" ]
+
+            try:
+                subprocess.check_call(cmd)
+            except:
+                raise Error("Failed to install '{}'".format(host))
+
     def run(self):
         cmdline = self._parser.parse_args()
         action = cmdline.a
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to