55 lines
2.8 KiB
Bash
55 lines
2.8 KiB
Bash
#!/bin/bash
|
|||
|
|
# One-time (idempotent) setup of Thor's LXC, run as root inside it from a checkout of this
|
||
|
|
# repo: `bash deploy/bootstrap.sh`. Later deploys go through thor_deploy.sh.
|
||
|
|
#
|
||
|
|
# Expects the base packages (python3-venv, git, nginx, webhook, sudo, curl), the `thor`
|
||
|
|
# user and the secrets in /etc/thor/*.env to exist already (see README, "Deploy").
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
SRC=$(cd "$(dirname "$0")/.." && pwd)
|
||
|
|
REPO_URL=${THOR_REPO_URL:-https://code.lotusguild.org/LotusGuild/thor.git}
|
||
|
|
|
||
|
|
id thor >/dev/null 2>&1 || useradd --system --home /var/lib/thor --create-home --shell /usr/sbin/nologin thor
|
||
|
|
install -d -m 0755 /opt/thor
|
||
|
|
install -d -m 0750 -o root -g thor /etc/thor
|
||
|
|
|
||
|
|
if [ ! -d /opt/thor/app/.git ]; then
|
||
|
|
git clone --quiet "$REPO_URL" /opt/thor/app
|
||
|
|
fi
|
||
|
|
# Only for the very first install, before main has code: THOR_BOOTSTRAP_REF=origin/<branch>.
|
||
|
|
if [ -n "${THOR_BOOTSTRAP_REF:-}" ]; then
|
||
|
|
git -C /opt/thor/app fetch --quiet origin
|
||
|
|
git -C /opt/thor/app checkout --quiet --detach "$THOR_BOOTSTRAP_REF"
|
||
|
|
fi
|
||
|
|
[ -x /opt/thor/venv/bin/python ] || python3 -m venv /opt/thor/venv
|
||
|
|
/opt/thor/venv/bin/pip install --quiet --disable-pip-version-check -r /opt/thor/app/requirements.txt
|
||
|
|
|
||
|
|
[ -f /etc/thor/thor.json ] || install -m 0640 -o root -g thor "$SRC/deploy/thor.example.json" /etc/thor/thor.json
|
||
|
|
printf 'THOR_SHA=%s\n' "$(git -C /opt/thor/app rev-parse HEAD)" > /opt/thor/revision.env
|
||
|
|
|
||
|
|
install -m 0644 "$SRC/deploy/systemd/thor.service" /etc/systemd/system/thor.service
|
||
|
|
install -m 0644 "$SRC/deploy/systemd/thor-monitor.service" /etc/systemd/system/thor-monitor.service
|
||
|
|
install -m 0644 "$SRC/deploy/nginx/thor" /etc/nginx/sites-enabled/thor
|
||
|
|
rm -f /etc/nginx/sites-enabled/default
|
||
|
|
nginx -t
|
||
|
|
|
||
|
|
# Deploy webhook: its own unprivileged user, allowed to run only the deploy script.
|
||
|
|
id webhook >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin webhook
|
||
|
|
install -m 0755 -o root -g root "$SRC/deploy/thor_deploy.sh" /usr/local/bin/thor_deploy.sh
|
||
|
|
install -m 0440 -o root -g root "$SRC/deploy/webhook/sudoers-thor-deploy" /etc/sudoers.d/thor-deploy
|
||
|
|
visudo -cf /etc/sudoers.d/thor-deploy
|
||
|
|
install -d -m 0750 -o root -g webhook /etc/webhook
|
||
|
|
if [ ! -f /etc/webhook/hooks.json ]; then
|
||
|
|
secret=$(python3 -c 'import secrets; print(secrets.token_hex(32))')
|
||
|
|
sed "s/REPLACE_WITH_WEBHOOK_SECRET/${secret}/" "$SRC/deploy/webhook/hooks.json.example" > /etc/webhook/hooks.json
|
||
|
|
chown root:webhook /etc/webhook/hooks.json
|
||
|
|
chmod 0640 /etc/webhook/hooks.json
|
||
|
|
echo "Generated a new webhook secret in /etc/webhook/hooks.json: copy it to the repo's WEBHOOK_SECRET."
|
||
|
|
fi
|
||
|
|
install -m 0644 "$SRC/deploy/webhook/webhook.service" /etc/systemd/system/webhook.service
|
||
|
|
|
||
|
|
systemctl daemon-reload
|
||
|
|
systemctl enable --quiet --now webhook thor thor-monitor
|
||
|
|
systemctl reload nginx
|
||
|
|
systemctl restart thor thor-monitor
|
||
|
|
echo "Thor bootstrapped at $(git -C /opt/thor/app rev-parse --short HEAD)"
|