-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_data.py
More file actions
331 lines (299 loc) · 13.1 KB
/
program_data.py
File metadata and controls
331 lines (299 loc) · 13.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import datetime
import getpass
import json
import os
import requests
import shutil
import subprocess
import tarfile
from get_token import GetToken
from log_setup import Logging, MLog
"""
NetApp / SolidFire
CPE
mnode support utility
"""
"""
global vaiarbles and functions
"""
# set up logging
logmsg = Logging.logmsg()
class ProgramData():
def __init__(self, args):
""" Very frequently used values """
self.util_version = "4.0"
self.base_url = "https://127.0.0.1"
self.debug = False
self.download_dir = "/data/bundle/share"
self.header_read = {}
self.header_write = {}
self.log_dir = "/var/log/"
self.logs_svc_container = ""
self.local_bundle = ""
self.mvip_user = args.stuser
self.mvip_pw = args.stpw
self.parent_id = ""
self.support_dir = "/var/log/mnode-support/"
self.target_cluster = ""
self.target_cluster_admin = ""
self.target_cluster_passwd = ""
self.token_life = 0
self.timeout = args.timeout
self.about = self._about()
self.auth_mvip = self._auth_mvip()
self.download_url = f'https://{self.about["mnode_host_ip"]}/logs/1/bundle'
def _about(self):
""" Get assets /mnode/#/about/routes.v1.about.get
Populate the instance ABOUT
"""
header = {"Accept":"*/*"}
url = (f'{self.base_url}/mnode/1/about')
response = requests.get(url, headers=header, data={}, verify=False, timeout=self.timeout)
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def _auth_mvip(self):
authmvip = self.about["token_url"].split('/')
return authmvip[2]
class Common():
def test_json_loads(json_string):
try:
json_return = json.loads(json_string)
return json_return
except ValueError as error:
logmsg.debug(json_string)
logmsg.info(f'{error}\nSee /var/log/mnode-support-util.log for details')
def select_target_cluster(repo):
""" List all storage clusters in inventory and select the target cluster
"""
logmsg.info("\nAvailable clusters:")
clusterlist = {}
for cluster in repo.assets[0]["storage"]:
if cluster["host_name"]:
logmsg.info(f'+ {cluster["host_name"]}')
clusterlist[(cluster["host_name"])] = cluster["id"]
else:
logmsg.info(f'+ {cluster["ip"]}')
clusterlist[(cluster["ip"])] = cluster["id"]
while True:
userinput = input("Enter the target cluster from the list: ").rstrip()
if userinput in clusterlist:
break
return clusterlist[userinput]
def file_download(repo, content, filename):
download_file = f'{repo.download_dir}/{filename}'
download_url = f'{repo.download_url}/{filename}'
try:
with open(download_file, "w") as outfile:
print(content, file=outfile)
logmsg.info(f'Download link: {download_url}')
except FileNotFoundError as error:
logmsg.debug(error)
def copy_file_to_download(repo, filename):
try:
output = subprocess.getoutput(f'docker cp {filename} {repo.logs_svc_container}:{repo.download_dir}')
logmsg.debug(f'copy_file_to_download: docker cp {output}')
except subprocess.CalledProcessError as error:
logmsg.info(f'subprocess error: {error}')
def copy_file_from_download(repo, filename):
base_filename = os.path.basename(filename)
try:
output = subprocess.getoutput(f'docker cp {repo.logs_svc_container}:{repo.download_dir}/{base_filename} /tmp')
logmsg.debug(f'copy_file_from_download: docker cp {output}')
except subprocess.CalledProcessError as error:
logmsg.info(f'subprocess error: {error}')
def cleanup_download_dir(repo):
try:
output = subprocess.getoutput(f'docker exec {repo.logs_svc_container} rm -rf {repo.download_dir}/*')
logmsg.debug(f'cleanup_download_dir: docker exec rm {output}')
except subprocess.CalledProcessError as error:
logmsg.info(f'subprocess error: {error}')
def make_download_tar(repo, bundle_type, file_list):
date_time = datetime.datetime.now()
time_stamp = date_time.strftime("%d-%b-%Y-%H.%M.%S")
tar_file_name = f'{bundle_type}-{time_stamp}.tar'
dest_tar_file = f'{repo.download_dir}/{tar_file_name}'
try:
output = subprocess.getoutput(f'docker exec {repo.logs_svc_container} tar cf {dest_tar_file} {repo.download_dir}/{file_list[0]} {repo.download_dir}/{file_list[1]}')
if 'tar: removing leading' in output:
return tar_file_name
else:
logmsg.info(f'tar failed: {output}')
except subprocess.CalledProcessError as error:
logmsg.info(f'subprocess error: {error}')
class PDApi():
""" routine api calls
"""
# disable ssl warnings so the log doesn't fill up
requests.packages.urllib3.disable_warnings()
def _send_get(repo, url, debug):
""" generic requests.get
"""
GetToken(repo)
try:
if debug == True:
logmsg.debug(f'Sending GET {url}')
response = requests.get(url, headers=repo.header_read, data={}, verify=False)
if response.status_code < 299:
if debug == True:
logmsg.debug(f'{response.status_code}:') # {response.text}')
return response
else:
logmsg.info(f'Failed return {response.status_code} See See /var/log/mnode-support-util.log for details.')
logmsg.debug(str(response.content))
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def _send_put(repo, url, payload):
""" generic requests.put
"""
GetToken(repo)
try:
logmsg.debug(f'Sending PUT {url}')
response = requests.put(url, headers=repo.header_write, data=json.dumps(payload), verify=False)
if response.status_code < 299:
logmsg.debug(f'{response.status_code}: {response.text}')
return response
else:
logmsg.info(f'Failed return {response.status_code} See See /var/log/mnode-support-util.log for details.')
logmsg.debug(str(response.content))
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def _send_post(repo, url, payload):
""" generic requests.post
"""
GetToken(repo)
try:
logmsg.debug(f'Sending POST {url}')
response = requests.post(url, headers=repo.header_write, data=json.dumps(payload), verify=False)
logmsg.debug(f'{response.status_code} {response.text}')
return response
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def _send_delete(repo, url):
""" generic requests.delete
"""
GetToken(repo)
try:
logmsg.debug(f'Sending DELETE {url}')
response = requests.delete(url, headers=repo.header_write, data={}, verify=False)
if response.status_code < 299:
logmsg.debug(f'{response.status_code}: {response.text}')
return response
else:
logmsg.info(f'Failed return {response.status_code} See See /var/log/mnode-support-util.log for details.')
logmsg.debug(str(response.content))
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def send_get_return_text(repo, url, debug):
""" send a GET return the text
"""
response = PDApi._send_get(repo, url, debug)
if response is not None:
return response.text
def send_get_return_json(repo, url, debug):
""" send a GET return the json
"""
response = PDApi._send_get(repo, url, debug)
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def send_get_return_status(repo, url, debug):
""" send a GET return the status code
"""
response = PDApi._send_get(repo, url, debug)
if response is not None:
return response.status_code
def send_put_return_json(repo, url, payload):
""" send a PUT return the json
"""
response = PDApi._send_put(repo, url, payload)
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def send_put_return_status(repo, url, payload):
""" send a PUT return the status code
"""
response = PDApi._send_put(repo, url, payload)
if response is not None:
return response.status_code
def send_put_return_json_nopayload(repo, url):
""" send a PUT return the json. No payload
"""
response = PDApi._send_put(repo, url, payload={})
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def send_post_return_json(repo, url, payload):
""" send a POST return the json
"""
response = PDApi._send_post(repo, url, payload)
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def send_post_return_status(repo, url, payload):
""" send a POST return the status code
"""
response = PDApi._send_post(repo, url, payload)
if response is not None:
return response.status_code
def send_delete_return_status(repo, url):
""" send a DELETE return the status code
"""
response = PDApi._send_delete(repo, url)
if response is not None:
return response.status_code
def send_delete_return_json(repo, url):
""" send a DELETE return the json
"""
response = PDApi._send_delete(repo, url)
if response is not None:
response_json = Common.test_json_loads(response.text)
return response_json
def check_cluster_creds(repo, mvip, host_name):
""" Ensure the creds specified with ('-su', '--stuser') ('-sp', '--stpw') work on the current cluster
Prompt for creds if the given creds fail
"""
url = f'https://{mvip}/json-rpc/12.0'
payload = "{\n\t\"method\": \"GetClusterInfo\",\n \"params\": {},\n \"id\": 1\n}"
try:
response = requests.post(url, auth=(repo.mvip_user, repo.mvip_pw), data=payload, verify=False)
stuser = repo.mvip_user
stpw = repo.mvip_pw
if response.status_code == 401:
while response.status_code == 401:
logmsg.info(f'The provided credentials failed on cluster {host_name}')
stuser = input(f'Enter admin userid for cluster {host_name} : ').rstrip()
stpw = getpass.getpass(prompt="Enter admin password: ")
response = requests.post(url, auth=(stuser, stpw), data=payload, verify=False)
return response.status_code, stuser, stpw
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def mip_send_get_return_json(creds, url, node_ip, payload):
""" MIP send GET return json
"""
url = f'https://{node_ip}:442/json-rpc/12.0/'
try:
logmsg.debug(f'Sending POST {url} {payload}')
response = requests.post(url, auth=(creds[1], creds[2]), data=payload, verify=False)
logmsg.debug(f'{response.status_code}: {response.text}')
if response.status_code > 299 and response.status_code != 409:
MLog.log_failed_return(response.status_code, response.text)
else:
response_json = Common.test_json_loads(response.text)
return response_json
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)
def mip_send_post_return_status(url, payload, creds):
""" MIP send POST return json
"""
try:
logmsg.debug(f'Sending POST {url} {payload}')
response = requests.post(url, auth=(creds[1], creds[2]), data=payload, verify=False)
logmsg.debug(f'{response.status_code}: {response.text}')
if response.status_code > 299 and response.status_code != 409:
MLog.log_failed_return(response.status_code, response.text)
else:
response_json = Common.test_json_loads(response.text)
return response_json
except requests.exceptions.RequestException as exception:
MLog.log_exception(exception)