autopep8 fix for flake8
[bottlenecks.git] / utils / infra_setup / heat / template.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 """to create heat templates from the base template
11 """
12 import os
13 import json
14 import shutil
15 import common
16 import consts.parameters as parameters
17
18
19 class TreeNode:
20
21     def __init__(self):
22         self.up = None
23         self.down = []
24         self.variable_name = ''
25         self.variable_value = 0
26
27     def add_child(self, node):
28         node.up = self
29         self.down.append(node)
30
31     def get_parent(self):
32         return self.up
33
34     def get_children(self):
35         if len(self.down) == 0:
36             return []
37         return self.down
38
39     def get_variable_name(self):
40         return self.variable_name
41
42     def get_variable_value(self):
43         return self.variable_value
44
45     def set_variable_name(self, name):
46         self.variable_name = name
47
48     def set_variable_value(self, value):
49         self.variable_value = value
50
51     def get_path(self):
52         ret_val = []
53         if not self.up:
54             ret_val.append(self)
55             return ret_val
56         for node in self.up.get_path():
57             ret_val.append(node)
58         ret_val.append(self)
59         return ret_val
60
61     def __str__(self):
62         return str(self.variable_name) + " --> " + str(self.variable_value)
63
64     def __repr__(self):
65         return str(self.variable_name) + " = " + str(self.variable_value)
66
67     @staticmethod
68     def _get_leaves(node, leaves):
69         children = node.get_children()
70         if len(children) == 0:
71             leaves.append(node)
72             return
73         for child in children:
74             TreeNode._get_leaves(child, leaves)
75
76     @staticmethod
77     def get_leaves(node):
78         leaves = list()
79         TreeNode._get_leaves(node, leaves)
80         return leaves
81
82 template_name = parameters.TEST_TEMPLATE_NAME
83
84
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
90
91     variables = deployment_configuration
92
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)
96     os.system(command)
97
98     # Creation of the tree with all the new configurations
99     common.LOG.info("Creation of a tree with all new configurations")
100     tree = TreeNode()
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]))
105
106         for value in variables[variable]:
107             for leaf in leaves:
108                 new_node = TreeNode()
109                 new_node.set_variable_name(variable)
110                 new_node.set_variable_value(value)
111                 leaf.add_child(new_node)
112
113     common.LOG.debug("CONFIGURATION TREE: " + str(tree))
114
115     common.LOG.info("Heat Template and metadata file creation")
116     leaves = TreeNode.get_leaves(tree)
117     counter = 1
118     for leaf in leaves:
119         heat_template_vars = leaf.get_path()
120         if os.path.isabs(template_base_name):
121             base_template = template_base_name
122         else:
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)
127
128         metadata = dict()
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()
135
136         # Save the metadata on a JSON file
137         with open(new_template + ".json", 'w') as outfile:
138             json.dump(metadata, outfile)
139
140         common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
141                          " created")
142         counter += 1
143
144     # Creation of the template files
145     common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
146                                        "created")
147
148
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