-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
297 lines (245 loc) · 9.64 KB
/
utils.py
File metadata and controls
297 lines (245 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python3
import os
import sys
import subprocess
from itertools import islice
from pathlib import Path
import yaml
import osias_variables
from ssh_tool import ssh_tool
class parser:
def __init__(self, config):
self.data = yaml.safe_load(config)
self.kolla_configs = {}
def get_server_ips(self, node_type, ip_type):
data = self.data[node_type]
ips = []
for myips in data:
ips.append(myips[ip_type])
return ips
def get_variables(self, variable, openstack_release=None, optional=False):
if "variables" in self.data and variable in self.data["variables"]:
data = self.data["variables"]
if variable in data:
return str(data[variable])
elif getattr(osias_variables, variable, None):
x = getattr(osias_variables, variable)
return x[openstack_release]
elif optional:
return None
raise Exception(f"Unable to location, {variable}, please specify.")
def get_kolla_configs(self):
if "etc" in self.data:
data = self.data["etc"]
results = {}
results = self.find_strings(data, data)
return results
return None
def find_paths(self, nested_dict, value, prepath=()):
def yielder(nested_dict, value, prepath=()):
for k, v in nested_dict.items():
path = prepath + (k,)
if v == value: # found value
path = ("/etc", *path)
dest = "/".join(path)
yield dest
elif hasattr(v, "items"): # v is a dict
yield from yielder(v, value, path)
return list(yielder(nested_dict, value, prepath=()))
def find_strings(self, nested_dict, orig_dict):
for key, value in nested_dict.items():
if isinstance(value, dict):
self.find_strings(value, orig_dict)
if isinstance(value, str):
x = *self.find_paths(orig_dict, value), value
for i in range(len(x)):
if i != len(x) - 1:
self.kolla_configs[x[i]] = x[len(x) - 1]
return self.kolla_configs
def get_all_ips_type(self, iptype):
data = ["control", "network", "storage", "compute", "monitor"]
ALL_IPS = []
for my_node_type in data:
ips = parser.get_server_ips(self, node_type=my_node_type, ip_type=iptype)
ALL_IPS.extend(ips)
ALL_IPS = list((dict.fromkeys(ALL_IPS))) # remove duplicates from list
ALL_IPS = list(filter(None, ALL_IPS)) # remove null values from list
if not ALL_IPS and iptype != "data":
raise Exception(f"{iptype} IPs are not set, empty list.")
return ALL_IPS
def get_each_servers_ips(self):
data = self.data.keys()
SERVERS = []
for my_node_type in data:
data = self.data.get(my_node_type)
for key, value in data.items():
if value["public"]: # Remove any empty servers
SERVERS.extend([value])
# Remove duplicate servers from the list
SERVERS = [i for n, i in enumerate(SERVERS) if i not in SERVERS[:n]]
return SERVERS
def bool_check_ips_exist(self, node_type, ip_type):
data = self.data[node_type]
for item in data:
return bool(item[ip_type])
def convert_to_list(parm):
if isinstance(parm, str):
tmpList = []
tmpList.append(parm)
return tmpList
return parm
def merge_dictionaries(default_dictionary, user_input_dictionary):
"""Merges user_input_dictionary into default dictionary;
default values will be overwritten by users input."""
return {**default_dictionary, **user_input_dictionary}
def create_ssh_client(target_node):
client = ssh_tool("ubuntu", target_node)
if not client.check_access():
raise Exception(
f"ERROR: Failed to connect to target node with IP {target_node} using SSH"
)
return client
def copy_file_on_server(script, servers):
servers = convert_to_list(servers)
for server in servers:
client = create_ssh_client(server)
client.scp_to(script)
def run_script_on_server(script, servers, args=None):
servers = convert_to_list(servers)
for server in servers:
client = create_ssh_client(server)
client.scp_to(script)
if args:
arguments = ""
for arg in args:
arguments += "".join((' "', arg, '"'))
cmd = "".join((script, arguments))
else:
cmd = script
print(cmd)
client.ssh("".join(("source ", cmd)))
def run_cmd_on_server(cmd, servers):
servers = convert_to_list(servers)
for server in servers:
client = create_ssh_client(server)
client.ssh(cmd)
def run_cmd(command, test=True, output=True):
print(f"\nCommand Issued: \n\t{command}\n")
stdout = None
try:
stdout = subprocess.check_output(
command, stderr=subprocess.STDOUT, shell=True, executable="/bin/bash"
)
except subprocess.CalledProcessError as e:
if test:
raise Exception(e.output.decode()) from e
print(e.output.decode())
if output:
print(f"\nCommand Output: \n{stdout.decode()}\n")
return stdout
def run_cmd_locally(command, test=True, output=True):
print(f"\nCommand Issued: \n\t{command}\n")
stdout = None
process = subprocess.Popen(command, stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), b""):
sys.stdout.buffer.write(c)
def check_ip_active(ip):
response = os.system("ping -c 1 " + ip + " > /dev/null 2>&1")
if response == 0:
print(f"Ping shows {ip} is in use (packets received)!")
return True
else:
print(f"Ping shows {ip} is NOT in use (packets lost)!")
return False
def check_private_ip_active(public_ip: str, private_ips: list):
result = dict()
result["active"] = []
result["inactive"] = []
for private_ip in private_ips:
try:
run_cmd_on_server(
"ping -c 1 " + private_ip + " > /dev/null 2>&1", public_ip
)
print(f"INFO: Ping shows {private_ip} is in use (packets received)!")
result["active"].append(private_ip)
except:
print(f"INFO: Ping shows {private_ip} is NOT in use (packets lost)!")
result["inactive"].append(private_ip)
return result
def create_kolla_config_files(data: dict):
with open("write_kolla_configs.sh", "w") as f:
f.write("#!/bin/bash")
f.write("\n\n")
f.write("set -euxo pipefail")
f.write("\n\n")
for file_location, file_contents in data.items():
directory = Path(file_location).parent
f.write(f"mkdir -p {directory}/ && touch {file_location}\n")
f.write(f"cat >> {file_location} << __EOF__\n")
f.write(f"{file_contents}\n")
f.write("__EOF__\n")
def create_multinode(input_dictionary, optional_variables):
control_items = list(islice(input_dictionary.items(), 3))
monitor_item = list(islice(input_dictionary.items(), 1))
control_labels = ["control", "network"]
secondary_labels = ["storage", "compute"]
monitor_label = ["monitor"]
multinode = {}
for label in control_labels:
multinode[label] = []
for i, value in enumerate(control_items):
multinode[label].append(i)
multinode[label][i] = {}
multinode[label][i]["public"] = value[1]["public"]
multinode[label][i]["private"] = value[1]["internal"]
multinode[label][i]["data"] = value[1]["data"]
for label in secondary_labels:
multinode[label] = []
for i, (k, v) in enumerate(input_dictionary.items()):
multinode[label].append(i)
multinode[label][i] = {}
multinode[label][i]["public"] = v["public"]
multinode[label][i]["private"] = v["internal"]
multinode[label][i]["data"] = v["data"]
for label in monitor_label:
multinode[label] = []
for i, (k, v) in enumerate(monitor_item):
multinode[label].append(i)
multinode[label][i] = {}
multinode[label][i]["public"] = v["public"]
multinode[label][i]["private"] = v["internal"]
multinode[label][i]["data"] = v["data"]
multinode["variables"] = {}
optional_variables = dict(
[
(x.split(":")[0].strip(), x.split(":")[1].strip("' "))
for x in optional_variables.strip("{}").split(",")
]
)
multinode["variables"].update(optional_variables)
return multinode
def create_new_ssh_key():
cleanup_cmd = "rm -f deploy_id_rsa"
run_cmd(cleanup_cmd)
cleanup_cmd = "rm -f deploy_id_rsa.pub"
run_cmd(cleanup_cmd)
create_key_cmd = "ssh-keygen -q -t rsa -N '' -f ./deploy_id_rsa"
run_cmd(create_key_cmd)
with open("deploy_id_rsa", "r") as f:
ssh_priv_key = f.read()
with open("deploy_id_rsa.pub", "r") as f:
ssh_public_key = f.read()
cleanup_cmd = "rm -f deploy_id_rsa"
run_cmd(cleanup_cmd)
cleanup_cmd = "rm -f deploy_id_rsa.pub"
run_cmd(cleanup_cmd)
return ssh_priv_key, ssh_public_key
def check_required_keys_not_null(required_keys, input_dictionary):
for key in required_keys:
if (key in input_dictionary) and (input_dictionary[key] != ""):
return True
raise Value_Required_to_Proceed(key)
def is_vm_pool_enabled(pool_start, pool_end):
return bool(pool_start != pool_end)
class Value_Required_to_Proceed(ValueError):
pass