From 5814527ea13019026536ce9f5b82c7c7fd3339c9 Mon Sep 17 00:00:00 2001 From: Megh Parikh Date: Mon, 8 Jan 2018 12:42:18 +0530 Subject: [PATCH] Add elementary portals docker compose file generation --- .gitignore | 5 +++++ README.md | 13 +++++++++++ dev.env | 1 + gen.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ kong.yml | 21 ++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 gen.py diff --git a/.gitignore b/.gitignore index f24a233..c8519a1 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ # End of https://www.gitignore.io/api/linux prod.env + +# portals +portals.yml +portals_initdb.sql +portals_passwords.yml diff --git a/README.md b/README.md index 0c2d3a7..18a8a48 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/dev.env b/dev.env index 828e6c9..509393e 100644 --- a/dev.env +++ b/dev.env @@ -9,3 +9,4 @@ AUTH_LDAP_PASSWORD=password # kong KONG_POSTGRES_PASSWORD=password KONG_ADMIN_PASSWORD=password +KONG_PORTALDB_ROOT_PASSWORD=password diff --git a/gen.py b/gen.py new file mode 100644 index 0000000..60ba711 --- /dev/null +++ b/gen.py @@ -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) diff --git a/kong.yml b/kong.yml index 4c03fb4..1e6d184 100644 --- a/kong.yml +++ b/kong.yml @@ -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: