From c7ef3cc3b0c0fdce4c2634fbcf855c1bc8fe5c44 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Mon, 13 May 2013 10:06:42 -0700 Subject: [PATCH] Create a utility for merging templates. --- merge.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mysql.yaml | 1 + nova-api.yaml | 1 + rabbitmq.yaml | 1 + 4 files changed, 101 insertions(+) create mode 100644 merge.py 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)) diff --git a/mysql.yaml b/mysql.yaml index 3c1658c2..f6142e2a 100644 --- a/mysql.yaml +++ b/mysql.yaml @@ -60,6 +60,7 @@ Resources: MySQL: Type: AWS::EC2::Instance Metadata: + OpenStack::Role: stateful OpenStack::ImageBuilder::Elements: [ mysql-migration ] mysql: create-users: diff --git a/nova-api.yaml b/nova-api.yaml index 15ce26d3..92ea3cae 100644 --- a/nova-api.yaml +++ b/nova-api.yaml @@ -54,6 +54,7 @@ Resources: InstanceType: {Ref: InstanceType} KeyName: {Ref: KeyName} Metadata: + OpenStack::Role: stateless OpenStack::ImageBuilder::Elements: [ nova-api ] heat: access_key_id: diff --git a/rabbitmq.yaml b/rabbitmq.yaml index 50f60048..701f2d04 100644 --- a/rabbitmq.yaml +++ b/rabbitmq.yaml @@ -15,6 +15,7 @@ Resources: RabbitMQ: Type: AWS::EC2::Instance Metadata: + OpenStack::Role: stateful OpenStack::ImageBuilder::Elements: [ rabbitmq-server ] rabbitmq: password_handle: {Ref: RabbitMQPasswordHandle} -- 2.16.6