Refactor jinja rendering of templates

We do the same thing for all deployments. Refactor the code
to utils.

Change-Id: Ic9db8d852fbfa50f4ac574ebe524c1fb1d85c588
This commit is contained in:
ramishra 2021-01-25 09:06:09 +05:30
parent 5027962dd7
commit e93244a2a5
5 changed files with 64 additions and 43 deletions

View File

@ -79,7 +79,8 @@ class TestUndercloudInstall(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--heat-native', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'undercloud.yaml', '-e',
@ -151,7 +152,7 @@ class TestUndercloudInstall(TestPluginV1):
'--local-ip=192.168.24.1/24',
'--templates=/usertht',
'--roles-file=foo/roles.yaml',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usertht/network_data_undercloud.yaml',
'--heat-native=False', '-e',
'/usertht/environments/undercloud.yaml', '-e',
'/usertht/environments/use-dns-for-vips.yaml', '-e',
@ -307,7 +308,8 @@ class TestUndercloudInstall(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--heat-native', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'undercloud.yaml', '-e',
@ -379,7 +381,8 @@ class TestUndercloudInstall(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--heat-native', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'undercloud.yaml', '-e',
@ -444,7 +447,8 @@ class TestUndercloudInstall(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--heat-native', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'undercloud.yaml', '-e',
@ -506,7 +510,8 @@ class TestUndercloudInstall(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--heat-native', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'undercloud.yaml', '-e',
@ -655,7 +660,8 @@ class TestUndercloudUpgrade(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--upgrade', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'lifecycle/undercloud-upgrade-prepare.yaml',
@ -723,7 +729,8 @@ class TestUndercloudUpgrade(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--upgrade', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'lifecycle/undercloud-upgrade-prepare.yaml',
@ -791,7 +798,8 @@ class TestUndercloudUpgrade(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--upgrade', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'lifecycle/undercloud-upgrade-prepare.yaml',
@ -857,7 +865,8 @@ class TestUndercloudUpgrade(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'-y', '--upgrade', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'lifecycle/undercloud-upgrade-prepare.yaml',
@ -929,7 +938,8 @@ class TestUndercloudUpgrade(TestPluginV1):
'undercloud', '--local-domain=localdomain',
'--local-ip=192.168.24.1/24',
'--templates=/usr/share/openstack-tripleo-heat-templates/',
'--networks-file=network_data_undercloud.yaml',
'--networks-file=/usr/share/openstack-tripleo-heat-templates/'
'network_data_undercloud.yaml',
'--upgrade', '-e',
'/usr/share/openstack-tripleo-heat-templates/environments/'
'lifecycle/undercloud-upgrade-prepare.yaml',

View File

@ -1651,6 +1651,37 @@ def build_stack_data(clients, stack_name, template,
return stack_data
def jinja_render_files(log, templates, working_dir,
roles_file=None, networks_file=None,
base_path=None, output_dir=None):
python_version = sys.version_info[0]
python_cmd = "python{}".format(python_version)
process_templates = os.path.join(
templates, 'tools/process-templates.py')
args = [python_cmd, process_templates]
if roles_file:
roles_file_path = get_roles_file_path(
roles_file, base_path)
args.extend(['--roles-data', roles_file_path])
if networks_file:
networks_file_path = get_networks_file_path(
networks_file, base_path)
args.extend(['--network-data', networks_file_path])
if base_path:
args.extend(['-p', base_path])
if output_dir:
args.extend(['-o', output_dir])
if run_command_and_log(log, args, working_dir) != 0:
msg = _("Problems generating templates.")
log.error(msg)
raise exceptions.DeploymentError(msg)
def process_multiple_environments(created_env_files, tht_root,
user_tht_root,
env_files_tracker=None,

View File

@ -25,7 +25,6 @@ import re
import shutil
import six
import subprocess
import sys
import tempfile
import time
import yaml
@ -287,33 +286,18 @@ class DeployOvercloud(command.Command):
self.log)
def _deploy_tripleo_heat_templates_tmpdir(self, stack, parsed_args):
# copy tht_root to temporary directory because we need to
# download any missing (e.g j2 rendered) files from the plan
tht_root = os.path.abspath(parsed_args.templates)
tht_tmp = tempfile.mkdtemp(prefix='tripleoclient-')
new_tht_root = "%s/tripleo-heat-templates" % tht_tmp
self.log.debug("Creating temporary templates tree in %s"
% new_tht_root)
python_version = sys.version_info[0]
python_cmd = "python{}".format(python_version)
try:
shutil.copytree(tht_root, new_tht_root, symlinks=True)
process_templates = os.path.join(
parsed_args.templates, 'tools/process-templates.py')
roles_file_path = utils.get_roles_file_path(
parsed_args.roles_file, new_tht_root)
networks_file_path = utils.get_networks_file_path(
parsed_args.networks_file, new_tht_root)
args = [python_cmd, process_templates, '--roles-data',
roles_file_path, '--network-data', networks_file_path,
'-p', new_tht_root]
if utils.run_command_and_log(
self.log, args, new_tht_root) != 0:
msg = _("Problems generating templates.")
self.log.error(msg)
raise exceptions.DeploymentError(msg)
utils.jinja_render_files(self.log, parsed_args.templates,
new_tht_root,
parsed_args.roles_file,
parsed_args.networks_file,
new_tht_root)
self._deploy_tripleo_heat_templates(stack, parsed_args,
new_tht_root, tht_root)
finally:

View File

@ -658,16 +658,11 @@ class Deploy(command.Command):
# generate jinja templates by its work dir location
self.log.debug(_("Using roles file %s") % roles_file_path)
process_templates = os.path.join(parsed_args.templates,
'tools/process-templates.py')
args = [self.python_cmd, process_templates, '--roles-data',
roles_file_path, '--network-data', networks_file_path,
'--output-dir', self.tht_render]
if utils.run_command_and_log(self.log, args, cwd=self.tht_render) != 0:
# TODO(aschultz): improve error messaging
msg = _("Problems generating templates.")
self.log.error(msg)
raise exceptions.DeploymentError(msg)
utils.jinja_render_files(self.log, parsed_args.templates,
working_dir=self.tht_render,
roles_file=roles_file_path,
networks_file=networks_file_path,
output_dir=self.tht_render)
# NOTE(aschultz): the next set of environment files are system included
# so we have to include them at the front of our environment list so a

View File

@ -563,7 +563,8 @@ def prepare_undercloud_deploy(upgrade=False, no_validations=True,
deploy_args.append('--networks-file=%s' % CONF['networks_file'])
else:
deploy_args.append('--networks-file=%s' %
constants.UNDERCLOUD_NETWORKS_FILE)
os.path.join(tht_templates,
constants.UNDERCLOUD_NETWORKS_FILE))
if yes:
deploy_args += ['-y']