#!/bin/bash # Lab NetBox for Rollcall's NetBox drivers (IPAM, device sync): a Debian 13 # container on the lab segment, so tests never write to a real NetBox. # Same install as netops (altyapi hosts/netops/kurulum.sh) without PowerDNS, # ACME and backups; plain HTTP on :80 (the segment is isolated). # NetBox lives under /netbox/ (BASE_PATH) so the region can serve its UI # through Rollcall (cmd/rollcall/nbproxy.go): Rollcall's ipam url is then # http://$MYIP/netbox/, and ROLLCALL_ORIGINS are where browsers post from. # The admin certificate signs in to NetBox through that proxy: NetBox takes # the user from X-Rollcall-User (REMOTE_AUTH), and Caddy drops that header # unless the request comes from ROLLCALL_IP with ROLLCALL_UI_SECRET (copy it # to the region's ipam.ui_login_secret_file). Such users land in the group # rollcall-admins, which may do everything (a lab). # # Run as root inside the container; rerunnable. Secrets are generated here: # /etc/lab-netbox/secrets.env (admin password, Rollcall's API token) # # On pve1 (example): # pct create 225 local:vztmpl/debian-13-standard_13.6-1_amd64.tar.zst --hostname lab-netbox \ # --cores 2 --memory 2048 --swap 512 --rootfs zfspool:8 --unprivileged 1 \ # --net0 name=eth0,bridge=pxenet,ip=10.213.77.6/24,gw=10.213.77.1 --nameserver 10.213.77.1 # pct start 225 && pct push 225 install.sh /root/install.sh && pct exec 225 -- bash /root/install.sh set -euo pipefail NETBOX_VERSION=${NETBOX_VERSION:-4.7.4} MYIP=${MYIP:-10.213.77.6} SITE=${SITE:-lab} ROLLCALL_ORIGINS=${ROLLCALL_ORIGINS:-"https://10.0.0.234 https://10.213.77.1"} ROLLCALL_IP=${ROLLCALL_IP:-10.213.77.1} PREFIXES=${PREFIXES:-"10.213.77.0/24 10.213.78.0/24 10.213.79.0/24 fd00:213:77::/64 fd00:213:78::/64"} export DEBIAN_FRONTEND=noninteractive apt-get update -q apt-get install -yq postgresql valkey-server caddy \ python3 python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev \ libpq-dev libssl-dev zlib1g-dev git curl jq install -d -m 700 /etc/lab-netbox S=/etc/lab-netbox/secrets.env if [ ! -f $S ]; then r() { python3 -c "import secrets,sys;print(secrets.token_urlsafe(int(sys.argv[1])))" "$1"; } cat > $S <> $S . $S # ------------------------------------------------------------------ PostgreSQL, Valkey sudo() { runuser -u "$2" -- "${@:3}"; } # "sudo -u postgres ..." without sudo if [ -z "$(sudo -u postgres psql -tAqc "SELECT 1 FROM pg_roles WHERE rolname='netbox'")" ]; then sudo -u postgres psql -qc "CREATE ROLE netbox LOGIN PASSWORD '$NETBOX_DB_PASS'" # UTF-8 whatever the container's locale (C in the Proxmox template) sudo -u postgres psql -qc "CREATE DATABASE netbox OWNER netbox ENCODING 'UTF8' LC_COLLATE 'C.UTF-8' LC_CTYPE 'C.UTF-8' TEMPLATE template0" fi sed -i 's/^# *databases .*/databases 16/' /etc/valkey/valkey.conf || true systemctl enable --now valkey-server # ------------------------------------------------------------------ NetBox id netbox >/dev/null 2>&1 || adduser --system --group --home /opt/netbox netbox D=/opt/netbox-$NETBOX_VERSION if [ ! -d $D ]; then curl -fsSL https://github.com/netbox-community/netbox/archive/refs/tags/v$NETBOX_VERSION.tar.gz | tar -xz -C /opt fi [ -L /opt/netbox ] || { [ -d /opt/netbox ] && rmdir /opt/netbox 2>/dev/null || true; } ln -sfn $D /opt/netbox cat > /opt/netbox/netbox/netbox/configuration.py </dev/null || true cd /opt/netbox && PYTHON=/usr/bin/python3 ./upgrade.sh >/var/log/netbox-upgrade.log 2>&1 || { tail -40 /var/log/netbox-upgrade.log; exit 1; } chown -R netbox:netbox /opt/netbox/netbox/media cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py sed -i "s/^bind = .*/bind = '127.0.0.1:8001'/" /opt/netbox/gunicorn.py cp /opt/netbox/contrib/netbox.service /opt/netbox/contrib/netbox-rq.service /etc/systemd/system/ systemctl daemon-reload systemctl enable --now netbox netbox-rq systemctl restart netbox netbox-rq cat > /etc/caddy/Caddyfile <> $S fi echo "--- ready" systemctl is-active postgresql valkey-server netbox netbox-rq caddy | paste -sd' ' . $S curl -fsS -o /dev/null -w "rollcall token: %{http_code}\n" -H "Authorization: Bearer $NETBOX_TOKEN_ROLLCALL" http://$MYIP/netbox/api/ipam/prefixes/