78 lines
3.4 KiB
Bash
78 lines
3.4 KiB
Bash
#!/usr/bin/env bash
|
|
# Run as root after uploading the binary, unit, and nginx snippet to a staging dir.
|
|
# Usage: bash install-server.sh /root/ads-stage-<timestamp>
|
|
set -euo pipefail
|
|
stage=${1:?staging directory required}
|
|
case "$stage" in /root/ads-stage-*) ;; *) echo 'Unexpected staging path' >&2; exit 1;; esac
|
|
test -f "$stage/ads-server"
|
|
test -f "$stage/ads-server.service"
|
|
test -f "$stage/nginx-ads-path.conf"
|
|
nginx -t
|
|
|
|
backup="/var/backups/ads/$(date -u +%Y%m%dT%H%M%SZ)"
|
|
install -d -m 0700 "$backup"
|
|
nginx_site=$(readlink -f /etc/nginx/sites-enabled/zz-ttstd-main.conf)
|
|
test -f "$nginx_site"
|
|
cp -a "$nginx_site" "$backup/ttstd-main.conf"
|
|
if test -e /opt/ads/ads-server; then cp -a /opt/ads/ads-server "$backup/ads-server"; fi
|
|
if test -e /etc/systemd/system/ads-server.service; then cp -a /etc/systemd/system/ads-server.service "$backup/ads-server.service"; fi
|
|
if test -e /etc/nginx/snippets/ads-path.conf; then cp -a /etc/nginx/snippets/ads-path.conf "$backup/ads-path.conf"; fi
|
|
|
|
if ! id -u ads >/dev/null 2>&1; then
|
|
useradd --system --user-group --home-dir /var/lib/ads --shell /usr/sbin/nologin ads
|
|
fi
|
|
install -d -m 0755 /opt/ads
|
|
install -d -m 0700 /etc/ads
|
|
install -d -m 0750 -o ads -g ads /var/lib/ads
|
|
if ! test -f /etc/ads/server.env; then
|
|
python3 - <<'PY'
|
|
import os, secrets
|
|
fd = os.open('/etc/ads/server.env', os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(fd, 'w') as f:
|
|
f.write('ADS_AGENT_TOKEN=' + secrets.token_hex(32) + '\n')
|
|
f.write('ADS_CLIENT_TOKEN=' + secrets.token_hex(32) + '\n')
|
|
PY
|
|
fi
|
|
chmod 0600 /etc/ads/server.env
|
|
install -m 0755 "$stage/ads-server" /opt/ads/ads-server.next
|
|
mv /opt/ads/ads-server.next /opt/ads/ads-server
|
|
install -m 0644 "$stage/ads-server.service" /etc/systemd/system/ads-server.service
|
|
install -m 0644 "$stage/nginx-ads-path.conf" /etc/nginx/snippets/ads-path.conf
|
|
|
|
# Insert only into the inspected ttstd.com:443 block, not the other sites.
|
|
NGINX_SITE="$nginx_site" python3 - <<'PY'
|
|
from pathlib import Path
|
|
import os
|
|
p = Path(os.environ['NGINX_SITE'])
|
|
text = p.read_text()
|
|
include = ' include /etc/nginx/snippets/ads-path.conf;'
|
|
if include not in text:
|
|
anchor = ' listen 443 ssl;\n server_name ttstd.com;'
|
|
if text.count(anchor) != 1:
|
|
raise SystemExit('Expected unique ttstd.com HTTPS server block; refusing ambiguous edit')
|
|
if 'location ^~ /ads/' in text or 'location /ads/' in text:
|
|
raise SystemExit('An existing /ads/ location must be reviewed before deployment')
|
|
text = text.replace(anchor, anchor + '\n\n' + include, 1)
|
|
p.write_text(text)
|
|
PY
|
|
|
|
if ! nginx -t; then
|
|
cp -a "$backup/ttstd-main.conf" "$nginx_site"
|
|
if test -f "$backup/ads-path.conf"; then cp -a "$backup/ads-path.conf" /etc/nginx/snippets/ads-path.conf; fi
|
|
echo "Nginx validation failed; site configuration restored from $backup" >&2
|
|
exit 1
|
|
fi
|
|
systemctl daemon-reload
|
|
systemctl enable ads-server.service
|
|
systemctl restart ads-server.service
|
|
for attempt in $(seq 1 20); do
|
|
if curl --fail --silent --max-time 2 http://127.0.0.1:18081/healthz >/dev/null; then break; fi
|
|
sleep 1
|
|
done
|
|
curl --fail --silent --max-time 5 http://127.0.0.1:18081/healthz
|
|
systemctl reload nginx
|
|
curl --noproxy '*' --fail --silent --max-time 10 --resolve ttstd.com:443:127.0.0.1 https://ttstd.com/ads/healthz
|
|
systemctl is-active ads-server nginx
|
|
systemctl show ads-server --property=MainPID,MemoryCurrent,MemoryPeak
|
|
printf 'Backup directory: %s\n' "$backup"
|