Files
deployer/deploy.py
2025-07-30 21:40:29 -04:00

58 lines
1.3 KiB
Python

from operator import itemgetter
from dataclasses import dataclass
import subprocess
import os
from time import time
@dataclass
class DeployerRequest:
user: str
repo: str
branch: str
commit_hash: str
DEPLOYERDIR = "/home/drm/live"
BASE_PORT = 3001
config = {"games": {"user": "drm", "repo": "games", "branch": "prod"}}
by_branch = {
(user, repo, branch): {"domain": domain, "port": BASE_PORT + i}
for i, (domain, (user, repo, branch)) in enumerate(config.items())
}
def handle_request(rq: DeployerRequest):
if (rq.user, rq.repo, rq.branch) not in by_branch:
return
domain, port = itemgetter("domain", "port")(
by_branch[(rq.user, rq.repo, rq.branch)]
)
deployment_id = int(time())
domain_dir = f"{DEPLOYERDIR}/{domain}"
deployment_dir = f"{domain_dir}/{deployment_id}"
os.makedirs(deployment_dir, exist_ok=True)
build_log = f"{deployment_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,
DEPLOYMENT_DIR=deployment_dir,
USER=rq.user,
REPO=rq.repo,
BRANCH=rq.branch,
COMMIT_HASH=rq.commit_hash,
PORT=port,
),
)