source: Klonkt/scripts/install.sh@ 1273101

main
Last change on this file since 1273101 was 1273101, checked in by roboburr <roboburr@…>, 2 months ago

fix(install): reset to FETCH_HEAD so updates work across branch/refspec (fixes "origin/stable unknown revision")

git fetch origin <branch> only updates FETCH_HEAD, not refs/remotes/origin/<branch>, on a
single-branch/shallow clone (or one originally on a different branch) — so `reset --hard
origin/stable` failed with "fatal: ambiguous argument 'origin/stable'", notably when switching
an existing main install to the stable channel. Reset to FETCH_HEAD instead, which always points
at what was just fetched.

  • scripts/install.sh — both the installer's re-run update step and the generated klonkt-update now reset --hard FETCH_HEAD instead of origin/$KLONKT_BRANCH

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[83088b66]1#!/usr/bin/env bash
2#
[bb42dfb]3# Klonkt — installer for a Debian/Ubuntu VPS.
4# Installs Node 20, Caddy (automatic HTTPS) and Klonkt as a systemd service.
[83088b66]5#
[bb42dfb]6# Safe on a server that ALREADY runs things: it won't upgrade your system Node,
7# auto-picks a free port, and skips Caddy if a webserver/reverse-proxy is already
8# listening on port 80/443 (you then get instructions to put Klonkt behind your
9# own proxy).
[83088b66]10#
[bb42dfb]11# Usage (as root), non-interactive:
[83088b66]12# curl -fsSL https://raw.githubusercontent.com/roboburr/klonkt/main/scripts/install.sh \
[bb42dfb]13# | sudo bash -s -- --domain klonkt.example.com
14# Or interactively from a downloaded file:
[83088b66]15# sudo bash install.sh
16#
[bb42dfb]17# Re-running on the same server = update (git pull + restart).
18# Fully isolated alternative: Docker (see docker-compose.yml in the repo).
[83088b66]19#
20set -euo pipefail
21
[bb42dfb]22# ── Settings (override via env var or flag) ────────────────────────────────
23KLONKT_REPO="${KLONKT_REPO:-https://github.com/roboburr/klonkt.git}"
[0343a7a]24# `stable` = the release channel: it only moves forward to a version that has been verified,
25# so a self-host auto-update (klonkt-update) never pulls work-in-progress. Use `--branch main`
26# for the bleeding-edge dev branch instead.
27KLONKT_BRANCH="${KLONKT_BRANCH:-stable}"
[83088b66]28KLONKT_DIR="${KLONKT_DIR:-/opt/klonkt}"
29KLONKT_USER="${KLONKT_USER:-klonkt}"
30KLONKT_PORT="${KLONKT_PORT:-3000}"
31KLONKT_DOMAIN="${KLONKT_DOMAIN:-}"
32KLONKT_LANG="${KLONKT_DEFAULT_LANG:-}"
33NODE_MAJOR="${NODE_MAJOR:-20}"
[bb42dfb]34NO_CADDY="${KLONKT_NO_CADDY:-}" # set to 1 to NEVER install Caddy (own proxy)
35NODE_FORCE="${NODE_FORCE:-}" # set to 1 to (re)install system Node anyway
[83088b66]36PORT_EXPLICIT=0
37
38while [ $# -gt 0 ]; do
39 case "$1" in
40 --domain) KLONKT_DOMAIN="$2"; shift 2;;
41 --repo) KLONKT_REPO="$2"; shift 2;;
42 --branch) KLONKT_BRANCH="$2"; shift 2;;
43 --dir) KLONKT_DIR="$2"; shift 2;;
44 --port) KLONKT_PORT="$2"; PORT_EXPLICIT=1; shift 2;;
45 --lang) KLONKT_LANG="$2"; shift 2;;
46 --no-caddy) NO_CADDY=1; shift;;
47 --force-node) NODE_FORCE=1; shift;;
48 -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0;;
[bb42dfb]49 *) echo "Unknown option: $1" >&2; exit 1;;
[83088b66]50 esac
51done
52
53log() { printf '\n\033[1;33m▸ %s\033[0m\n' "$*"; }
54ok() { printf '\033[1;32m ✓ %s\033[0m\n' "$*"; }
55warn() { printf '\033[1;33m ! %s\033[0m\n' "$*"; }
56die() { printf '\033[1;31m✗ %s\033[0m\n' "$*" >&2; exit 1; }
57as_klonkt() { runuser -u "$KLONKT_USER" -- env HOME="$KLONKT_DIR" "$@"; }
58port_busy() { ss -ltnH 2>/dev/null | awk '{print $4}' | grep -qE "[:.]${1}$"; }
59
[bb42dfb]60[ "$(id -u)" = 0 ] || die "Run this as root (sudo bash install.sh)."
61command -v apt-get >/dev/null || die "Debian/Ubuntu only (apt). On other systems use the Docker route."
[83088b66]62
63if [ -z "$KLONKT_DOMAIN" ]; then
[bb42dfb]64 read -rp "Domain for Klonkt (e.g. klonkt.example.com): " KLONKT_DOMAIN </dev/tty || true
[83088b66]65fi
[bb42dfb]66[ -n "$KLONKT_DOMAIN" ] || die "No domain given (--domain or KLONKT_DOMAIN)."
[83088b66]67case "$KLONKT_REPO" in
[bb42dfb]68 *OWNER/*) die "Set the real repo URL first: --repo https://github.com/<you>/klonkt.git (or KLONKT_REPO=...).";;
[83088b66]69esac
70
71export DEBIAN_FRONTEND=noninteractive
72
[bb42dfb]73# ── Preflight: see what's already running, adapt instead of clobbering ──────
74log "Preflight (what's already running?)…"
[83088b66]75apt-get update -y >/dev/null
76apt-get install -y iproute2 >/dev/null 2>&1 || true
77
[bb42dfb]78# Port: busy? With --port → error. Otherwise auto-pick a free one.
[83088b66]79if port_busy "$KLONKT_PORT"; then
80 if [ "$PORT_EXPLICIT" = 1 ]; then
[bb42dfb]81 die "Port ${KLONKT_PORT} is already in use. Pick a free port with --port."
[83088b66]82 fi
83 picked=""
84 for p in $(seq "$KLONKT_PORT" $((KLONKT_PORT+30))); do
85 port_busy "$p" || { picked="$p"; break; }
86 done
[bb42dfb]87 [ -n "$picked" ] || die "No free port found near ${KLONKT_PORT}. Provide one with --port."
88 warn "port ${KLONKT_PORT} busy → Klonkt uses ${picked}"
[83088b66]89 KLONKT_PORT="$picked"
90else
[bb42dfb]91 ok "port ${KLONKT_PORT} free"
[83088b66]92fi
93
[bb42dfb]94# Webserver on 80/443 that isn't Caddy? → skip Caddy, own-proxy mode.
[83088b66]95FOREIGN_PROXY=0
96if [ -z "$NO_CADDY" ] && command -v ss >/dev/null 2>&1; then
97 if ss -ltnpH 2>/dev/null | grep -E '[:.](80|443) ' | grep -viq 'caddy'; then
98 NO_CADDY=1; FOREIGN_PROXY=1
[bb42dfb]99 warn "something is already listening on port 80/443 (not Caddy) → NOT installing Caddy; you'll get proxy instructions"
[83088b66]100 fi
101fi
102
[bb42dfb]103# ── Node: respect an existing version, don't silently upgrade ──────────────
[83088b66]104log "Node ${NODE_MAJOR}.x…"
105if command -v node >/dev/null 2>&1 && [ -z "$NODE_FORCE" ]; then
106 CUR="$(node -v | sed 's/v//;s/\..*//')"
107 if [ "$CUR" -lt "$NODE_MAJOR" ]; then
[bb42dfb]108 die "Node $(node -v) is already installed on this server; Klonkt needs ≥${NODE_MAJOR}.
109 I will NOT auto-upgrade your system Node — that could break other apps.
110 Options: (a) use the Docker route (own Node, touches nothing), or
111 (b) upgrade Node yourself, or (c) force with NODE_FORCE=1 (at your own risk)."
[83088b66]112 fi
[bb42dfb]113 ok "using existing node $(node -v)"
[83088b66]114else
115 curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -
116 apt-get install -y nodejs
[bb42dfb]117 ok "node $(node -v) installed"
[83088b66]118fi
119
[bb42dfb]120log "Other packages…"
[83088b66]121apt-get install -y curl ca-certificates git gnupg openssl build-essential python3
[bb42dfb]122apt-get install -y webp >/dev/null 2>&1 || true # cwebp = image→WebP (optional)
123ok "base packages"
[83088b66]124
125if [ -z "$NO_CADDY" ]; then
126 log "Caddy (reverse proxy + auto-HTTPS)…"
127 if ! command -v caddy >/dev/null 2>&1; then
128 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
129 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list
130 apt-get update -y
131 apt-get install -y caddy
132 fi
[bb42dfb]133 ok "caddy present"
[83088b66]134fi
135
[bb42dfb]136log "Service user '${KLONKT_USER}'…"
[83088b66]137id -u "$KLONKT_USER" >/dev/null 2>&1 || useradd --system --home-dir "$KLONKT_DIR" --shell /usr/sbin/nologin "$KLONKT_USER"
[bb42dfb]138ok "user"
[83088b66]139
[bb42dfb]140log "Fetching Klonkt source…"
[83088b66]141if [ -d "$KLONKT_DIR/.git" ]; then
142 git -C "$KLONKT_DIR" remote set-url origin "$KLONKT_REPO"
143 git -C "$KLONKT_DIR" fetch --depth 1 origin "$KLONKT_BRANCH"
[1273101]144 # Reset to FETCH_HEAD, not origin/$KLONKT_BRANCH: a single-branch/shallow clone (or one
145 # originally on a different branch, e.g. main → stable) has no origin/<branch> tracking ref,
146 # so `reset --hard origin/stable` fails with "ambiguous argument". FETCH_HEAD always points
147 # at what we just fetched, regardless of refspec or which branch the clone started on.
148 git -C "$KLONKT_DIR" reset --hard FETCH_HEAD
[83088b66]149else
[bb42dfb]150 [ -e "$KLONKT_DIR" ] && [ -n "$(ls -A "$KLONKT_DIR" 2>/dev/null)" ] && die "$KLONKT_DIR already exists and is not a git checkout. Pick --dir, or clean it up."
[83088b66]151 mkdir -p "$KLONKT_DIR"
152 git clone --depth 1 --branch "$KLONKT_BRANCH" "$KLONKT_REPO" "$KLONKT_DIR"
153fi
154mkdir -p "$KLONKT_DIR/storage/media" "$KLONKT_DIR/storage/audio"
155chown -R "$KLONKT_USER:$KLONKT_USER" "$KLONKT_DIR"
156ok "code in $KLONKT_DIR"
157
[bb42dfb]158log "Installing dependencies (npm ci)…"
[83088b66]159as_klonkt bash -c "cd '$KLONKT_DIR' && npm ci --omit=dev"
160ok "node_modules"
161
162log ".env…"
163ENV="$KLONKT_DIR/.env"
164if [ ! -f "$ENV" ]; then
165 SECRET="$(openssl rand -hex 32)"
166 {
167 echo "NODE_ENV=production"
168 echo "PORT=${KLONKT_PORT}"
[f99bbe8]169 # Bind to loopback only: Caddy (this host) reaches it; the internet cannot
170 # hit the app directly on its port, bypassing HTTPS.
171 echo "HOST=127.0.0.1"
[83088b66]172 echo "SESSION_SECRET=${SECRET}"
173 echo "DATABASE_PATH=./storage/database.sqlite"
174 echo "MEDIA_PATH=./storage/media"
175 echo "AUDIO_PATH=./storage/audio"
176 echo "PUBLIC_BASE_URL=https://${KLONKT_DOMAIN}"
177 [ -n "$KLONKT_LANG" ] && echo "KLONKT_DEFAULT_LANG=${KLONKT_LANG}"
178 } > "$ENV"
179 chown "$KLONKT_USER:$KLONKT_USER" "$ENV"; chmod 600 "$ENV"
[f99bbe8]180 ok "new .env (random SESSION_SECRET, app bound to 127.0.0.1)"
[83088b66]181else
[bb42dfb]182 # sync the port in an existing .env with the chosen port
[83088b66]183 if grep -q '^PORT=' "$ENV"; then sed -i "s/^PORT=.*/PORT=${KLONKT_PORT}/" "$ENV"; fi
[f99bbe8]184 # harden older installs: bind to loopback if not already configured
185 grep -q '^HOST=' "$ENV" || echo "HOST=127.0.0.1" >> "$ENV"
186 ok "kept existing .env (port synced, bound to 127.0.0.1)"
[83088b66]187fi
188
[bb42dfb]189log "systemd service…"
[83088b66]190NODE_BIN="$(command -v node)"
191cat > /etc/systemd/system/klonkt.service <<EOF
192[Unit]
193Description=Klonkt
194After=network-online.target
195Wants=network-online.target
196
197[Service]
198Type=simple
199User=${KLONKT_USER}
200WorkingDirectory=${KLONKT_DIR}
201ExecStart=${NODE_BIN} src/server.js
202Environment=NODE_ENV=production
203Restart=always
204RestartSec=3
205NoNewPrivileges=true
206ProtectSystem=full
207PrivateTmp=true
208
209[Install]
210WantedBy=multi-user.target
211EOF
212systemctl daemon-reload
213systemctl enable --now klonkt
[bb42dfb]214ok "klonkt.service running on 127.0.0.1:${KLONKT_PORT}"
[83088b66]215
216if [ -z "$NO_CADDY" ]; then
[bb42dfb]217 log "Caddy config for ${KLONKT_DOMAIN}…"
[83088b66]218 CADDY=/etc/caddy/Caddyfile
219 SITE_BLOCK="${KLONKT_DOMAIN} {
220 reverse_proxy 127.0.0.1:${KLONKT_PORT}
221 encode gzip zstd
222}"
223 touch "$CADDY"
224 if grep -q '/usr/share/caddy' "$CADDY"; then
225 cp "$CADDY" "${CADDY}.bak.$(date +%s)"
226 printf '%s\n' "$SITE_BLOCK" > "$CADDY"
227 elif ! grep -q "^${KLONKT_DOMAIN} {" "$CADDY"; then
228 printf '\n%s\n' "$SITE_BLOCK" >> "$CADDY"
229 fi
[bb42dfb]230 caddy validate --config "$CADDY" --adapter caddyfile >/dev/null 2>&1 || die "Caddy config invalid — check $CADDY"
[83088b66]231 systemctl reload caddy 2>/dev/null || systemctl restart caddy
[bb42dfb]232 ok "caddy serving ${KLONKT_DOMAIN}"
[83088b66]233fi
234
[bb42dfb]235log "Update command 'klonkt-update'…"
[83088b66]236cat > /usr/local/bin/klonkt-update <<EOF
237#!/usr/bin/env bash
238set -euo pipefail
239D="${KLONKT_DIR}"
240B=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD 2>/dev/null || true)
241runuser -u ${KLONKT_USER} -- git -C "\$D" fetch --depth 1 origin ${KLONKT_BRANCH}
[1273101]242runuser -u ${KLONKT_USER} -- git -C "\$D" reset --hard FETCH_HEAD
[83088b66]243A=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD)
[09a2061]244if [ "\$B" = "\$A" ]; then
245 echo "Klonkt is already up to date (\$A) — nothing to do."
246 exit 0
247fi
[83088b66]248if ! runuser -u ${KLONKT_USER} -- git -C "\$D" diff --quiet "\$B" "\$A" -- package-lock.json 2>/dev/null; then
249 runuser -u ${KLONKT_USER} -- env HOME="\$D" bash -c "cd '\$D' && npm ci --omit=dev"
250fi
251systemctl restart klonkt
[bb42dfb]252echo "Klonkt updated (\$A) + restarted."
[83088b66]253EOF
254chmod +x /usr/local/bin/klonkt-update
255ok "klonkt-update"
256
257echo
258echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
[bb42dfb]259echo " Klonkt is running! 🎉"
[83088b66]260echo
261if [ -n "$NO_CADDY" ]; then
[bb42dfb]262 echo " Klonkt listens on: http://127.0.0.1:${KLONKT_PORT}"
[83088b66]263 if [ "$FOREIGN_PROXY" = 1 ]; then
[bb42dfb]264 echo " A webserver is already running on 80/443 — put Klonkt behind it."
[83088b66]265 fi
[bb42dfb]266 echo " Example nginx:"
[83088b66]267 echo " location / { proxy_pass http://127.0.0.1:${KLONKT_PORT}; proxy_set_header Host \$host;"
268 echo " proxy_set_header X-Forwarded-Proto \$scheme; }"
[bb42dfb]269 echo " Example Caddy:"
[83088b66]270 echo " ${KLONKT_DOMAIN} { reverse_proxy 127.0.0.1:${KLONKT_PORT} }"
271else
[bb42dfb]272 echo " • Open your site: https://${KLONKT_DOMAIN}"
[83088b66]273fi
[bb42dfb]274echo " • First run: go to /auth/register and create your admin account."
[83088b66]275echo
[bb42dfb]276echo " Manage: systemctl status klonkt · journalctl -u klonkt -f · klonkt-update"
277echo " Lost password: cd ${KLONKT_DIR} && runuser -u ${KLONKT_USER} -- env HOME=${KLONKT_DIR} npm run reset-admin"
[83088b66]278echo
[bb42dfb]279echo " DNS: make sure A + AAAA of ${KLONKT_DOMAIN} point to this server."
[83088b66]280echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
Note: See TracBrowser for help on using the repository browser.