forked from Ursinus-JJJKA/lower-wismer-ordering-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
79 lines (71 loc) · 3.6 KB
/
run.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
import argparse
import os.path
import platform
import secrets
import socket
import subprocess
from urllib.parse import quote_plus
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# 10.254.254.254 should be connecting to nothing
s.connect(('10.254.254.254', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
WEBSERVER_DEFAULTS = {"DATABASE_URL":"mongodb+srv://<username>:<password>@<atlas_url>.mongodb.net/db_name",
"WEBSERVER_PORT":"8000",
"JWT_SECRET_KEY":secrets.token_hex(32),
"JWT_ALGORITHM":"HS256",
"JWT_LIFETIME_MINUTES":30,
"KITCHEN_JWT_LIFETIME_MINUTES":60*12}
MONGO_DEFAULTS = {"MONGODB_USERNAME":secrets.token_urlsafe(16),
"MONGODB_PASSWORD":secrets.token_urlsafe(16),
"MONGODB_PORT":"27017",
"MONGODB_DB":"food_db"}
parser = argparse.ArgumentParser(description="Handle .env config and run the webserver")
parser.add_argument('--atlas', action="store_true")
parser.add_argument('--new', '--clear-env', action="store_true")
parser.add_argument('-y', '--defaults', action="store_false")
if __name__ == "__main__":
args = parser.parse_args()
if not os.path.exists("lwo.key") or not os.path.exists("lwo.crt"):
if platform.system() == "Windows":
raise RuntimeError("TLS cert or keyfile is missing and cannot be autogenerated on Windows")
else:
subprocess.run(['openssl', 'req', '-x509', '-newkey', 'rsa:4096', '-sha256', '-days', '365', '-nodes',
'-keyout', 'lwo.key', '-out', 'lwo.crt', '-subj', '/CN=LWO_AutoGenerated', '-addext', f'subjectAltName=IP:{get_ip()}'])
if args.atlas:
if args.new or not os.path.exists(".env"):
# Generate data
for field,default in WEBSERVER_DEFAULTS.items():
res = (args.defaults and input(f"{field}={default}:").strip()) or default
WEBSERVER_DEFAULTS[field] = res
# Make .env file
with open(".env","w") as file:
file.write('\n'.join(f'{a}={b}' for a,b in WEBSERVER_DEFAULTS.items()))
# Run docker compose in daemon mode
subprocess.run(["docker", "compose", "--profile", "atlasdb", "up", "-d"])
else:
if args.new or not os.path.exists(".env"):
# Generate MONGO vars
for field,default in MONGO_DEFAULTS.items():
res = (args.defaults and input(f"{field}={default}:").strip()) or default
MONGO_DEFAULTS[field] = res
auth = f"{quote_plus(MONGO_DEFAULTS['MONGODB_USERNAME'])}:{quote_plus(MONGO_DEFAULTS['MONGODB_PASSWORD'])}@" if ('MONGODB_USERNAME' in MONGO_DEFAULTS and 'MONGODB_PASSWORD' in MONGO_DEFAULTS) else ""
WEBSERVER_DEFAULTS["DATABASE_URL"] = f"mongodb://{auth}lwo_db:{MONGO_DEFAULTS['MONGODB_PORT']}/{MONGO_DEFAULTS['MONGODB_DB']}?authSource=admin"
# Generate WEBSERVER vars
for field,default in WEBSERVER_DEFAULTS.items():
res = (args.defaults and input(f"{field}={default}:").strip()) or default
WEBSERVER_DEFAULTS[field] = res
# Make .env file
with open(".env","w") as file:
file.write('\n'.join(f'{a}={b}' for a,b in MONGO_DEFAULTS.items()))
file.write('\n\n')
file.write('\n'.join(f'{a}={b}' for a,b in WEBSERVER_DEFAULTS.items()))
# Run docker compose in daemon mode
subprocess.run(["docker", "compose", "--profile", "localdb", "up", "-d"])