Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / utils / 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
24     def __init__(self, dest, bootstrapping_path, provisioning_path, postconfig_path, finalize_path):
25         self.dest = dest
26         self.bootstrapping_path = bootstrapping_path
27         self.provisioning_path = provisioning_path
28         self.postconfig_path = postconfig_path
29         self.finalize_path = finalize_path
30
31     def generate_playbooks(self):
32         self._generate_playbook(self.bootstrapping_path, self.bootstrapping_playbook)
33         self._generate_playbook(self.provisioning_path, self.provisioning_playbook)
34         self._generate_playbook(self.postconfig_path, self.postconfig_playbook)
35         self._generate_playbook(self.finalize_path, self.finalize_playbook)
36
37     def _generate_playbook(self, directory, name):
38         graph = self._get_dependency_graph(directory)
39         topsort = cmtopologicalsort.TopSort(graph)
40         sortedlists = topsort.sort()
41         with open(self.dest + '/' + name, 'w') as f:
42             for entry in sortedlists:
43                 for e in entry:
44                     fullpath = directory + '/' + e
45                     if os.path.exists(fullpath):
46                         f.write('- import_playbook: ' + e + '\n')
47
48     def _get_dependency_graph(self, directory):
49         entries = os.listdir(directory)
50         graph = {}
51         for entry in entries:
52             entryfull = directory + '/' + entry
53             if os.path.isfile(entryfull) or os.path.islink(entryfull):
54                 requires = self._get_required_playbooks(entryfull)
55                 graph[entry] = requires
56         return graph
57
58     @staticmethod
59     def _get_required_playbooks(playbook):
60         requires = []
61         with open(playbook) as f:
62             lines = f.read().splitlines()
63             # parse the lines containing:
64             # cmframework.requires: <comma separated list of playbooks>
65             for line in lines:
66                 if 'cmframework.requires:' in line:
67                     data = line.split(':')
68                     if len(data) != 2:
69                         continue
70                     tmp = data[1].replace(" ", "")
71                     requires = tmp.split(',')
72                     break
73         return requires
74
75
76 if __name__ == '__main__':
77     playbooks = AnsiblePlaybooks('/tmp/', '/etc/lcm/playbooks/installation/bootstrapping',
78                                  '/etc/lcm/playbooks/installation/provisioning',
79                                  '/etc/lcm/playbooks/installation/postconfig',
80                                  '/etc/lcm/playbooks/installation/finalize')
81     playbooks.generate_playbooks()