From c3ef3f2dd57f2b7dc415053b9795692dfb994584 Mon Sep 17 00:00:00 2001 From: Daniel McCrystal Date: Tue, 7 Jan 2025 18:37:10 -0500 Subject: [PATCH] first commit --- .gitignore | 2 ++ Makefile | 9 +++++++++ deploy.py | 22 ++++++++++++++++++++++ deploy.sh | 39 +++++++++++++++++++++++++++++++++++++++ listen.py | 18 ++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 deploy.py create mode 100644 deploy.sh create mode 100644 listen.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a66904 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/__pycache__/* +venv/* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9fa9acf --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ + +build: + @: + +deploy: + venv/bin/waitress-serve --host 0.0.0.0 --port 5000 listen:app + +stop: + @: \ No newline at end of file diff --git a/deploy.py b/deploy.py new file mode 100644 index 0000000..a1a542a --- /dev/null +++ b/deploy.py @@ -0,0 +1,22 @@ +from dataclasses import dataclass +import subprocess +import os + +@dataclass +class DeployerRequest: + repo: str + branch: str + commit_hash: str + + + +def handle_request(rq: DeployerRequest): + subprocess.run( + ["./deploy.sh"], + env=dict( + os.environ, + REPO=rq.repo, + BRANCH=rq.branch, + COMMIT_HASH=rq.commit_hash, + ), + ) \ No newline at end of file diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..9af8355 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,39 @@ +#!/bin/bash +$rootdir=$(pwd) +$deploydir=$rootdir/deployments/$REPO/$BRANCH + +mkdir -p $deploydir +cd $deploydir +git clone $CLONE_URL . + +git fetch origin $BRANCH +git reset --hard origin/$BRANCH +git checkout $COMMIT_HASH + +make build +echo " +[Unit] +Description=Deployment of $REPO/$BRANCH +After=network.target + +[Service] +Type=simple +ExecStart=make deploy +User=drm +WorkingDirectory=$deploydir +Restart=on-failure +StandardOutput=file:$deploydir/server.log +StandardError=file:$deploydir/server.log + +[Install] +WantedBy=multi-user.target +" > deploy.service +systemctl daemon reload + +$service="deployer-$repo-$branch.service" +ln -s $deploydir/deploy.service /etc/systemd/system/$service +systemctl stop $service +systemctl start $service + + + diff --git a/listen.py b/listen.py new file mode 100644 index 0000000..d0adedc --- /dev/null +++ b/listen.py @@ -0,0 +1,18 @@ +from flask import Flask, request + +from .deploy import handle_request, DeployerRequest + +app = Flask(__name__) + + +@app.route("/gitea", methods=["POST"]) +def gitea(): + data = request.get_json() + + handle_request( + DeployerRequest( + repo = data["repository"]["name"], + branch = data["ref"].strip("refs/heads/"), + commit_hash = data["after"] + ) + ) \ No newline at end of file