1593592dd10e2ae96c8bae58e43f55f678b22b8a
[ccsdk/distribution.git] / ansible-server / src / main / ansible-server / BuildPlaybookParams.py
1
2 from os import listdir
3 from os.path import isfile, join
4 import os.path
5 import shutil
6 import subprocess
7
8 import cherrypy
9
10
11 def buildInventorySysCall(ansible_path, ansible_inv, node_list, playbook_dir, target_inv, hostgrouplist, hostnamelist):
12     if not node_list:
13         LocalNodeList = "host"
14         LocalCredentials = "localhost    ansible_connection=local"
15         f = open(playbook_dir + "/" + target_inv, "w")
16         f.write("[" + LocalNodeList + "]\n")
17         f.write(LocalCredentials)
18         f.close()
19     else:
20         # Get credentials from file
21         data_inventory_orig = {}
22         data_inventory_target = {}
23         curr_group = None
24
25         cherrypy.log("***> " + ansible_path + "/" + ansible_inv)
26         f = open(ansible_path + "/" + ansible_inv, "r")
27         for line in f:
28             line = line.rstrip()
29
30             if len(line) > 0:
31                 if '#' not in line:
32                     if "[" in line and "]" in line:
33                         data_inventory_orig[line] = []
34                         curr_group = line
35                     else:
36                         data_inventory_orig[curr_group].append(line)
37         f.close()
38
39         for node in node_list:
40             Fail = True
41             if "[" + node + "]" in data_inventory_orig:
42                 if not "[" + node + "]" in data_inventory_target:
43                     cherrypy.log("RESET", "[" + node + "]")
44                     data_inventory_target["[" + node + "]"] = []
45                 else:
46                     cherrypy.log("OK", "[" + node + "]")
47                 Fail = False
48                 for cred in data_inventory_orig["[" + node + "]"]:
49                     data_inventory_target["[" + node + "]"].append(cred)
50             else:
51                 for key in data_inventory_orig:
52                     if node + " " in " ".join(data_inventory_orig[key]):
53                         if key not in data_inventory_target:
54                             data_inventory_target[key] = []
55                         for cred in data_inventory_orig[key]:
56                             if node + " " in cred:
57                                 data_inventory_target[key].append(cred)
58                                 Fail = False
59
60             if Fail:
61                 data_inventory_target["[" + node + "]"] = \
62                     [node + " ansible_connection=ssh ansible_ssh_user=na ansible_ssh_private_key_file=na"]
63
64         f = open(playbook_dir + "/" + target_inv, "w")
65         for key in data_inventory_target:
66             f.write(key + "\n")
67             for rec in data_inventory_target[key]:
68                 hostgrouplist.append(key.replace("[", '').replace("]", ''))
69                 hostnamelist.append(rec.split(' ')[0])
70                 f.write(rec + "\n")
71         f.close()
72
73
74 def getPlaybookFile(ansible_path, playbook_name, playbook_type, playbook_dir):
75     # Get playbooks from files
76
77     version = None
78     target_playbook_name = None
79
80     if '@' in playbook_name:
81         version = playbook_name.split("@")[1]
82         version = version.replace('.yml', '')
83         version = version.replace('.tar.gz', '')
84
85     onlyfiles = [f for f in listdir(ansible_path) if isfile(join(ansible_path, f))]
86
87     version_max = '0.00'
88     version_target = ''
89
90     for filename in onlyfiles:
91         if playbook_type in filename:
92             temp_version = filename.split("@")[1]
93             temp_version = temp_version.replace('.yml', '')
94             temp_version = temp_version.replace('.tar.gz', '')
95             if version_max < temp_version:
96                 version_max = temp_version
97
98             if version is not None:
99                 if version in playbook_name:
100                     version_target = version
101                     target_playbook_name = filename
102
103     if target_playbook_name is None:
104         for filename in onlyfiles:
105             if playbook_type in filename and version_max in filename:
106                 target_playbook_name = filename
107                 version_target = version_max
108
109     if target_playbook_name:
110         src = ansible_path + "/" + target_playbook_name
111         if not os.path.exists(src):
112             return ''
113
114         if ".tar.gz" in target_playbook_name:
115             dest = playbook_dir + "/" + playbook_type + ".tar.gz"
116             shutil.copy2(src, dest)
117             subprocess.call(['tar', '-xvzf', dest, "-C", playbook_dir])
118         else:
119             dest = playbook_dir + "/" + playbook_type + ".yml"
120             shutil.copy2(src, dest)
121
122     return version_target

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use