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