1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 """to create heat templates from the base template
16 import consts.parameters as parameters
23 self.variable_name = ''
24 self.variable_value = 0
26 def add_child(self, node):
28 self.down.append(node)
33 def get_children(self):
34 if len(self.down) == 0:
38 def get_variable_name(self):
39 return self.variable_name
41 def get_variable_value(self):
42 return self.variable_value
44 def set_variable_name(self, name):
45 self.variable_name = name
47 def set_variable_value(self, value):
48 self.variable_value = value
55 for node in self.up.get_path():
61 return str(self.variable_name) + " --> " + str(self.variable_value)
64 return str(self.variable_name) + " = " + str(self.variable_value)
67 def _get_leaves(node, leaves):
68 children = node.get_children()
69 if len(children) == 0:
72 for child in children:
73 TreeNode._get_leaves(child, leaves)
78 TreeNode._get_leaves(node, leaves)
81 template_name = parameters.TEST_TEMPLATE_NAME
83 def generates_templates(base_heat_template, deployment_configuration):
84 # parameters loaded from file
85 template_dir = common.get_template_dir()
86 template_extension = parameters.TEMPLATE_EXTENSION
87 template_base_name = base_heat_template
89 variables = deployment_configuration
91 # Delete the templates generated in previous running
92 common.LOG.info("Removing the heat templates already generated")
93 command = "rm {}{}_*".format(template_dir, template_name)
96 # Creation of the tree with all the new configurations
97 common.LOG.info("Creation of a tree with all new configurations")
99 for variable in variables:
100 leaves = TreeNode.get_leaves(tree)
101 common.LOG.debug("LEAVES: " + str(leaves))
102 common.LOG.debug("VALUES: " + str(variables[variable]))
104 for value in variables[variable]:
106 new_node = TreeNode()
107 new_node.set_variable_name(variable)
108 new_node.set_variable_value(value)
109 leaf.add_child(new_node)
111 common.LOG.debug("CONFIGURATION TREE: " + str(tree))
113 common.LOG.info("Heat Template and metadata file creation")
114 leaves = TreeNode.get_leaves(tree)
117 heat_template_vars = leaf.get_path()
118 if os.path.isabs(template_base_name):
119 base_template = template_base_name
121 base_template = template_dir + template_base_name
122 new_template = template_dir + template_name
123 new_template += "_" + str(counter) + template_extension
124 shutil.copy(base_template, new_template)
127 for var in heat_template_vars:
128 if var.get_variable_name():
129 common.replace_in_file(new_template, "#" +
130 var.get_variable_name(),
131 var.get_variable_value())
132 metadata[var.get_variable_name()] = var.get_variable_value()
134 # Save the metadata on a JSON file
135 with open(new_template + ".json", 'w') as outfile:
136 json.dump(metadata, outfile)
138 common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
142 # Creation of the template files
143 common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
147 def get_all_heat_templates(template_dir, template_extension):
148 template_files = list()
149 for dirname, dirnames, filenames in os.walk(template_dir):
150 for filename in filenames:
151 if template_extension in filename and filename.endswith(template_extension) and template_name in filename:
152 template_files.append(filename)
153 template_files.sort()
154 return template_files