83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
from operator import itemgetter
|
|
from dataclasses import dataclass
|
|
import subprocess
|
|
import os
|
|
from time import time
|
|
from textwrap import dedent
|
|
|
|
|
|
@dataclass
|
|
class DeployerRequest:
|
|
user: str
|
|
repo: str
|
|
branch: str
|
|
commit_hash: str
|
|
|
|
|
|
DEPLOYERDIR = "/home/drm/live"
|
|
BASE_PORT = 3001
|
|
|
|
config = {
|
|
"deployer": ("drm", "deployer", "main"),
|
|
"games": ("drm", "games", "prod"),
|
|
}
|
|
by_branch = {
|
|
(user, repo, branch): {"domain": domain, "port": BASE_PORT + i}
|
|
for i, (domain, (user, repo, branch)) in enumerate(config.items())
|
|
}
|
|
|
|
|
|
def build_caddyfile():
|
|
excluded = {"deployer"}
|
|
with open("Caddyfile", "w+") as file:
|
|
for domain, port in map(itemgetter("domain", "port"), by_branch.values()):
|
|
if domain in excluded:
|
|
continue
|
|
|
|
entry = f"""
|
|
{domain}.drm.dev {{
|
|
reverse_proxy localhost:{port}
|
|
}}
|
|
"""
|
|
file.write(dedent(entry))
|
|
|
|
|
|
def handle_request(rq: DeployerRequest):
|
|
if (rq.user, rq.repo, rq.branch) not in by_branch:
|
|
print(
|
|
f"{rq.user}/{rq.repo}/{rq.branch} is not registered in the deployer config"
|
|
)
|
|
print(by_branch)
|
|
return
|
|
|
|
domain, port = itemgetter("domain", "port")(
|
|
by_branch[(rq.user, rq.repo, rq.branch)]
|
|
)
|
|
deployment_id = str(int(time()))
|
|
|
|
domain_dir = f"{DEPLOYERDIR}/{domain}"
|
|
logs_dir = f"{domain_dir}/logs/{deployment_id}"
|
|
os.makedirs(logs_dir, exist_ok=True)
|
|
|
|
build_log = f"{logs_dir}/build.log"
|
|
|
|
subprocess.Popen(
|
|
["./deploy.sh"],
|
|
user="root",
|
|
stdout=open(build_log, "w"),
|
|
stderr=open(build_log, "a"),
|
|
shell=True,
|
|
env=dict(
|
|
os.environ,
|
|
DOMAIN=domain,
|
|
DOMAIN_DIR=domain_dir,
|
|
DEPLOYMENT_ID=deployment_id,
|
|
LOGS_DIR=logs_dir,
|
|
USER=rq.user,
|
|
REPO=rq.repo,
|
|
BRANCH=rq.branch,
|
|
COMMIT_HASH=rq.commit_hash,
|
|
PORT=str(port),
|
|
),
|
|
)
|