10 'HeatTemplateFormatVersion': '2012-12-12',
15 description = 'Description'
16 parameters = 'Parameters'
18 resources = 'Resources'
20 properties = 'Properties'
22 depends_on = 'DependsOn'
23 get_attr = 'Fn::GetAtt'
29 'heat_template_version': '2014-10-16',
32 get_resource = 'get_resource'
33 get_param = 'get_param'
34 description = 'description'
35 parameters = 'parameters'
37 resources = 'resources'
39 properties = 'properties'
41 depends_on = 'depends_on'
48 def apply_maps(template):
49 """Apply Merge::Map within template.
51 Any dict {'Merge::Map': {'Foo': 'Bar', 'Baz': 'Quux'}}
52 will resolve to ['Bar', 'Quux'] - that is a dict with key
53 'Merge::Map' is replaced entirely by that dict['Merge::Map'].values().
55 if isinstance(template, dict):
56 if 'Merge::Map' in template:
58 apply_maps(value) for value in template['Merge::Map'].values()
61 return dict((key, apply_maps(value))
62 for key, value in template.items())
63 elif isinstance(template, list):
64 return [apply_maps(item) for item in template]
69 def apply_scaling(template, scaling, in_copies=None):
70 """Apply a set of scaling operations to template.
72 This is a single pass recursive function: for each call we process one
73 dict or list and recurse to handle children containers.
75 Values are handled via scale_value.
77 Keys in dicts are copied per the scaling rule.
78 Values are either replaced or copied depending on whether the given
79 scaling rule is in in_copies.
81 in_copies is reset to None when a dict {'Merge::Map': someobject} is
84 in_copies = dict(in_copies or {})
85 # Shouldn't be needed but to avoid unexpected side effects/bugs we short
89 if isinstance(template, dict):
90 if 'Merge::Map' in template:
93 for key, value in template.items():
94 for prefix, copy_num, new_key in scale_value(
95 key, scaling, in_copies):
97 # e.g. Compute0, 1, Compute1Foo
98 in_copies[prefix] = prefix[:-1] + str(copy_num)
99 if isinstance(value, (dict, list)):
100 new_value = apply_scaling(value, scaling, in_copies)
101 new_template[new_key] = new_value
103 new_values = list(scale_value(value, scaling, in_copies))
104 # We have nowhere to multiply a non-container value of a
105 # dict, so it may be copied or unchanged but not scaled.
106 assert len(new_values) == 1
107 new_template[new_key] = new_values[0][2]
109 del in_copies[prefix]
111 elif isinstance(template, list):
113 for value in template:
114 if isinstance(value, (dict, list)):
115 new_template.append(apply_scaling(value, scaling, in_copies))
117 for _, _, new_value in scale_value(value, scaling, in_copies):
118 new_template.append(new_value)
121 raise Exception("apply_scaling called with non-container %r" % template)
124 def scale_value(value, scaling, in_copies):
125 """Scale out a value.
127 :param value: The value to scale (not a container).
128 :param scaling: The scaling map to use.
129 :param in_copies: What containers we're currently copying.
130 :return: An iterator of the new values for the value as tuples:
131 (prefix, copy_num, value). E.g. Compute0, 1, Compute1Foo
132 prefix and copy_num are only set when:
133 - a prefix in scaling matches value
134 - and that prefix is not in in_copies
136 if isinstance(value, (str, unicode)):
137 for prefix, copies in scaling.items():
138 if not value.startswith(prefix):
140 suffix = value[len(prefix):]
141 if prefix in in_copies:
142 # Adjust to the copy number we're on
143 yield None, None, in_copies[prefix] + suffix
146 for n in range(copies):
147 yield prefix, n, prefix[:-1] + str(n) + suffix
149 yield None, None, value
151 yield None, None, value
154 def parse_scaling(scaling_args):
155 """Translate a list of scaling requests to a dict prefix:count."""
156 scaling_args = scaling_args or []
158 for item in scaling_args:
159 key, value = item.split('=')
161 result[key + '0'] = value
165 def _translate_role(role, master_role, slave_roles):
168 if role == master_role:
170 if role not in slave_roles:
174 def translate_role(role, master_role, slave_roles):
175 r = _translate_role(role, master_role, slave_roles)
176 if not isinstance(r, basestring):
177 raise Exception('%s -> %r' % (role, r))
180 def resolve_params(item, param, value):
181 if item in ({lang.get_param: param}, {lang.get_resource: param}):
183 if isinstance(item, dict):
184 copy_item = dict(item)
185 for k, v in iter(copy_item.items()):
186 item[k] = resolve_params(v, param, value)
187 elif isinstance(item, list):
188 copy_item = list(item)
191 new_item.append(resolve_params(v, param, value))
195 MERGABLE_TYPES = {'OS::Nova::Server':
197 'AWS::EC2::Instance':
198 {'image': 'ImageId'},
199 'AWS::AutoScaling::LaunchConfiguration':
202 INCLUDED_TEMPLATE_DIR = os.getcwd()
205 def resolve_includes(template, params=None):
209 for key, value in iter(template.items()):
210 if key == '__include__':
211 new_params = dict(params) # do not propagate up the stack
212 if not isinstance(value, dict):
213 raise ValueError('__include__ must be a mapping')
214 if 'path' not in value:
215 raise ValueError('__include__ must have path')
216 if 'params' in value:
217 if not isinstance(value['params'], dict):
218 raise ValueError('__include__ params must be a mapping')
219 new_params.update(value['params'])
220 with open(value['path']) as include_file:
221 sub_template = yaml.safe_load(include_file.read())
222 if 'subkey' in value:
223 if ((not isinstance(value['subkey'], int)
224 and not isinstance(sub_template, dict))):
225 raise RuntimeError('subkey requires mapping root or'
226 ' integer for list root')
227 sub_template = sub_template[value['subkey']]
228 for k, v in iter(new_params.items()):
229 sub_template = resolve_params(sub_template, k, v)
230 new_template.update(resolve_includes(sub_template))
232 if isinstance(value, dict):
233 new_template[key] = resolve_includes(value)
235 new_template[key] = value
241 parser = argparse.ArgumentParser()
242 parser.add_argument('templates', nargs='+')
243 parser.add_argument('--master-role', nargs='?',
244 help='Translate slave_roles to this')
245 parser.add_argument('--slave-roles', nargs='*',
246 help='Translate all of these to master_role')
247 parser.add_argument('--included-template-dir', nargs='?',
248 default=INCLUDED_TEMPLATE_DIR,
249 help='Path for resolving included templates')
250 parser.add_argument('--output',
251 help='File to write output to. - for stdout',
253 parser.add_argument('--scale', action="append",
254 help="Names to scale out. Pass Prefix=1 to cause a key Prefix0Foo to "
255 "be copied to Prefix1Foo in the output, and value Prefix0Bar to be"
256 "renamed to Prefix1Bar inside that copy, or copied to Prefix1Bar "
257 "outside of any copy.")
259 '--change-image-params', action='store_true', default=False,
260 help="Change parameters in templates to match resource names. This was "
261 " the default at one time but it causes issues when parameter "
262 " names need to remain stable.")
264 '--hot', action='store_true', default=False,
265 help="Assume source templates are in the HOT format, and generate a "
266 "HOT template artifact.")
267 args = parser.parse_args(argv)
272 templates = args.templates
273 scaling = parse_scaling(args.scale)
274 merged_template = merge(templates, args.master_role, args.slave_roles,
275 args.included_template_dir, scaling=scaling,
276 change_image_params=args.change_image_params)
277 if args.output == '-':
278 out_file = sys.stdout
280 out_file = file(args.output, 'wt')
281 out_file.write(merged_template)
284 def merge(templates, master_role=None, slave_roles=None,
285 included_template_dir=INCLUDED_TEMPLATE_DIR,
286 scaling=None, change_image_params=None):
287 scaling = scaling or {}
289 end_template = dict(lang.base_template)
291 for template_path in templates:
292 template = yaml.safe_load(open(template_path))
293 # Resolve __include__ tags
294 template = resolve_includes(template)
295 end_template[lang.description].append(template.get(lang.description,
297 new_parameters = template.get(lang.parameters, {})
298 for p, pbody in sorted(new_parameters.items()):
299 if p in end_template.get(lang.parameters, {}):
300 if pbody != end_template[lang.parameters][p]:
301 errors.append('Parameter %s from %s conflicts.' % (p,
304 if lang.parameters not in end_template:
305 end_template[lang.parameters] = {}
306 end_template[lang.parameters][p] = pbody
308 new_outputs = template.get(lang.outputs, {})
309 for o, obody in sorted(new_outputs.items()):
310 if o in end_template.get(lang.outputs, {}):
311 if pbody != end_template[lang.outputs][p]:
312 errors.append('Output %s from %s conflicts.' % (o,
315 if lang.outputs not in end_template:
316 end_template[lang.outputs] = {}
317 end_template[lang.outputs][o] = obody
319 new_resources = template.get(lang.resources, {})
320 for r, rbody in sorted(new_resources.items()):
321 if rbody[lang.type] in MERGABLE_TYPES:
322 if change_image_params:
323 if 'image' in MERGABLE_TYPES[rbody[lang.type]]:
324 image_key = MERGABLE_TYPES[rbody[lang.type]]['image']
325 # XXX Assuming ImageId is always a Ref
326 ikey_val = end_template[lang.parameters][rbody[lang.properties][image_key][lang.get_param]]
327 del end_template[lang.parameters][rbody[lang.properties][image_key][lang.get_param]]
328 role = rbody.get(lang.metadata, {}).get('OpenStack::Role', r)
329 role = translate_role(role, master_role, slave_roles)
331 resource_changes.append((r, role))
332 if role in end_template.get(lang.resources, {}):
333 new_metadata = rbody.get(lang.metadata, {})
334 for m, mbody in iter(new_metadata.items()):
335 if m in end_template[lang.resources][role].get(lang.metadata, {}):
336 if m == 'OpenStack::ImageBuilder::Elements':
337 end_template[lang.resources][role][lang.metadata][m].extend(mbody)
339 if mbody != end_template[lang.resources][role][lang.metadata][m]:
340 errors.append('Role %s metadata key %s conflicts.' %
343 role_res = end_template[lang.resources][role]
344 if role_res[lang.type] == 'OS::Heat::StructuredConfig':
345 end_template[lang.resources][role][lang.properties]['config'][m] = mbody
347 end_template[lang.resources][role][lang.metadata][m] = mbody
349 if lang.resources not in end_template:
350 end_template[lang.resources] = {}
351 end_template[lang.resources][role] = rbody
352 if change_image_params:
353 if 'image' in MERGABLE_TYPES[rbody[lang.type]]:
354 ikey = '%sImage' % (role)
355 end_template[lang.resources][role][lang.properties][image_key] = {lang.get_param: ikey}
356 end_template[lang.parameters][ikey] = ikey_val
357 elif rbody[lang.type] == 'FileInclude':
358 # we trust os.path.join to DTRT: if FileInclude path isn't
359 # absolute, join to included_template_dir (./)
360 with open(os.path.join(included_template_dir, rbody['Path'])) as rfile:
361 include_content = yaml.safe_load(rfile.read())
362 subkeys = rbody.get('SubKey','').split('.')
363 while len(subkeys) and subkeys[0]:
364 include_content = include_content[subkeys.pop(0)]
365 for replace_param, replace_value in iter(rbody.get(lang.parameters,
367 include_content = resolve_params(include_content,
370 if lang.resources not in end_template:
371 end_template[lang.resources] = {}
372 end_template[lang.resources][r] = include_content
374 if r in end_template.get(lang.resources, {}):
375 if rbody != end_template[lang.resources][r]:
376 errors.append('Resource %s from %s conflicts' % (r,
379 if lang.resources not in end_template:
380 end_template[lang.resources] = {}
381 end_template[lang.resources][r] = rbody
383 end_template = apply_scaling(end_template, scaling)
384 end_template = apply_maps(end_template)
386 def fix_ref(item, old, new):
387 if isinstance(item, dict):
388 copy_item = dict(item)
389 for k, v in sorted(copy_item.items()):
390 if k == lang.get_resource and v == old:
393 if k == lang.depends_on and v == old:
396 if k == lang.get_attr and isinstance(v, list) and v[0] == old:
401 if k == 'AllowedResources' and isinstance(v, list) and old in v:
407 elif isinstance(item, list):
408 copy_item = list(item)
412 for change in resource_changes:
413 fix_ref(end_template, change[0], change[1])
417 sys.stderr.write("ERROR: %s\n" % e)
418 end_template[lang.description] = ','.join(end_template[lang.description])
419 return yaml.safe_dump(end_template, default_flow_style=False)
421 if __name__ == "__main__":