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.
15 from cmframework.utils import cmtopologicalsort
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'
25 def __init__(self, dest, bootstrapping_path, provisioning_path, postconfig_path, finalize_path):
27 self.bootstrapping_path = bootstrapping_path
28 self.provisioning_path = provisioning_path
29 self.postconfig_path = postconfig_path
30 self.finalize_path = finalize_path
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)
38 number_of_playbooks = bootstrapping_size \
43 playbook_percentage_value = 99.0 / number_of_playbooks
45 self._generate_playbook(bootstrapping_lists, self.bootstrapping_playbook, 0, playbook_percentage_value)
46 progress_start = bootstrapping_size*playbook_percentage_value
47 self._generate_playbook(provisioning_lists, self.provisioning_playbook, progress_start, playbook_percentage_value)
48 progress_start = (bootstrapping_size+provisioning_size)*playbook_percentage_value
49 self._generate_playbook(postconfig_lists, self.postconfig_playbook, progress_start, playbook_percentage_value)
50 progress_start = (bootstrapping_size+provisioning_size+postconfig_size)*playbook_percentage_value
51 self._generate_playbook(finalize_lists, self.finalize_playbook, progress_start, playbook_percentage_value)
53 def _get_playbook_lists(self, directory):
54 graph = self._get_dependency_graph(directory)
55 topsort = cmtopologicalsort.TopSort(graph)
56 sortedlists = topsort.sort()
59 for entry in sortedlists:
62 return sortedlists, size
64 def _generate_playbook(self, sortedlists, name, progress_start, playbook_percentage_value):
65 progress = progress_start
66 with open(self.dest + '/' + name, 'w') as f:
67 for entry in sortedlists:
69 f.write('- import_playbook: ' + self.progress_playbook +
70 ' installation_progress_phase=' + name +
71 ' installation_progress_playbook=' + e +
72 ' installation_progress=' + str(progress) + '\n')
73 f.write('- import_playbook: ' + e + '\n')
74 progress = progress+playbook_percentage_value
76 def _get_dependency_graph(self, directory):
77 entries = os.listdir(directory)
80 entryfull = directory + '/' + entry
81 if os.path.isfile(entryfull) or os.path.islink(entryfull):
82 requires = self._get_required_playbooks(entryfull)
83 existing_requires = []
84 for require in requires:
85 requirefull = directory + '/' + require
86 if os.path.exists(requirefull):
87 existing_requires.append(require)
88 graph[entry] = existing_requires
93 def _get_required_playbooks(playbook):
95 with open(playbook) as f:
96 lines = f.read().splitlines()
97 # parse the lines containing:
98 # cmframework.requires: <comma separated list of playbooks>
100 if 'cmframework.requires:' in line:
101 data = line.split(':')
104 tmp = data[1].replace(" ", "")
105 requires = tmp.split(',')
111 if __name__ == '__main__':
112 playbooks = AnsiblePlaybooks('/tmp/', '/etc/lcm/playbooks/installation/bootstrapping',
113 '/etc/lcm/playbooks/installation/provisioning',
114 '/etc/lcm/playbooks/installation/postconfig',
115 '/etc/lcm/playbooks/installation/finalize')
116 playbooks.generate_playbooks()