updates
This commit is contained in:
45
deploy.py
45
deploy.py
@@ -1,34 +1,57 @@
|
||||
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):
|
||||
log_folder = f"/home/drm/deployer-logs/{rq.repo}/{rq.branch}"
|
||||
os.makedirs(log_folder, exist_ok=True)
|
||||
log_file = f"{log_folder}/{int(time())}.log"
|
||||
|
||||
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(log_file, "w"),
|
||||
stderr=open(log_file, "a"),
|
||||
stdout=open(build_log, "w"),
|
||||
stderr=open(build_log, "a"),
|
||||
shell=True,
|
||||
env=dict(
|
||||
os.environ,
|
||||
DEPLOYERDIR=DEPLOYERDIR,
|
||||
os.environ,
|
||||
DOMAIN=domain,
|
||||
DEPLOYMENT_DIR=deployment_dir,
|
||||
USER=rq.user,
|
||||
REPO=rq.repo,
|
||||
BRANCH=rq.branch,
|
||||
COMMIT_HASH=rq.commit_hash,
|
||||
PORT=port,
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user