forked from Spirent-Terraform-Modules/terraform-vsphere-aion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-aion.py
328 lines (277 loc) · 10.9 KB
/
setup-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
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
import requests
import json
import argparse
import time
import logging
import sys
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=True):
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), resp.content))
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 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="Cluser/Node IP/host", type=str,
required=True)
parser.add_argument("--cluster_name", help="Node Name", type=str,
default="")
parser.add_argument("--node_name", help="Node Name", type=str,
default="")
parser.add_argument("--admin_first_name", help="Admin First Name", type=str,
default="")
parser.add_argument("--admin_last_name", help="Admin Last Name", type=str,
default="")
parser.add_argument("--admin_email", help="Admin Email", type=str,
default="")
parser.add_argument("--admin_password", help="Admin Email", type=str,
required=True)
parser.add_argument("--org_id", help="Organization ID", type=str,
default="")
parser.add_argument("--org_domains", help="Organization Domains", type=csv_list,
default="")
parser.add_argument("--org_subdomain", help="Organization Subdomain", type=str,
default="")
parser.add_argument("--metrics_opt_out", help="Metrics Opt Out", type=str2bool,
default=False)
parser.add_argument("--http_enabled", help="HTTP Enabled", type=str2bool,
default=False)
parser.add_argument("--local_admin_password", help="HTTP Enabled", type=str,
default="")
parser.add_argument("--node_storage_provider", help="Node Storage Provider", type=str,
default="local")
parser.add_argument("--node_storage_remote_uri", help="Node Storage Remote URL", type=str,
default="")
parser.add_argument("--wait_timeout", help="Time in seconds to wait for platform initialization", type=str,
default=900)
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 Exceoption("admin password must be specified")
return args
def get_server_init_data(c, org, user_info):
# Config Auto Fill
if not c.get("org_id"):
c["org_id"] = org["id"]
if not c.get("org_name"):
c["org_name"] = org["name"]
if not c.get("org_domains"):
c["org_domains"] = org["domains"]
if not c.get("org_subdomain"):
c["org_subdomain"] = org["subdomain"]
if not c.get("cluster_name"):
c["cluster_name"] = c["platform_addr"]
if not c.get("node_name"):
c["node_name"] = c["platform_addr"]
if not c.get("admin_firt_name"):
c["admin_first_name"] = user_info["first"]
if not c.get("admin_last_name"):
c["admin_last_name"] = user_info["last"]
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"]
email_settings = None
# Send Initialization
data = {
"cluster": {
"name": c["cluster_name"],
"admin": {
"first": c["admin_first_name"],
"last": c["admin_last_name"],
"password": c["admin_password"],
"email": c["admin_email"],
},
"organization": {
"id": c["org_id"],
"name": c["org_name"],
"subdomain": c["org_subdomain"],
"domains": c["org_domains"]
},
"email_settings": email_settings,
"metrics_opt_out": c["metrics_opt_out"],
"web_settings": {
"http": {
"enabled": c["http_enabled"],
}
}
},
"node": {
"name": c["node_name"],
"local_admin_password": c["local_admin_password"],
"storage": {
"provider": c["node_storage_provider"],
"remote_uri": c["node_storage_remote_uri"]
}
}
}
return 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))
if c["local_addr"]:
app_url = "http://" + c["local_addr"]
else:
app_url = "http://" + c["platform_addr"]
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))
hdrs = {
"Accept": "application/json",
"Content-Type": "application/json",
}
# Local Storage
data = {
"config": {
"provider": "local",
"remote_uri": ""
}
}
start_time = time.time()
wait_time = int(c["wait_timeout"])
# retry 1st app_url request until timeout or success
while True:
try:
local_storage = request(app_url + "/api/local/storage/test", headers=hdrs, data=data).json()
except Exception as e:
local_storage = None
if local_storage:
break
if (time.time() - start_time) > wait_time:
raise Exception("Failed to receive respone from %s" % app_url + "/api/local/storage/test")
time.sleep(5)
LOG.debug("retrying %s" % app_url + "/api/local/storage/test")
LOG.debug("localStorage: %s" % json.dumps(local_storage))
data = get_server_init_data(c, org_info, user_info)
LOG.debug("ServerFormingNewCluster: %s" % json.dumps(data))
r = request(app_url + "/api/local/initialization/server-forming-new-cluster", headers=hdrs, data=data)
completed = False
start_time = time.time()
wait_time = int(c["wait_timeout"])
if wait_time:
LOG.info("Waiting for AION platform initialization to complete...")
while True:
try:
r = request(app_url + "/api/local/initialization").json()
except Exception as e:
LOG.debug("installation status exception which may not be error: %s" % str(e))
r = None
if r:
LOG.debug("initialization status: %s\n" % json.dumps(r))
if r["initialized"]:
completed = True
break
if r.get("status") == "error":
raise Exception("failed to configure platform")
if (time.time() - start_time) > wait_time:
LOG.warning(
"platform initialization didn't complete in %d seconds. platform wait timed out." % wait_time)
break
time.sleep(5)
if not completed:
raise Exception("platform initialization did not complete")
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)
if __name__ == "__main__":
try:
main()
except Exception as e:
LOG.error('%s' % str(e))
LOG.debug('Error in setup-aion', exc_info=True)
sys.exit(str(e))
'''
python3 setup-aion.py --aion_url "https://spirent.spirentaion.com" --platform_addr "10.109.121.113"
--aion_user <user> --aion_password <password> --admin_password <password>
'''