-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·418 lines (334 loc) · 14.3 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·418 lines (334 loc) · 14.3 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
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
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python
from __future__ import print_function
import errno
import json
import os
import re
import sys
from distutils.spawn import find_executable
# On Python 2.7, input() evaluates what the user types, and raw_input() simply returns it
# On Python 3, input() returns it, and there is no raw_input() function
try:
input = raw_input
except NameError:
pass
# On Python 2.7, failing to parse JSON throws a ValueError; on Python 3 it throws json.decoder.JSONDecodeError
try:
from json.decoder import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError
def generate_jwt_secret():
try:
import secrets
return secrets.token_urlsafe()
except ImportError:
import random
import string
letters_and_digits = string.ascii_letters + string.digits
return ''.join(random.choice(letters_and_digits) for i in range(10))
OPENVIDU_SECRET = "MY_SECRET"
AM_STORE_NAME = "default"
S3_SERVER = "ovehub-ove-asset-storage"
S3_PORT = "9000"
S3_ACCESS_KEY = "MINIO_ACCESS_KEY"
S3_SECRET_KEY = "MINIO_SECRET_KEY"
MONGO_HOST = "ovehub-ove-asset-mongo"
MONGO_PORT = 27017
MONGO_USER = "user"
MONGO_PASSWORD = "password"
MONGO_DB = "db"
MONGO_COLLECTION = "auth"
MONGO_AUTH_MECHANISM = "SCRAM-SHA-256"
JWT_SECRET = generate_jwt_secret()
def bundle_dir():
return sys._MEIPASS if getattr(sys, 'frozen', False) else os.path.dirname(os.path.abspath(__file__))
def check_dependencies():
if find_executable("docker") is None:
print("WARN: docker is required to run the system")
print("For install instructions, visit https://docs.docker.com/install/")
if find_executable("docker-compose") is None:
print("WARN: docker-compose is required to run the generated configs")
print("For install instructions, visit https://docs.docker.com/compose/install/")
def get_default_ip():
try:
from netifaces import ifaddresses, gateways, AF_INET
except:
print("WARN: Failed to import netifaces, so cannot determine default ip")
return ""
try:
default_iface = gateways()['default'][AF_INET][1]
addr = ifaddresses(default_iface)
return addr[AF_INET][0]['addr']
except:
print("WARN: Failed to determine the default ip")
return ""
def read_var(message, default_value, note=None):
if note is not None:
print("NOTE:", note)
result = input("%s (default: %s): " % (message, default_value))
result = result.strip()
result = result if len(result) > 0 else default_value
print("\tINFO: %s = %s" % (message, result))
return result
def read_flag(message, default_value, note=None):
if note is not None:
print("NOTE:", note)
result = input("%s (default: %s)? " % (message, default_value))
result = result.strip()
result = result if len(result) > 0 else default_value
result = True if result.lower() in ('yes', 'true', 't', 'y', 'ye', '1') else False
print("\tINFO: %s = %s" % (message, 'yes' if result else 'no'))
return result
def exit_msg():
print("")
input("Press Enter to exit")
def intro_msg(params):
print("This setup script will generate docker-compose scripts using the following versions:")
print("\tNOTE: If these versions differ from the latest available, please use the latest setup generator.")
print("")
print("\t OVE Version: ", params['OVE_VERSION'])
print("\t OVE Apps Version: ", params['OVE_APPS_VERSION'])
print("\t OVE Services Version: ", params['OVE_SERVICES_VERSION'])
print("\t OVE UI Version: ", params['OVE_UI_VERSION'])
print("\t Tuoris Version: ", params['TUORIS_VERSION'])
print("\t OpenVidu Version: ", params['OPENVIDU_VERSION'])
if params['ASSET_MANAGER_VERSION']:
print("\t Asset Manager Version: ", params['ASSET_MANAGER_VERSION'])
print("")
def outro_msg(proceed, params):
print("")
print("Thank you for using this setup tool!")
if proceed:
print("")
print("---")
print("Your docker-compose configs have been generated. You can execute them directly by using:")
print("")
print("\t docker-compose -f docker-compose.setup.ove.yml up -d")
if params['ASSET_MANAGER_VERSION']:
print("\t docker-compose -f docker-compose.setup.am.yml up -d")
print("\t NOTE: If you copy the compose files to another machine, please also copy the generated files in the 'config/' directory")
print("")
print("NOTE: -d flag runs the docker commands in detached mode")
print("---")
def load_version_numbers(release):
try:
bundle_wd = bundle_dir()
with open(os.path.join(bundle_wd, "versions.json")) as fp:
versions = json.load(fp)
return versions['releases'][release]
except KeyError:
return None
except JSONDecodeError:
print("ERROR: Unable to parse versions.json file")
exit_msg()
sys.exit()
except:
print("Error: Unable to read versions.json file")
exit_msg()
sys.exit()
def get_stable_version():
try:
bundle_wd = bundle_dir()
with open(os.path.join(bundle_wd, "versions.json")) as fp:
versions = json.load(fp)
return versions['stable']
except JSONDecodeError:
print("ERROR: Unable to parse versions.json file")
exit_msg()
sys.exit()
except:
print("Error: Unable to read versions.json file")
exit_msg()
sys.exit()
def read_script_params():
print("")
print("General settings")
print("")
ip = ""
while not ip:
ip = read_var("Machine hostname or ip address", get_default_ip())
stable_version = get_stable_version()
use_stable = read_flag("Use 'stable' version", "yes")
if use_stable:
versions = load_version_numbers(stable_version)
else:
use_latest = read_flag("Use 'latest' version", "yes")
if use_latest:
versions = load_version_numbers('latest')
else:
version = ""
while load_version_numbers(version) is None:
version = read_var("Version number (x.y.z)", stable_version)
versions = load_version_numbers(version)
ove_version = versions['ove']
ove_apps_version = versions['ove-apps']
ove_services_version = versions['ove-services']
ove_ui_version = versions['ove-ui']
tuoris_version = versions['tuoris']
openvidu_version = versions['openvidu']
asset_manager_version = versions.get('asset-manager', None)
# defaults
am_store_name = None
s3_enabled = False
s3_server = None
s3_external_ip = None
s3_external_port = None
s3_port = None
s3_access_key = None
s3_secret_key = None
mongo_enabled = False
mongo_host = None
mongo_port = None
mongo_user = None
mongo_password = None
mongo_db = None
mongo_collection = None
mongo_auth_mechanism = None
jwt_secret = None
defaults = read_flag("Use default settings", "yes")
if defaults:
get_val = lambda prompt, default_val: default_val
get_flag = lambda prompt, default_val: default_val
else:
get_val = read_var
get_flag = read_flag
print("")
print("OVE setup")
print("")
openvidu_secret = get_val("OpenVidu Secret", OPENVIDU_SECRET)
if asset_manager_version:
print("")
print("OVE Asset Manager setup")
print("")
am_store_name = get_val("Asset Manager default store name", AM_STORE_NAME)
s3_enabled = get_flag("Add Minio instance (S3 store) to docker-compose file", "yes")
if s3_enabled:
s3_server = S3_SERVER
s3_port = S3_PORT
s3_external_ip = get_val("S3 external ip or hostname", ip)
s3_external_port = get_val("S3 external port", S3_PORT)
else:
s3_server = get_val("S3 server", ip)
s3_port = get_val("S3 server port", S3_PORT)
s3_external_ip = s3_server
s3_external_port = s3_port
s3_access_key = get_val("S3 Access key", S3_ACCESS_KEY)
s3_secret_key = get_val("S3 Secret key", S3_SECRET_KEY)
mongo_enabled = get_flag("Add MongoDB instance to docker-compose file", "yes")
if mongo_enabled:
mongo_host = MONGO_HOST
mongo_port = get_val("MongoDB external port", MONGO_PORT)
else:
mongo_host = get_val("MongoDB hostname", ip)
mongo_port = get_val("MongoDB port", MONGO_PORT)
mongo_user = get_val("MongoDB username", MONGO_USER)
mongo_password = get_val("MongoDB password", MONGO_PASSWORD)
mongo_db = get_val("MongoDB database name", MONGO_DB)
mongo_collection = get_val("MongoDB authentication collection name", MONGO_COLLECTION)
mongo_auth_mechanism = get_val("MongoDB auth mechanism", MONGO_AUTH_MECHANISM)
jwt_secret = get_val("Authentication JWT Key", JWT_SECRET)
return {
'PUBLIC_HOSTNAME': ip,
'OVE_VERSION': ove_version,
'OVE_APPS_VERSION': ove_apps_version,
'OVE_SERVICES_VERSION': ove_services_version,
'OVE_UI_VERSION': ove_ui_version,
'TUORIS_VERSION': tuoris_version,
'OPENVIDU_VERSION': openvidu_version,
'OPENVIDU_SECRET': openvidu_secret,
'ASSET_MANAGER_VERSION': asset_manager_version,
'AM_STORE_NAME': am_store_name,
'S3_ENABLED': s3_enabled,
'S3_SERVER': s3_server,
'S3_PORT': s3_port,
'S3_EXT_PORT': s3_external_port,
'S3_EXT_IP': s3_external_ip,
'S3_ACCESS_KEY': s3_access_key,
'S3_SECRET_KEY': s3_secret_key,
'MONGO_ENABLED': mongo_enabled,
'MONGO_HOST': mongo_host,
'MONGO_PORT': mongo_port,
'MONGO_USER': mongo_user,
'MONGO_PASSWORD': mongo_password,
'MONGO_DB': mongo_db,
'MONGO_COLLECTION': mongo_collection,
'MONGO_AUTH_MECHANISM': mongo_auth_mechanism,
'JWT_SECRET': jwt_secret
}
def generate_scripts(input_filename, output_filename, params):
# todo; some file validation here
with open(input_filename, mode="r") as fin:
content = fin.read()
for k, v in params.items():
content = content.replace("${%s}" % k, str(v))
content = content.replace("$%s" % k, str(v))
if not params['S3_ENABLED']:
content = re.sub(pattern=r"[ ]*## BEGIN S3 CONFIG ##.*## END S3 CONFIG ##[ ]*\n?",
repl="", string=content, flags=re.MULTILINE | re.DOTALL)
content = re.sub(pattern=r"[ ]*## BEGIN S3 STORAGE ##.*## END S3 STORAGE ##[ ]*\n?",
repl="", string=content, flags=re.MULTILINE | re.DOTALL)
if not params['MONGO_ENABLED']:
content = re.sub(pattern=r"[ ]*## BEGIN MONGO CONFIG ##.*## END MONGO CONFIG ##[ ]*\n?",
repl="", string=content, flags=re.MULTILINE | re.DOTALL)
content = re.sub(pattern=r"[ ]*## BEGIN MONGO STORAGE ##.*## END MONGO STORAGE ##[ ]*\n?",
repl="", string=content, flags=re.MULTILINE | re.DOTALL)
if not (params['MONGO_ENABLED'] or params['S3_ENABLED']):
content = re.sub(pattern=r"[ ]*## BEGIN STORAGE ##.*## END STORAGE ##[ ]*\n?",
repl="", string=content, flags=re.MULTILINE | re.DOTALL)
# cleanup "special comments"
content = re.sub(pattern=r"[ ]*##.*##[ ]*\n?", repl="", string=content)
output_path = os.path.dirname(output_filename)
try:
os.makedirs(output_path)
except OSError as ex:
if ex.errno == errno.EEXIST and os.path.isdir(output_path):
pass
else:
raise
with open(output_filename, mode="w") as fout:
fout.write(content)
print("Generated new config file in ", output_filename)
def main():
check_dependencies()
bundle_wd = bundle_dir()
params = read_script_params()
intro_msg(params)
proceed = read_flag("Accept and proceed", "yes")
print("")
if proceed:
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "docker-compose.ove.yml"),
output_filename=os.path.join(os.getcwd(), "docker-compose.setup.ove.yml"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "Spaces.json"),
output_filename=os.path.join(os.getcwd(), "config", "Spaces.json"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "default.conf"),
output_filename=os.path.join(os.getcwd(), "config", "default.conf"))
if params['ASSET_MANAGER_VERSION']:
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "docker-compose.am.yml"),
output_filename=os.path.join(os.getcwd(), "docker-compose.setup.am.yml"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "credentials.template.json"),
output_filename=os.path.join(os.getcwd(), "config", "credentials.json"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "init_mongo.js"),
output_filename=os.path.join(os.getcwd(), "config", "init_mongo.js"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "auth.template.json"),
output_filename=os.path.join(os.getcwd(), "config", "auth.json"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "whitelist.template.json"),
output_filename=os.path.join(os.getcwd(), "config", "whitelist.json"))
generate_scripts(params=params,
input_filename=os.path.join(bundle_wd, "templates", "config", "worker.template.json"),
output_filename=os.path.join(os.getcwd(), "config", "worker.json"))
outro_msg(proceed, params)
exit_msg()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
# ignore
pass