grocery-bot

Log | Files | Refs | README

provision-lxc.sh (3285B)


      1 #!/usr/bin/env bash
      2 set -euo pipefail
      3 
      4 # Provisions the Debian 12 LXC container Anton will run in. Run as root on
      5 # the Proxmox host itself (not inside any container). This only creates and
      6 # starts the container -- it does NOT install the bot. Once this finishes,
      7 # copy this repo into the container and run install.sh there (see
      8 # anton-bot.service's header comment for the full sequence).
      9 #
     10 # Usage:
     11 #   ./provision-lxc.sh
     12 #
     13 # Override defaults via env vars if needed, e.g.:
     14 #   ANTON_LXC_STORAGE=local-zfs ANTON_LXC_BRIDGE=vmbr1 ./provision-lxc.sh
     15 
     16 CTID="${ANTON_LXC_CTID:-$(pvesh get /cluster/nextid)}"
     17 HOSTNAME="${ANTON_LXC_HOSTNAME:-anton}"
     18 STORAGE="${ANTON_LXC_STORAGE:-local-zfs}"
     19 TEMPLATE_STORAGE="${ANTON_LXC_TEMPLATE_STORAGE:-local}"
     20 BRIDGE="${ANTON_LXC_BRIDGE:-vmbr0}"
     21 IP_CONFIG="${ANTON_LXC_IP:-dhcp}"
     22 DISK_GB="${ANTON_LXC_DISK_GB:-8}"
     23 MEMORY_MB="${ANTON_LXC_MEMORY_MB:-512}"
     24 CORES="${ANTON_LXC_CORES:-1}"
     25 SSH_PUBKEY_FILE="${ANTON_LXC_SSH_PUBKEY_FILE:-}"
     26 
     27 if [[ $EUID -ne 0 ]]; then
     28     echo "Run this as root on the Proxmox host: sudo $0" >&2
     29     exit 1
     30 fi
     31 
     32 if ! command -v pct &>/dev/null; then
     33     echo "pct not found -- this must be run on a Proxmox host, not inside a container." >&2
     34     exit 1
     35 fi
     36 
     37 echo "==> Storage pool '${STORAGE}' and bridge '${BRIDGE}' are assumed to exist on this host."
     38 echo "    If either is wrong, check 'pvesm status' / the Proxmox network config and re-run"
     39 echo "    with ANTON_LXC_STORAGE / ANTON_LXC_BRIDGE overridden."
     40 
     41 echo "==> Finding the current debian-12-standard template"
     42 pveam update >/dev/null
     43 TEMPLATE="$(pveam available --section system | awk '/debian-12-standard/ {print $2}' | sort -V | tail -1)"
     44 if [[ -z "$TEMPLATE" ]]; then
     45     echo "No debian-12-standard template found in the appliance list." >&2
     46     exit 1
     47 fi
     48 
     49 if ! pveam list "$TEMPLATE_STORAGE" 2>/dev/null | grep -q "$TEMPLATE"; then
     50     echo "==> Downloading ${TEMPLATE} to storage '${TEMPLATE_STORAGE}'"
     51     pveam download "$TEMPLATE_STORAGE" "$TEMPLATE"
     52 else
     53     echo "==> ${TEMPLATE} already present on '${TEMPLATE_STORAGE}'"
     54 fi
     55 
     56 CREATE_ARGS=(
     57     "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}"
     58     --hostname "$HOSTNAME"
     59     --memory "$MEMORY_MB"
     60     --cores "$CORES"
     61     --rootfs "${STORAGE}:${DISK_GB}"
     62     --net0 "name=eth0,bridge=${BRIDGE},ip=${IP_CONFIG}"
     63     --unprivileged 1
     64     --onboot 1
     65 )
     66 
     67 if [[ -n "$SSH_PUBKEY_FILE" ]]; then
     68     echo "==> Using SSH key from ${SSH_PUBKEY_FILE} for root login (no password set)"
     69     CREATE_ARGS+=(--ssh-public-keys "$SSH_PUBKEY_FILE")
     70 else
     71     # Generated rather than left interactive so this script stays non-interactive
     72     # end to end. Change it after first login.
     73     ROOT_PASSWORD="$(openssl rand -base64 18)"
     74     CREATE_ARGS+=(--password "$ROOT_PASSWORD")
     75 fi
     76 
     77 echo "==> Creating container ${CTID} (${HOSTNAME})"
     78 pct create "${CREATE_ARGS[@]}"
     79 
     80 echo "==> Starting container ${CTID}"
     81 pct start "$CTID"
     82 
     83 echo "==> Container ${CTID} (${HOSTNAME}) is up."
     84 echo "    Enter it with: pct enter ${CTID}"
     85 if [[ -z "$SSH_PUBKEY_FILE" ]]; then
     86     echo "    Generated root password: ${ROOT_PASSWORD}"
     87     echo "    Change it immediately after first login (passwd)."
     88 fi
     89 echo
     90 echo "    Next: copy this repo into the container and run install.sh there"
     91 echo "    (see the setup steps at the top of anton-bot.service)."