7 def apply_scaling(template, scaling, in_copies=None):
8 """Apply a set of scaling operations to template.
10 This is a single pass recursive function: for each call we process one
11 dict or list and recurse to handle children containers.
13 Values are handled via scale_value.
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.
19 in_copies = dict(in_copies or {})
20 # Shouldn't be needed but to avoid unexpected side effects/bugs we short
24 if isinstance(template, dict):
26 for key, value in template.items():
27 for prefix, copy_num, new_key in scale_value(
28 key, scaling, in_copies):
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
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]
44 elif isinstance(template, list):
46 for value in template:
47 if isinstance(value, (dict, list)):
48 new_template.append(apply_scaling(value, scaling, in_copies))
50 for _, _, new_value in scale_value(value, scaling, in_copies):
51 new_template.append(new_value)
54 raise Exception("apply_scaling called with non-container %r" % template)
57 def scale_value(value, scaling, in_copies):
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
69 if isinstance(value, (str, unicode)):
70 for prefix, copies in scaling.items():
71 if not value.startswith(prefix):
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
79 for n in range(copies):
80 yield prefix, n, prefix[:-1] + str(n) + suffix
82 yield None, None, value
84 yield None, None, value
87 def parse_scaling(scaling_args):
88 """Translate a list of scaling requests to a dict prefix:count."""
89 scaling_args = scaling_args or []
91 for item in scaling_args:
92 key, value = item.split('=')
94 result[key + '0'] = value
98 def _translate_role(role, master_role, slave_roles):
101 if role == master_role:
103 if role not in slave_roles:
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))
113 def resolve_params(item, param, value):
114 if item == {'Ref': param}:
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)
124 new_item.append(resolve_params(v, param, value))
128 MERGABLE_TYPES = {'OS::Nova::Server':
130 'AWS::EC2::Instance':
131 {'image': 'ImageId'},
132 'AWS::AutoScaling::LaunchConfiguration':
135 INCLUDED_TEMPLATE_DIR = os.getcwd()
138 def resolve_includes(template, params=None):
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))
165 if isinstance(value, dict):
166 new_template[key] = resolve_includes(value)
168 new_template[key] = value
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',
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
199 out_file = file(args.output, 'wt')
200 out_file.write(merged_template)
203 def merge(templates, master_role=None, slave_roles=None,
204 included_template_dir=INCLUDED_TEMPLATE_DIR,
206 scaling = scaling or {}
208 end_template={'HeatTemplateFormatVersion': '2012-12-12',
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',
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,
224 if 'Parameters' not in end_template:
225 end_template['Parameters'] = {}
226 end_template['Parameters'][p] = pbody
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,
235 if 'Outputs' not in end_template:
236 end_template['Outputs'] = {}
237 end_template['Outputs'][o] = obody
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)
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)
258 if mbody != end_template['Resources'][role]['Metadata'][m]:
259 errors.append('Role %s metadata key %s conflicts.' %
262 end_template['Resources'][role]['Metadata'][m] = mbody
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',
281 include_content = resolve_params(include_content,
284 end_template['Resources'][r] = include_content
286 if r in end_template.get('Resources', {}):
287 if rbody != end_template['Resources'][r]:
288 errors.append('Resource %s from %s conflicts' % (r,
291 if 'Resources' not in end_template:
292 end_template['Resources'] = {}
293 end_template['Resources'][r] = rbody
295 end_template = apply_scaling(end_template, scaling)
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:
304 if k == 'DependsOn' and v == old:
307 if k == 'Fn::GetAtt' and isinstance(v, list) and v[0] == old:
312 if k == 'AllowedResources' and isinstance(v, list) and old in v:
318 elif isinstance(item, list):
319 copy_item = list(item)
323 for change in resource_changes:
324 fix_ref(end_template, change[0], change[1])
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)
332 if __name__ == "__main__":