Repositories | Polyback | Log in to see private repositories


polyback-website / 72da45acf3f9d55c765a106282796db58a749f3f / container/deploy/install.sh

4,981 bytes | parent directory

#!/bin/sh
# Set up (or update) Deploy Updates on the server: run as root in the server's container, from a
# checkout of the website repository. Safe to run again: it installs the current build-runner.py
# and deployd.py and restarts them. A website deploy does NOT do this: the root daemon only
# changes when an administrator runs this by hand.
#   sh container/deploy/install.sh
# Does: toolchain packages (Alpine), Odin built from source in /opt/odin, Emscripten in
# /opt/emscripten (the engine's web build, as its tools/web/Containerfile sets it up), the polybuild user, the
# folders under /srv/polygon/deploy, the polybuild MariaDB user (unix socket login, only the
# Deploy* tables), and the daemons' lines in /srv/polygon/start.sh.
set -e
cd "$(dirname "$0")"
ROOT=/srv/polygon/deploy
ODIN_COMMIT=fa342bb54 # dev-2026-08: what the engine and bootstrapper are developed with
EMSCRIPTEN_TAG=4.0.11 # the last Emscripten that expects LLVM 21 (the engine's web build)
DB=polygon

apk add --no-progress -q build-base git python3 py3-pymysql rsync zip unzip file tar xz \
	clang21 llvm21 llvm21-dev lld21 mingw-w64-gcc rust cargo \
	libx11-dev libxi-dev libxcursor-dev mesa-dev pulseaudio-dev alsa-lib-dev binaryen nodejs npm

if [ "$(/opt/odin/odin version 2>/dev/null | sed 's/.*://')" != "$ODIN_COMMIT" ]; then
	[ -d /opt/odin/.git ] || git clone -q https://github.com/odin-lang/Odin.git /opt/odin
	(cd /opt/odin && git fetch -q origin && git checkout -q -f "$ODIN_COMMIT" &&
		PATH=/usr/lib/llvm21/bin:$PATH LLVM_CONFIG=/usr/lib/llvm21/bin/llvm-config ./build_odin.sh release)
	chmod -R a+rX /opt/odin
fi

# Emscripten on the system LLVM and binaryen (a fixed install: no .git, so no freshness checks)
if [ "$(cat /opt/emscripten/.polyback-tag 2>/dev/null)" != "$EMSCRIPTEN_TAG" ]; then
	WL=$(command -v wasm-ld || ls /usr/bin/wasm-ld* | head -1)
	ln -sf "$WL" /usr/lib/llvm21/bin/wasm-ld
	rm -rf /opt/emscripten.new
	git clone -q --depth 1 --branch "$EMSCRIPTEN_TAG" https://github.com/emscripten-core/emscripten.git /opt/emscripten.new
	(cd /opt/emscripten.new && ./bootstrap &&
		printf "LLVM_ROOT = '/usr/lib/llvm21/bin'\nBINARYEN_ROOT = '/usr'\nNODE_JS = '/usr/bin/node'\n" > .emscripten &&
		rm -rf .git && echo "$EMSCRIPTEN_TAG" > .polyback-tag)
	rm -rf /opt/emscripten && mv /opt/emscripten.new /opt/emscripten
	chmod -R a+rX /opt/emscripten
fi

getent group polybuild >/dev/null || addgroup -S polybuild
id polybuild >/dev/null 2>&1 || adduser -S -D -h /home/polybuild -s /bin/sh -G polybuild -g "Polyback build runner" polybuild
chmod 700 /home/polybuild
# it reads the git server's bare repositories, which belong to www-data
cat >/home/polybuild/.gitconfig <<'CFG'
[safe]
	directory = *
CFG
chown polybuild:polybuild /home/polybuild/.gitconfig

mkdir -p $ROOT/bin $ROOT/releases $ROOT/work $ROOT/artifacts /var/log/polyback
chown polybuild:polybuild $ROOT/work $ROOT/artifacts
chmod 700 $ROOT/work $ROOT/releases
chmod 755 /srv /srv/polygon $ROOT $ROOT/bin $ROOT/artifacts
install -m 644 common.py $ROOT/bin/common.py
install -m 755 build-runner.py deployd.py $ROOT/bin/
[ -f $ROOT/deploy.conf ] || install -m 644 deploy.conf.example $ROOT/deploy.conf

# the tables (the rest of the schema is untouched here), and which part of it is applied
mariadb -uroot $DB -e "SELECT 1 FROM DeployBuilds LIMIT 1" >/dev/null 2>&1 || {
	sed -n '/^-- Deploy Updates (/,$p' ../db/schema.sql | mariadb -uroot $DB
}
mariadb -uroot -e "
CREATE USER IF NOT EXISTS polybuild@localhost IDENTIFIED VIA unix_socket;
GRANT SELECT ON $DB.DeploySources TO polybuild@localhost;
GRANT SELECT ON $DB.DeployState TO polybuild@localhost;
GRANT SELECT, UPDATE ON $DB.DeployBuilds TO polybuild@localhost;
GRANT SELECT, INSERT, UPDATE ON $DB.DeployDaemons TO polybuild@localhost;"

if ! grep -q deployd.py /srv/polygon/start.sh; then
	cp -p /srv/polygon/start.sh /srv/polygon/start.sh.before-deploy-updates
	cat >>/srv/polygon/start.sh <<'START'
# Deploy Updates (/admin/deploy-updates): the build runner (as polybuild) and the deploy daemon
# (root), each in a session of its own and started again if it stops
if ! running "deploy/bin/build-runner.py"; then
  setsid su -s /bin/sh polybuild -c 'cd /; while :; do python3 /srv/polygon/deploy/bin/build-runner.py; sleep 5; done' </dev/null >>$LOG/build-runner.log 2>&1 &
fi
if ! running "deploy/bin/deployd.py"; then
  setsid sh -c 'cd /; while :; do python3 /srv/polygon/deploy/bin/deployd.py; sleep 5; done' </dev/null >>$LOG/deployd.log 2>&1 &
fi
START
fi

# load the new code: the loops in start.sh start the daemons again
busy=$(mariadb -uroot -N $DB -e "SELECT (SELECT COUNT(*) FROM DeployBuilds WHERE Status = 'running') + (SELECT COUNT(*) FROM DeployActions WHERE Status = 'running')")
if [ "$busy" != 0 ]; then
	echo "installed, but a build or deploy is running: run me again when it is done to restart the daemons"
	exit 1
fi
pkill -f "^python3 $ROOT/bin/(build-runner|deployd).py" || true
/bin/sh /srv/polygon/start.sh
echo "installed"