Skip to content

Commit

Permalink
Port Cheetah templates to Jinja2
Browse files Browse the repository at this point in the history
Cheetah is unmaintained since 2010 and it's unlikely to get Python3
support soon. Also, the rest of OpenStack (mostly) standardized on
Jinja2.

Implements: blueprint jinja-templating-conversion
Change-Id: Ia15f00ee96d3c1d55d7c290f20ccc988e4c52e1a
  • Loading branch information
saschpe committed Sep 2, 2013
1 parent c3e8517 commit fa0d610
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[run]
branch = True
source = nova
omit = nova/tests/*,nova/openstack/*,DynamicallyCompiledCheetahTemplate.py
omit = nova/tests/*,nova/openstack/*

[report]
ignore-errors = True
6 changes: 3 additions & 3 deletions nova/cloudpipe/client.ovpn.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

# NOVA user connection
# Edit the following lines to point to your cert files:
cert $certfile
key $keyfile
cert {{ certfile }}
key {{ keyfile }}

ca cacert.pem

client
dev tap
proto udp

remote $ip $port
remote {{ ip }} {{ port }}
resolv-retry infinite
nobind

Expand Down
24 changes: 12 additions & 12 deletions nova/console/xvp.conf.template
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# One time password use with time window
OTP ALLOW IPCHECK HTTP 60
#if $multiplex_port
MULTIPLEX $multiplex_port
#end if
{% if multiplex_port %}
MULTIPLEX {{ multiplex_port }}
{% endif %}

#for $pool in $pools
POOL $pool.address
DOMAIN $pool.address
MANAGER root $pool.password
HOST $pool.address
{% for pool in pools %}
POOL {{ pool.address }}
DOMAIN {{ pool.address }}
MANAGER root {{ pool.password }}
HOST {{ pool.address }}
VM - dummy 0123456789ABCDEF
#for $console in $pool.consoles
VM #if $multiplex_port then '-' else $console.port # $console.instance_name $pass_encode($console.password)
#end for
#end for
{% for console in pool.console %}
VM {% if multiplex_port %}-{% else %}{{ console.port }} # {{ console.instance_name }} {{ console.password|pass_encode }}{% endif %}
{% endfor %}
{% endfor %}
13 changes: 7 additions & 6 deletions nova/console/xvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import signal

from Cheetah import Template
import jinja2
from oslo.config import cfg

from nova import context
Expand Down Expand Up @@ -108,11 +108,12 @@ def _rebuild_xvp_conf(self, context):
self._xvp_stop()
return
conf_data = {'multiplex_port': CONF.console_xvp_multiplex_port,
'pools': pools,
'pass_encode': self.fix_console_password}
config = str(Template.Template(self.xvpconf_template,
searchList=[conf_data]))
self._write_conf(config)
'pools': pools}
tmpl_path, tmpl_file = os.path.split(CONF.injected_network_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
env.filters['pass_encode'] = self.fix_console_password
template = env.get_template(tmpl_file)
self._write_conf(template.render(conf_data))
self._xvp_restart()

def _write_conf(self, config):
Expand Down
15 changes: 7 additions & 8 deletions nova/virt/baremetal/net-dhcp.ubuntu.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
auto lo
iface lo inet loopback

#for $ifc in $interfaces
auto ${ifc.name}
iface ${ifc.name} inet dhcp
{% for ifc in interfaces -%}
auto {{ ifc.name }}
iface {{ ifc.name }} inet dhcp

#if $use_ipv6
iface ${ifc.name} inet6 dhcp
#end if

#end for
{% if use_ipv6 -%}
iface {{ ifc.name }} inet6 dhcp
{%- endif %}
{%- endfor %}
32 changes: 16 additions & 16 deletions nova/virt/baremetal/net-static.ubuntu.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
auto lo
iface lo inet loopback

#for $ifc in $interfaces
auto ${ifc.name}
iface ${ifc.name} inet static
address ${ifc.address}
netmask ${ifc.netmask}
gateway ${ifc.gateway}
#if $ifc.dns
dns-nameservers ${ifc.dns}
#end if
{% for ifc in interfaces -%}
auto {{ ifc.name }}
iface {{ ifc.name }} inet static
address {{ ifc.address }}
netmask {{ ifc.netmask }}
gateway {{ ifc.gateway }}
{%- if ifc.dns %}
dns-nameservers {{ ifc.dns }}
{%- endif %}

#if $use_ipv6
iface ${ifc.name} inet6 static
address ${ifc.address_v6}
netmask ${ifc.netmask_v6}
gateway ${ifc.gateway_v6}
#end if
{% if use_ipv6 -%}
iface {{ ifc.name }} inet6 static
address {{ ifc.address_v6 }}
netmask {{ ifc.netmask_v6 }}
gateway {{ ifc.gateway_v6 }}
{%- endif %}

#end for
{%- endfor %}
37 changes: 11 additions & 26 deletions nova/virt/baremetal/pxe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import datetime
import os

import jinja2
from oslo.config import cfg

from nova.compute import flavors
Expand Down Expand Up @@ -71,16 +72,6 @@
CONF.register_opts(pxe_opts, baremetal_group)
CONF.import_opt('use_ipv6', 'nova.netconf')

CHEETAH = None


def _get_cheetah():
global CHEETAH
if CHEETAH is None:
from Cheetah import Template
CHEETAH = Template.Template
return CHEETAH


def build_pxe_network_config(network_info):
interfaces = bm_utils.map_network_interfaces(network_info, CONF.use_ipv6)
Expand Down Expand Up @@ -124,26 +115,20 @@ def build_pxe_config(deployment_id, deployment_key, deployment_iscsi_iqn,
'pxe_append_params': CONF.baremetal.pxe_append_params,
'pxe_network_config': network_config,
}
cheetah = _get_cheetah()
pxe_config = str(cheetah(
open(CONF.baremetal.pxe_config_template).read(),
searchList=[{'pxe_options': pxe_options,
'ROOT': '${ROOT}',
}]))
return pxe_config
tmpl_path, tmpl_file = os.path.split(CONF.baremetal.pxe_config_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
template = env.get_template(tmpl_file)
return template.render({'pxe_options': pxe_options,
'ROOT': '${ROOT}'})


def build_network_config(network_info):
interfaces = bm_utils.map_network_interfaces(network_info, CONF.use_ipv6)
cheetah = _get_cheetah()
network_config = str(cheetah(
open(CONF.baremetal.net_config_template).read(),
searchList=[
{'interfaces': interfaces,
'use_ipv6': CONF.use_ipv6,
}
]))
return network_config
tmpl_path, tmpl_file = os.path.split(CONF.baremetal.net_config_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
template = env.get_template(tmpl_file)
return template.render({'interfaces': interfaces,
'use_ipv6': CONF.use_ipv6})


def get_deploy_aki_id(instance_type):
Expand Down
8 changes: 4 additions & 4 deletions nova/virt/baremetal/pxe_config.template
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
default deploy

label deploy
kernel ${pxe_options.deployment_aki_path}
append initrd=${pxe_options.deployment_ari_path} selinux=0 disk=cciss/c0d0,sda,hda,vda iscsi_target_iqn=${pxe_options.deployment_iscsi_iqn} deployment_id=${pxe_options.deployment_id} deployment_key=${pxe_options.deployment_key} troubleshoot=0 ${pxe_options.pxe_append_params}
kernel {{ pxe_options.deployment_aki_path }}
append initrd={{ pxe_options.deployment_ari_path }} selinux=0 disk=cciss/c0d0,sda,hda,vda iscsi_target_iqn={{ pxe_options.deployment_iscsi_iqn }} deployment_id={{ pxe_options.deployment_id }} deployment_key={{ pxe_options.deployment_key }} troubleshoot=0 {{ pxe_options.pxe_append_params|default("", true) }}
ipappend 3


label boot
kernel ${pxe_options.aki_path}
append initrd=${pxe_options.ari_path} root=${ROOT} ro ${pxe_options.pxe_append_params} ${pxe_options.pxe_network_config}
kernel {{ pxe_options.aki_path }}
append initrd={{ pxe_options.ari_path }} root={{ ROOT }} ro {{ pxe_options.pxe_append_params|default("", true) }} {{ pxe_options.pxe_network_config|default("", true) }}
25 changes: 6 additions & 19 deletions nova/virt/baremetal/tilera.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import base64
import os

import jinja2
from oslo.config import cfg

from nova.compute import flavors
Expand All @@ -44,28 +45,14 @@
CONF.import_opt('net_config_template', 'nova.virt.baremetal.pxe',
group='baremetal')

CHEETAH = None


def _get_cheetah():
global CHEETAH
if CHEETAH is None:
from Cheetah import Template
CHEETAH = Template.Template
return CHEETAH


def build_network_config(network_info):
interfaces = bm_utils.map_network_interfaces(network_info, CONF.use_ipv6)
cheetah = _get_cheetah()
network_config = str(cheetah(
open(CONF.baremetal.net_config_template).read(),
searchList=[
{'interfaces': interfaces,
'use_ipv6': CONF.use_ipv6,
}
]))
return network_config
tmpl_path, tmpl_file = os.path.split(CONF.baremetal.net_config_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
template = env.get_template(tmpl_file)
return template.render({'interfaces': interfaces,
'use_ipv6': CONF.use_ipv6})


def get_image_dir_path(instance):
Expand Down
42 changes: 21 additions & 21 deletions nova/virt/interfaces.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
auto lo
iface lo inet loopback

#for $ifc in $interfaces
auto ${ifc.name}
iface ${ifc.name} inet static
address ${ifc.address}
netmask ${ifc.netmask}
broadcast ${ifc.broadcast}
#if $ifc.gateway
gateway ${ifc.gateway}
#end if
#if $ifc.dns
dns-nameservers ${ifc.dns}
#end if
{% for ifc in interfaces -%}
auto {{ ifc.name }}
iface {{ ifc.name }} inet static
address {{ ifc.address }}
netmask {{ ifc.netmask }}
broadcast {{ ifc.broadcast }}
{%- if ifc.gateway %}
gateway {{ ifc.gateway }}
{%- endif %}
{%- if ifc.dns %}
dns-nameservers {{ ifc.dns }}
{%- endif %}

#if $use_ipv6
iface ${ifc.name} inet6 static
address ${ifc.address_v6}
netmask ${ifc.netmask_v6}
#if $ifc.gateway_v6
gateway ${ifc.gateway_v6}
#end if
#end if
{% if use_ipv6 -%}
iface {{ ifc.name }} inet6 static
address {{ ifc.address_v6 }}
netmask {{ ifc.netmask_v6 }}
{%- if ifc.gateway_v6 %}
gateway {{ ifc.gateway_v6 }}
{%- endif %}
{%- endif %}

#end for
{%- endfor %}
23 changes: 7 additions & 16 deletions nova/virt/netutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

"""Network-related utilities for supporting libvirt connection code."""

import os

import jinja2
import netaddr

from oslo.config import cfg
Expand All @@ -32,16 +34,6 @@
CONF.import_opt('use_ipv6', 'nova.netconf')
CONF.import_opt('injected_network_template', 'nova.virt.disk.api')

Template = None


def _late_load_cheetah():
global Template
if Template is None:
t = __import__('Cheetah.Template', globals(), locals(),
['Template'], -1)
Template = t.Template


def get_net_and_mask(cidr):
net = netaddr.IPNetwork(cidr)
Expand Down Expand Up @@ -138,9 +130,8 @@ def get_injected_network_template(network_info, use_ipv6=CONF.use_ipv6,


def build_template(template, nets, ipv6_is_available):
_late_load_cheetah()

ifc_template = open(template).read()
return str(Template(ifc_template,
searchList=[{'interfaces': nets,
'use_ipv6': ipv6_is_available}]))
tmpl_path, tmpl_file = os.path.split(CONF.injected_network_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
template = env.get_template(tmpl_file)
return template.render({'interfaces': nets,
'use_ipv6': ipv6_is_available})
15 changes: 7 additions & 8 deletions nova/virt/xenapi/vm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,11 +2214,11 @@ def _prepare_injectables(inst, network_info):
prepares the ssh key and the network configuration file to be
injected into the disk image
"""
#do the import here - Cheetah.Template will be loaded
#only if injection is performed
from Cheetah import Template as t
template = t.Template
template_data = open(CONF.injected_network_template).read()
#do the import here - Jinja2 will be loaded only if injection is performed
import jinja2
tmpl_path, tmpl_file = os.path.split(CONF.injected_network_template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path))
template = env.get_template(tmpl_file)

metadata = inst['metadata']
key = str(inst['key_data'])
Expand Down Expand Up @@ -2301,9 +2301,8 @@ def _prepare_injectables(inst, network_info):
interfaces_info.append(interface_info)

if interfaces_info:
net = str(template(template_data,
searchList=[{'interfaces': interfaces_info,
'use_ipv6': CONF.use_ipv6}]))
net = template.render({'interfaces': interfaces_info,
'use_ipv6': CONF.use_ipv6})
return key, net, metadata


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
d2to1>=0.2.10,<0.3
pbr>=0.5.16,<0.6
SQLAlchemy>=0.7.8,<0.7.99
Cheetah>=2.4.4
amqplib>=0.6.1
anyjson>=0.2.4
argparse
boto
eventlet>=0.9.17
Jinja2
kombu>=1.0.4
lxml>=2.3
routes>=1.12.3
Expand Down

0 comments on commit fa0d610

Please sign in to comment.