Port all templates to HOT
[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 class Cfn(object):
8
9     base_template = {
10         'HeatTemplateFormatVersion': '2012-12-12',
11         'Description': []
12     }
13     get_resource = 'Ref'
14     get_param = 'Ref'
15     description = 'Description'
16     parameters = 'Parameters'
17     outputs = 'Outputs'
18     resources = 'Resources'
19     type = 'Type'
20     properties = 'Properties'
21     metadata = 'Metadata'
22     depends_on = 'DependsOn'
23     get_attr = 'Fn::GetAtt'
24
25
26 class Hot(object):
27
28     base_template = {
29         'heat_template_version': '2014-10-16',
30         'description': []
31     }
32     get_resource = 'get_resource'
33     get_param = 'get_param'
34     description = 'description'
35     parameters = 'parameters'
36     outputs = 'outputs'
37     resources = 'resources'
38     type = 'type'
39     properties = 'properties'
40     metadata = 'metadata'
41     depends_on = 'depends_on'
42     get_attr = 'get_attr'
43
44
45 lang = Cfn()
46
47
48 def apply_maps(template):
49     """Apply Merge::Map within template.
50
51     Any dict {'Merge::Map': {'Foo': 'Bar', 'Baz': 'Quux'}}
52     will resolve to ['Bar', 'Quux'] - that is a dict with key
53     'Merge::Map' is replaced entirely by that dict['Merge::Map'].values().
54     """
55     if isinstance(template, dict):
56         if 'Merge::Map' in template:
57             return sorted(
58                 apply_maps(value) for value in template['Merge::Map'].values()
59                 )
60         else:
61             return dict((key, apply_maps(value))
62                 for key, value in template.items())
63     elif isinstance(template, list):
64         return [apply_maps(item) for item in template]
65     else:
66         return template
67
68
69 def apply_scaling(template, scaling, in_copies=None):
70     """Apply a set of scaling operations to template.
71
72     This is a single pass recursive function: for each call we process one
73     dict or list and recurse to handle children containers.
74
75     Values are handled via scale_value.
76
77     Keys in dicts are copied per the scaling rule.
78     Values are either replaced or copied depending on whether the given
79     scaling rule is in in_copies.
80
81     in_copies is reset to None when a dict {'Merge::Map': someobject} is
82     encountered.
83     """
84     in_copies = dict(in_copies or {})
85     # Shouldn't be needed but to avoid unexpected side effects/bugs we short
86     # circuit no-ops.
87     if not scaling:
88         return template
89     if isinstance(template, dict):
90         if 'Merge::Map' in template:
91             in_copies = None
92         new_template = {}
93         for key, value in template.items():
94             for prefix, copy_num, new_key in scale_value(
95                 key, scaling, in_copies):
96                 if prefix:
97                     # e.g. Compute0, 1, Compute1Foo
98                     in_copies[prefix] = prefix[:-1] + str(copy_num)
99                 if isinstance(value, (dict, list)):
100                     new_value = apply_scaling(value, scaling, in_copies)
101                     new_template[new_key] = new_value
102                 else:
103                     new_values = list(scale_value(value, scaling, in_copies))
104                     # We have nowhere to multiply a non-container value of a
105                     # dict, so it may be copied or unchanged but not scaled.
106                     assert len(new_values) == 1
107                     new_template[new_key] = new_values[0][2]
108                 if prefix:
109                     del in_copies[prefix]
110         return new_template
111     elif isinstance(template, list):
112         new_template = []
113         for value in template:
114             if isinstance(value, (dict, list)):
115                 new_template.append(apply_scaling(value, scaling, in_copies))
116             else:
117                 for _, _, new_value in scale_value(value, scaling, in_copies):
118                     new_template.append(new_value)
119         return new_template
120     else:
121         raise Exception("apply_scaling called with non-container %r" % template)
122
123
124 def scale_value(value, scaling, in_copies):
125     """Scale out a value.
126
127     :param value: The value to scale (not a container).
128     :param scaling: The scaling map to use.
129     :param in_copies: What containers we're currently copying.
130     :return: An iterator of the new values for the value as tuples:
131         (prefix, copy_num, value). E.g. Compute0, 1, Compute1Foo
132         prefix and copy_num are only set when:
133          - a prefix in scaling matches value
134          - and that prefix is not in in_copies
135     """
136     if isinstance(value, (str, unicode)):
137         for prefix, copies in scaling.items():
138             if not value.startswith(prefix):
139                 continue
140             suffix = value[len(prefix):]
141             if prefix in in_copies:
142                 # Adjust to the copy number we're on
143                 yield None, None, in_copies[prefix] + suffix
144                 return
145             else:
146                 for n in range(copies):
147                     yield prefix, n, prefix[:-1] + str(n) + suffix
148                 return
149         yield None, None, value
150     else:
151         yield None, None, value
152
153
154 def parse_scaling(scaling_args):
155     """Translate a list of scaling requests to a dict prefix:count."""
156     scaling_args = scaling_args or []
157     result = {}
158     for item in scaling_args:
159         key, value = item.split('=')
160         value = int(value)
161         result[key + '0'] = value
162     return result
163
164
165 def _translate_role(role, master_role, slave_roles):
166     if not master_role:
167         return role
168     if role == master_role:
169         return role
170     if role not in slave_roles:
171         return role
172     return master_role
173
174 def translate_role(role, master_role, slave_roles):
175     r = _translate_role(role, master_role, slave_roles)
176     if not isinstance(r, basestring):
177         raise Exception('%s -> %r' % (role, r))
178     return r
179
180 def resolve_params(item, param, value):
181     if item in ({lang.get_param: param}, {lang.get_resource: param}):
182         return value
183     if isinstance(item, dict):
184         copy_item = dict(item)
185         for k, v in iter(copy_item.items()):
186             item[k] = resolve_params(v, param, value)
187     elif isinstance(item, list):
188         copy_item = list(item)
189         new_item = []
190         for v in copy_item:
191             new_item.append(resolve_params(v, param, value))
192         item = new_item
193     return item
194
195 MERGABLE_TYPES = {'OS::Nova::Server':
196                   {'image': 'image'},
197                   'AWS::EC2::Instance':
198                   {'image': 'ImageId'},
199                   'AWS::AutoScaling::LaunchConfiguration':
200                   {},
201                  }
202 INCLUDED_TEMPLATE_DIR = os.getcwd()
203
204
205 def resolve_includes(template, params=None):
206     new_template = {}
207     if params is None:
208         params = {}
209     for key, value in iter(template.items()):
210         if key == '__include__':
211             new_params = dict(params) # do not propagate up the stack
212             if not isinstance(value, dict):
213                 raise ValueError('__include__ must be a mapping')
214             if 'path' not in value:
215                 raise ValueError('__include__ must have path')
216             if 'params' in value:
217                 if not isinstance(value['params'], dict):
218                     raise ValueError('__include__ params must be a mapping')
219                 new_params.update(value['params'])
220             with open(value['path']) as include_file:
221                 sub_template = yaml.safe_load(include_file.read())
222                 if 'subkey' in value:
223                     if ((not isinstance(value['subkey'], int)
224                          and not isinstance(sub_template, dict))):
225                         raise RuntimeError('subkey requires mapping root or'
226                                            ' integer for list root')
227                     sub_template = sub_template[value['subkey']]
228                 for k, v in iter(new_params.items()):
229                     sub_template = resolve_params(sub_template, k, v)
230                 new_template.update(resolve_includes(sub_template))
231         else:
232             if isinstance(value, dict):
233                 new_template[key] = resolve_includes(value)
234             else:
235                 new_template[key] = value
236     return new_template
237
238 def main(argv=None):
239     if argv is None:
240         argv = sys.argv[1:]
241     parser = argparse.ArgumentParser()
242     parser.add_argument('templates', nargs='+')
243     parser.add_argument('--master-role', nargs='?',
244                         help='Translate slave_roles to this')
245     parser.add_argument('--slave-roles', nargs='*',
246                         help='Translate all of these to master_role')
247     parser.add_argument('--included-template-dir', nargs='?',
248                         default=INCLUDED_TEMPLATE_DIR,
249                         help='Path for resolving included templates')
250     parser.add_argument('--output',
251                         help='File to write output to. - for stdout',
252                         default='-')
253     parser.add_argument('--scale', action="append",
254         help="Names to scale out. Pass Prefix=1 to cause a key Prefix0Foo to "
255         "be copied to Prefix1Foo in the output, and value Prefix0Bar to be"
256         "renamed to Prefix1Bar inside that copy, or copied to Prefix1Bar "
257         "outside of any copy.")
258     parser.add_argument(
259         '--change-image-params', action='store_true', default=False,
260         help="Change parameters in templates to match resource names. This was "
261              " the default at one time but it causes issues when parameter "
262              " names need to remain stable.")
263     parser.add_argument(
264         '--hot', action='store_true', default=False,
265         help="Assume source templates are in the HOT format, and generate a "
266              "HOT template artifact.")
267     args = parser.parse_args(argv)
268     if args.hot:
269         global lang
270         lang = Hot()
271
272     templates = args.templates
273     scaling = parse_scaling(args.scale)
274     merged_template = merge(templates, args.master_role, args.slave_roles,
275                             args.included_template_dir, scaling=scaling,
276                             change_image_params=args.change_image_params)
277     if args.output == '-':
278         out_file = sys.stdout
279     else:
280         out_file = file(args.output, 'wt')
281     out_file.write(merged_template)
282
283
284 def merge(templates, master_role=None, slave_roles=None,
285           included_template_dir=INCLUDED_TEMPLATE_DIR,
286           scaling=None, change_image_params=None):
287     scaling = scaling or {}
288     errors = []
289     end_template = dict(lang.base_template)
290     resource_changes=[]
291     for template_path in templates:
292         template = yaml.safe_load(open(template_path))
293         # Resolve __include__ tags
294         template = resolve_includes(template)
295         end_template[lang.description].append(template.get(lang.description,
296                                                         template_path))
297         new_parameters = template.get(lang.parameters, {})
298         for p, pbody in sorted(new_parameters.items()):
299             if p in end_template.get(lang.parameters, {}):
300                 if pbody != end_template[lang.parameters][p]:
301                     errors.append('Parameter %s from %s conflicts.' % (p,
302                                                                        template_path))
303                 continue
304             if lang.parameters not in end_template:
305                 end_template[lang.parameters] = {}
306             end_template[lang.parameters][p] = pbody
307
308         new_outputs = template.get(lang.outputs, {})
309         for o, obody in sorted(new_outputs.items()):
310             if o in end_template.get(lang.outputs, {}):
311                 if pbody != end_template[lang.outputs][p]:
312                     errors.append('Output %s from %s conflicts.' % (o,
313                                                                        template_path))
314                 continue
315             if lang.outputs not in end_template:
316                 end_template[lang.outputs] = {}
317             end_template[lang.outputs][o] = obody
318
319         new_resources = template.get(lang.resources, {})
320         for r, rbody in sorted(new_resources.items()):
321             if rbody[lang.type] in MERGABLE_TYPES:
322                 if change_image_params:
323                     if 'image' in MERGABLE_TYPES[rbody[lang.type]]:
324                         image_key = MERGABLE_TYPES[rbody[lang.type]]['image']
325                         # XXX Assuming ImageId is always a Ref
326                         ikey_val = end_template[lang.parameters][rbody[lang.properties][image_key][lang.get_param]]
327                         del end_template[lang.parameters][rbody[lang.properties][image_key][lang.get_param]]
328                 role = rbody.get(lang.metadata, {}).get('OpenStack::Role', r)
329                 role = translate_role(role, master_role, slave_roles)
330                 if role != r:
331                     resource_changes.append((r, role))
332                 if role in end_template.get(lang.resources, {}):
333                     new_metadata = rbody.get(lang.metadata, {})
334                     for m, mbody in iter(new_metadata.items()):
335                         if m in end_template[lang.resources][role].get(lang.metadata, {}):
336                             if m == 'OpenStack::ImageBuilder::Elements':
337                                 end_template[lang.resources][role][lang.metadata][m].extend(mbody)
338                                 continue
339                             if mbody != end_template[lang.resources][role][lang.metadata][m]:
340                                 errors.append('Role %s metadata key %s conflicts.' %
341                                               (role, m))
342                             continue
343                         role_res = end_template[lang.resources][role]
344                         if role_res[lang.type] == 'OS::Heat::StructuredConfig':
345                             end_template[lang.resources][role][lang.properties]['config'][m] = mbody
346                         else:
347                             end_template[lang.resources][role][lang.metadata][m] = mbody
348                     continue
349                 if lang.resources not in end_template:
350                     end_template[lang.resources] = {}
351                 end_template[lang.resources][role] = rbody
352                 if change_image_params:
353                     if 'image' in MERGABLE_TYPES[rbody[lang.type]]:
354                         ikey = '%sImage' % (role)
355                         end_template[lang.resources][role][lang.properties][image_key] = {lang.get_param: ikey}
356                         end_template[lang.parameters][ikey] = ikey_val
357             elif rbody[lang.type] == 'FileInclude':
358                 # we trust os.path.join to DTRT: if FileInclude path isn't
359                 # absolute, join to included_template_dir (./)
360                 with open(os.path.join(included_template_dir, rbody['Path'])) as rfile:
361                     include_content = yaml.safe_load(rfile.read())
362                     subkeys = rbody.get('SubKey','').split('.')
363                     while len(subkeys) and subkeys[0]:
364                         include_content = include_content[subkeys.pop(0)]
365                     for replace_param, replace_value in iter(rbody.get(lang.parameters,
366                                                                        {}).items()):
367                         include_content = resolve_params(include_content,
368                                                          replace_param,
369                                                          replace_value)
370                     if lang.resources not in end_template:
371                         end_template[lang.resources] = {}
372                     end_template[lang.resources][r] = include_content
373             else:
374                 if r in end_template.get(lang.resources, {}):
375                     if rbody != end_template[lang.resources][r]:
376                         errors.append('Resource %s from %s conflicts' % (r,
377                                                                          template_path))
378                     continue
379                 if lang.resources not in end_template:
380                     end_template[lang.resources] = {}
381                 end_template[lang.resources][r] = rbody
382
383     end_template = apply_scaling(end_template, scaling)
384     end_template = apply_maps(end_template)
385
386     def fix_ref(item, old, new):
387         if isinstance(item, dict):
388             copy_item = dict(item)
389             for k, v in sorted(copy_item.items()):
390                 if k == lang.get_resource and v == old:
391                     item[k] = new
392                     continue
393                 if k == lang.depends_on and v == old:
394                     item[k] = new
395                     continue
396                 if k == lang.get_attr and isinstance(v, list) and v[0] == old:
397                     new_list = list(v)
398                     new_list[0] = new
399                     item[k] = new_list
400                     continue
401                 if k == 'AllowedResources' and isinstance(v, list) and old in v:
402                     while old in v:
403                         pos = v.index(old)
404                         v[pos] = new
405                     continue
406                 fix_ref(v, old, new)
407         elif isinstance(item, list):
408             copy_item = list(item)
409             for v in item:
410                 fix_ref(v, old, new)
411
412     for change in resource_changes:
413         fix_ref(end_template, change[0], change[1])
414
415     if errors:
416         for e in errors:
417             sys.stderr.write("ERROR: %s\n" % e)
418     end_template[lang.description] = ','.join(end_template[lang.description])
419     return yaml.safe_dump(end_template, default_flow_style=False)
420
421 if __name__ == "__main__":
422       main()