5a91a1b80e84b8a399338b64193ae0475f1566f3
[ta/config-manager.git] / cmansibleplaybooks.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import os
15 from cmframework.utils import cmtopologicalsort
16
17
18 class AnsiblePlaybooks(object):
19     bootstrapping_playbook = 'bootstrapping-playbook.yml'
20     provisioning_playbook = 'provisioning-playbook.yml'
21     postconfig_playbook = 'postconfig-playbook.yml'
22     finalize_playbook = 'finalize-playbook.yml'
23     progress_playbook = 'report_installation_progress.yml'
24
25     def __init__(self, dest, bootstrapping_path, provisioning_path, postconfig_path, finalize_path):
26         self.dest = dest
27         self.bootstrapping_path = bootstrapping_path
28         self.provisioning_path = provisioning_path
29         self.postconfig_path = postconfig_path
30         self.finalize_path = finalize_path
31
32     def generate_playbooks(self):
33         bootstrapping_lists, bootstrapping_size = self._get_playbook_lists(self.bootstrapping_path)
34         provisioning_lists, provisioning_size = self._get_playbook_lists(self.provisioning_path)
35         postconfig_lists, postconfig_size = self._get_playbook_lists(self.postconfig_path)
36         finalize_lists, finalize_size = self._get_playbook_lists(self.finalize_path)
37
38         number_of_playbooks = bootstrapping_size+provisioning_size+postconfig_size+finalize_size
39
40         playbook_percentage_value = 99.0 / number_of_playbooks
41
42         self._generate_playbook(bootstrapping_lists,
43                                 self.bootstrapping_playbook,
44                                 0,
45                                 playbook_percentage_value)
46
47         progress_start = bootstrapping_size*playbook_percentage_value
48         self._generate_playbook(provisioning_lists,
49                                 self.provisioning_playbook,
50                                 progress_start,
51                                 playbook_percentage_value)
52         progress_start = (bootstrapping_size+provisioning_size)*playbook_percentage_value
53         self._generate_playbook(postconfig_lists, self.postconfig_playbook,
54                                 progress_start, playbook_percentage_value)
55         progress_start = (bootstrapping_size +
56                           provisioning_size+postconfig_size)*playbook_percentage_value
57         self._generate_playbook(finalize_lists,
58                                 self.finalize_playbook,
59                                 progress_start,
60                                 playbook_percentage_value)
61
62     def _get_playbook_lists(self, directory):
63         graph = self._get_dependency_graph(directory)
64         topsort = cmtopologicalsort.TopSort(graph)
65         sortedlists = topsort.sort()
66
67         size = 0
68         for entry in sortedlists:
69             size += len(entry)
70
71         return sortedlists, size
72
73     def _generate_playbook(self, sortedlists, name, progress_start, playbook_percentage_value):
74         progress = progress_start
75         with open(self.dest + '/' + name, 'w') as f:
76             for entry in sortedlists:
77                 for e in entry:
78                     f.write('- import_playbook: ' + self.progress_playbook +
79                             ' installation_progress_phase=' + name +
80                             ' installation_progress_playbook=' + e +
81                             ' installation_progress=' + str(progress) + '\n')
82                     f.write('- import_playbook: ' + e + '\n')
83                     progress = progress+playbook_percentage_value
84
85     def _get_dependency_graph(self, directory):
86         entries = os.listdir(directory)
87         graph = {}
88         for entry in entries:
89             entryfull = directory + '/' + entry
90             if os.path.isfile(entryfull) or os.path.islink(entryfull):
91                 requires = self._get_required_playbooks(entryfull)
92                 existing_requires = []
93                 for require in requires:
94                     requirefull = directory + '/' + require
95                     if os.path.exists(requirefull):
96                         existing_requires.append(require)
97                 graph[entry] = existing_requires
98
99         return graph
100
101     @staticmethod
102     def _get_required_playbooks(playbook):
103         requires = []
104         with open(playbook) as f:
105             lines = f.read().splitlines()
106             # parse the lines containing:
107             # cmframework.requires: <comma separated list of playbooks>
108             for line in lines:
109                 if 'cmframework.requires:' in line:
110                     data = line.split(':')
111                     if len(data) != 2:
112                         continue
113                     tmp = data[1].replace(" ", "")
114                     requires = tmp.split(',')
115                     break
116
117         return requires
118
119
120 if __name__ == '__main__':
121     playbooks = AnsiblePlaybooks('/tmp/', '/etc/lcm/playbooks/installation/bootstrapping',
122                                  '/etc/lcm/playbooks/installation/provisioning',
123                                  '/etc/lcm/playbooks/installation/postconfig',
124                                  '/etc/lcm/playbooks/installation/finalize')
125     playbooks.generate_playbooks()