2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
22 parser = argparse.ArgumentParser(
23 description='Configure host network interfaces using a JSON'
24 ' config file format.')
25 parser.add_argument('-p', '--base_path', metavar='BASE_PATH',
26 help="""base path of templates to process.""",
28 parser.add_argument('-r', '--roles-data', metavar='ROLES_DATA',
29 help="""relative path to the roles_data.yaml file.""",
30 default='roles_data.yaml')
31 parser.add_argument('--safe',
33 help="""Enable safe mode (do not overwrite files).""",
35 opts = parser.parse_args(argv[1:])
40 def _j2_render_to_file(j2_template, j2_data, outfile_name=None,
42 yaml_f = outfile_name or j2_template.replace('.j2.yaml', '.yaml')
43 print('rendering j2 template to file: %s' % outfile_name)
45 if not overwrite and os.path.exists(outfile_name):
46 print('ERROR: path already exists for file: %s' % outfile_name)
50 # Render the j2 template
51 template = jinja2.Environment().from_string(j2_template)
52 r_template = template.render(**j2_data)
53 except jinja2.exceptions.TemplateError as ex:
54 error_msg = ("Error rendering template %s : %s"
55 % (yaml_f, six.text_type(ex)))
57 raise Exception(error_msg)
58 with open(outfile_name, 'w') as out_f:
59 out_f.write(r_template)
62 def process_templates(template_path, role_data_path, overwrite):
64 with open(role_data_path) as role_data_file:
65 role_data = yaml.safe_load(role_data_file)
67 j2_excludes_path = os.path.join(template_path, 'j2_excludes.yaml')
68 with open(j2_excludes_path) as role_data_file:
69 j2_excludes = yaml.safe_load(role_data_file)
71 role_names = [r.get('name') for r in role_data]
74 r_map[r.get('name')] = r
75 excl_templates = ['%s/%s' % (template_path, e)
76 for e in j2_excludes.get('name')]
78 if os.path.isdir(template_path):
79 for subdir, dirs, files in os.walk(template_path):
81 file_path = os.path.join(subdir, f)
82 # We do two templating passes here:
83 # 1. *.role.j2.yaml - we template just the role name
84 # and create multiple files (one per role)
85 # 2. *.j2.yaml - we template with all roles_data,
86 # and create one file common to all roles
87 if f.endswith('.role.j2.yaml'):
88 print("jinja2 rendering role template %s" % f)
89 with open(file_path) as j2_template:
90 template_data = j2_template.read()
91 print("jinja2 rendering roles %s" % ","
93 for role in role_names:
94 j2_data = {'role': role}
95 # (dprince) For the undercloud installer we don't
96 # want to have heat check nova/glance API's
97 if r_map[role].get('disable_constraints', False):
98 j2_data['disable_constraints'] = True
101 os.path.basename(f).replace('.role.j2.yaml',
103 out_f_path = os.path.join(subdir, out_f)
104 if not (out_f_path in excl_templates):
105 _j2_render_to_file(template_data, j2_data,
106 out_f_path, overwrite)
108 print('skipping rendering of %s' % out_f_path)
109 elif f.endswith('.j2.yaml'):
110 print("jinja2 rendering normal template %s" % f)
111 with open(file_path) as j2_template:
112 template_data = j2_template.read()
113 j2_data = {'roles': role_data}
114 out_f = file_path.replace('.j2.yaml', '.yaml')
115 _j2_render_to_file(template_data, j2_data, out_f,
119 print('Unexpected argument %s' % template_path)
121 opts = parse_opts(sys.argv)
123 role_data_path = os.path.join(opts.base_path, opts.roles_data)
125 process_templates(opts.base_path, role_data_path, (not opts.safe))