diff options
author | 2013-05-13 10:06:42 -0700 | |
---|---|---|
committer | 2013-06-06 09:25:27 -0700 | |
commit | c7ef3cc3b0c0fdce4c2634fbcf855c1bc8fe5c44 (patch) | |
tree | 9a2c8ee238c4ec4911d687ac693f58ced6075ce8 /merge.py | |
parent | a9a7de4fcb1c7f80b365f20f5605bbc2de8f538a (diff) |
Create a utility for merging templates.
Diffstat (limited to 'merge.py')
-rw-r--r-- | merge.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/merge.py b/merge.py new file mode 100644 index 00000000..5d634d61 --- /dev/null +++ b/merge.py @@ -0,0 +1,98 @@ +import sys +import yaml + +templates = list(sys.argv[1:]) + +errors = [] +end_template={'HeatTemplateFormatVersion': '2012-12-12', + 'Description': []} +resource_changes=[] +for template_path in templates: + template = yaml.safe_load(open(template_path)) + end_template['Description'].append(template.get('Description', + template_path)) + new_parameters = template.get('Parameters', {}) + for p, pbody in iter(new_parameters.items()): + if p in end_template.get('Parameters', {}): + if pbody != end_template['Parameters'][p]: + errors.append('Parameter %s from %s conflicts.' % (p, + template_path)) + continue + if 'Parameters' not in end_template: + end_template['Parameters'] = {} + end_template['Parameters'][p] = pbody + + new_outputs = template.get('Outputs', {}) + for o, obody in iter(new_outputs.items()): + if o in end_template.get('Outputs', {}): + if pbody != end_template['Outputs'][p]: + errors.append('Output %s from %s conflicts.' % (o, + template_path)) + continue + if 'Outputs' not in end_template: + end_template['Outputs'] = {} + 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']] + role = rbody.get('Metadata', {}).get('OpenStack::Role', r) + if role != r: + resource_changes.append((r, role)) + if role in end_template.get('Resources', {}): + new_metadata = rbody.get('Metadata', {}) + for m, mbody in iter(new_metadata.items()): + if m in end_template['Resources'][role].get('Metadata', {}): + if m == 'OpenStack::ImageBuilder::Elements': + end_template['Resources'][role]['Metadata'][m].extend(mbody) + continue + if mbody != end_template['Resources'][role]['Metadata'][m]: + errors.append('Role %s metadata key %s conflicts.' % + (role, m)) + continue + end_template['Resources'][role]['Metadata'][m] = mbody + continue + 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'} + else: + if r in end_template.get('Resources', {}): + if rbody != end_template['Resources'][r]: + errors.append('Resource %s from %s conflicts' % (r, + template_path)) + continue + if 'Resources' not in end_template: + end_template['Resources'] = {} + end_template['Resources'][r] = rbody + +def fix_ref(item, old, new): + if isinstance(item, dict): + copy_item = dict(item) + for k, v in iter(copy_item.items()): + if k == 'Ref' and v == old: + item[k] = new + continue + if k == 'Fn::GetAtt' and isinstance(v, list) and v[0] == old: + new_list = list(v) + new_list[0] = new + item[k] = new_list + continue + fix_ref(v, old, new) + elif isinstance(item, list): + copy_item = list(item) + for v in item: + fix_ref(v, old, new) + +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) +end_template['Description'] = ','.join(end_template['Description']) +sys.stdout.write(yaml.safe_dump(end_template, default_flow_style=False)) |