Files

180 lines
7.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Create an unprivileged Debian 13 LXC container on a Proxmox VE host and
# install Immich Magic Proxy in it. Run as root on the Proxmox host, with the
# release tarball and lxc/install.sh beside this script:
#
# IMMICH_URL=http://192.168.1.20:2283 PUBLIC_BASE_URL=https://photos.example.com \
# ADMIN_PASSWORD='long random string' ./proxmox-create.sh immich-magic-proxy-<version>.tar.gz
#
# Optional settings (environment variables, defaults in brackets):
# CTID [next free] CT_HOSTNAME [immich-magic-proxy] STORAGE [local-lvm]
# TEMPLATE_STORAGE [local] BRIDGE [vmbr0] IP [dhcp, or e.g. 192.168.1.50/24]
# GATEWAY [] VLAN [] CORES [1] MEMORY [512] DISK [4] PORT [3000]
# SMTP_URL MAIL_FROM SITE_NAME DIGEST_HOUR TZ [host's timezone]
# ROOT_PASSWORD [] SSH_PUBLIC_KEYS [] (a file of public keys for root)
# CONSOLE_AUTOLOGIN [0] (1: the Proxmox console logs in as root, no password)
# Without ROOT_PASSWORD or CONSOLE_AUTOLOGIN=1, root can't log in at the
# Proxmox console; use `pct enter <ctid>` on the host instead.
# ADMIN_PASSWORD is only for /admin.
set -euo pipefail
die () { echo "error: $*" >&2; exit 1; }
log () { echo "==> $*"; }
command -v pct >/dev/null || die "run this on a Proxmox VE host"
[ "$(id -u)" -eq 0 ] || die "run as root"
[ $# -eq 1 ] && [ -f "$1" ] || die "usage: $0 <release tarball>"
tarball=$(readlink -f "$1")
installer="$(dirname "$(readlink -f "$0")")/install.sh"
[ -f "$installer" ] || die "install.sh must be next to this script"
: "${IMMICH_URL:?set IMMICH_URL to the internal URL of Immich}"
: "${PUBLIC_BASE_URL:?set PUBLIC_BASE_URL to the public URL of this proxy}"
: "${ADMIN_PASSWORD:?set ADMIN_PASSWORD for the /admin console}"
CTID=${CTID:-$(pvesh get /cluster/nextid)}
# Not HOSTNAME: bash sets that to the Proxmox host's own name
CT_HOSTNAME=${CT_HOSTNAME:-immich-magic-proxy}
STORAGE=${STORAGE:-local-lvm}
TEMPLATE_STORAGE=${TEMPLATE_STORAGE:-local}
BRIDGE=${BRIDGE:-vmbr0}
IP=${IP:-dhcp}
CORES=${CORES:-1}
MEMORY=${MEMORY:-512}
DISK=${DISK:-4}
PORT=${PORT:-3000}
CONSOLE_AUTOLOGIN=${CONSOLE_AUTOLOGIN:-0}
case "$CONSOLE_AUTOLOGIN" in 0|1) ;; *) die "CONSOLE_AUTOLOGIN must be 0 or 1" ;; esac
TZ=${TZ:-$(cat /etc/timezone 2>/dev/null || timedatectl show -p Timezone --value 2>/dev/null || echo Etc/UTC)}
net="name=eth0,bridge=$BRIDGE,ip=$IP"
[ -n "${GATEWAY:-}" ] && net="$net,gw=$GATEWAY"
[ -n "${VLAN:-}" ] && net="$net,tag=$VLAN"
ssh_args=()
if [ -n "${SSH_PUBLIC_KEYS:-}" ]; then
[ -f "$SSH_PUBLIC_KEYS" ] || die "SSH_PUBLIC_KEYS must be a file of public keys"
ssh_args=(--ssh-public-keys "$SSH_PUBLIC_KEYS")
fi
# ----- Debian 13 template ----------------------------------------------------
log "Finding the Debian 13 template"
pveam update >/dev/null
template=$(pveam available --section system | awk '{print $2}' | grep -E '^debian-13-standard_.*_amd64\.tar\.(zst|xz|gz)$' | sort -V | tail -n1)
[ -n "$template" ] || die "no debian-13-standard template offered by pveam"
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$template"; then
log "Downloading $template"
pveam download "$TEMPLATE_STORAGE" "$template"
fi
# ----- container -------------------------------------------------------------
log "Creating container $CTID ($CT_HOSTNAME)"
pct create "$CTID" "$TEMPLATE_STORAGE:vztmpl/$template" \
--hostname "$CT_HOSTNAME" \
--unprivileged 1 \
--features nesting=1 \
--cores "$CORES" --memory "$MEMORY" --swap 512 \
--rootfs "$STORAGE:$DISK" \
--net0 "$net" \
--timezone "$TZ" \
--onboot 1 \
"${ssh_args[@]}" \
--description "Immich Magic Proxy: invite-only sharing for Immich"
pct start "$CTID"
# Root password goes in over stdin, so it never shows in the process list
if [ -n "${ROOT_PASSWORD:-}" ]; then
printf 'root:%s\n' "$ROOT_PASSWORD" | pct exec "$CTID" -- chpasswd
fi
# Console login banner. agetty fills in \4{eth0} with the current IPv4
# address, and redraws it when the address changes (e.g. DHCP renewal).
{
printf 'Immich Magic Proxy (Debian \\r)\n\n'
printf ' Web: http://\\4{eth0}:%s Admin: %s/admin\n' "$PORT" "$PUBLIC_BASE_URL"
if [ "$CONSOLE_AUTOLOGIN" != 1 ]; then
printf ' Login: root at this console needs ROOT_PASSWORD set at install;\n'
printf ' otherwise run "pct enter %s" on the Proxmox host.\n' "$CTID"
printf ' The /admin password is not the root password.\n'
fi
printf '\n'
} | pct exec "$CTID" -- sh -c 'cat > /etc/issue'
# Console autologin: the Proxmox console (tty1, served by container-getty@1)
# logs straight in as root. Anyone with console access to this container in
# Proxmox gets root in it; SSH still can't log in as root without a key.
if [ "$CONSOLE_AUTOLOGIN" = 1 ]; then
log "Enabling root autologin on the Proxmox console"
pct exec "$CTID" -- sh -c 'd=/etc/systemd/system/container-getty@1.service.d
install -d -m 0755 "$d"
printf "%s\n" "[Service]" "ExecStart=" \
"ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM" \
> "$d/autologin.conf"
systemctl daemon-reload
systemctl restart container-getty@1'
fi
log "Waiting for the network"
for _ in $(seq 1 60); do
pct exec "$CTID" -- sh -c 'getent hosts deb.debian.org >/dev/null' && break
sleep 2
done
pct exec "$CTID" -- sh -c 'getent hosts deb.debian.org >/dev/null' || die "container $CTID has no network or DNS"
log "Updating Debian"
pct exec "$CTID" -- sh -c 'DEBIAN_FRONTEND=noninteractive apt-get update -q && DEBIAN_FRONTEND=noninteractive apt-get -y -q dist-upgrade'
# ----- settings, then install ------------------------------------------------
# Double-quoted with \ " $ ` escaped: the form systemd's EnvironmentFile
# parser reads back exactly, so passwords keep any spaces, quotes or
# backslashes. (It does not support shell-style 'a'\''b' concatenation.)
env_line () {
local value=$2
value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//\$/\\\$}
value=${value//\`/\\\`}
printf '%s="%s"\n' "$1" "$value"
}
settings=$(mktemp)
trap 'rm -f -- "$settings"' EXIT
{
env_line IMMICH_URL "$IMMICH_URL"
env_line PUBLIC_BASE_URL "$PUBLIC_BASE_URL"
env_line ADMIN_PASSWORD "$ADMIN_PASSWORD"
for name in SMTP_URL SMTP_HOST SMTP_PORT SMTP_USER SMTP_PASS SMTP_SECURE MAIL_FROM SITE_NAME DIGEST_HOUR; do
if [ -n "${!name:-}" ]; then env_line "$name" "${!name}"; fi
done
env_line TZ "$TZ"
env_line IPP_PORT "$PORT"
} > "$settings"
pct exec "$CTID" -- install -d -m 0755 /root/imp-install
pct push "$CTID" "$tarball" /root/imp-install/release.tar.gz
pct push "$CTID" "$installer" /root/imp-install/install.sh --perms 0755
# The env file is written before install.sh runs, so it starts the service
pct exec "$CTID" -- install -d -m 0750 /etc/immich-magic-proxy
pct push "$CTID" "$settings" /etc/immich-magic-proxy/env --perms 0600
pct exec "$CTID" -- /root/imp-install/install.sh /root/imp-install/release.tar.gz
pct exec "$CTID" -- rm -rf /root/imp-install
log "Checking the service"
for _ in $(seq 1 30); do
pct exec "$CTID" -- curl -s -o /dev/null "http://127.0.0.1:$PORT/share/healthcheck" && break
sleep 1
done
pct exec "$CTID" -- systemctl --no-pager --lines=5 status immich-magic-proxy || true
address=$(pct exec "$CTID" -- hostname -I | awk '{print $1}')
cat <<EOF
Immich Magic Proxy is running in container $CTID at http://$address:$PORT
Next:
1. Point your reverse proxy for $PUBLIC_BASE_URL at http://$address:$PORT
2. Open $PUBLIC_BASE_URL/admin and sign in with ADMIN_PASSWORD
Shell: pct enter $CTID (or the Proxmox console, with ROOT_PASSWORD or CONSOLE_AUTOLOGIN=1)
Settings: pct exec $CTID -- nano /etc/immich-magic-proxy/env (then: systemctl restart immich-magic-proxy)
Logs: pct exec $CTID -- journalctl -u immich-magic-proxy -f
Upgrade: pct push $CTID <new tarball> /root/release.tar.gz && pct push $CTID install.sh /root/install.sh --perms 0755 &&
pct exec $CTID -- /root/install.sh /root/release.tar.gz
EOF