Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Remove the updater's release download dirs

Juan Denisf122acce0198
diff --git a/scripts/test/test_update.sh b/scripts/test/test_update.sh
index 90370656..012d9c68 100644
--- a/scripts/test/test_update.sh
+++ b/scripts/test/test_update.sh
@@ -974,6 +974,73 @@ EOF
fi
fi
+# --------------------------------------------------------------------------
+# T20b — deploy_release removes its download and unpack dirs. It used to leave
+# the ~70 MB tarball's mktemp dir behind on every update (a 25 GB box had 11
+# of them, 644 MB), and a failed download stranded its dir as well. An
+# operator's offline tarball must survive; stale leftovers are swept.
+# --------------------------------------------------------------------------
+t="$WORK/t20b"; mkdir -p "$t/bin" "$t/tmp" "$t/src/serverkit/scripts" "$t/live" "$t/slot"
+if [ -z "$tarch" ] || ! command -v sha256sum >/dev/null 2>&1; then
+ skip "deploy_release scratch cleanup — needs sha256sum + a known arch (runs on Linux CI)"
+else
+ printf '#!/bin/sh\n' > "$t/src/serverkit/serverkit"
+ tar czf "$t/release.tar.gz" -C "$t/src" serverkit
+ sha="$(sha256sum "$t/release.tar.gz" | cut -d' ' -f1)"
+ printf '%s serverkit-v9.9.9-linux-%s.tar.gz\n' "$sha" "$tarch" > "$t/checksums.txt"
+ cat > "$t/bin/curl" <<EOF
+#!/usr/bin/env bash
+out=""; url=""
+while [ \$# -gt 0 ]; do
+ case "\$1" in -o) out="\$2"; shift 2 ;; -*) shift ;; *) url="\$1"; shift ;; esac
+done
+[ -n "\${T20B_CURL_FAIL:-}" ] && exit 22
+case "\$url" in
+ *checksums.txt) cp "$t/checksums.txt" "\$out" ;;
+ *) cp "$t/release.tar.gz" "\$out" ;;
+esac
+EOF
+ chmod +x "$t/bin/curl"
+ # A leftover from an older updater (swept) and a fresh one (kept: it could
+ # be a concurrent download).
+ mkdir -p "$t/tmp/tmp.OLDLEFTOVER" "$t/tmp/tmp.FRESHDL"
+ : > "$t/tmp/tmp.OLDLEFTOVER/serverkit-v1.0.0-linux-amd64.tar.gz"
+ : > "$t/tmp/tmp.FRESHDL/serverkit-v1.0.1-linux-amd64.tar.gz"
+ touch -d '2 hours ago' "$t/tmp/tmp.OLDLEFTOVER/serverkit-v1.0.0-linux-amd64.tar.gz"
+
+ _t20b_deploy() {
+ (
+ set -Eeuo pipefail
+ export PATH="$t/bin:$PATH" TMPDIR="$t/tmp"
+ DRY_RUN=0; INSTALL_DIR="$t/live"; SERVERKIT_MIRROR_URL="http://mirror.invalid"
+ SERVERKIT_OFFLINE_TARBALL="${1:-}"
+ preserve_installed_plugins() { :; }
+ trap cleanup_on_exit EXIT
+ deploy_release "$t/slot/target" v9.9.9
+ ) >/dev/null 2>&1
+ }
+ _t20b_scratch() { find "$t/tmp" -mindepth 1 -maxdepth 1 ! -name tmp.FRESHDL | wc -l; }
+
+ if _t20b_deploy && [ -x "$t/slot/target/serverkit" ] && [ "$(_t20b_scratch)" -eq 0 ] \
+ && [ -d "$t/tmp/tmp.FRESHDL" ]; then
+ ok "deploy_release leaves no download/unpack dir behind and sweeps stale ones"
+ else
+ bad "deploy_release left scratch in TMPDIR: [$(ls "$t/tmp" | tr '\n' ' ')]"
+ fi
+
+ if _t20b_deploy "$t/release.tar.gz" && [ -f "$t/release.tar.gz" ] && [ "$(_t20b_scratch)" -eq 0 ]; then
+ ok "deploy_release keeps an operator's offline tarball"
+ else
+ bad "deploy_release deleted the offline tarball or left scratch: [$(ls "$t/tmp" | tr '\n' ' ')]"
+ fi
+
+ if ! T20B_CURL_FAIL=1 _t20b_deploy && [ "$(_t20b_scratch)" -eq 0 ]; then
+ ok "a failed download leaves no dir behind"
+ else
+ bad "a failed download stranded its dir: [$(ls "$t/tmp" | tr '\n' ' ')]"
+ fi
+fi
+
# --------------------------------------------------------------------------
# T21 — U5/U8: a health-check-triggered rollback halts, which fires the EXIT
# trap (cleanup_on_exit) — that trap must NOT run a second rollback on top of
diff --git a/scripts/update.sh b/scripts/update.sh
index 465a4ee92..cee3c85e 100644
--- a/scripts/update.sh
+++ b/scripts/update.sh
@@ -848,13 +848,21 @@ download_release() {
tmp_dir="$(mktemp -d)"
output="$tmp_dir/serverkit-${version}-linux-${arch}.tar.gz"
+ # This runs in a command substitution, so the caller never learns tmp_dir
+ # on failure: remove it here before halting, or every failed download
+ # strands a directory in /tmp.
step "Downloading release tarball (${arch})..." >&2
- curl -sfL "$tarball_url" -o "$output" || halt "Failed to download $tarball_url"
+ if ! curl -sfL "$tarball_url" -o "$output"; then
+ rm -rf "$tmp_dir"
+ halt "Failed to download $tarball_url"
+ fi
step "Verifying checksum..." >&2
if curl -sfL "$checksum_url" -o "$tmp_dir/checksums.txt"; then
cd "$tmp_dir"
if ! sha256sum -c <(grep "serverkit-${version}-linux-${arch}.tar.gz" checksums.txt) >/dev/null 2>&1; then
+ cd /
+ rm -rf "$tmp_dir"
halt "Checksum verification failed for release tarball."
fi
good "Checksum verified" >&2
@@ -865,6 +873,20 @@ download_release() {
echo "$output"
}
+# Remove release downloads that earlier updates left in the temp dir (before
+# the download dir was cleaned up, every update stranded ~70 MB there). Only
+# directories holding a release tarball and untouched for an hour are taken,
+# so a concurrent download is never pulled out from under itself.
+sweep_stale_downloads() {
+ local tarball
+ while IFS= read -r tarball; do
+ [ -n "$tarball" ] || continue
+ rm -rf "$(dirname "$tarball")" 2>/dev/null || true
+ done < <(find "${TMPDIR:-/tmp}" -mindepth 2 -maxdepth 2 -path '*/tmp.*/serverkit-*-linux-*.tar.gz' \
+ -mmin +60 2>/dev/null || true)
+ return 0
+}
+
# ---------------------------------------------------------------------------
# Blue/green directory management
# ---------------------------------------------------------------------------
@@ -1323,14 +1345,23 @@ deploy_release() {
phase "Downloading Release"
local tarball stage unpacked
+ sweep_stale_downloads
tarball="$(download_release "$version")"
+ # The download dir is ours to remove; an operator's offline tarball is not.
+ # Both scratch dirs are recorded globally so cleanup_on_exit removes them
+ # when a halt below aborts the update.
+ if [ "$tarball" != "${SERVERKIT_OFFLINE_TARBALL:-}" ]; then
+ RELEASE_DOWNLOAD_DIR="$(dirname "$tarball")"
+ fi
if [ "$DRY_RUN" = "1" ]; then
info "[dry-run] would unpack $tarball into $target"
+ remove_release_scratch
return 0
fi
stage="$(mktemp -d)"
+ RELEASE_STAGE_DIR="$stage"
tar xzf "$tarball" -C "$stage"
unpacked="$stage/serverkit"
@@ -1355,7 +1386,7 @@ deploy_release() {
rm -rf "$target"
cp -a "$unpacked" "$target"
- rm -rf "$stage"
+ remove_release_scratch
chmod +x "$target/serverkit"
chmod +x "$target/scripts/"*.sh 2>/dev/null || true
@@ -1564,11 +1595,26 @@ rollback() {
fi
}
+# Remove deploy_release's download and unpack dirs. Warn-and-continue: a
+# scratch dir that will not delete must never fail an update or its rollback.
+remove_release_scratch() {
+ if [ -n "${RELEASE_DOWNLOAD_DIR:-}" ]; then
+ rm -rf "$RELEASE_DOWNLOAD_DIR" 2>/dev/null || true
+ RELEASE_DOWNLOAD_DIR=""
+ fi
+ if [ -n "${RELEASE_STAGE_DIR:-}" ]; then
+ rm -rf "$RELEASE_STAGE_DIR" 2>/dev/null || true
+ RELEASE_STAGE_DIR=""
+ fi
+ return 0
+}
+
# If the update fails after we have switched the symlink, roll back to the
# previous slot automatically. Registered as the EXIT trap by the run block
# below; defined here (above the source guard) so it stays unit-testable.
cleanup_on_exit() {
local rc=$?
+ remove_release_scratch
[ "$rc" -eq 0 ] && return 0
if [ "$DRY_RUN" = "0" ] && [ -n "${PREVIOUS_DIR:-}" ] && \
[ "${ROLLING_BACK:-0}" != "1" ] && [ "${HEALTH_PASSED:-0}" != "1" ]; then