heat api management and template creation functions
[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 class TreeNode:
19
20     def __init__(self):
21         self.up = None
22         self.down = []
23         self.variable_name = ''
24         self.variable_value = 0
25
26     def add_child(self, node):
27         node.up = self
28         self.down.append(node)
29
30     def get_parent(self):
31         return self.up
32
33     def get_children(self):
34         if len(self.down) == 0:
35             return []
36         return self.down
37
38     def get_variable_name(self):
39         return self.variable_name
40
41     def get_variable_value(self):
42         return self.variable_value
43
44     def set_variable_name(self, name):
45         self.variable_name = name
46
47     def set_variable_value(self, value):
48         self.variable_value = value
49
50     def get_path(self):
51         ret_val = []
52         if not self.up:
53             ret_val.append(self)
54             return ret_val
55         for node in self.up.get_path():
56             ret_val.append(node)
57         ret_val.append(self)
58         return ret_val
59
60     def __str__(self):
61         return str(self.variable_name) + " --> " + str(self.variable_value)
62
63     def __repr__(self):
64         return str(self.variable_name) + " = " + str(self.variable_value)
65
66     @staticmethod
67     def _get_leaves(node, leaves):
68         children = node.get_children()
69         if len(children) == 0:
70             leaves.append(node)
71             return
72         for child in children:
73             TreeNode._get_leaves(child, leaves)
74
75     @staticmethod
76     def get_leaves(node):
77         leaves = list()
78         TreeNode._get_leaves(node, leaves)
79         return leaves
80
81 template_name = parameters.TEST_TEMPLATE_NAME
82
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
88
89     variables = deployment_configuration
90
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)
94     os.system(command)
95
96     # Creation of the tree with all the new configurations
97     common.LOG.info("Creation of a tree with all new configurations")
98     tree = TreeNode()
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]))
103
104         for value in variables[variable]:
105             for leaf in leaves:
106                 new_node = TreeNode()
107                 new_node.set_variable_name(variable)
108                 new_node.set_variable_value(value)
109                 leaf.add_child(new_node)
110
111     common.LOG.debug("CONFIGURATION TREE: " + str(tree))
112
113     common.LOG.info("Heat Template and metadata file creation")
114     leaves = TreeNode.get_leaves(tree)
115     counter = 1
116     for leaf in leaves:
117         heat_template_vars = leaf.get_path()
118         if os.path.isabs(template_base_name):
119             base_template = template_base_name
120         else:
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)
125
126         metadata = dict()
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()
133
134         # Save the metadata on a JSON file
135         with open(new_template + ".json", 'w') as outfile:
136             json.dump(metadata, outfile)
137
138         common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
139                          " created")
140         counter += 1
141
142     # Creation of the template files
143     common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
144                                        "created")
145
146
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