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'
24 def __init__(self, dest, bootstrapping_path, provisioning_path, postconfig_path, finalize_path):
26 self.bootstrapping_path = bootstrapping_path
27 self.provisioning_path = provisioning_path
28 self.postconfig_path = postconfig_path
29 self.finalize_path = finalize_path
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)
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:
44 fullpath = directory + '/' + e
45 if os.path.exists(fullpath):
46 f.write('- import_playbook: ' + e + '\n')
48 def _get_dependency_graph(self, directory):
49 entries = os.listdir(directory)
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
59 def _get_required_playbooks(playbook):
61 with open(playbook) as f:
62 lines = f.read().splitlines()
63 # parse the lines containing:
64 # cmframework.requires: <comma separated list of playbooks>
66 if 'cmframework.requires:' in line:
67 data = line.split(':')
70 tmp = data[1].replace(" ", "")
71 requires = tmp.split(',')
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()