053a683aae6ef1d90eb26ffc3c68fd9e3436809b
[apex-tripleo-heat-templates.git] / tripleo_heat_merge / merge.py
1 import os
2 import sys
3 import yaml
4 import argparse
5
6
7 def _translate_role(role, master_role, slave_roles):
8     if not master_role:
9         return role
10     if role == master_role:
11         return role
12     if role not in slave_roles:
13         return role
14     return master_role
15
16 def translate_role(role, master_role, slave_roles):
17     r = _translate_role(role, master_role, slave_roles)
18     if not isinstance(r, basestring):
19         raise Exception('%s -> %r' % (role, r))
20     return r
21
22 def resolve_params(item, param, value):
23     if item == {'Ref': param}:
24         return value
25     if isinstance(item, dict):
26         copy_item = dict(item)
27         for k, v in iter(copy_item.items()):
28             item[k] = resolve_params(v, param, value)
29     elif isinstance(item, list):
30         copy_item = list(item)
31         new_item = []
32         for v in copy_item:
33             new_item.append(resolve_params(v, param, value))
34         item = new_item
35     return item
36
37 MERGABLE_TYPES = {'OS::Nova::Server':
38                   {'image': 'image'},
39                   'AWS::EC2::Instance':
40                   {'image': 'ImageId'},
41                   'AWS::AutoScaling::LaunchConfiguration':
42                   {},
43                  }
44 INCLUDED_TEMPLATE_DIR = os.getcwd()
45
46
47 def resolve_includes(template, params=None):
48     new_template = {}
49     if params is None:
50         params = {}
51     for key, value in iter(template.items()):
52         if key == '__include__':
53             new_params = dict(params) # do not propagate up the stack
54             if not isinstance(value, dict):
55                 raise ValueError('__include__ must be a mapping')
56             if 'path' not in value:
57                 raise ValueError('__include__ must have path')
58             if 'params' in value:
59                 if not isinstance(value['params'], dict):
60                     raise ValueError('__include__ params must be a mapping')
61                 new_params.update(value['params'])
62             with open(value['path']) as include_file:
63                 sub_template = yaml.safe_load(include_file.read())
64                 if 'subkey' in value:
65                     if ((not isinstance(value['subkey'], int)
66                          and not isinstance(sub_template, dict))):
67                         raise RuntimeError('subkey requires mapping root or'
68                                            ' integer for list root')
69                     sub_template = sub_template[value['subkey']]
70                 for k, v in iter(new_params.items()):
71                     sub_template = resolve_params(sub_template, k, v)
72                 new_template.update(resolve_includes(sub_template))
73         else:
74             if isinstance(value, dict):
75                 new_template[key] = resolve_includes(value)
76             else:
77                 new_template[key] = value
78     return new_template
79
80 def main(argv=None):
81     if argv is None:
82         argv = sys.argv[1:]
83     parser = argparse.ArgumentParser()
84     parser.add_argument('templates', nargs='+')
85     parser.add_argument('--master-role', nargs='?',
86                         help='Translate slave_roles to this')
87     parser.add_argument('--slave-roles', nargs='*',
88                         help='Translate all of these to master_role')
89     parser.add_argument('--included-template-dir', nargs='?',
90                         default=INCLUDED_TEMPLATE_DIR,
91                         help='Path for resolving included templates')
92     args = parser.parse_args(argv)
93     templates = args.templates
94     merged_template = merge(templates, args.master_role, args.slave_roles,
95                             args.included_template_dir)
96     sys.stdout.write(merged_template)
97
98 def merge(templates, master_role=None, slave_roles=None,
99           included_template_dir=INCLUDED_TEMPLATE_DIR):
100     errors = []
101     end_template={'HeatTemplateFormatVersion': '2012-12-12',
102                   'Description': []}
103     resource_changes=[]
104     for template_path in templates:
105         template = yaml.safe_load(open(template_path))
106         # Resolve __include__ tags
107         template = resolve_includes(template)
108         end_template['Description'].append(template.get('Description',
109                                                         template_path))
110         new_parameters = template.get('Parameters', {})
111         for p, pbody in sorted(new_parameters.items()):
112             if p in end_template.get('Parameters', {}):
113                 if pbody != end_template['Parameters'][p]:
114                     errors.append('Parameter %s from %s conflicts.' % (p,
115                                                                        template_path))
116                 continue
117             if 'Parameters' not in end_template:
118                 end_template['Parameters'] = {}
119             end_template['Parameters'][p] = pbody
120
121         new_outputs = template.get('Outputs', {})
122         for o, obody in sorted(new_outputs.items()):
123             if o in end_template.get('Outputs', {}):
124                 if pbody != end_template['Outputs'][p]:
125                     errors.append('Output %s from %s conflicts.' % (o,
126                                                                        template_path))
127                 continue
128             if 'Outputs' not in end_template:
129                 end_template['Outputs'] = {}
130             end_template['Outputs'][o] = obody
131
132         new_resources = template.get('Resources', {})
133         for r, rbody in sorted(new_resources.items()):
134             if rbody['Type'] in MERGABLE_TYPES:
135                 if 'image' in MERGABLE_TYPES[rbody['Type']]:
136                     image_key = MERGABLE_TYPES[rbody['Type']]['image']
137                     # XXX Assuming ImageId is always a Ref
138                     ikey_val = end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
139                     del end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
140                 role = rbody.get('Metadata', {}).get('OpenStack::Role', r)
141                 role = translate_role(role, master_role, slave_roles)
142                 if role != r:
143                     resource_changes.append((r, role))
144                 if role in end_template.get('Resources', {}):
145                     new_metadata = rbody.get('Metadata', {})
146                     for m, mbody in iter(new_metadata.items()):
147                         if m in end_template['Resources'][role].get('Metadata', {}):
148                             if m == 'OpenStack::ImageBuilder::Elements':
149                                 end_template['Resources'][role]['Metadata'][m].extend(mbody)
150                                 continue
151                             if mbody != end_template['Resources'][role]['Metadata'][m]:
152                                 errors.append('Role %s metadata key %s conflicts.' %
153                                               (role, m))
154                             continue
155                         end_template['Resources'][role]['Metadata'][m] = mbody
156                     continue
157                 if 'Resources' not in end_template:
158                     end_template['Resources'] = {}
159                 end_template['Resources'][role] = rbody
160                 if 'image' in MERGABLE_TYPES[rbody['Type']]:
161                     ikey = '%sImage' % (role)
162                     end_template['Resources'][role]['Properties'][image_key] = {'Ref': ikey}
163                     end_template['Parameters'][ikey] = ikey_val
164             elif rbody['Type'] == 'FileInclude':
165                 # we trust os.path.join to DTRT: if FileInclude path isn't
166                 # absolute, join to included_template_dir (./)
167                 with open(os.path.join(included_template_dir, rbody['Path'])) as rfile:
168                     include_content = yaml.safe_load(rfile.read())
169                     subkeys = rbody.get('SubKey','').split('.')
170                     while len(subkeys) and subkeys[0]:
171                         include_content = include_content[subkeys.pop(0)]
172                     for replace_param, replace_value in iter(rbody.get('Parameters',
173                                                                        {}).items()):
174                         include_content = resolve_params(include_content,
175                                                          replace_param,
176                                                          replace_value)
177                     end_template['Resources'][r] = include_content
178             else:
179                 if r in end_template.get('Resources', {}):
180                     if rbody != end_template['Resources'][r]:
181                         errors.append('Resource %s from %s conflicts' % (r,
182                                                                          template_path))
183                     continue
184                 if 'Resources' not in end_template:
185                     end_template['Resources'] = {}
186                 end_template['Resources'][r] = rbody
187
188     def fix_ref(item, old, new):
189         if isinstance(item, dict):
190             copy_item = dict(item)
191             for k, v in sorted(copy_item.items()):
192                 if k == 'Ref' and v == old:
193                     item[k] = new
194                     continue
195                 if k == 'DependsOn' and v == old:
196                     item[k] = new
197                     continue
198                 if k == 'Fn::GetAtt' and isinstance(v, list) and v[0] == old:
199                     new_list = list(v)
200                     new_list[0] = new
201                     item[k] = new_list
202                     continue
203                 if k == 'AllowedResources' and isinstance(v, list) and old in v:
204                     while old in v:
205                         pos = v.index(old)
206                         v[pos] = new
207                     continue
208                 fix_ref(v, old, new)
209         elif isinstance(item, list):
210             copy_item = list(item)
211             for v in item:
212                 fix_ref(v, old, new)
213
214     for change in resource_changes:
215         fix_ref(end_template, change[0], change[1])
216
217     if errors:
218         for e in errors:
219             sys.stderr.write("ERROR: %s\n" % e)
220     end_template['Description'] = ','.join(end_template['Description'])
221     return yaml.safe_dump(end_template, default_flow_style=False)
222
223 if __name__ == "__main__":
224       main()