platforms/qemu and platforms/proxmox each have their own lab.sh and lab.conf; content/ holds everything shared (roles, seed, site, helpers). The dispatcher, preflight and command implementations are common and take platform differences from the backend. Proxmox backend: qm create with q35/SATA/e1000, ISOs and seed ISOs in an ISO storage, provisioning over qm guest exec (no Python packages needed on the host), template + clone for reset, and peers/ to emit WireGuard configs. The role scripts now ride on the seed ISO, so provisioning transfers nothing. build-bundle.sh produces a self-contained tarball per platform, for hosts that cannot reach the git server. Both configs are generic: no absolute path from this machine remains.
173 lines
4.9 KiB
Bash
173 lines
4.9 KiB
Bash
# shellcheck shell=bash
|
|
# Shared helpers for lab.sh. Sourced, never executed directly.
|
|
|
|
LAB_SH_VERSION="0.1.0"
|
|
|
|
# ------------------------------------------------------------------ output
|
|
if [[ -t 1 && -z ${NO_COLOR:-} ]]; then
|
|
C_RESET=$'\e[0m'; C_BOLD=$'\e[1m'; C_DIM=$'\e[2m'
|
|
C_RED=$'\e[31m'; C_GREEN=$'\e[32m'; C_YELLOW=$'\e[33m'; C_BLUE=$'\e[34m'
|
|
else
|
|
C_RESET=""; C_BOLD=""; C_DIM=""; C_RED=""; C_GREEN=""; C_YELLOW=""; C_BLUE=""
|
|
fi
|
|
|
|
VERBOSE=${VERBOSE:-0}
|
|
STRICT=${STRICT:-0}
|
|
|
|
info() { printf '%s\n' "$*"; }
|
|
raw() { printf '%s\n' "$*"; }
|
|
ok() { printf '%s[ ok ]%s %s\n' "$C_GREEN" "$C_RESET" "$*"; }
|
|
warn() { printf '%s[warn]%s %s\n' "$C_YELLOW" "$C_RESET" "$*" >&2; }
|
|
fail() { printf '%s[fail]%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; }
|
|
die() { fail "$*"; exit 1; }
|
|
step() { printf '\n%s==> %s%s\n' "$C_BLUE" "$*" "$C_RESET"; }
|
|
detail() { if (( VERBOSE )); then printf '%s %s%s\n' "$C_DIM" "$*" "$C_RESET"; fi; return 0; }
|
|
|
|
# Substitute the lab-wide tokens in a role script.
|
|
render_role() {
|
|
local src=$1 dst=$2
|
|
sed -e "s|@@DOMAIN@@|${DOMAIN}|g" \
|
|
-e "s|@@NETBIOS@@|${NETBIOS}|g" \
|
|
-e "s|@@BASE_DN@@|${BASE_DN}|g" \
|
|
-e "s|@@ADMIN_PASSWORD@@|${LAB_ADMIN_PASSWORD}|g" \
|
|
-e "s|@@SAFE_MODE_PASSWORD@@|${LAB_SAFE_MODE_PASSWORD}|g" \
|
|
"$src" > "$dst"
|
|
}
|
|
|
|
# Deterministic MAC from the last two octets: 10.10.10.31 -> 52:54:00:0a:1f:00
|
|
# (six octets — a five-byte value is rejected as "invalid ethernet address")
|
|
qemu_mac_for() {
|
|
local ip=$1
|
|
local b3=${ip##*.}
|
|
local rest=${ip%.*}
|
|
local b2=${rest##*.}
|
|
printf '52:54:00:%02x:%02x:00' "$b2" "$b3"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ commands
|
|
have_cmd() { command -v "$1" >/dev/null 2>&1; }
|
|
|
|
require_cmd() {
|
|
have_cmd "$1" || die "required command not found: $1${2:+ ($2)}"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ config
|
|
load_config() {
|
|
LAB_CONF=${LAB_CONF:-$LAB_DIR/lab.conf}
|
|
[[ -r $LAB_CONF ]] || die "config not found: $LAB_CONF"
|
|
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
source "$LAB_CONF"
|
|
set +a
|
|
|
|
# A local override wins over the committed config (sourced last on purpose).
|
|
if [[ -r $LAB_DIR/lab.conf.local ]]; then
|
|
set -a
|
|
# shellcheck disable=SC1091
|
|
source "$LAB_DIR/lab.conf.local"
|
|
set +a
|
|
fi
|
|
|
|
: "${LAB_ROOT:?LAB_ROOT is not set}"
|
|
: "${LAB_ISO_CACHE:?LAB_ISO_CACHE is not set}"
|
|
: "${LAB_PREFIX:?LAB_PREFIX is not set}"
|
|
|
|
BACKEND=${BACKEND:-qemu}
|
|
BASE_DN="DC=${DOMAIN%%.*},DC=${DOMAIN#*.}"
|
|
# Each platform names its bridge differently (br-lab / vmbr10).
|
|
LAB_BRIDGE=${LAB_BRIDGE:-${BRIDGE:-${PVE_BRIDGE:-}}}
|
|
: "${LAB_BRIDGE:?no lab bridge configured (BRIDGE or PVE_BRIDGE)}"
|
|
MIN_FREE_GB=${MIN_FREE_GB:-20}
|
|
SEED_DIR=${SEED_DIR:-$LAB_ROOT/seeds}
|
|
DISK_FILL_RATIO=${DISK_FILL_RATIO:-60}
|
|
ISO_BUDGET_GB=${ISO_BUDGET_GB:-8}
|
|
|
|
load_vms
|
|
}
|
|
|
|
# VMS is a small whitespace-separated table; load it into parallel arrays.
|
|
load_vms() {
|
|
VM_NAMES=(); VM_IPS=(); VM_RAM=(); VM_DISK=(); VM_VCPU=(); VM_ROLES=()
|
|
|
|
local name ip ram disk vcpu role
|
|
while read -r name ip ram disk vcpu role; do
|
|
[[ -z ${name:-} || ${name:0:1} == '#' ]] && continue
|
|
VM_NAMES+=("$name")
|
|
VM_IPS+=("$ip")
|
|
VM_RAM+=("$ram")
|
|
VM_DISK+=("$disk")
|
|
VM_VCPU+=("$vcpu")
|
|
VM_ROLES+=("$role")
|
|
done <<< "${VMS:-}"
|
|
|
|
(( ${#VM_NAMES[@]} > 0 )) || die "no VMs defined in VMS (lab.conf)"
|
|
}
|
|
|
|
vm_index() {
|
|
local name=$1 i
|
|
for i in "${!VM_NAMES[@]}"; do
|
|
if [[ ${VM_NAMES[$i]} == "$name" ]]; then
|
|
printf '%s\n' "$i"
|
|
return 0
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Bash-native TCP probe: no nc dependency, 1 s timeout.
|
|
tcp_open() {
|
|
local host=$1 port=$2
|
|
timeout 1 bash -c "exec 3<>/dev/tcp/${host}/${port}" 2>/dev/null
|
|
}
|
|
|
|
sum_vm_ram_mb() {
|
|
local total=0 r
|
|
for r in "${VM_RAM[@]}"; do total=$(( total + r )); done
|
|
printf '%s\n' "$total"
|
|
}
|
|
|
|
sum_vm_disk_gb() {
|
|
local total=0 d
|
|
for d in "${VM_DISK[@]}"; do total=$(( total + d )); done
|
|
printf '%s\n' "$total"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ filesystem
|
|
# Walk up until the path exists; useful before LAB_ROOT is created.
|
|
existing_ancestor() {
|
|
local p=$1
|
|
while [[ ! -e $p ]]; do p=$(dirname -- "$p"); done
|
|
printf '%s\n' "$p"
|
|
}
|
|
|
|
fstype_of() {
|
|
stat -f -c %T -- "$(existing_ancestor "$1")" 2>/dev/null || printf 'unknown\n'
|
|
}
|
|
|
|
device_of() {
|
|
df --output=source -- "$(existing_ancestor "$1")" 2>/dev/null | tail -n1
|
|
}
|
|
|
|
free_gb_of() {
|
|
df -BG --output=avail -- "$(existing_ancestor "$1")" 2>/dev/null | tail -n1 | tr -dc '0-9'
|
|
}
|
|
|
|
# ext4 marks a filesystem "emergency_ro" in /proc/mounts after a fatal I/O error.
|
|
is_emergency_ro() {
|
|
local dev
|
|
dev=$(device_of "$1")
|
|
[[ -n $dev ]] || return 1
|
|
awk -v d="$dev" '$1 == d && $4 ~ /emergency_ro/ { found = 1 } END { exit(found ? 0 : 1) }' /proc/mounts
|
|
}
|
|
|
|
is_mounted() {
|
|
local target
|
|
target=$(mountpoint_of "$1")
|
|
[[ -n $target && $target != "/" ]]
|
|
}
|
|
|
|
mountpoint_of() {
|
|
findmnt -rn --target "$(existing_ancestor "$1")" -o TARGET 2>/dev/null
|
|
}
|