|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Deploy wt-echo-server to echo.web-transport.dev. |
| 3 | +# |
| 4 | +# Cross-compiles wt-echo-server locally for x86_64-linux-gnu ReleaseFast, |
| 5 | +# scps it onto the droplet, and restarts the systemd unit. The server repo |
| 6 | +# at /opt/quic-zig/ is NOT touched by this script — we only swap the binary |
| 7 | +# at /opt/quic-zig/zig-out/bin/wt-echo-server. |
| 8 | +# |
| 9 | +# Prerequisites: |
| 10 | +# - Zig 0.16.0 in PATH (or ZIG env var pointing at the 0.16.0 binary). |
| 11 | +# - SSH access to root@echo.web-transport.dev (or the DEPLOY_HOST below). |
| 12 | +# |
| 13 | +# Usage: |
| 14 | +# ./deploy.sh # build + deploy |
| 15 | +# DRY_RUN=1 ./deploy.sh # build only, skip scp/restart |
| 16 | +# |
| 17 | +# Service info on the host: |
| 18 | +# Unit: /etc/systemd/system/wt-echo.service |
| 19 | +# Binary: /opt/quic-zig/zig-out/bin/wt-echo-server |
| 20 | +# Port: UDP 4433 (bound 0.0.0.0) |
| 21 | +# Certs: /etc/letsencrypt/live/echo.web-transport.dev/{fullchain,privkey}.pem |
| 22 | +# (renewed by certbot timer — outside this script) |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +DEPLOY_HOST=${DEPLOY_HOST:-root@137.184.1.19} |
| 27 | +SERVICE=${SERVICE:-wt-echo} |
| 28 | +REMOTE_BIN=${REMOTE_BIN:-/opt/quic-zig/zig-out/bin/wt-echo-server} |
| 29 | +TARGET=${TARGET:-x86_64-linux-gnu} |
| 30 | +ZIG=${ZIG:-zig} |
| 31 | + |
| 32 | +cd "$(dirname "$0")" |
| 33 | + |
| 34 | +echo "=== build (target=$TARGET, mode=ReleaseFast) ===" |
| 35 | +"$ZIG" build -Dtarget="$TARGET" -Doptimize=ReleaseFast |
| 36 | +local_bin="zig-out/bin/wt-echo-server" |
| 37 | +ls -lh "$local_bin" |
| 38 | + |
| 39 | +if [[ ${DRY_RUN:-0} == 1 ]]; then |
| 40 | + echo "DRY_RUN=1 — skipping upload/restart" |
| 41 | + exit 0 |
| 42 | +fi |
| 43 | + |
| 44 | +echo |
| 45 | +echo "=== stage new binary on $DEPLOY_HOST ===" |
| 46 | +# Upload alongside the current binary as *.new so the swap is atomic — |
| 47 | +# rename + restart happens in one SSH session below. |
| 48 | +scp "$local_bin" "$DEPLOY_HOST:${REMOTE_BIN}.new" |
| 49 | + |
| 50 | +echo |
| 51 | +echo "=== swap + restart ===" |
| 52 | +ssh "$DEPLOY_HOST" bash -se <<EOF |
| 53 | +set -euo pipefail |
| 54 | +# Backup the running binary (keep last one for rollback). |
| 55 | +if [[ -f "$REMOTE_BIN" ]]; then |
| 56 | + cp -f "$REMOTE_BIN" "${REMOTE_BIN}.bak" |
| 57 | +fi |
| 58 | +mv "${REMOTE_BIN}.new" "$REMOTE_BIN" |
| 59 | +chmod +x "$REMOTE_BIN" |
| 60 | +systemctl restart "$SERVICE" |
| 61 | +sleep 2 |
| 62 | +systemctl is-active --quiet "$SERVICE" || { |
| 63 | + echo "ERROR: $SERVICE failed to come up; rolling back" |
| 64 | + mv -f "${REMOTE_BIN}.bak" "$REMOTE_BIN" |
| 65 | + systemctl restart "$SERVICE" |
| 66 | + exit 1 |
| 67 | +} |
| 68 | +echo "--- status ---" |
| 69 | +systemctl status "$SERVICE" --no-pager -l | head -10 |
| 70 | +echo "--- listening ---" |
| 71 | +ss -lunp | grep 4433 || echo "(no 4433 listener visible — check logs)" |
| 72 | +EOF |
| 73 | + |
| 74 | +echo |
| 75 | +echo "Deploy OK. Rollback if needed:" |
| 76 | +echo " ssh $DEPLOY_HOST 'mv ${REMOTE_BIN}.bak $REMOTE_BIN && systemctl restart $SERVICE'" |
0 commit comments