162 lines
5.7 KiB
Bash
Executable File
162 lines
5.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Install or upgrade Immich Magic Proxy inside a Debian 13 (trixie) LXC
|
|
# container, as a systemd service. Run as root inside the container:
|
|
#
|
|
# ./install.sh immich-magic-proxy-<version>.tar.gz
|
|
#
|
|
# Re-running with a newer tarball upgrades in place. Settings in
|
|
# /etc/immich-magic-proxy and data in /var/lib/immich-magic-proxy are kept.
|
|
set -euo pipefail
|
|
|
|
APP=immich-magic-proxy
|
|
PREFIX=/opt/$APP
|
|
CONF_DIR=/etc/$APP
|
|
DATA_DIR=/var/lib/$APP
|
|
NODE_MAJOR=24
|
|
KEEP_RELEASES=3
|
|
|
|
die () { echo "error: $*" >&2; exit 1; }
|
|
log () { echo "==> $*"; }
|
|
|
|
[ "$(id -u)" -eq 0 ] || die "run as root"
|
|
[ $# -eq 1 ] && [ -f "$1" ] || die "usage: $0 <release tarball>"
|
|
tarball=$(readlink -f "$1")
|
|
[ -r /etc/debian_version ] || die "this installer is for Debian"
|
|
|
|
# ----- Node.js ---------------------------------------------------------------
|
|
# Debian's own nodejs is too old (node:sqlite needs 22.13+), so use NodeSource's
|
|
# signed apt repository, which also delivers Node security updates via apt.
|
|
node_major () { node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0; }
|
|
if [ "$(node_major)" -lt "$NODE_MAJOR" ]; then
|
|
log "Installing Node.js $NODE_MAJOR"
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get update -q
|
|
apt-get install -y -q --no-install-recommends ca-certificates curl gpg
|
|
install -d -m 0755 /etc/apt/keyrings
|
|
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor --yes -o /etc/apt/keyrings/nodesource.gpg
|
|
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" \
|
|
> /etc/apt/sources.list.d/nodesource.list
|
|
apt-get update -q
|
|
apt-get install -y -q --no-install-recommends nodejs
|
|
fi
|
|
[ "$(node_major)" -ge "$NODE_MAJOR" ] || die "Node.js $NODE_MAJOR could not be installed"
|
|
|
|
# ----- user and directories --------------------------------------------------
|
|
if ! id "$APP" >/dev/null 2>&1; then
|
|
log "Creating system user $APP"
|
|
useradd --system --home-dir "$DATA_DIR" --no-create-home --shell /usr/sbin/nologin "$APP"
|
|
fi
|
|
install -d -m 0755 -o root -g root "$PREFIX" "$PREFIX/releases"
|
|
install -d -m 0750 -o root -g "$APP" "$CONF_DIR"
|
|
install -d -m 0750 -o "$APP" -g "$APP" "$DATA_DIR"
|
|
|
|
# ----- unpack the release ----------------------------------------------------
|
|
staging=$(mktemp -d)
|
|
trap 'rm -rf -- "$staging"' EXIT
|
|
tar -xzf "$tarball" -C "$staging"
|
|
release_dir=$(find "$staging" -mindepth 1 -maxdepth 1 -type d | head -n1)
|
|
[ -n "$release_dir" ] && [ -f "$release_dir/dist/index.js" ] || die "$1 is not a release tarball"
|
|
version=$(cat "$release_dir/VERSION")
|
|
target="$PREFIX/releases/$version"
|
|
log "Installing release $version"
|
|
if [ ! -d "$target" ]; then
|
|
# Code is root-owned, so the service account cannot modify it
|
|
chown -R root:root "$release_dir"
|
|
chmod -R u=rwX,go=rX "$release_dir"
|
|
mv "$release_dir" "$target"
|
|
fi
|
|
# Stamp with the install time (tar restores the build's), which pruning sorts by
|
|
touch "$target"
|
|
|
|
# ----- settings (kept across upgrades) --------------------------------------
|
|
if [ ! -f "$CONF_DIR/config.json" ]; then
|
|
install -m 0640 -o root -g "$APP" "$target/config.json" "$CONF_DIR/config.json"
|
|
fi
|
|
if [ ! -f "$CONF_DIR/env" ]; then
|
|
log "Writing $CONF_DIR/env - edit it before first use"
|
|
install -m 0600 -o root -g root /dev/null "$CONF_DIR/env"
|
|
cat > "$CONF_DIR/env" <<'EOF'
|
|
# Immich Magic Proxy settings. Restart after editing:
|
|
# systemctl restart immich-magic-proxy
|
|
# Put a value with spaces or symbols in double quotes, with a backslash
|
|
# before any \ " $ or ` inside it, e.g. ADMIN_PASSWORD="pa\$\$ word"
|
|
|
|
# Immich's internal URL, and this proxy's public URL (used in email links)
|
|
IMMICH_URL=http://immich.lan:2283
|
|
PUBLIC_BASE_URL=https://photos.example.com
|
|
|
|
# Turns on /admin, where you add albums and recipients
|
|
ADMIN_PASSWORD=
|
|
|
|
# Outgoing email for invitations and new-photo notifications
|
|
#SMTP_URL=smtp://user:password@smtp.example.com:587
|
|
#MAIL_FROM="Our photos <photos@example.com>"
|
|
#SITE_NAME="Our photos"
|
|
|
|
# Hour (0-23, in TZ) that notification emails go out
|
|
#DIGEST_HOUR=8
|
|
#TZ=Etc/UTC
|
|
|
|
#IPP_PORT=3000
|
|
EOF
|
|
fi
|
|
# Holds the admin and SMTP passwords. Only root reads it: systemd loads it
|
|
# before the service drops to its own user.
|
|
chown root:root "$CONF_DIR/env"
|
|
chmod 0600 "$CONF_DIR/env"
|
|
|
|
# ----- systemd service -------------------------------------------------------
|
|
cat > /etc/systemd/system/$APP.service <<EOF
|
|
[Unit]
|
|
Description=Immich Magic Proxy
|
|
Wants=network-online.target
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=$APP
|
|
Group=$APP
|
|
WorkingDirectory=$PREFIX/current
|
|
Environment=NODE_ENV=production
|
|
Environment=DATA_DIR=$DATA_DIR
|
|
Environment=IPP_CONFIG=$CONF_DIR/config.json
|
|
EnvironmentFile=$CONF_DIR/env
|
|
ExecStart=/usr/bin/node dist/index.js
|
|
Restart=always
|
|
RestartSec=5
|
|
|
|
# Hardening. These need namespaces, which an LXC container has with nesting
|
|
# on (Proxmox's default for unprivileged containers).
|
|
NoNewPrivileges=yes
|
|
ProtectSystem=strict
|
|
ProtectHome=yes
|
|
PrivateTmp=yes
|
|
ReadWritePaths=$DATA_DIR
|
|
CapabilityBoundingSet=
|
|
RestrictSUIDSGID=yes
|
|
LockPersonality=yes
|
|
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
|
UMask=0077
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
ln -sfn "$target" "$PREFIX/current"
|
|
systemctl daemon-reload
|
|
systemctl enable "$APP" >/dev/null 2>&1
|
|
if grep -q '^ADMIN_PASSWORD=.\+' "$CONF_DIR/env"; then
|
|
systemctl restart "$APP"
|
|
log "Started $APP $version"
|
|
else
|
|
log "Installed $APP $version, not started: set ADMIN_PASSWORD (and the rest) in $CONF_DIR/env, then run"
|
|
log " systemctl restart $APP"
|
|
fi
|
|
|
|
# ----- keep the newest few releases -----------------------------------------
|
|
current=$(readlink -f "$PREFIX/current")
|
|
find "$PREFIX/releases" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' | sort -rn | tail -n +$((KEEP_RELEASES + 1)) |
|
|
while read -r _ old; do
|
|
[ "$old" = "$current" ] || rm -rf -- "$old"
|
|
done
|