updates
This commit is contained in:
2
Makefile
2
Makefile
@@ -7,5 +7,5 @@ build:
|
|||||||
venv/bin/pip install -r requirements.txt
|
venv/bin/pip install -r requirements.txt
|
||||||
|
|
||||||
|
|
||||||
deploy:
|
start:
|
||||||
sudo venv/bin/waitress-serve --host 0.0.0.0 --port 5000 listen:app
|
sudo venv/bin/waitress-serve --host 0.0.0.0 --port 5000 listen:app
|
||||||
|
|||||||
35
deploy.py
35
deploy.py
@@ -1,8 +1,10 @@
|
|||||||
|
from operator import itemgetter
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DeployerRequest:
|
class DeployerRequest:
|
||||||
user: str
|
user: str
|
||||||
@@ -10,25 +12,46 @@ class DeployerRequest:
|
|||||||
branch: str
|
branch: str
|
||||||
commit_hash: str
|
commit_hash: str
|
||||||
|
|
||||||
|
|
||||||
DEPLOYERDIR = "/home/drm/live"
|
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):
|
def handle_request(rq: DeployerRequest):
|
||||||
log_folder = f"/home/drm/deployer-logs/{rq.repo}/{rq.branch}"
|
if (rq.user, rq.repo, rq.branch) not in by_branch:
|
||||||
os.makedirs(log_folder, exist_ok=True)
|
return
|
||||||
log_file = f"{log_folder}/{int(time())}.log"
|
|
||||||
|
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(
|
subprocess.Popen(
|
||||||
["./deploy.sh"],
|
["./deploy.sh"],
|
||||||
user="root",
|
user="root",
|
||||||
stdout=open(log_file, "w"),
|
stdout=open(build_log, "w"),
|
||||||
stderr=open(log_file, "a"),
|
stderr=open(build_log, "a"),
|
||||||
shell=True,
|
shell=True,
|
||||||
env=dict(
|
env=dict(
|
||||||
os.environ,
|
os.environ,
|
||||||
DEPLOYERDIR=DEPLOYERDIR,
|
DOMAIN=domain,
|
||||||
|
DEPLOYMENT_DIR=deployment_dir,
|
||||||
USER=rq.user,
|
USER=rq.user,
|
||||||
REPO=rq.repo,
|
REPO=rq.repo,
|
||||||
BRANCH=rq.branch,
|
BRANCH=rq.branch,
|
||||||
COMMIT_HASH=rq.commit_hash,
|
COMMIT_HASH=rq.commit_hash,
|
||||||
|
PORT=port,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
53
deploy.sh
53
deploy.sh
@@ -1,42 +1,47 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
deploydir=$DEPLOYERDIR/$REPO/$BRANCH
|
cd $DEPLOYMENT_DIR
|
||||||
|
|
||||||
mkdir -p $deploydir/src
|
# Create our service configuration
|
||||||
cd $deploydir
|
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 \
|
git clone \
|
||||||
-b $BRANCH \
|
-b $BRANCH \
|
||||||
"http://deployer:$(cat /home/drm/deployer/token)@localhost:3000/$USER/$REPO" \
|
"http://deployer:$(cat /home/drm/deployer/token)@$gitea/$USER/$REPO" \
|
||||||
./src
|
./src
|
||||||
|
|
||||||
cd src
|
cd src
|
||||||
git fetch origin $BRANCH
|
git fetch origin $BRANCH
|
||||||
git reset --hard origin/$BRANCH
|
git reset --hard origin/$BRANCH
|
||||||
git checkout $COMMIT_HASH
|
git checkout $COMMIT_HASH
|
||||||
|
|
||||||
|
# build the dist artifact from within the src directory
|
||||||
make build
|
make build
|
||||||
|
|
||||||
cd ..
|
cd ..
|
||||||
echo "
|
|
||||||
[Unit]
|
|
||||||
Description=Deployment of $REPO/$BRANCH
|
|
||||||
After=network.target
|
|
||||||
|
|
||||||
[Service]
|
service="deployer-$DOMAIN.service"
|
||||||
Type=simple
|
ln -sf $DEPLOYMENT_DIR/$service_file /etc/systemd/system/$service
|
||||||
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
|
|
||||||
|
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl restart $service
|
systemctl restart $service
|
||||||
|
|||||||
Reference in New Issue
Block a user