e3a0b4b5f860f7cdbc6f7ab56cc6130540e39cd8
[ccsdk/distribution.git] / ansible-server / src / main / ansible-server / BuildPlaybookParams.py
1 '''
2 /*-
3 * ============LICENSE_START=======================================================
4 * ONAP : APPC
5 * ================================================================================
6 * Copyright (C) 2017-2019 AT&T Intellectual Property.  All rights reserved.
7 * ================================================================================
8 * Copyright (C) 2017 Amdocs
9 * =============================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 *      http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21
22 * ============LICENSE_END=========================================================
23 */
24 '''
25
26 from os import listdir
27 from os.path import isfile, join
28 import os.path
29 import shutil
30 import subprocess
31
32 import cherrypy
33
34
35 def buildInventorySysCall(ansible_path, ansible_inv, node_list, playbook_dir, target_inv, hostgrouplist, hostnamelist):
36     if not node_list:
37         local_node_list = "host"
38         local_credentials = "localhost    ansible_connection=local"
39
40         f = open(playbook_dir + "/" + target_inv, "w")
41         f.write("[" + local_node_list + "]\n")
42         f.write(local_credentials)
43         f.close()
44     else:
45         # Get credentials from file
46         data_inventory_orig = {}
47         data_inventory_target = {}
48         curr_group = None
49
50         cherrypy.log("***> " + ansible_path + "/" + ansible_inv)
51         f = open(ansible_path + "/" + ansible_inv, "r")
52         for line in f:
53             line = line.rstrip()
54
55             if len(line) > 0:
56                 if '#' not in line:
57                     if "[" in line and "]" in line:
58                         data_inventory_orig[line] = []
59                         curr_group = line
60                     else:
61                         data_inventory_orig[curr_group].append(line)
62         f.close()
63
64         for node in node_list:
65             fail_flag = True
66             if "[" + node + "]" in data_inventory_orig:
67                 if "[" + node + "]" not in data_inventory_target:
68                     cherrypy.log("RESET", "[" + node + "]")
69                     data_inventory_target["[" + node + "]"] = []
70                 else:
71                     cherrypy.log("OK", "[" + node + "]")
72                 fail_flag = False
73                 for cred in data_inventory_orig["[" + node + "]"]:
74                     data_inventory_target["[" + node + "]"].append(cred)
75             else:
76                 for key in data_inventory_orig:
77                     if node + " " in " ".join(data_inventory_orig[key]):
78                         if key not in data_inventory_target:
79                             data_inventory_target[key] = []
80                         for cred in data_inventory_orig[key]:
81                             if node + " " in cred:
82                                 data_inventory_target[key].append(cred)
83                                 fail_flag = False
84
85             if fail_flag:
86                 data_inventory_target["[" + node + "]"] = \
87                     [node + " ansible_connection=ssh ansible_ssh_user=na ansible_ssh_private_key_file=na"]
88
89         f = open(playbook_dir + "/" + target_inv, "w")
90         for key in data_inventory_target:
91             f.write(key + "\n")
92             for rec in data_inventory_target[key]:
93                 hostgrouplist.append(key.replace("[", '').replace("]", ''))
94                 hostnamelist.append(rec.split(' ')[0])
95                 f.write(rec + "\n")
96         f.close()
97
98
99 def getPlaybookFile(ansible_path, playbook_name, playbook_type, playbook_dir):
100     # Get playbooks from files
101
102     version = None
103     target_playbook_name = None
104
105     if '@' in playbook_name:
106         version = playbook_name.split("@")[1]
107         version = version.replace('.yml', '')
108         version = version.replace('.tar.gz', '')
109
110     onlyfiles = [f for f in listdir(ansible_path) if isfile(join(ansible_path, f))]
111
112     version_max = '0.00'
113     version_target = ''
114
115     for filename in onlyfiles:
116         if playbook_type in filename:
117             temp_version = filename.split("@")[1]
118             temp_version = temp_version.replace('.yml', '')
119             temp_version = temp_version.replace('.tar.gz', '')
120             if version_max < temp_version:
121                 version_max = temp_version
122
123             if version is not None:
124                 if version in playbook_name:
125                     version_target = version
126                     target_playbook_name = filename
127
128     if target_playbook_name is None:
129         for filename in onlyfiles:
130             if playbook_type in filename and version_max in filename:
131                 target_playbook_name = filename
132                 version_target = version_max
133
134     if target_playbook_name:
135         src = ansible_path + "/" + target_playbook_name
136         if not os.path.exists(src):
137             return ''
138
139         if ".tar.gz" in target_playbook_name:
140             dest = playbook_dir + "/" + playbook_type + ".tar.gz"
141             shutil.copy2(src, dest)
142             subprocess.call(['tar', '-xvzf', dest, "-C", playbook_dir])
143         else:
144             dest = playbook_dir + "/" + playbook_type + ".yml"
145             shutil.copy2(src, dest)
146
147     return version_target