75 lines
1.8 KiB
Python
75 lines
1.8 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 = {
|
|
"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(
|
|
[
|
|
"tmux",
|
|
"new-session",
|
|
"-L", # pass environment variables
|
|
"-d", # detach
|
|
"-s", # name
|
|
f"{domain}-build-{deployment_id}",
|
|
f"'sudo ./deploy.sh > {build_log} 2>&1'",
|
|
],
|
|
shell=True,
|
|
user="root",
|
|
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),
|
|
),
|
|
)
|