Files
Fluxer-selfhost-unraid/scripts/prepare-unraid.sh
T
Michael Sloan b910932394 Fix VAPID key generation on Unraid tail.
Unraid treats a separated byte count as a filename, so the counts are attached to the tail and head flags.
2026-11-23 22:13:12 -05:00

347 lines
12 KiB
Bash

#!/usr/bin/env bash
# Prepare a Fluxer compose stack on Unraid.
# Downloads the official bundle, generates .env once, and bind-mounts data
# outside docker.img. Does not start Fluxer unless you pass --up.
#
# Usage (on the Unraid server, from a clone of this repo):
# sh scripts/prepare-unraid.sh chat.example.com
# sh scripts/prepare-unraid.sh chat.example.com --up
# sh scripts/prepare-unraid.sh chat.example.com --direct
# sh scripts/prepare-unraid.sh chat.example.com --update
#
# --proxy (default) plain HTTP on 0.0.0.0:18080 for Nginx Proxy Manager,
# SWAG, or Traefik. Use this when 80/443 are already taken.
# --direct Fluxer's own Caddy binds 80, 443/tcp, and 443/udp and gets a
# certificate. Only if those ports are free and the domain points here.
# --update re-download https://fluxer.dev/compose/latest over the stack files.
# Keeps .env and the data directories.
# --up run `docker compose up -d` after a successful prepare.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
STACK="${FLUXER_STACK:-$ROOT/stack}"
APPDATA="${FLUXER_APPDATA:-/mnt/user/appdata/fluxer}"
BUNDLE_URL="${FLUXER_BUNDLE_URL:-https://fluxer.dev/compose/latest}"
DOMAIN=""
MODE="proxy"
EXPLICIT_MODE=0
DO_UPDATE=0
DO_UP=0
usage() {
echo "Usage: sh scripts/prepare-unraid.sh <domain> [--proxy|--direct] [--update] [--up]" >&2
echo "Example: sh scripts/prepare-unraid.sh chat.example.com" >&2
exit 1
}
for arg in "$@"; do
case "$arg" in
--proxy) MODE="proxy"; EXPLICIT_MODE=1 ;;
--direct) MODE="direct"; EXPLICIT_MODE=1 ;;
--update) DO_UPDATE=1 ;;
--up) DO_UP=1 ;;
-h|--help) usage ;;
--*) echo "Unknown option: $arg" >&2; usage ;;
*)
if [[ -n "$DOMAIN" ]]; then
echo "Only one domain is accepted." >&2
usage
fi
DOMAIN="$arg"
;;
esac
done
if [[ -z "$DOMAIN" ]]; then
usage
fi
DOMAIN="${DOMAIN#https://}"
DOMAIN="${DOMAIN#http://}"
DOMAIN="${DOMAIN%%/*}"
DOMAIN="${DOMAIN%%:*}"
if [[ ! "$DOMAIN" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]]; then
echo "Domain must be a hostname such as chat.example.com, with no scheme or path." >&2
exit 1
fi
case "$APPDATA" in
/boot|/boot/*)
echo "Refusing to store Fluxer data on the Unraid USB flash drive ($APPDATA)." >&2
exit 1
;;
esac
case "$STACK" in
/boot|/boot/*)
echo "Refusing to unpack the compose bundle onto the Unraid USB ($STACK)." >&2
exit 1
;;
esac
if [[ "${FLUXER_ALLOW_ANY_PATH:-0}" != "1" ]]; then
if [[ ! -d /mnt/user ]]; then
echo "This script is meant to run on Unraid (/mnt/user is missing)." >&2
echo "Set FLUXER_ALLOW_ANY_PATH=1 only for a local dry run." >&2
exit 1
fi
fi
if [[ "${FLUXER_SKIP_DOCKER_CHECK:-0}" != "1" ]]; then
if ! command -v docker >/dev/null 2>&1 || ! docker compose version >/dev/null 2>&1; then
echo "Docker Compose is not available." >&2
echo "Install Unraid's Compose Manager plugin, then run this again." >&2
exit 1
fi
COMPOSE_VERSION="$(docker compose version --short | tr -d 'v')"
OLDEST="$(printf '%s\n%s\n' "2.24.0" "$COMPOSE_VERSION" | sort -V | head -n 1)"
if [[ "$OLDEST" != "2.24.0" ]]; then
echo "Docker Compose $COMPOSE_VERSION is too old." >&2
echo "Fluxer's proxy file uses compose tags that need 2.24 or newer." >&2
exit 1
fi
fi
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing command: $1" >&2
exit 1
fi
}
need_cmd curl
need_cmd tar
need_cmd openssl
need_cmd awk
mkdir -p "$STACK" "$APPDATA"
for dir in edge-data edge-config postgres valkey nats meilisearch seaweedfs media-proxy-tmp; do
mkdir -p "$APPDATA/$dir"
done
download_bundle() {
local tmp archive
tmp="$(mktemp -d)"
archive="$tmp/fluxer-compose.tar.gz"
echo "Downloading official compose bundle from $BUNDLE_URL"
curl -fsSL "$BUNDLE_URL" -o "$archive"
tar -xzf "$archive" -C "$STACK"
rm -rf "$tmp"
date -u +"%Y-%m-%dT%H:%M:%SZ" > "$STACK/.bundle-fetched"
}
if [[ ! -f "$STACK/docker-compose.yml" || "$DO_UPDATE" -eq 1 ]]; then
download_bundle
else
echo "Keeping existing bundle in $STACK (pass --update to replace it)."
fi
if [[ ! -f "$STACK/docker-compose.yml" || ! -f "$STACK/.env.example" ]]; then
echo "The bundle did not contain docker-compose.yml and .env.example." >&2
exit 1
fi
cp "$ROOT/unraid/docker-compose.unraid.yml" "$STACK/docker-compose.unraid.yml"
# Set or replace one KEY=value line. Matches a commented "# KEY=" form too.
set_env() {
local key="$1"
local value="$2"
local file="$3"
local tmp
tmp="$(mktemp)"
# Only a real assignment line: "KEY=" or the bundle's "# KEY=" slot.
# A looser match also rewrites comment examples such as
# "# COMPOSE_FILE=docker-compose.yml:...".
awk -v k="$key" -v v="$value" '
BEGIN { found = 0 }
$0 ~ ("^" k "=") || $0 ~ ("^# " k "=") {
if (found == 0) {
print k "=" v
found = 1
}
next
}
{ print }
END {
if (found == 0) print k "=" v
}
' "$file" > "$tmp"
mv "$tmp" "$file"
}
env_value() {
local key="$1"
local file="$2"
awk -F= -v k="$key" '$1 == k { print substr($0, index($0, "=") + 1); exit }' "$file"
}
fill_if_empty() {
local key="$1"
local value="$2"
local file="$3"
local current
current="$(env_value "$key" "$file")"
if [[ -z "$current" ]]; then
set_env "$key" "$value" "$file"
fi
}
hex32() {
openssl rand -hex 32
}
if [[ ! -f "$STACK/.env" ]]; then
echo "Creating $STACK/.env and generating secrets."
cp "$STACK/.env.example" "$STACK/.env"
chmod 600 "$STACK/.env"
NEW_ENV=1
for key in \
POSTGRES_PASSWORD \
MEILI_MASTER_KEY \
FLUXER_S3_SECRET_KEY \
LIVEKIT_API_SECRET \
FLUXER_SUDO_MODE_SECRET \
FLUXER_CONNECTION_INITIATION_SECRET \
FLUXER_PROFILE_PSEUDONYM_SECRET \
FLUXER_GATEWAY_RPC_AUTH_TOKEN \
FLUXER_ERLANG_COOKIE \
FLUXER_MEDIA_PROXY_SECRET_KEY \
FLUXER_ADMIN_SECRET_KEY_BASE \
FLUXER_ADMIN_OAUTH_CLIENT_SECRET \
FLUXER_SETUP_TOKEN
do
fill_if_empty "$key" "$(hex32)" "$STACK/.env"
done
fill_if_empty "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SECRET_BASE64" \
"$(openssl rand -base64 32 | tr -d '\n')" "$STACK/.env"
fill_if_empty "FLUXER_ACTIVITYPUB_SIGNING_KEY_BASE64" \
"$(openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -outform DER 2>/dev/null | base64 | tr -d '\n')" \
"$STACK/.env"
if [[ -z "$(env_value FLUXER_VAPID_PUBLIC_KEY "$STACK/.env")" || -z "$(env_value FLUXER_VAPID_PRIVATE_KEY "$STACK/.env")" ]]; then
vapid="$STACK/vapid.pem"
openssl ecparam -name prime256v1 -genkey -noout -out "$vapid" 2>/dev/null
pub="$(openssl ec -in "$vapid" -pubout -outform DER 2>/dev/null | tail -c65 | base64 | tr '/+' '_-' | tr -d '=\n')"
priv="$(openssl ec -in "$vapid" -outform DER 2>/dev/null | tail -c+8 | head -c32 | base64 | tr '/+' '_-' | tr -d '=\n')"
rm -f "$vapid"
set_env "FLUXER_VAPID_PUBLIC_KEY" "$pub" "$STACK/.env"
set_env "FLUXER_VAPID_PRIVATE_KEY" "$priv" "$STACK/.env"
fi
# Home-server sizes. The bundle's defaults cap Postgres at 5 GB and reserve
# 3 GB, which is rexctl's large-machine tune, not a quiet Unraid box.
# Applied only while creating .env, so later edits are kept.
set_env "FLUXER_POSTGRES_MEMORY_LIMIT" "1gb" "$STACK/.env"
set_env "FLUXER_POSTGRES_MEMORY_RESERVATION" "256mb" "$STACK/.env"
set_env "FLUXER_POSTGRES_SHM_SIZE" "256mb" "$STACK/.env"
set_env "FLUXER_POSTGRES_SHARED_BUFFERS" "128MB" "$STACK/.env"
set_env "FLUXER_POSTGRES_EFFECTIVE_CACHE_SIZE" "512MB" "$STACK/.env"
set_env "FLUXER_POSTGRES_MAINTENANCE_WORK_MEM" "64MB" "$STACK/.env"
set_env "FLUXER_POSTGRES_MAX_WAL_SIZE" "512MB" "$STACK/.env"
set_env "FLUXER_API_MEMORY_LIMIT" "768mb" "$STACK/.env"
set_env "FLUXER_API_MEMORY_RESERVATION" "256mb" "$STACK/.env"
set_env "FLUXER_WORKER_MEMORY_LIMIT" "768mb" "$STACK/.env"
set_env "FLUXER_WORKER_MEMORY_RESERVATION" "256mb" "$STACK/.env"
set_env "FLUXER_GATEWAY_MEMORY_LIMIT" "512mb" "$STACK/.env"
set_env "FLUXER_GATEWAY_MEMORY_RESERVATION" "256mb" "$STACK/.env"
set_env "FLUXER_MEILISEARCH_MEMORY_LIMIT" "512mb" "$STACK/.env"
set_env "FLUXER_MEILISEARCH_MAX_INDEXING_MEMORY" "128mb" "$STACK/.env"
set_env "FLUXER_SEAWEEDFS_MEMORY_LIMIT" "512mb" "$STACK/.env"
set_env "FLUXER_SEAWEEDFS_GOMEMLIMIT" "384MiB" "$STACK/.env"
set_env "FLUXER_MEDIA_PROXY_MEMORY_LIMIT" "512mb" "$STACK/.env"
else
echo "Keeping existing secrets in $STACK/.env"
chmod 600 "$STACK/.env"
NEW_ENV=0
fi
set_env "FLUXER_SETTINGS_LOADED" "true" "$STACK/.env"
set_env "FLUXER_DOMAIN" "$DOMAIN" "$STACK/.env"
set_env "COMPOSE_PROJECT_NAME" "fluxer" "$STACK/.env"
set_env "COMPOSE_PATH_SEPARATOR" ":" "$STACK/.env"
set_env "FLUXER_APPDATA" "$APPDATA" "$STACK/.env"
# Mode is applied when .env is new, or when this run was given --proxy/--direct.
# A plain --update leaves a hand-edited COMPOSE_FILE alone.
if [[ "$NEW_ENV" -eq 1 || "$EXPLICIT_MODE" -eq 1 ]]; then
if [[ "$MODE" == "proxy" ]]; then
set_env "COMPOSE_FILE" "docker-compose.yml:docker-compose.proxy.yml:docker-compose.unraid.yml" "$STACK/.env"
set_env "FLUXER_EDGE_BIND" "0.0.0.0:18080" "$STACK/.env"
else
set_env "COMPOSE_FILE" "docker-compose.yml:docker-compose.unraid.yml" "$STACK/.env"
fi
fi
if [[ -d "$APPDATA/postgres" ]] && [[ -n "$(ls -A "$APPDATA/postgres" 2>/dev/null || true)" ]]; then
echo "Postgres data already exists. Changing FLUXER_DOMAIN does not move old links."
echo "Do not change POSTGRES_PASSWORD, MEILI_MASTER_KEY, or the VAPID keys."
fi
if [[ "${FLUXER_SKIP_DOCKER_CHECK:-0}" != "1" ]]; then
echo "Checking compose file against Docker Compose $COMPOSE_VERSION"
config_out="$(mktemp)"
if ! docker compose --project-directory "$STACK" config >"$config_out"; then
rm -f "$config_out"
echo "docker compose config failed. Fix the error above before starting." >&2
exit 1
fi
rm -f "$config_out"
if docker volume inspect fluxer_postgres-data >/dev/null 2>&1; then
mountpoint="$(docker volume inspect -f '{{ .Options.device }}' fluxer_postgres-data 2>/dev/null || true)"
echo "Docker volume fluxer_postgres-data already exists (device: ${mountpoint:-not a bind})."
echo "If that volume was created without this Unraid file, Postgres will ignore $APPDATA/postgres."
echo "On a brand new install, remove the empty fluxer_* volumes before the first up."
fi
fi
compose_file="$(env_value COMPOSE_FILE "$STACK/.env")"
case "$compose_file" in
*docker-compose.proxy.yml*) SHOW_MODE="proxy" ;;
*) SHOW_MODE="direct" ;;
esac
echo
echo "Prepared Fluxer for $DOMAIN"
echo " Stack: $STACK"
echo " Data: $APPDATA"
echo " Mode: $SHOW_MODE"
if [[ "$SHOW_MODE" == "proxy" ]]; then
echo " Edge: http://<unraid-lan-ip>:18080"
echo "Point the reverse proxy at that address. See README.md and unraid/npm-advanced.conf."
echo "Still forward TCP 7881 and UDP 7882 to this server for voice and video."
else
echo " Edge: ports 80, 443/tcp, and 443/udp must reach this server."
echo "Also forward TCP 7881 and UDP 7882 for voice and video."
fi
echo
echo "The admin setup token is FLUXER_SETUP_TOKEN in:"
echo " $STACK/.env"
echo "Show it with:"
echo " awk -F= '\$1==\"FLUXER_SETUP_TOKEN\"{print \$2}' \"$STACK/.env\""
echo
echo "Back up that .env somewhere off the server before you start."
echo "Postgres, Meilisearch, and the VAPID keys cannot be changed after the first start."
if [[ "$DO_UP" -eq 1 ]]; then
echo
echo "Starting Fluxer. The first pull is large."
docker compose --project-directory "$STACK" up -d
echo
echo "Watch startup with:"
echo " docker compose --project-directory \"$STACK\" ps"
echo " docker compose --project-directory \"$STACK\" logs -f api"
else
echo
echo "Start it when DNS and the proxy or port forwards are in place:"
echo " docker compose --project-directory \"$STACK\" up -d"
fi