DevStack: Use Jinja2 for templating when creating new VMs

This patch is changing the configure-vm.py script to use Jinja2
templating when creating new VMs instead of string interpolation.

Jinja templating allows more complex templating such as conditionals
which will greatly simplify the code for creating Legacy BIOS or UEFI
supported VMs.

Change-Id: Id4a8432923fa71ca8f3f28edd1235d3a318c3e95
Partial-Bug: #1625616
This commit is contained in:
Lucas Alvares Gomes 2016-09-21 14:49:51 -03:00
parent c328dddd13
commit d4d2183c48
2 changed files with 18 additions and 15 deletions

View File

@ -16,6 +16,7 @@
import argparse
import os.path
import jinja2
import libvirt
templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
@ -80,8 +81,10 @@ def main():
parser.add_argument('--disk-format', default='qcow2',
help='Disk format to use.')
args = parser.parse_args()
with open(templatedir + '/vm.xml', 'rb') as f:
source_template = f.read()
env = jinja2.Environment(loader=jinja2.FileSystemLoader(templatedir))
template = env.get_template('vm.xml')
params = {
'name': args.name,
'imagefile': args.image,
@ -108,7 +111,7 @@ def main():
params['console'] = CONSOLE_LOG % {'console_log': args.console_log}
else:
params['console'] = CONSOLE_PTY
libvirt_template = source_template % params
libvirt_template = template.render(**params)
conn = libvirt.open("qemu:///system")
a = conn.defineXML(libvirt_template)

View File

@ -1,10 +1,10 @@
<domain type='%(engine)s'>
<name>%(name)s</name>
<memory unit='KiB'>%(memory)s</memory>
<vcpu>%(cpus)s</vcpu>
<domain type='{{ engine }}'>
<name>{{ name }}</name>
<memory unit='KiB'>{{ memory }}</memory>
<vcpu>{{ cpus }}</vcpu>
<os>
<type arch='%(arch)s' machine='pc-1.0'>hvm</type>
<boot dev='%(bootdev)s'/>
<type arch='{{ arch }}' machine='pc-1.0'>hvm</type>
<boot dev='{{ bootdev }}'/>
<bootmenu enable='no'/>
<bios useserial='yes'/>
</os>
@ -18,10 +18,10 @@
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>%(emulator)s</emulator>
<emulator>{{ emulator }}</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='%(disk_format)s' cache='writeback'/>
<source file='%(imagefile)s'/>
<driver name='qemu' type='{{ disk_format }}' cache='writeback'/>
<source file='{{ imagefile }}'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
@ -29,8 +29,8 @@
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'>
<source bridge='%(bridge)s'/>
<model type='%(nicdriver)s'/>
<source bridge='{{ bridge }}'/>
<model type='{{ nicdriver }}'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</interface>
<input type='mouse' bus='ps2'/>
@ -39,7 +39,7 @@
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
%(console)s
{{ console }}
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>