Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RDY] Format domain XML from a file to allow for more flexibility #903

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions nix/libvirtd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ in
'';
};

deployment.libvirtd.template = mkOption {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm confused what's the point of having this as a nixops option ?
What if someone changes this to:

deployment.libvirtd.template = ''
<other template that doesn't use the same variables>
''

That'll basically make it inconsistent with what the python code expects

type = types.str;
default = ''<domain type="{domain_type}">
<name>{name}</name>
<memory unit="MiB">{memory_size}</memory>
<vcpu>{vcpu}</vcpu>
{os}
<devices>
<emulator>{emulator}</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2"/>
<source file="{diskPath}"/>
<target dev="hda"/>
</disk>
{interfaces}
<input type="keyboard" bus="usb"/>
<input type="mouse" bus="usb"/>
{extra_devices}
</devices>
{extra_domain}
</domain>'';
description = ''
Template for the libvirt domain. You can use all other parameters.
'';
};

deployment.libvirtd.vcpu = mkOption {
default = 1;
type = types.int;
Expand Down
48 changes: 16 additions & 32 deletions nixops/backends/libvirtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -185,38 +186,19 @@ def _make_os(defn):
" <cmdline>%s</cmdline>"% defn.cmdline if len(defn.kernel) > 0 else "",
'</os>']


domain_fmt = "\n".join([
'<domain type="{5}">',
' <name>{0}</name>',
' <memory unit="MiB">{1}</memory>',
' <vcpu>{4}</vcpu>',
'\n'.join(_make_os(defn)),
' <devices>',
' <emulator>{2}</emulator>',
' <disk type="file" device="disk">',
' <driver name="qemu" type="qcow2"/>',
' <source file="{3}"/>',
' <target dev="hda"/>',
' </disk>',
'\n'.join([iface(n) for n in defn.networks]),
' <graphics type="vnc" port="-1" autoport="yes"/>' if not defn.headless else "",
' <input type="keyboard" bus="usb"/>',
' <input type="mouse" bus="usb"/>',
defn.extra_devices,
' </devices>',
defn.extra_domain,
'</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(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I tried to replace the memory_size=defn.memory_size, by **defn (more elegant) but couldn't succeed.

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 + ('<graphics type="vnc" port="-1" autoport="yes"/>' if not defn.headless else ""),
)
return result

def _parse_ip(self):
"""
Expand Down Expand Up @@ -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):
Expand Down