Files
deployer/deploy.py
2025-08-17 20:12:10 -04:00

72 lines
1.7 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 = "/mnt/ssd/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 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
else:
print(f"deploying {rq.user}/{rq.repo}/{rq.branch} ({rq.commit_hash})")
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),
),
)
# force redeploy