Support DependsOn
[apex-tripleo-heat-templates.git] / merge.py
1 import sys
2 import yaml
3
4 templates = list(sys.argv[1:])
5
6 errors = []
7 end_template={'HeatTemplateFormatVersion': '2012-12-12',
8               'Description': []}
9 resource_changes=[]
10 for template_path in templates:
11     template = yaml.safe_load(open(template_path))
12     end_template['Description'].append(template.get('Description',
13                                                     template_path))
14     new_parameters = template.get('Parameters', {})
15     for p, pbody in iter(new_parameters.items()):
16         if p in end_template.get('Parameters', {}):
17             if pbody != end_template['Parameters'][p]:
18                 errors.append('Parameter %s from %s conflicts.' % (p,
19                                                                    template_path))
20             continue
21         if 'Parameters' not in end_template:
22             end_template['Parameters'] = {}
23         end_template['Parameters'][p] = pbody
24
25     new_outputs = template.get('Outputs', {})
26     for o, obody in iter(new_outputs.items()):
27         if o in end_template.get('Outputs', {}):
28             if pbody != end_template['Outputs'][p]:
29                 errors.append('Output %s from %s conflicts.' % (o,
30                                                                    template_path))
31             continue
32         if 'Outputs' not in end_template:
33             end_template['Outputs'] = {}
34         end_template['Outputs'][o] = obody
35
36     new_resources = template.get('Resources', {})
37     for r, rbody in iter(new_resources.items()):
38         if rbody['Type'] == 'AWS::EC2::Instance':
39             # XXX Assuming ImageId is always a Ref
40             del end_template['Parameters'][rbody['Properties']['ImageId']['Ref']]
41             role = rbody.get('Metadata', {}).get('OpenStack::Role', r)
42             if role != r:
43                 resource_changes.append((r, role))
44             if role in end_template.get('Resources', {}):
45                 new_metadata = rbody.get('Metadata', {})
46                 for m, mbody in iter(new_metadata.items()):
47                     if m in end_template['Resources'][role].get('Metadata', {}):
48                         if m == 'OpenStack::ImageBuilder::Elements':
49                             end_template['Resources'][role]['Metadata'][m].extend(mbody)
50                             continue
51                         if mbody != end_template['Resources'][role]['Metadata'][m]:
52                             errors.append('Role %s metadata key %s conflicts.' %
53                                           (role, m))
54                         continue
55                     end_template['Resources'][role]['Metadata'][m] = mbody
56                 continue
57             if 'Resources' not in end_template:
58                 end_template['Resources'] = {}
59             end_template['Resources'][role] = rbody
60             ikey = '%sImage' % (role)
61             end_template['Resources'][role]['Properties']['ImageId'] = {'Ref': ikey}
62             end_template['Parameters'][ikey] = {'Type': 'String'}
63         else:
64             if r in end_template.get('Resources', {}):
65                 if rbody != end_template['Resources'][r]:
66                     errors.append('Resource %s from %s conflicts' % (r,
67                                                                      template_path))
68                 continue
69             if 'Resources' not in end_template:
70                 end_template['Resources'] = {}
71             end_template['Resources'][r] = rbody
72
73 def fix_ref(item, old, new):
74     if isinstance(item, dict):
75         copy_item = dict(item)
76         for k, v in iter(copy_item.items()):
77             if k == 'Ref' and v == old:
78                 item[k] = new
79                 continue
80             if k == 'DependsOn' and v == old:
81                 item[k] = new
82                 continue
83             if k == 'Fn::GetAtt' and isinstance(v, list) and v[0] == old:
84                 new_list = list(v)
85                 new_list[0] = new
86                 item[k] = new_list
87                 continue
88             if k == 'AllowedResources' and isinstance(v, list) and old in v:
89                 while old in v:
90                     pos = v.index(old)
91                     v[pos] = new
92                 continue
93             fix_ref(v, old, new)
94     elif isinstance(item, list):
95         copy_item = list(item)
96         for v in item:
97             fix_ref(v, old, new)
98
99 for change in resource_changes:
100     fix_ref(end_template, change[0], change[1])
101             
102 if errors:
103     for e in errors:
104         sys.stderr.write("ERROR: %s\n" % e)
105 end_template['Description'] = ','.join(end_template['Description'])
106 sys.stdout.write(yaml.safe_dump(end_template, default_flow_style=False))