1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 This file contains the code to Generate the heat templates from the base
24 from experimental_framework import common
25 from experimental_framework.constants import framework_parameters as fp
30 This class represent the node of the configuration tree.\
31 Each node represents a single configuration value for a single
32 configuration parameter.
38 self.variable_name = ''
39 self.variable_value = 0
41 def add_child(self, node):
43 Adds a node as a child for the current node
44 :param node: node to be added as a child (type: TreeNode)
48 self.down.append(node)
52 Returns the parent node of the current one
53 :return type: TreeNode
57 def get_children(self):
59 Returns the children of the current node
60 :return type: list of TreeNode
62 if len(self.down) == 0:
67 def get_variable_name(self):
69 Returns the name of the variable correspondent to the current node
72 return self.variable_name
74 def get_variable_value(self):
76 Returns the value of the variable correspondent to the current node
77 :return type: str or int
79 return self.variable_value
81 def set_variable_name(self, name):
83 Sets the name of the variable for the current node
84 :param name: Name of the variable (type: str)
87 self.variable_name = name
89 def set_variable_value(self, value):
91 Sets the value of the variable for the current node
92 :param value: value of the variable (type: str)
95 self.variable_value = value
99 Returns all the path from the current node to the root of the tree.
100 :return type: list of TreeNode
106 for node in self.up.get_path():
112 return str(self.variable_name) + " --> " + str(self.variable_value)
115 return str(self.variable_name) + " = " + str(self.variable_value)
118 def _get_leaves(node, leaves):
120 Returns all the leaves of a tree.
121 It changes the "leaves" list.
122 :param node: root of the tree (type: TreeNode)
123 :param leaves: partial list of leaves (type: list of TreeNode)
126 children = node.get_children()
127 if len(children) == 0:
130 for child in children:
131 TreeNode._get_leaves(child, leaves)
134 def get_leaves(node):
136 Returns all the leaves of a tree.
137 :param node: root of the tree (TreeNode)
141 TreeNode._get_leaves(node, leaves)
145 template_name = fp.EXPERIMENT_TEMPLATE_NAME
148 def generates_templates(base_heat_template, deployment_configuration):
150 Generates the heat templates for the experiments
153 # Load useful parameters from file
154 template_dir = common.get_template_dir()
155 template_file_extension = fp.TEMPLATE_FILE_EXTENSION
156 template_base_name = base_heat_template
158 variables = deployment_configuration
160 # Delete the templates eventually generated in previous running of the
162 common.LOG.info("Removing the heat templates previously generated")
163 os.system("rm " + template_dir + template_name + "_*")
165 # Creation of the tree with all the new configurations
166 common.LOG.info("Creation of the tree with all the new configurations")
168 for variable in variables:
169 leaves = TreeNode.get_leaves(tree)
170 common.LOG.debug("LEAVES: " + str(leaves))
171 common.LOG.debug("VALUES: " + str(variables[variable]))
173 for value in variables[variable]:
175 new_node = TreeNode()
176 new_node.set_variable_name(variable)
177 new_node.set_variable_value(value)
178 leaf.add_child(new_node)
180 common.LOG.debug("CONFIGURATION TREE: " + str(tree))
182 common.LOG.info("Heat Template and metadata file creation")
183 leaves = TreeNode.get_leaves(tree)
186 heat_template_vars = leaf.get_path()
187 if os.path.isabs(template_base_name):
188 base_template = template_base_name
190 base_template = template_dir + template_base_name
191 if os.path.isabs(template_name):
192 new_template = template_name
194 new_template = template_dir + template_name
195 new_template += "_" + str(counter) + template_file_extension
196 shutil.copy(base_template, new_template)
199 for var in heat_template_vars:
200 if var.get_variable_name():
201 common.replace_in_file(new_template, "#" +
202 var.get_variable_name(),
203 var.get_variable_value())
204 metadata[var.get_variable_name()] = var.get_variable_value()
206 # Save the metadata on a JSON file
207 with open(new_template + ".json", 'w') as outfile:
208 json.dump(metadata, outfile)
210 common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
214 # Creation of the template files
215 common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
219 def get_all_heat_templates(template_dir, template_file_extension):
221 Loads and returns all the generated heat templates
222 :param template_dir: directory to search in (type: str)
223 :param template_file_extension: extension of the file for templates
227 template_files = list()
228 for dirname, dirnames, filenames in os.walk(template_dir):
229 for filename in filenames:
230 if template_file_extension in filename and \
231 filename.endswith(template_file_extension) and \
232 template_name in filename:
233 template_files.append(filename)
234 template_files.sort()
235 return template_files