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

Cirros0.3.5 support #584

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lago.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Requires: sudo
%{python2_sitelib}/%{name}/providers/*.py*
%{python2_sitelib}/%{name}/providers/libvirt/*.py*
%{python2_sitelib}/%{name}/providers/libvirt/templates/*.xml
%{python2_sitelib}/%{name}/providers/libvirt/templates/*.j2

%{python2_sitelib}/%{name}-%{version}-py*.egg-info
%{_bindir}/lagocli
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<domain type='kvm'>
<name>@NAME@</name>
<memory unit='MiB'>@MEM_SIZE@</memory>
<name>{{ name }}</name>
<memory unit='MiB'>{{ mem_size }}</memory>
<iothreads>1</iothreads>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
Expand All @@ -13,7 +13,7 @@
<vmport state='off'/>
</features>
<devices>
<emulator>@QEMU_KVM@</emulator>
<emulator>{{ qemu_kvm }}</emulator>
<memballoon model='none'/>
<controller type='usb' model='none'>
</controller>
Expand All @@ -27,7 +27,7 @@
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<backend model='random'>{{ '/dev/urandom' if libvirt_ver >= 2002001 else '/dev/random' }}</backend>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</rng>
<console type='pty'>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<domain type='kvm'>
<name>@NAME@</name>
<memory unit='MiB'>@MEM_SIZE@</memory>
<name>{{ name }}</name>
<memory unit='MiB'>{{ mem_size }}</memory>
<iothreads>1</iothreads>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
Expand All @@ -13,7 +13,7 @@
<vmport state='off'/>
</features>
<devices>
<emulator>@QEMU_KVM@</emulator>
<emulator>{{ qemu_kvm }}</emulator>
<memballoon model='none'/>
<controller type='usb' model='none'>
</controller>
Expand Down
33 changes: 32 additions & 1 deletion lago/providers/libvirt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
Utilities to help deal with the libvirt python bindings
"""
import libvirt
import pkg_resources
import xmltodict
import lxml.etree
import logging
import pkg_resources
from jinja2 import Environment, PackageLoader, TemplateNotFound
from lago.config import config

LOGGER = logging.getLogger(__name__)

#: Mapping of domain statuses values to human readable strings
DOMAIN_STATES = {
libvirt.VIR_DOMAIN_NOSTATE: 'no state',
Expand Down Expand Up @@ -100,6 +104,33 @@ def get_template(basename):
)


def get_domain_template(distro, libvirt_ver, **kwargs):
"""
Get a rendered Jinja2 domain template

Args:
distro(str): domain distro
libvirt_ver(int): libvirt version
kwargs(dict): args for template render

Returns:
str: rendered template
"""
env = Environment(
loader=PackageLoader('lago', 'providers/libvirt/templates'),
trim_blocks=True,
lstrip_blocks=True,
)

template_name = 'dom_template-{0}.xml.j2'.format(distro)
try:
template = env.get_template(template_name)
except TemplateNotFound:
LOGGER.debug('could not find template %s using default', template_name)
template = env.get_template('dom_template-base.xml.j2')
return template.render(libvirt_ver=libvirt_ver, **kwargs)


def dict_to_xml(spec, full_document=False):
"""
Convert dict to XML
Expand Down
37 changes: 19 additions & 18 deletions lago/providers/libvirt/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, vm):
name=self.vm.virt_env.uuid + libvirt_url,
libvirt_url=libvirt_url,
)
self._libvirt_ver = self.libvirt_con.getVersion()

caps_raw_xml = self.libvirt_con.getCapabilities()
self._caps = ET.fromstring(caps_raw_xml)
Expand Down Expand Up @@ -367,19 +368,10 @@ def cpu_vendor(self):
"""
return self._cpu.vendor

def _load_domain_xml(self):
if self.vm.distro() == 'el6':
dom_raw_xml = libvirt_utils.get_template('dom_template-el6.xml')
else:
dom_raw_xml = libvirt_utils.get_template('dom_template.xml')
return dom_raw_xml

def _libvirt_name(self):
return self.vm.virt_env.prefixed_name(self.vm.name())

def _libvirt_xml(self):
dom_raw_xml = self._load_domain_xml()

def _get_qemu_kvm_path(self):
qemu_kvm_path = self._caps.findtext(
"guest[os_type='hvm']/arch[@name='x86_64']/domain[@type='kvm']"
"/emulator"
Expand All @@ -393,19 +385,28 @@ def _libvirt_xml(self):
)

if not qemu_kvm_path:
raise Exception('kvm executable not found')
raise utils.LagoException('kvm executable not found')

return qemu_kvm_path

replacements = {
'@NAME@': self._libvirt_name(),
'@MEM_SIZE@': self.vm._spec.get('memory', 16 * 1024),
'@QEMU_KVM@': qemu_kvm_path,
def _load_xml(self):

args = {
'distro': self.vm.distro(),
'libvirt_ver': self._libvirt_ver,
'name': self._libvirt_name(),
'mem_size': self.vm.spec.get('memory', 16 * 1024),
'qemu_kvm': self._get_qemu_kvm_path()
}

for key, val in replacements.items():
dom_raw_xml = dom_raw_xml.replace(key, str(val), 1)
dom_raw_xml = libvirt_utils.get_domain_template(**args)

parser = ET.XMLParser(remove_blank_text=True)
dom_xml = ET.fromstring(dom_raw_xml, parser)
return ET.fromstring(dom_raw_xml, parser)

def _libvirt_xml(self):

dom_xml = self._load_xml()

for child in self._cpu:
dom_xml.append(child)
Expand Down
5 changes: 5 additions & 0 deletions lago/templates/sysprep-cirros0.3.5.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% import 'sysprep-macros.j2' as macros %}
{% include 'sysprep-base.j2' %}

{{ macros.iscsi(name=iscsi_name, hostname=hostname) }}
{{ macros.network_devices_cirros(mappings=mappings) }}
12 changes: 12 additions & 0 deletions lago/templates/sysprep-macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ source /etc/network/interfaces.d/*.cfg \
{% endfor %}
{% endfilter %}
{% endmacro %}


{% macro network_devices_cirros(mappings) %}
write /etc/network/interfaces:auto lo \
iface lo inet loopback \
{% filter dedent %}
{%+ for iface, mac in mappings.viewitems() %}
auto {{ iface }} \
iface {{ iface }} inet dhcp \
{% endfor %}
{% endfilter %}
{% endmacro %}