import os from jinja2 import FileSystemLoader, Environment from oshipka.util.os import run_cmd from provision.util import find_port, find_private_ipv4 USER = "pi2" PARENT_DOMAIN = "pi2.dev" oshipka_path = os.environ.get('OSHIPKA_PATH') TEMPLATES_PATH = os.path.join(oshipka_path, "provision", "templates") TMP_PATH = os.path.join(oshipka_path, "provision", "tmp") os.makedirs(TMP_PATH, exist_ok=True) env = Environment( loader=FileSystemLoader(searchpath=TEMPLATES_PATH), ) def prod_install(): pwd = next(run_cmd('pwd')).strip() project_name = os.path.basename(pwd) project_domain = "{}.{}".format(project_name, PARENT_DOMAIN) port = find_port() upstream_ip = find_private_ipv4() ctx = dict( user=USER, pwd=pwd, oshipka_path=oshipka_path, project_name=project_name, project_domain=project_domain, upstream_ip=upstream_ip, port=port, ) service_tmpl = env.get_template('gunicorn.service') service_txt = service_tmpl.render(ctx) with open(os.path.join(TMP_PATH, "{}.service".format(project_name)), 'w') as f: f.write(service_txt) worker_service_tmpl = env.get_template('worker.service') worker_service_txt = worker_service_tmpl.render(ctx) with open(os.path.join(TMP_PATH, "{}_worker.service".format(project_name)), 'w') as f: f.write(worker_service_txt) nginx_tmpl = env.get_template('nginx.conf') nginx_txt = nginx_tmpl.render(ctx) with open(os.path.join(TMP_PATH, "{}.conf".format(project_domain)), 'w') as f: f.write(nginx_txt) if __name__ == "__main__": prod_install()