| [5462bab] | 1 | #!/usr/bin/env bash
|
|---|
| 2 | #
|
|---|
| 3 | # (Re)write /usr/local/bin/klonkt-update so it matches how this server runs.
|
|---|
| 4 | #
|
|---|
| 5 | # Why this exists: the updater is generated once at install time. A server
|
|---|
| 6 | # that later migrated to the split layout (klonkt@<slug> units) kept its old
|
|---|
| 7 | # updater, which still restarts the retired klonkt.service. Result: the code
|
|---|
| 8 | # on disk updates, the restart quietly fails, and the old process keeps
|
|---|
| 9 | # serving — half old routes, half new templates, which is how you get a 500
|
|---|
| 10 | # on one page and nothing in the logs that says why.
|
|---|
| 11 | #
|
|---|
| 12 | # Idempotent; safe to run any time:
|
|---|
| 13 | #
|
|---|
| 14 | # sudo bash /opt/klonkt/scripts/klonkt-refresh-updater.sh
|
|---|
| 15 | #
|
|---|
| 16 | set -euo pipefail
|
|---|
| 17 |
|
|---|
| 18 | KLONKT_DIR="${KLONKT_DIR:-/opt/klonkt}"
|
|---|
| 19 | KLONKT_USER="${KLONKT_USER:-klonkt}"
|
|---|
| 20 | DATA_ROOT="${KLONKT_DATA_ROOT:-/var/lib/klonkt}"
|
|---|
| 21 | # Follow whatever branch the checkout is on (stable for most self-hosters).
|
|---|
| 22 | BRANCH="${KLONKT_BRANCH:-$(git -C "$KLONKT_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null || echo stable)}"
|
|---|
| 23 |
|
|---|
| 24 | [ "$(id -u)" = 0 ] || { echo "run this as root (sudo)." >&2; exit 1; }
|
|---|
| 25 | [ -d "$KLONKT_DIR/.git" ] || { echo "no git checkout at $KLONKT_DIR" >&2; exit 1; }
|
|---|
| 26 |
|
|---|
| 27 | cat > /usr/local/bin/klonkt-update <<EOF
|
|---|
| 28 | #!/usr/bin/env bash
|
|---|
| 29 | set -euo pipefail
|
|---|
| 30 | D="${KLONKT_DIR}"
|
|---|
| 31 | B=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD 2>/dev/null || true)
|
|---|
| 32 | runuser -u ${KLONKT_USER} -- git -C "\$D" fetch --depth 1 origin ${BRANCH}
|
|---|
| 33 | runuser -u ${KLONKT_USER} -- git -C "\$D" checkout -qf -B ${BRANCH} FETCH_HEAD
|
|---|
| 34 | A=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD)
|
|---|
| 35 | if [ "\$B" = "\$A" ]; then
|
|---|
| 36 | echo "Klonkt is already up to date (\$A) — nothing to do."
|
|---|
| 37 | exit 0
|
|---|
| 38 | fi
|
|---|
| 39 | if ! runuser -u ${KLONKT_USER} -- git -C "\$D" diff --quiet "\$B" "\$A" -- package-lock.json 2>/dev/null; then
|
|---|
| 40 | runuser -u ${KLONKT_USER} -- env HOME="\$D" bash -c "cd '\$D' && npm ci --omit=dev"
|
|---|
| 41 | fi
|
|---|
| 42 | # Restart every instance sharing this checkout: one directory with an .env
|
|---|
| 43 | # under the data root per instance. No instances there = the pre-split
|
|---|
| 44 | # single-service layout, which still runs plain klonkt.service.
|
|---|
| 45 | N=0
|
|---|
| 46 | for d in ${DATA_ROOT}/*/; do
|
|---|
| 47 | [ -f "\$d/.env" ] || continue
|
|---|
| 48 | s=\$(basename "\$d")
|
|---|
| 49 | systemctl restart "klonkt@\$s" && N=\$((N+1))
|
|---|
| 50 | done
|
|---|
| 51 | if [ "\$N" = 0 ]; then
|
|---|
| 52 | systemctl restart klonkt
|
|---|
| 53 | echo "Klonkt updated (\$A) + restarted."
|
|---|
| 54 | else
|
|---|
| 55 | echo "Klonkt updated (\$A) + restarted \$N instance(s)."
|
|---|
| 56 | fi
|
|---|
| 57 | EOF
|
|---|
| 58 | chmod +x /usr/local/bin/klonkt-update
|
|---|
| 59 | echo "klonkt-update rewritten: branch ${BRANCH}, code ${KLONKT_DIR}, instances under ${DATA_ROOT}"
|
|---|