-
Notifications
You must be signed in to change notification settings - Fork 4
/
release-aion.py
221 lines (180 loc) · 7.13 KB
/
release-aion.py
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
import requests
import json
import argparse
import time
import logging
import sys
import ast
HDRS = {
"Accept": "application/json",
"Content-Type": "application/json"
}
LOG = logging.getLogger("setup_aion")
def request(url, data=None, method=None, headers=HDRS, params={}, allow_redirects=True,
files=None, stream=False, verify=False):
if not method:
if data:
method = 'POST'
else:
method = 'GET'
if isinstance(data, dict) or isinstance(data, list):
data = json.dumps(data)
with requests.Session() as s:
req = requests.Request(method, url, headers=headers, params=params, data=data, files=files)
prepreq = s.prepare_request(req)
resp = s.send(prepreq, timeout=15, allow_redirects=allow_redirects, stream=stream, verify=verify)
if not resp.ok:
raise Exception("request error: url:%s, code:%s, data:%s" % (url, str(resp.status_code), str(resp.text)))
return resp
def csv_list(vstr, sep=','):
''' Convert a string of comma separated values to floats
@returns iterable of floats
'''
values = []
for v in vstr.split(sep):
if v:
values.append(v)
return values
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_app_url(c):
if c["local_addr"]:
return "http://" + c["local_addr"]
else:
return "http://" + c["platform_addr"]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--aion_url",
help="AION URL. An example URL would be https://example.spirentaion.com", type=str,
default="", required=True)
parser.add_argument("--aion_user", help="AION user", type=str,
required=True)
parser.add_argument("--aion_password", help="AION password", type=str,
required=True)
parser.add_argument("--local_addr",
help="Local API IP/host. Will use platform_addr if not specified.",
type=str, default="")
parser.add_argument("--platform_addr", help="Cluster/Node IP/host", type=str,
required=True)
parser.add_argument("--admin_email", help="Admin Email", type=str,
default="")
parser.add_argument("--admin_password", help="Admin Password", type=str,
required=True)
parser.add_argument("--local_admin_password", help="Local Admin Password", type=str,
default="")
parser.add_argument("-v", "--verbose", help="Verbose logging", type=str2bool,
default=False)
parser.add_argument("--log_file", help="Log file for output. stdout when not set", type=str, default="")
args = parser.parse_args()
if args.admin_password == "":
raise Exception("admin password must be specified")
return args
def uninstall_entitlements(aion_url, access_token, app_url, app_token, platform_addr):
hdrs = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + app_token,
}
aion_hdrs = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + access_token,
}
local_entitlements = request(app_url + "/api/lic/entitlements?" +
"workspace_id=all&entdetail=summary&show_hardware=false&" +
"show_software=true&include_set=8",
headers=hdrs).json()
LOG.debug("local entitlements: %s" % json.dumps(local_entitlements, indent=4))
cluster = request(app_url + "/api/cluster/clusters/my",
headers=hdrs).json()
host_id = cluster["id"]
for e in local_entitlements:
disable = request(app_url + "/api/lic/entitlements/" + e["id"] + "/disable",
headers=hdrs, data=e).json()
LOG.debug("disable entitlements: %s" % json.dumps(disable))
details = ast.literal_eval(e["details"])
data = {
"host_id": host_id,
"disabled_code": disable["disabled_code"]
}
request(aion_url + "/api/lic/entitlements/" + details["temeva_id"] + "/unhost",
headers=aion_hdrs, data=data)
def main():
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
LOG.addHandler(handler)
args = parse_args()
if args.verbose:
LOG.setLevel(logging.DEBUG)
else:
LOG.setLevel(logging.INFO)
if args.log_file:
log_handler = logging.FileHandler(args.log_file)
log_handler.setFormatter(formatter)
LOG.addHandler(log_handler)
c = args.__dict__
LOG.debug("Config: %s" % json.dumps(c))
aion_url = c["aion_url"]
org_info = request(aion_url + "/api/iam/organizations/default").json()
LOG.debug("org_info: %s" % json.dumps(org_info))
data = {
"grant_type": "password",
"username": c["aion_user"],
"password": c["aion_password"],
"scope": org_info["id"]
}
r = request(aion_url + "/api/iam/oauth2/token", data=data).json()
access_token = r["access_token"]
LOG.debug("access_token: %s" % access_token)
hdrs = {
"Accept": "application/json",
"Authorization": "Bearer " + access_token,
}
user_info = request(aion_url + "/api/iam/users/my", headers=hdrs).json()
LOG.debug("userInfo: %s" % json.dumps(user_info))
if not c.get("admin_email"):
c["admin_email"] = user_info["email"]
if not c.get("local_admin_password"):
c["local_admin_password"] = c["admin_password"]
app_url = get_app_url(c)
org_info = request(app_url + "/api/iam/organizations/default").json()
LOG.debug("org_info: %s" % json.dumps(org_info))
data = {
"grant_type": "password",
"username": c["admin_email"],
"password": c["admin_password"],
"scope": org_info["id"]
}
r = request(app_url + "/api/iam/oauth2/token", data=data).json()
app_token = r["access_token"]
hdrs = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + app_token,
}
data = {
"url": aion_url,
"username": c["aion_user"],
"password": c["aion_password"]
}
request(app_url + "/api/cluster/settings/temeva/login", headers=hdrs, data=data)
uninstall_entitlements(aion_url, access_token, app_url, app_token, c["platform_addr"])
if __name__ == "__main__":
try:
main()
except Exception as e:
LOG.error('%s' % str(e))
LOG.debug('Error in release-aion', exc_info=True)
sys.exit(str(e))
'''
python3 release-aion.py --aion_url "https://spirent.spirentaion.com" --platform_addr "10.109.121.113"
--aion_user <user> --aion_password <password> --admin_password <password>
'''