From d4d2183c486128677f71cfdb9164886cb2014fed Mon Sep 17 00:00:00 2001 From: Lucas Alvares Gomes Date: Wed, 21 Sep 2016 14:49:51 -0300 Subject: [PATCH] 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 --- devstack/tools/ironic/scripts/configure-vm.py | 9 ++++--- devstack/tools/ironic/templates/vm.xml | 24 +++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/devstack/tools/ironic/scripts/configure-vm.py b/devstack/tools/ironic/scripts/configure-vm.py index cc466629de..5a235b6dd1 100755 --- a/devstack/tools/ironic/scripts/configure-vm.py +++ b/devstack/tools/ironic/scripts/configure-vm.py @@ -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) diff --git a/devstack/tools/ironic/templates/vm.xml b/devstack/tools/ironic/templates/vm.xml index 64d7899ba4..78297fdd04 100644 --- a/devstack/tools/ironic/templates/vm.xml +++ b/devstack/tools/ironic/templates/vm.xml @@ -1,10 +1,10 @@ - - %(name)s - %(memory)s - %(cpus)s + + {{ name }} + {{ memory }} + {{ cpus }} - hvm - + hvm + @@ -18,10 +18,10 @@ restart restart - %(emulator)s + {{ emulator }} - - + +
@@ -29,8 +29,8 @@
- - + +
@@ -39,7 +39,7 @@
- %(console)s + {{ console }}