-
Notifications
You must be signed in to change notification settings - Fork 9
/
ideam.py
406 lines (333 loc) · 15.6 KB
/
ideam.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python2
import sys
import os
import time
VERSION = '0.0-1'
# No ideam.conf in /etc/ideam if during development
if os.path.exists("/etc/ideam/ideam.conf"):
sys.path.append("/usr/share/ideam")
os.chdir("/usr/share/ideam")
import modules.start as container_start
import modules.install as container_setup
import modules.setup as setup
import modules.generate_password as password
from datetime import datetime
from modules.utils import setup_logging
import argparse
import subprocess
from modules.utils import output_ok, output_error
import traceback
import json
class MyParser(argparse.ArgumentParser):
""" HACK: Display a help message than just a failure message, if user types wrong arguments. """
def error(self, message):
sys.stderr.write('error: %s\n\n' % message)
self.print_help()
sys.exit(2)
def install(arguments):
""" Installs docker images and containers."""
setup_logging(log_file=arguments.log_file)
if arguments.limit:
container_setup.limit_install(str(arguments.limit).split(","))
else:
container_setup.check_dependencies(log_file=arguments.log_file)
container_setup.stop_containers(["apigateway","broker","ldapd","catalogue","videoserver","webserver","elasticsearch","konga"],log_file=arguments.log_file)
container_setup.remove_containers(["apigateway","broker","ldapd","catalogue","videoserver","webserver","elasticsearch","konga"],log_file=arguments.log_file)
container_setup.remove_volumes(["apigateway","broker","cat","elk","ldapd","webserver"],log_file=arguments.log_file)
password.set_passwords(arguments.config_file)
container_setup.docker_setup(log_file=arguments.log_file,config_path=arguments.config_file)
setup.initial_setup(log_file=arguments.log_file)
password.update_passwords(arguments.config_file)
setup.initial_setup_cleanup(log_file=arguments.log_file)
# cmd = "sh /setup/setup_database.sh databasequeue"
# initial_setup.setup_database(cmd, success_msg="Created admin user ",
# failure_msg="Creation of admin user failed.",
# log_file=arguments.log_file,
# exit_on_fail=True)
def start(arguments):
""" Starts all docker containers. """
setup_logging(log_file=arguments.log_file)
container_start.start_containers(["apigateway","broker","ldapd","elasticsearch","videoserver","webserver","catalogue","konga"],arguments.log_file)
container_start.start_volumes(["apigateway","broker","cat","elk","ldapd","webserver"],log_file=arguments.log_file)
if arguments.limit:
container_start.start_services(str(arguments.limit).split(","))
else:
container_start.start_services(["apigateway","broker","ldapd","elasticsearch","videoserver","webserver","catalogue"])
def restart(arguments):
""" Stops and starts all docker containers. """
if arguments.limit:
container_setup.stop_containers([arguments.limit],log_file=arguments.log_file)
container_start.start_services(str(arguments.limit).split(","))
else:
container_setup.stop_containers(["apigateway","broker","ldapd","elasticsearch","videoserver","webserver","catalogue"],log_file=arguments.log_file) # Stops all containers
container_start.start_services(["apigateway","broker","ldapd","elasticsearch","videoserver","webserver","catalogue"])
def str2bool(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 test(arguments):
cmd = "./tests/create_entity.sh testdevice1"
testdevice1_key = ""
try:
process = subprocess.check_output(cmd, shell=True)
register = json.loads(process)
testdevice1_key = register["apiKey"]
output_ok("REGISTER API: Created entity testdevice1. API KEY is " + testdevice1_key)
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/create_entity.sh testdevice2"
testdevice2_key = ""
try:
process = subprocess.check_output(cmd, shell=True)
register = json.loads(process)
testdevice2_key = register["apiKey"]
output_ok("REGISTER API: Created entity testdevice2. API KEY is " + testdevice2_key)
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/follow.sh "+ testdevice2_key + " testdevice2 testdevice1"
try:
process = subprocess.check_output(cmd, shell=True)
if "200 OK" in process:
output_ok("FOLLOW API: testdevice2 made a follow request to testdevice1")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
time.sleep(1)
cmd = "./tests/subscribe.sh testdevice1.follow " + testdevice1_key
try:
process = subprocess.check_output(cmd, shell=True)
subscribe = json.loads(process)
if subscribe[0]["data"]["requestor"] == "testdevice2" and subscribe[0]["data"]["permission"] == "read" :
output_ok("FOLLOW API: testdevice1 has recieved the follow request")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/share.sh " + testdevice1_key + " testdevice1 testdevice2"
try:
process = subprocess.check_output(cmd, shell=True)
if "200 OK" in process:
output_ok("SHARE API: testdevice1 authorised a share request to testdevice2")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
time.sleep(1)
cmd = "./tests/subscribe.sh testdevice2.notify " + testdevice2_key
try:
process = subprocess.check_output(cmd, shell=True)
subscribe = json.loads(process)
if "Approved" in json.dumps(subscribe):
output_ok("SHARE API: testdevice2 has recieved the share approval")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/bind.sh testdevice2 testdevice1.protected " + testdevice2_key
try:
process = subprocess.check_output(cmd, shell=True)
if "200 OK" in process:
output_ok("BIND API: testdevice2 has bound its queue to testdevice1.protected exchange")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/publish.sh testdevice1 " + testdevice1_key
try:
process = subprocess.check_output(cmd, shell=True)
if "202 Accepted" in process:
output_ok("PUBLISH API: Published message as testdevice1.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
time.sleep(1)
cmd = "./tests/subscribe.sh testdevice2 " + testdevice2_key
try:
process = subprocess.check_output(cmd, shell=True)
subscribe = json.loads(process)
if "testdata" in subscribe[0]["data"]["body"]:
output_ok("SUBSCRIBE API: testdevice2 has recieved the data published by testdevice1")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/unbind.sh testdevice2 testdevice1.protected " + testdevice2_key
try:
process = subprocess.check_output(cmd, shell=True)
if "200 OK" in process:
output_ok("UNBIND API: testdevice2 has unbound its queue from testdevice1.protected exchange")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/publish.sh testdevice1 " + testdevice1_key
try:
process = subprocess.check_output(cmd, shell=True)
if "202 Accepted" in process:
output_ok("PUBLISH API: Published message as testdevice1.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
time.sleep(1)
cmd = "./tests/subscribe.sh testdevice2 " + testdevice2_key
try:
process = subprocess.check_output(cmd, shell=True)
subscribe = json.loads(process)
if not subscribe:
output_ok("SUBSCRIBE API: testdevice2 has not recieved the data published by testdevice1")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/catalogue.sh testdevice1"
try:
process = subprocess.check_output(cmd, shell=True)
if "testdevice1" in process:
output_ok("CATALOGUE API: Device testdevice1 found in catalogue.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/deregister.sh testdevice1"
try:
process = subprocess.check_output(cmd, shell=True)
if "success" in process:
output_ok("DEREGISTER API: Device testdevice1 removed.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "sh tests/deregister.sh testdevice2"
try:
process = subprocess.check_output(cmd, shell=True)
if "success" in process:
output_ok("DEREGISTER API: Device testdevice2 removed.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
cmd = "./tests/catalogue.sh testdevice1"
try:
process = subprocess.check_output(cmd, shell=True)
if "apitestingdashboard" not in process:
output_ok("CATALOGUE API: Device testdevice1 not found in catalogue.")
else:
output_error(process,
error_message=traceback.format_exc())
except:
output_error(process,
error_message=traceback.format_exc())
exit()
def remove(arguments):
subprocess.check_output("find {} -mindepth 3 -delete".format(arguments.rm_data_path), shell=True)
if __name__ == '__main__':
default_log_file = "/tmp/" + datetime.now().strftime("ideam-%Y-%m-%d-%H-%M.log")
parser = MyParser()
subparsers = parser.add_subparsers(dest='command')
# install command
install_parser = subparsers.add_parser('install',
description="Installs all the required docker containers for the middleware",
help='Fresh installation of all the docker containers for the middleware')
install_parser.add_argument("-l", "--limit",
help="Limits the ansible installation to the servers mentioned. A comma separated list"
" of servers like --limit apigateway,broker",
required=False,
default="")
# This requires sudo permission and further installation happens as root which is not desired.
# TODO: Delete data directories (which require root privileges) and let installation be run as normal user.
# install_parser.add_argument("-r", "--remove", type=str2bool, nargs='?', const=True,
# help="Removes all the previous contents in the data directory. "
# "Passing -r will delete the data directories and if not then"
# " the data wont be deleted. Data directories are mentioned in the"
# " /etc/ideam/ideam.conf file.")
# install_parser.add_argument("-d", "--rm-data-path",
# help="Specify data directory in this argument if its not in /var/ideam/data. "
# "Default data directories are mentioned in the /etc/ideam/ideam.conf file.",
# default="/var/ideam/data")
install_parser.add_argument("-f", "--config-file",
help="Path to the conf file. See /etc/ideam/ideam.conf for an example.",
default="/etc/ideam/ideam.conf")
install_parser.add_argument("--log-file", help="Path to log file",
default=default_log_file)
# start command
start_parser = subparsers.add_parser('start', help='Start all the docker containers in the middleware')
start_parser.add_argument("-l",
"--limit",
help="Limits the ansible installation to the servers mentioned. A comma separated list"
" of servers like --limit apigateway,broker",
required=False,
default="")
start_parser.add_argument("--log-file", help="Path to log file",
default=default_log_file)
start_parser.add_argument("--with-idps",help="Include IDPS installation after starting IDEAM or any of its components", action="store_true")
# restart command
restart_parser = subparsers.add_parser('restart', help='Restart all the docker containers in the middleware')
restart_parser.add_argument("--log-file", help="Path to log file",
default=default_log_file)
# remove data command
remove_parser = subparsers.add_parser('rmdata', help='Remove all contents in the data directory')
remove_parser.add_argument("-d", "--rm-data-path",
help="Path to data directory. Installation using deb file will have /var/ideam/data as"
"directory. See /etc/ideam/ideam.conf for an details on data directory.",
default="/var/ideam/data")
test_parser = subparsers.add_parser('test', help='Test all API endpoints')
args = parser.parse_args()
if args.command == "install":
install(args)
elif args.command == "start":
start(args)
elif args.command == "restart":
start(args)
elif args.command == "rmdata":
remove(args)
elif args.command == "test":
test(args)