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
24 self.variable_name = ''
25 self.variable_value = 0
27 def add_child(self, node):
29 self.down.append(node)
34 def get_children(self):
35 if len(self.down) == 0:
39 def get_variable_name(self):
40 return self.variable_name
42 def get_variable_value(self):
43 return self.variable_value
45 def set_variable_name(self, name):
46 self.variable_name = name
48 def set_variable_value(self, value):
49 self.variable_value = value
56 for node in self.up.get_path():
62 return str(self.variable_name) + " --> " + str(self.variable_value)
65 return str(self.variable_name) + " = " + str(self.variable_value)
68 def _get_leaves(node, leaves):
69 children = node.get_children()
70 if len(children) == 0:
73 for child in children:
74 TreeNode._get_leaves(child, leaves)
79 TreeNode._get_leaves(node, leaves)
82 template_name = parameters.TEST_TEMPLATE_NAME
85 def generates_templates(base_heat_template, deployment_configuration):
86 # parameters loaded from file
87 template_dir = common.get_template_dir()
88 template_extension = parameters.TEMPLATE_EXTENSION
89 template_base_name = base_heat_template
91 variables = deployment_configuration
93 # Delete the templates generated in previous running
94 common.LOG.info("Removing the heat templates already generated")
95 command = "rm {}{}_*".format(template_dir, template_name)
98 # Creation of the tree with all the new configurations
99 common.LOG.info("Creation of a tree with all new configurations")
101 for variable in variables:
102 leaves = TreeNode.get_leaves(tree)
103 common.LOG.debug("LEAVES: " + str(leaves))
104 common.LOG.debug("VALUES: " + str(variables[variable]))
106 for value in variables[variable]:
108 new_node = TreeNode()
109 new_node.set_variable_name(variable)
110 new_node.set_variable_value(value)
111 leaf.add_child(new_node)
113 common.LOG.debug("CONFIGURATION TREE: " + str(tree))
115 common.LOG.info("Heat Template and metadata file creation")
116 leaves = TreeNode.get_leaves(tree)
119 heat_template_vars = leaf.get_path()
120 if os.path.isabs(template_base_name):
121 base_template = template_base_name
123 base_template = template_dir + template_base_name
124 new_template = template_dir + template_name
125 new_template += "_" + str(counter) + template_extension
126 shutil.copy(base_template, new_template)
129 for var in heat_template_vars:
130 if var.get_variable_name():
131 common.replace_in_file(new_template, "#" +
132 var.get_variable_name(),
133 var.get_variable_value())
134 metadata[var.get_variable_name()] = var.get_variable_value()
136 # Save the metadata on a JSON file
137 with open(new_template + ".json", 'w') as outfile:
138 json.dump(metadata, outfile)
140 common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
144 # Creation of the template files
145 common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
149 def get_all_heat_templates(template_dir, template_extension):
150 template_files = list()
151 for dirname, dirnames, filenames in os.walk(template_dir):
152 for filename in filenames:
153 if template_extension in filename and filename.endswith(
154 template_extension) and template_name in filename:
155 template_files.append(filename)
156 template_files.sort()
157 return template_files