#!/usr/bin/env bash set -Eeuo pipefail # Kostenverdeler deployment script for a Proxmox VE host. # Run this from the PVE shell as root: # bash deploy-kostenverdeler-pve.sh # # Adjust the variables below before running. CTID="${CTID:-120}" HOSTNAME="${HOSTNAME:-kostenverdeler}" STORAGE="${STORAGE:-local-lvm}" TEMPLATE_STORAGE="${TEMPLATE_STORAGE:-local}" DISK_SIZE="${DISK_SIZE:-4}" MEMORY="${MEMORY:-256}" CORES="${CORES:-1}" BRIDGE="${BRIDGE:-vmbr0}" # DHCP by default. For static IP, use for example: # NET0_IP="192.168.1.50/24" # GATEWAY="192.168.1.1" NET0_IP="${NET0_IP:-dhcp}" GATEWAY="${GATEWAY:-}" # Leave DOMAIN empty to serve on port 8080. # Set DOMAIN="kosten.henrystuifzand.nl" if DNS points to this container/reverse proxy. DOMAIN="${DOMAIN:-}" REPO_URL="${REPO_URL:-https://git.henrystuifzand.nl/henry/kostenverdeler.git}" APP_DIR="${APP_DIR:-/var/www/kostenverdeler}" TEMPLATE="${TEMPLATE:-debian-12-standard_12.7-1_amd64.tar.zst}" log() { printf '\n\033[1;32m==>\033[0m %s\n' "$*" } die() { printf '\n\033[1;31mERROR:\033[0m %s\n' "$*" >&2 exit 1 } require_root() { [[ "${EUID}" -eq 0 ]] || die "Run dit script als root op de Proxmox host." } require_pve() { command -v pct >/dev/null 2>&1 || die "pct niet gevonden. Run dit script in de Proxmox VE shell." command -v pveam >/dev/null 2>&1 || die "pveam niet gevonden. Run dit script in de Proxmox VE shell." } template_path() { printf '/var/lib/vz/template/cache/%s' "${TEMPLATE}" } ensure_template() { if [[ -f "$(template_path)" ]]; then log "Debian template bestaat al: ${TEMPLATE}" return fi log "Template-lijst bijwerken" pveam update log "Debian template downloaden: ${TEMPLATE}" pveam download "${TEMPLATE_STORAGE}" "${TEMPLATE}" } create_container() { pct status "${CTID}" >/dev/null 2>&1 && die "Container ${CTID} bestaat al. Kies een andere CTID of verwijder de bestaande container." local net0="name=eth0,bridge=${BRIDGE},ip=${NET0_IP}" if [[ -n "${GATEWAY}" && "${NET0_IP}" != "dhcp" ]]; then net0="${net0},gw=${GATEWAY}" fi log "LXC container ${CTID} aanmaken" pct create "${CTID}" "$(template_path)" \ --hostname "${HOSTNAME}" \ --ostype debian \ --storage "${STORAGE}" \ --rootfs "${STORAGE}:${DISK_SIZE}" \ --memory "${MEMORY}" \ --cores "${CORES}" \ --net0 "${net0}" \ --unprivileged 1 \ --features nesting=1 \ --start 1 } wait_for_network() { log "Wachten tot de container netwerk heeft" for _ in {1..60}; do if pct exec "${CTID}" -- bash -lc 'getent hosts deb.debian.org >/dev/null 2>&1'; then return fi sleep 2 done die "Container kreeg geen werkende netwerkverbinding." } install_app() { log "Pakketten installeren in container" pct exec "${CTID}" -- bash -lc 'apt-get update && apt-get install -y --no-install-recommends ca-certificates curl git caddy' log "Applicatie clonen" pct exec "${CTID}" -- bash -lc "rm -rf '${APP_DIR}' && mkdir -p '$(dirname "${APP_DIR}")' && git clone '${REPO_URL}' '${APP_DIR}'" log "Caddy configureren" if [[ -n "${DOMAIN}" ]]; then pct exec "${CTID}" -- bash -lc "cat > /etc/caddy/Caddyfile <<'CADDY' ${DOMAIN} { root * ${APP_DIR} file_server } CADDY" else pct exec "${CTID}" -- bash -lc "cat > /etc/caddy/Caddyfile <<'CADDY' :8080 { root * ${APP_DIR} file_server } CADDY" fi pct exec "${CTID}" -- bash -lc 'systemctl enable --now caddy && systemctl reload caddy' } container_ip() { pct exec "${CTID}" -- bash -lc "hostname -I | awk '{print \$1}'" 2>/dev/null || true } print_result() { local ip ip="$(container_ip)" log "Klaar" if [[ -n "${DOMAIN}" ]]; then printf 'App URL: https://%s\n' "${DOMAIN}" elif [[ -n "${ip}" ]]; then printf 'App URL: http://%s:8080\n' "${ip}" else printf 'App URL: http://:8080\n' fi cat <