-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-rest.py
More file actions
68 lines (51 loc) · 1.98 KB
/
run-rest.py
File metadata and controls
68 lines (51 loc) · 1.98 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
import yaml
import requests
import os
import datetime
import tarfile
# replace here
address = "http://localhost:9200"
user = "elastic"
password = "changeme"
now = datetime.datetime.now()
folder_name = (now.strftime("%Y%m%d-%H%M%S"))
# read this yml
# todo
# read it from the web if you have access to internet.
# https://github.com/elastic/support-diagnostics/blob/7.1.5/src/main/resources/elastic-rest.yml
with open('/Users/surfer/elastic/source/es_rest_python/elastic-rest.yml') as file:
obj = yaml.safe_load(file)
for k, v in obj.items():
extension = ".json"
if 'extension' in v.keys() and v['extension'] == '.txt':
extension = ".txt"
# create file name
filename = k + extension
if type(v) is dict:
for k2, v2 in v.items():
# create cat directory
if 'subdir' in v.keys():
try:
os.makedirs(folder_name + "/" + v['subdir'])
except OSError as error:
pass
if k2 == "versions":
list_of_dict_values = list(v2.values())
response = requests.get(address + list_of_dict_values[-1], auth=(user, password))
# print(address + list_of_dict_values[-1])
list_of_dict_values[-1]
if 'subdir' in v.keys():
path = folder_name + "/" + v['subdir'] + "/" + filename
else:
path = folder_name + "/" + filename
# print("echo " + '"' + address + list_of_dict_values[-1] + '"')
# print("curl " + '"' + address + list_of_dict_values[-1] + '"' + " -o " + path)
with open(path, 'w') as f:
f.write(response.text)
try:
archive_name = folder_name + ".tar.gz"
tar = tarfile.open(archive_name, "w:gz")
tar.add(folder_name)
tar.close()
except IOError as error:
pass