5 parser = argparse.ArgumentParser()
6 parser.add_argument('templates', nargs='+')
7 parser.add_argument('--master-role', nargs='?',
8 help='Translate slave_roles to this')
9 parser.add_argument('--slave-roles', nargs='*',
10 help='Translate all of these to master_role')
12 args = parser.parse_args()
14 templates = args.templates
16 def _translate_role(role):
18 if not args.master_role:
20 if role == args.master_role:
22 if role not in args.slave_roles:
24 return args.master_role
26 def translate_role(role):
27 r = _translate_role(role)
28 if not isinstance(r, basestring):
29 raise Exception('%s -> %r' % (role, r))
32 def resolve_params(item, param, value):
33 if item == {'Ref': param}:
35 if isinstance(item, dict):
36 copy_item = dict(item)
37 for k, v in iter(copy_item.items()):
38 item[k] = resolve_params(v, param, value)
39 elif isinstance(item, list):
40 copy_item = list(item)
43 new_item.append(resolve_params(v, param, value))
49 end_template={'HeatTemplateFormatVersion': '2012-12-12',
52 for template_path in templates:
53 template = yaml.safe_load(open(template_path))
54 end_template['Description'].append(template.get('Description',
56 new_parameters = template.get('Parameters', {})
57 for p, pbody in sorted(new_parameters.items()):
58 if p in end_template.get('Parameters', {}):
59 if pbody != end_template['Parameters'][p]:
60 errors.append('Parameter %s from %s conflicts.' % (p,
63 if 'Parameters' not in end_template:
64 end_template['Parameters'] = {}
65 end_template['Parameters'][p] = pbody
67 new_outputs = template.get('Outputs', {})
68 for o, obody in sorted(new_outputs.items()):
69 if o in end_template.get('Outputs', {}):
70 if pbody != end_template['Outputs'][p]:
71 errors.append('Output %s from %s conflicts.' % (o,
74 if 'Outputs' not in end_template:
75 end_template['Outputs'] = {}
76 end_template['Outputs'][o] = obody
78 new_resources = template.get('Resources', {})
79 for r, rbody in sorted(new_resources.items()):
80 if rbody['Type'] == 'AWS::EC2::Instance':
81 # XXX Assuming ImageId is always a Ref
82 ikey_val = end_template['Parameters'][rbody['Properties']['ImageId']['Ref']]
83 del end_template['Parameters'][rbody['Properties']['ImageId']['Ref']]
84 role = rbody.get('Metadata', {}).get('OpenStack::Role', r)
85 role = translate_role(role)
87 resource_changes.append((r, role))
88 if role in end_template.get('Resources', {}):
89 new_metadata = rbody.get('Metadata', {})
90 for m, mbody in iter(new_metadata.items()):
91 if m in end_template['Resources'][role].get('Metadata', {}):
92 if m == 'OpenStack::ImageBuilder::Elements':
93 end_template['Resources'][role]['Metadata'][m].extend(mbody)
95 if mbody != end_template['Resources'][role]['Metadata'][m]:
96 errors.append('Role %s metadata key %s conflicts.' %
99 end_template['Resources'][role]['Metadata'][m] = mbody
101 if 'Resources' not in end_template:
102 end_template['Resources'] = {}
103 end_template['Resources'][role] = rbody
104 ikey = '%sImage' % (role)
105 end_template['Resources'][role]['Properties']['ImageId'] = {'Ref': ikey}
106 end_template['Parameters'][ikey] = ikey_val
107 elif rbody['Type'] == 'FileInclude':
108 with open(rbody['Path']) as rfile:
109 include_content = yaml.safe_load(rfile.read())
110 subkeys = rbody.get('SubKey','').split('.')
111 while len(subkeys) and subkeys[0]:
112 include_content = include_content[subkeys.pop(0)]
113 for replace_param, replace_value in iter(rbody.get('Parameters',
115 include_content = resolve_params(include_content,
118 end_template['Resources'][r] = include_content
120 if r in end_template.get('Resources', {}):
121 if rbody != end_template['Resources'][r]:
122 errors.append('Resource %s from %s conflicts' % (r,
125 if 'Resources' not in end_template:
126 end_template['Resources'] = {}
127 end_template['Resources'][r] = rbody
129 def fix_ref(item, old, new):
130 if isinstance(item, dict):
131 copy_item = dict(item)
132 for k, v in sorted(copy_item.items()):
133 if k == 'Ref' and v == old:
136 if k == 'DependsOn' and v == old:
139 if k == 'Fn::GetAtt' and isinstance(v, list) and v[0] == old:
144 if k == 'AllowedResources' and isinstance(v, list) and old in v:
150 elif isinstance(item, list):
151 copy_item = list(item)
155 for change in resource_changes:
156 fix_ref(end_template, change[0], change[1])
160 sys.stderr.write("ERROR: %s\n" % e)
161 end_template['Description'] = ','.join(end_template['Description'])
162 sys.stdout.write(yaml.safe_dump(end_template, default_flow_style=False))