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+provisioning_size+postconfig_size+finalize_size
40 playbook_percentage_value = 99.0 / number_of_playbooks
42 self._generate_playbook(bootstrapping_lists,
43 self.bootstrapping_playbook,
45 playbook_percentage_value)
47 progress_start = bootstrapping_size*playbook_percentage_value
48 self._generate_playbook(provisioning_lists,
49 self.provisioning_playbook,
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,
60 playbook_percentage_value)
62 def _get_playbook_lists(self, directory):
63 graph = self._get_dependency_graph(directory)
64 topsort = cmtopologicalsort.TopSort(graph)
65 sortedlists = topsort.sort()
68 for entry in sortedlists:
71 return sortedlists, size
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:
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
85 def _get_dependency_graph(self, directory):
86 entries = os.listdir(directory)
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
102 def _get_required_playbooks(playbook):
104 with open(playbook) as f:
105 lines = f.read().splitlines()
106 # parse the lines containing:
107 # cmframework.requires: <comma separated list of playbooks>
109 if 'cmframework.requires:' in line:
110 data = line.split(':')
113 tmp = data[1].replace(" ", "")
114 requires = tmp.split(',')
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()