Skip to content

Commit

Permalink
Add elementary portals docker compose file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
meghprkh committed Jan 9, 2018
1 parent 9a0b703 commit 5814527
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
# End of https://www.gitignore.io/api/linux

prod.env

# portals
portals.yml
portals_initdb.sql
portals_passwords.yml
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ Prod
dotenv prod.env
docker stack deploy -c auth.yml auth
```

# Directory Structure
```
.
├── deployconfig
└── portals
├── portal_1
│   ├── backend
│   └── frontend
└── portal_2
   ├── backend
   └── frontend
```
1 change: 1 addition & 0 deletions dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ AUTH_LDAP_PASSWORD=password
# kong
KONG_POSTGRES_PASSWORD=password
KONG_ADMIN_PASSWORD=password
KONG_PORTALDB_ROOT_PASSWORD=password
64 changes: 64 additions & 0 deletions gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import print_function
import os, shutil
import yaml
import random, string

try:
passwords = yaml.load(open('portals_passwords'))
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) ]

print("\n=============== Building frontend ===============\n")
for portal in portals:
print("\n##### " + portal + "\n")
os.chdir(portal + "/frontend")
os.system("yarn install")
os.system("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:
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)],
"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)
yaml.dump(kongconf, open('portals.yml', 'w'), default_flow_style=False)
yaml.dump(passwords, open('portals_passwords.yml', 'w'), default_flow_style=False)
21 changes: 21 additions & 0 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,24 @@ services:
limits:
cpus: "0.25"
memory: 200M

########## CONTEST SERVICES ##########

portaldb:
image: mariadb:10
volumes:
- portaldb:/var/lib/mysql
- ./portals_initdb.sql:/docker-entrypoint-initdb.d/portals_initdb.sql
environment:
# Make root password instead of random root password for backup since no sqldumps
MYSQL_ROOT_PASSWORD: ${KONG_PORTALDB_ROOT_PASSWORD}

portaladmin:
image: adminer
environment:
ADMINER_DEFAULT_SERVER: portaldb
ports:
- 9001:8080

volumes:
portaldb:

0 comments on commit 5814527

Please sign in to comment.