raise Exception('%s -> %r' % (role, r))
return r
+def resolve_params(item, param, value):
+ if item == {'Ref': param}:
+ return value
+ if isinstance(item, dict):
+ copy_item = dict(item)
+ for k, v in iter(copy_item.items()):
+ item[k] = resolve_params(v, param, value)
+ elif isinstance(item, list):
+ copy_item = list(item)
+ new_item = []
+ for v in copy_item:
+ new_item.append(resolve_params(v, param, value))
+ item = new_item
+ return item
+
+MERGABLE_TYPES = {'OS::Nova::Server':
+ {'image': 'image'},
+ 'AWS::EC2::Instance':
+ {'image': 'ImageId'},
+ 'AWS::AutoScaling::LaunchConfiguration':
+ {},
+ }
+
+
+def resolve_includes(template, params=None):
+ new_template = {}
+ if params is None:
+ params = {}
+ for key, value in iter(template.items()):
+ if key == '__include__':
+ new_params = dict(params) # do not propagate up the stack
+ if not isinstance(value, dict):
+ raise ValueError('__include__ must be a mapping')
+ if 'path' not in value:
+ raise ValueError('__include__ must have path')
+ if 'params' in value:
+ if not isinstance(value['params'], dict):
+ raise ValueError('__include__ params must be a mapping')
+ new_params.update(value['params'])
+ with open(value['path']) as include_file:
+ sub_template = yaml.safe_load(include_file.read())
+ if 'subkey' in value:
+ if ((not isinstance(value['subkey'], int)
+ and not isinstance(sub_template, dict))):
+ raise RuntimeError('subkey requires mapping root or'
+ ' integer for list root')
+ sub_template = sub_template[value['subkey']]
+ for k, v in iter(new_params.items()):
+ sub_template = resolve_params(sub_template, k, v)
+ new_template.update(resolve_includes(sub_template))
+ else:
+ if isinstance(value, dict):
+ new_template[key] = resolve_includes(value)
+ else:
+ new_template[key] = value
+ return new_template
+
errors = []
end_template={'HeatTemplateFormatVersion': '2012-12-12',
'Description': []}
resource_changes=[]
for template_path in templates:
template = yaml.safe_load(open(template_path))
+ # Resolve __include__ tags
+ template = resolve_includes(template)
end_template['Description'].append(template.get('Description',
template_path))
new_parameters = template.get('Parameters', {})
- for p, pbody in iter(new_parameters.items()):
+ for p, pbody in sorted(new_parameters.items()):
if p in end_template.get('Parameters', {}):
if pbody != end_template['Parameters'][p]:
errors.append('Parameter %s from %s conflicts.' % (p,
end_template['Parameters'][p] = pbody
new_outputs = template.get('Outputs', {})
- for o, obody in iter(new_outputs.items()):
+ for o, obody in sorted(new_outputs.items()):
if o in end_template.get('Outputs', {}):
if pbody != end_template['Outputs'][p]:
errors.append('Output %s from %s conflicts.' % (o,
end_template['Outputs'][o] = obody
new_resources = template.get('Resources', {})
- for r, rbody in iter(new_resources.items()):
- if rbody['Type'] == 'AWS::EC2::Instance':
- # XXX Assuming ImageId is always a Ref
- del end_template['Parameters'][rbody['Properties']['ImageId']['Ref']]
+ for r, rbody in sorted(new_resources.items()):
+ if rbody['Type'] in MERGABLE_TYPES:
+ if 'image' in MERGABLE_TYPES[rbody['Type']]:
+ image_key = MERGABLE_TYPES[rbody['Type']]['image']
+ # XXX Assuming ImageId is always a Ref
+ ikey_val = end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
+ del end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
role = rbody.get('Metadata', {}).get('OpenStack::Role', r)
role = translate_role(role)
if role != r:
if 'Resources' not in end_template:
end_template['Resources'] = {}
end_template['Resources'][role] = rbody
- ikey = '%sImage' % (role)
- end_template['Resources'][role]['Properties']['ImageId'] = {'Ref': ikey}
- end_template['Parameters'][ikey] = {'Type': 'String'}
+ if 'image' in MERGABLE_TYPES[rbody['Type']]:
+ ikey = '%sImage' % (role)
+ end_template['Resources'][role]['Properties'][image_key] = {'Ref': ikey}
+ end_template['Parameters'][ikey] = ikey_val
+ elif rbody['Type'] == 'FileInclude':
+ with open(rbody['Path']) as rfile:
+ include_content = yaml.safe_load(rfile.read())
+ subkeys = rbody.get('SubKey','').split('.')
+ while len(subkeys) and subkeys[0]:
+ include_content = include_content[subkeys.pop(0)]
+ for replace_param, replace_value in iter(rbody.get('Parameters',
+ {}).items()):
+ include_content = resolve_params(include_content,
+ replace_param,
+ replace_value)
+ end_template['Resources'][r] = include_content
else:
if r in end_template.get('Resources', {}):
if rbody != end_template['Resources'][r]:
def fix_ref(item, old, new):
if isinstance(item, dict):
copy_item = dict(item)
- for k, v in iter(copy_item.items()):
+ for k, v in sorted(copy_item.items()):
if k == 'Ref' and v == old:
item[k] = new
continue
for change in resource_changes:
fix_ref(end_template, change[0], change[1])
-
+
if errors:
for e in errors:
sys.stderr.write("ERROR: %s\n" % e)