From 38d48dbf03268809f7e53953e68a2669a31f0fd5 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Tue, 6 Mar 2018 19:02:45 +0900 Subject: [PATCH] libvirt: new setting deployment.libvirtd.template Allows for maximum flexibility in the definition of the libvirt domain. Also considers the possibility of domain creation failing --- nix/libvirtd.nix | 26 ++++++++++++++++++++ nixops/backends/libvirtd.py | 48 +++++++++++++------------------------ 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/nix/libvirtd.nix b/nix/libvirtd.nix index dfa4f0f99..38f27be11 100644 --- a/nix/libvirtd.nix +++ b/nix/libvirtd.nix @@ -53,6 +53,32 @@ in ''; }; + deployment.libvirtd.template = mkOption { + type = types.str; + default = '' + {name} + {memory_size} + {vcpu} + {os} + + {emulator} + + + + + + {interfaces} + + + {extra_devices} + + {extra_domain} + ''; + description = '' + Template for the libvirt domain. You can use all other parameters. + ''; + }; + deployment.libvirtd.vcpu = mkOption { default = 1; type = types.int; diff --git a/nixops/backends/libvirtd.py b/nixops/backends/libvirtd.py index 0e4540b40..15597f73d 100644 --- a/nixops/backends/libvirtd.py +++ b/nixops/backends/libvirtd.py @@ -29,6 +29,7 @@ def __init__(self, xml, config): x = xml.find("attrs/attr[@name='libvirtd']/attrs") assert x is not None + self.domain_tpl = x.find("attr[@name='template']/string").get("value") self.vcpu = x.find("attr[@name='vcpu']/int").get("value") self.memory_size = x.find("attr[@name='memorySize']/int").get("value") self.extra_devices = x.find("attr[@name='extraDevicesXML']/string").get("value") @@ -185,38 +186,19 @@ def _make_os(defn): " %s"% defn.cmdline if len(defn.kernel) > 0 else "", ''] - - domain_fmt = "\n".join([ - '', - ' {0}', - ' {1}', - ' {4}', - '\n'.join(_make_os(defn)), - ' ', - ' {2}', - ' ', - ' ', - ' ', - ' ', - ' ', - '\n'.join([iface(n) for n in defn.networks]), - ' ' if not defn.headless else "", - ' ', - ' ', - defn.extra_devices, - ' ', - defn.extra_domain, - '', - ]) - - return domain_fmt.format( - self._vm_id(), - defn.memory_size, - qemu, - self._disk_path(defn), - defn.vcpu, - defn.domain_type + result = defn.domain_tpl.format( + name=self._vm_id(), + memory_size=defn.memory_size, + os='\n'.join(_make_os(defn)), + emulator=qemu, + interfaces='\n'.join([iface(n) for n in defn.networks]), + diskPath=self._disk_path(defn), + vcpu=defn.vcpu, + domain_type=defn.domain_type, + extra_domain=defn.extra_domain, + extra_devices=defn.extra_devices + ('' if not defn.headless else ""), ) + return result def _parse_ip(self): """ @@ -260,7 +242,9 @@ def start(self): self.private_ipv4 = self._parse_ip() else: self.log("starting...") - self.dom.create() + if self.dom.create() is False: + self.log("Failed to create domain "); + return False self._wait_for_ip(0) def get_ssh_name(self):