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:
parent
c328dddd13
commit
d4d2183c48
@ -16,6 +16,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
import jinja2
|
||||||
import libvirt
|
import libvirt
|
||||||
|
|
||||||
templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
|
templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
|
||||||
@ -80,8 +81,10 @@ def main():
|
|||||||
parser.add_argument('--disk-format', default='qcow2',
|
parser.add_argument('--disk-format', default='qcow2',
|
||||||
help='Disk format to use.')
|
help='Disk format to use.')
|
||||||
args = parser.parse_args()
|
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 = {
|
params = {
|
||||||
'name': args.name,
|
'name': args.name,
|
||||||
'imagefile': args.image,
|
'imagefile': args.image,
|
||||||
@ -108,7 +111,7 @@ def main():
|
|||||||
params['console'] = CONSOLE_LOG % {'console_log': args.console_log}
|
params['console'] = CONSOLE_LOG % {'console_log': args.console_log}
|
||||||
else:
|
else:
|
||||||
params['console'] = CONSOLE_PTY
|
params['console'] = CONSOLE_PTY
|
||||||
libvirt_template = source_template % params
|
libvirt_template = template.render(**params)
|
||||||
conn = libvirt.open("qemu:///system")
|
conn = libvirt.open("qemu:///system")
|
||||||
|
|
||||||
a = conn.defineXML(libvirt_template)
|
a = conn.defineXML(libvirt_template)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<domain type='%(engine)s'>
|
<domain type='{{ engine }}'>
|
||||||
<name>%(name)s</name>
|
<name>{{ name }}</name>
|
||||||
<memory unit='KiB'>%(memory)s</memory>
|
<memory unit='KiB'>{{ memory }}</memory>
|
||||||
<vcpu>%(cpus)s</vcpu>
|
<vcpu>{{ cpus }}</vcpu>
|
||||||
<os>
|
<os>
|
||||||
<type arch='%(arch)s' machine='pc-1.0'>hvm</type>
|
<type arch='{{ arch }}' machine='pc-1.0'>hvm</type>
|
||||||
<boot dev='%(bootdev)s'/>
|
<boot dev='{{ bootdev }}'/>
|
||||||
<bootmenu enable='no'/>
|
<bootmenu enable='no'/>
|
||||||
<bios useserial='yes'/>
|
<bios useserial='yes'/>
|
||||||
</os>
|
</os>
|
||||||
@ -18,10 +18,10 @@
|
|||||||
<on_reboot>restart</on_reboot>
|
<on_reboot>restart</on_reboot>
|
||||||
<on_crash>restart</on_crash>
|
<on_crash>restart</on_crash>
|
||||||
<devices>
|
<devices>
|
||||||
<emulator>%(emulator)s</emulator>
|
<emulator>{{ emulator }}</emulator>
|
||||||
<disk type='file' device='disk'>
|
<disk type='file' device='disk'>
|
||||||
<driver name='qemu' type='%(disk_format)s' cache='writeback'/>
|
<driver name='qemu' type='{{ disk_format }}' cache='writeback'/>
|
||||||
<source file='%(imagefile)s'/>
|
<source file='{{ imagefile }}'/>
|
||||||
<target dev='vda' bus='virtio'/>
|
<target dev='vda' bus='virtio'/>
|
||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
|
||||||
</disk>
|
</disk>
|
||||||
@ -29,8 +29,8 @@
|
|||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
|
||||||
</controller>
|
</controller>
|
||||||
<interface type='bridge'>
|
<interface type='bridge'>
|
||||||
<source bridge='%(bridge)s'/>
|
<source bridge='{{ bridge }}'/>
|
||||||
<model type='%(nicdriver)s'/>
|
<model type='{{ nicdriver }}'/>
|
||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
|
||||||
</interface>
|
</interface>
|
||||||
<input type='mouse' bus='ps2'/>
|
<input type='mouse' bus='ps2'/>
|
||||||
@ -39,7 +39,7 @@
|
|||||||
<model type='cirrus' vram='9216' heads='1'/>
|
<model type='cirrus' vram='9216' heads='1'/>
|
||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
|
||||||
</video>
|
</video>
|
||||||
%(console)s
|
{{ console }}
|
||||||
<memballoon model='virtio'>
|
<memballoon model='virtio'>
|
||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
|
||||||
</memballoon>
|
</memballoon>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user