Skip to content

Commit

Permalink
Move domain templates to Jinja2
Browse files Browse the repository at this point in the history
For now only for simple replacements, so there is not a big difference
from what we had before.

Signed-off-by: Nadav Goldin <[email protected]>
  • Loading branch information
nvgoldin committed Jun 22, 2017
1 parent 5a7b499 commit d94158e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
1 change: 1 addition & 0 deletions lago.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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 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

0 comments on commit d94158e

Please sign in to comment.