-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.py
90 lines (77 loc) · 3.33 KB
/
gen.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
from __future__ import print_function
import os, shutil
import yaml
import random, string
try:
passwords = yaml.load(open('portals_passwords.yml'))
except Exception:
passwords = {}
def genRandomPassword():
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(25))
def getPassword(portal):
password = passwords.get(portal)
if password is None:
password = genRandomPassword()
passwords[portal] = password
return password
os.chdir('../portals')
portals = [ portal for portal in os.listdir('./') if os.path.isdir(portal) ]
portalconfig = {}
print("\n=============== Reading config ===============\n")
for portal in portals:
print("\n##### " + portal + "\n")
try:
portalconfig[portal] = yaml.load(open(portal + '/config.yml'))
except Exception:
portalconfig[portal] = {}
print("\n=============== Building frontend ===============\n")
for portal in portals:
if os.environ.get('ONLY_BUILD') and os.environ['ONLY_BUILD'] != portal:
continue
print("\n##### " + portal + "\n")
extraenv = ''
for key, value in portalconfig[portal].iteritems():
extraenv += "%s=\"%s\" " % (key, value)
os.chdir(portal + "/frontend")
os.system("yarn install")
os.system(extraenv + "INFERNO_APP_BACKEND_URL=/k/{0}/api PUBLIC_URL='/k/{0}/' yarn build".format(portal))
os.chdir("../../")
print("\n=============== Making MySQL init file ===============\n")
initdbfile = open('../deployconfig/portals_initdb.sql', 'w')
for portal in portals:
print("CREATE USER IF NOT EXISTS '{}'@'%' IDENTIFIED BY '{}';".format(portal, getPassword(portal)), file=initdbfile)
print("CREATE DATABASE IF NOT EXISTS {};".format(portal), file=initdbfile)
print("GRANT ALL PRIVILEGES ON {0}.* TO '{0}'@'%' IDENTIFIED BY '{1}';".format(portal, getPassword(portal)), file=initdbfile)
print("FLUSH PRIVILEGES;", file=initdbfile)
print("\n=============== Making docker compose file ===============\n")
services = {}
for portal in portals:
extraenv = []
for key, value in portalconfig[portal].iteritems():
extraenv.append("%s=%s" % (key, value))
services[portal + '_backend'] = {
"image": "node:8",
"working_dir": "/home/node/app",
"environment": [
"NODE_ENV=production",
"DBURI=mysql://%s:%s@portaldb/%s" % (portal, getPassword(portal), portal),
"PUBLIC_API_URL=https://felicity.iiit.ac.in/k/%s/api" % portal,
"PUBLIC_FRONTEND_URL=https://felicity.iiit.ac.in/k/%s" % portal,
] + extraenv,
"volumes": [ "../portals/%s/backend:/home/node/app" % portal ],
"command": "bash -c 'yarn install && yarn start'"
}
services[portal + '_frontend'] = {
"image": "node:8",
"working_dir": "/home/node/app",
"environment": [ "NODE_ENV=production" ],
"volumes": [ "../portals/%s/frontend/build:/home/node/app" % portal ],
"command": "bash -c 'yarn global add serve && serve -s'"
}
os.chdir('../deployconfig')
kongconf = yaml.load(open('kong.yml'))
kongconf['services'].update(services)
misc_portals = yaml.load(open('misc_portals.yml'))
kongconf['services'].update(misc_portals)
yaml.dump(kongconf, open('portals.yml', 'w'), default_flow_style=False)
yaml.dump(passwords, open('portals_passwords.yml', 'w'), default_flow_style=False)