| 1 | #!/usr/bin/env bash
|
|---|
| 2 | #
|
|---|
| 3 | # Add a Klonkt instance. An instance is a data directory and an .env file; the
|
|---|
| 4 | # code in /opt/klonkt is shared with every other instance and is not copied.
|
|---|
| 5 | #
|
|---|
| 6 | # sudo bash scripts/klonkt-add-instance.sh <slug> <domain> [port]
|
|---|
| 7 | # sudo bash scripts/klonkt-add-instance.sh blog blog.example.com --no-caddy
|
|---|
| 8 | #
|
|---|
| 9 | # Leave the port out and a free one is chosen. Run klonkt-update once and every
|
|---|
| 10 | # instance on the machine moves to the new code together.
|
|---|
| 11 |
|
|---|
| 12 | set -euo pipefail
|
|---|
| 13 |
|
|---|
| 14 | KLONKT_DIR="${KLONKT_DIR:-/opt/klonkt}"
|
|---|
| 15 | KLONKT_USER="${KLONKT_USER:-klonkt}"
|
|---|
| 16 | DATA_ROOT="${KLONKT_DATA_ROOT:-/var/lib/klonkt}"
|
|---|
| 17 | NO_CADDY="${KLONKT_NO_CADDY:-}"
|
|---|
| 18 | LANG_DEFAULT="${KLONKT_DEFAULT_LANG:-}"
|
|---|
| 19 |
|
|---|
| 20 | SLUG=""; DOMAIN=""; PORT=""
|
|---|
| 21 | for arg in "$@"; do
|
|---|
| 22 | case "$arg" in
|
|---|
| 23 | --no-caddy) NO_CADDY=1 ;;
|
|---|
| 24 | -*) echo "unknown option: $arg" >&2; exit 2 ;;
|
|---|
| 25 | *) if [ -z "$SLUG" ]; then SLUG="$arg"
|
|---|
| 26 | elif [ -z "$DOMAIN" ]; then DOMAIN="$arg"
|
|---|
| 27 | elif [ -z "$PORT" ]; then PORT="$arg"
|
|---|
| 28 | fi ;;
|
|---|
| 29 | esac
|
|---|
| 30 | done
|
|---|
| 31 |
|
|---|
| 32 | say() { printf ' %s\n' "$*"; }
|
|---|
| 33 | step() { printf '\n== %s\n' "$*"; }
|
|---|
| 34 | die() { printf '\nERROR: %s\n' "$*" >&2; exit 1; }
|
|---|
| 35 |
|
|---|
| 36 | [ "$(id -u)" = 0 ] || die "run this as root (sudo)."
|
|---|
| 37 | [ -n "$SLUG" ] && [ -n "$DOMAIN" ] || die "usage: $0 <slug> <domain> [port] [--no-caddy]"
|
|---|
| 38 | [[ "$SLUG" =~ ^[a-z0-9][a-z0-9._-]*$ ]] || die "slug must be lowercase letters, digits, dot, dash or underscore."
|
|---|
| 39 |
|
|---|
| 40 | DATA_DIR="$DATA_ROOT/$SLUG"
|
|---|
| 41 | ENV_FILE="$DATA_DIR/.env"
|
|---|
| 42 |
|
|---|
| 43 | step "Preflight"
|
|---|
| 44 | [ -d "$KLONKT_DIR" ] || die "no shared code at $KLONKT_DIR. Install Klonkt first."
|
|---|
| 45 | [ -f "$KLONKT_DIR/src/config/paths.js" ] || die \
|
|---|
| 46 | "this build is too old to share one checkout between instances.
|
|---|
| 47 | Update first: klonkt-update"
|
|---|
| 48 | id -u "$KLONKT_USER" >/dev/null 2>&1 || die "user $KLONKT_USER does not exist"
|
|---|
| 49 | [ -e "$DATA_DIR" ] && die "$DATA_DIR already exists. Pick another slug."
|
|---|
| 50 | [ -f /etc/systemd/system/klonkt@.service ] || {
|
|---|
| 51 | [ -f "$KLONKT_DIR/deploy/klonkt@.service" ] || die "missing $KLONKT_DIR/deploy/klonkt@.service"
|
|---|
| 52 | install -m 0644 "$KLONKT_DIR/deploy/klonkt@.service" /etc/systemd/system/klonkt@.service
|
|---|
| 53 | systemctl daemon-reload
|
|---|
| 54 | say "installed the systemd template (first instance on this machine)"
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | # Pick a port nobody is listening on and no other instance has claimed.
|
|---|
| 58 | if [ -z "$PORT" ]; then
|
|---|
| 59 | for p in $(seq 3000 3099); do
|
|---|
| 60 | grep -rqs "^PORT=${p}$" "$DATA_ROOT"/*/.env && continue
|
|---|
| 61 | ss -ltnH "sport = :$p" 2>/dev/null | grep -q . && continue
|
|---|
| 62 | PORT="$p"; break
|
|---|
| 63 | done
|
|---|
| 64 | [ -n "$PORT" ] || die "no free port found in 3000-3099; pass one explicitly."
|
|---|
| 65 | fi
|
|---|
| 66 | say "slug $SLUG, domain $DOMAIN, port $PORT"
|
|---|
| 67 |
|
|---|
| 68 | step "Creating $DATA_DIR"
|
|---|
| 69 | mkdir -p "$DATA_DIR"
|
|---|
| 70 |
|
|---|
| 71 | step "Writing .env"
|
|---|
| 72 | SECRET="$(openssl rand -hex 32)"
|
|---|
| 73 | {
|
|---|
| 74 | echo "NODE_ENV=production"
|
|---|
| 75 | echo "PORT=${PORT}"
|
|---|
| 76 | # Loopback only: the reverse proxy reaches it, the internet cannot bypass HTTPS.
|
|---|
| 77 | echo "HOST=127.0.0.1"
|
|---|
| 78 | echo "SESSION_SECRET=${SECRET}"
|
|---|
| 79 | echo "PUBLIC_BASE_URL=https://${DOMAIN}"
|
|---|
| 80 | echo "DATABASE_PATH=${DATA_DIR}/database.sqlite"
|
|---|
| 81 | echo "MEDIA_PATH=${DATA_DIR}/media"
|
|---|
| 82 | echo "AUDIO_PATH=${DATA_DIR}/audio"
|
|---|
| 83 | [ -n "$LANG_DEFAULT" ] && echo "KLONKT_DEFAULT_LANG=${LANG_DEFAULT}"
|
|---|
| 84 | } > "$ENV_FILE"
|
|---|
| 85 | say "random SESSION_SECRET, data paths under $DATA_DIR"
|
|---|
| 86 |
|
|---|
| 87 | step "Ownership and permissions"
|
|---|
| 88 | chown -R "$KLONKT_USER:$KLONKT_USER" "$DATA_DIR"
|
|---|
| 89 | chmod 750 "$DATA_DIR"
|
|---|
| 90 | chmod 600 "$ENV_FILE"
|
|---|
| 91 | say "owned by $KLONKT_USER; .env readable only by that user"
|
|---|
| 92 |
|
|---|
| 93 | step "Starting klonkt@$SLUG"
|
|---|
| 94 | systemctl enable --now "klonkt@$SLUG"
|
|---|
| 95 | sleep 3
|
|---|
| 96 | systemctl is-active --quiet "klonkt@$SLUG" || {
|
|---|
| 97 | echo; journalctl -u "klonkt@$SLUG" -n 30 --no-pager || true
|
|---|
| 98 | die "klonkt@$SLUG did not start."
|
|---|
| 99 | }
|
|---|
| 100 | curl -fsS --max-time 8 -o /dev/null "http://127.0.0.1:${PORT}/" \
|
|---|
| 101 | && say "responding on 127.0.0.1:${PORT}" \
|
|---|
| 102 | || say "WARNING: no answer yet on 127.0.0.1:${PORT}; check journalctl -u klonkt@$SLUG -f"
|
|---|
| 103 |
|
|---|
| 104 | if [ -z "$NO_CADDY" ] && command -v caddy >/dev/null 2>&1; then
|
|---|
| 105 | step "Caddy"
|
|---|
| 106 | CADDY=/etc/caddy/Caddyfile
|
|---|
| 107 | if grep -q "^${DOMAIN} {" "$CADDY" 2>/dev/null; then
|
|---|
| 108 | say "a block for ${DOMAIN} already exists, left untouched"
|
|---|
| 109 | else
|
|---|
| 110 | cp "$CADDY" "${CADDY}.bak.$(date +%s)" 2>/dev/null || true
|
|---|
| 111 | printf '\n%s {\n reverse_proxy 127.0.0.1:%s\n encode gzip zstd\n}\n' "$DOMAIN" "$PORT" >> "$CADDY"
|
|---|
| 112 | caddy validate --config "$CADDY" --adapter caddyfile >/dev/null 2>&1 \
|
|---|
| 113 | || die "Caddy config invalid after adding ${DOMAIN} — check $CADDY (a .bak was made)"
|
|---|
| 114 | systemctl reload caddy 2>/dev/null || systemctl restart caddy
|
|---|
| 115 | say "serving ${DOMAIN}"
|
|---|
| 116 | fi
|
|---|
| 117 | fi
|
|---|
| 118 |
|
|---|
| 119 | cat <<EOF
|
|---|
| 120 |
|
|---|
| 121 | Instance ready.
|
|---|
| 122 |
|
|---|
| 123 | data $DATA_DIR
|
|---|
| 124 | unit klonkt@$SLUG
|
|---|
| 125 | port 127.0.0.1:$PORT
|
|---|
| 126 | code $KLONKT_DIR (shared with every other instance)
|
|---|
| 127 |
|
|---|
| 128 | Next: open https://${DOMAIN}/auth/register and create the admin account.
|
|---|
| 129 |
|
|---|
| 130 | status systemctl status klonkt@$SLUG
|
|---|
| 131 | logs journalctl -u klonkt@$SLUG -f
|
|---|
| 132 | update klonkt-update # updates the code once, restarts all instances
|
|---|
| 133 | backup $DATA_DIR # this directory is the whole instance
|
|---|
| 134 | EOF
|
|---|