From 93954f3190f7bbcbf29df2f68ebc400acb47cda9 Mon Sep 17 00:00:00 2001 From: Daniel McCrystal Date: Wed, 30 Jul 2025 21:40:29 -0400 Subject: [PATCH] updates --- Makefile | 2 +- deploy.py | 45 ++++++++++++++++++++++++++++++++++----------- deploy.sh | 53 +++++++++++++++++++++++++++++------------------------ 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index cd7e0da..0468020 100644 --- a/Makefile +++ b/Makefile @@ -7,5 +7,5 @@ build: venv/bin/pip install -r requirements.txt -deploy: +start: sudo venv/bin/waitress-serve --host 0.0.0.0 --port 5000 listen:app diff --git a/deploy.py b/deploy.py index a6a495f..da904b9 100644 --- a/deploy.py +++ b/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, ), ) diff --git a/deploy.sh b/deploy.sh index 7aa63e7..d658742 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,42 +1,47 @@ #!/bin/bash -deploydir=$DEPLOYERDIR/$REPO/$BRANCH +cd $DEPLOYMENT_DIR -mkdir -p $deploydir/src -cd $deploydir +# Create our service configuration +service_file=$DOMAIN.service +echo " +[Unit] +Description=Deployment of $DOMAIN (from $REPO/$BRANCH) +After=network.target +[Service] +Type=simple +ExecStart=make start +User=drm +WorkingDirectory=$DEPLOYMENT_DIR/src +Restart=on-failure +StandardOutput=file:$DEPLOYMENT_DIR/start.log +StandardError=file:$DEPLOYMENT_DIR/start.log + +[Install] +WantedBy=multi-user.target +" > $service_file + +# create a folder to put the source code +mkdir -p src + +# put the source code in that folder +gitea=localhost:3000 git clone \ -b $BRANCH \ - "http://deployer:$(cat /home/drm/deployer/token)@localhost:3000/$USER/$REPO" \ + "http://deployer:$(cat /home/drm/deployer/token)@$gitea/$USER/$REPO" \ ./src - cd src git fetch origin $BRANCH git reset --hard origin/$BRANCH git checkout $COMMIT_HASH +# build the dist artifact from within the src directory make build cd .. -echo " -[Unit] -Description=Deployment of $REPO/$BRANCH -After=network.target -[Service] -Type=simple -ExecStart=make deploy -User=drm -WorkingDirectory=$deploydir/src -Restart=on-failure -StandardOutput=file:$deploydir/server.log -StandardError=file:$deploydir/server.log - -[Install] -WantedBy=multi-user.target -" > deploy.service - -service="deployer-$REPO-$BRANCH.service" -ln -sf $deploydir/deploy.service /etc/systemd/system/$service +service="deployer-$DOMAIN.service" +ln -sf $DEPLOYMENT_DIR/$service_file /etc/systemd/system/$service systemctl daemon-reload systemctl restart $service