Cluster-Problem
This commit is contained in:
207
libsupabase.sh
207
libsupabase.sh
@@ -1,97 +1,192 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
########################################
|
||||
# Logging / Errors
|
||||
########################################
|
||||
|
||||
# -------------------------
|
||||
# Logging helpers (stderr!)
|
||||
# -------------------------
|
||||
_is_tty() { [[ -t 2 ]]; }
|
||||
_ts() { date '+%F %T'; }
|
||||
|
||||
log() { echo "[$(_ts)] $*"; }
|
||||
log() { echo "[$(_ts)] $*" >&2; }
|
||||
info() { log "INFO: $*"; }
|
||||
warn() { log "WARN: $*"; }
|
||||
die() { log "ERROR: $*"; exit 1; }
|
||||
|
||||
on_error() {
|
||||
die "Failed at line $1: $2 (exit=$3)"
|
||||
}
|
||||
setup_traps() {
|
||||
trap 'on_error $LINENO "$BASH_COMMAND" "$?"' ERR
|
||||
}
|
||||
|
||||
########################################
|
||||
# Preconditions
|
||||
########################################
|
||||
|
||||
need_cmd() {
|
||||
local c
|
||||
for c in "$@"; do
|
||||
command -v "$c" >/dev/null 2>&1 || die "Missing command: $c"
|
||||
done
|
||||
}
|
||||
|
||||
########################################
|
||||
on_error() {
|
||||
local lineno="$1" cmd="$2" code="$3"
|
||||
die "Failed at line ${lineno}: ${cmd} (exit=${code})"
|
||||
}
|
||||
|
||||
setup_traps() {
|
||||
trap 'on_error "${LINENO}" "${BASH_COMMAND}" "$?"' ERR
|
||||
}
|
||||
|
||||
# -------------------------
|
||||
# Proxmox helpers
|
||||
########################################
|
||||
# -------------------------
|
||||
pve_storage_exists() {
|
||||
local st="$1"
|
||||
pvesm status --storage "$st" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
pve_bridge_exists() {
|
||||
[[ -d "/sys/class/net/$1/bridge" ]]
|
||||
local br="$1"
|
||||
[[ -d "/sys/class/net/${br}/bridge" ]]
|
||||
}
|
||||
|
||||
pve_storage_exists() {
|
||||
pvesm status --storage "$1" >/dev/null 2>&1
|
||||
# Return a list of all VMIDs in the cluster (VM + CT)
|
||||
# Output: one vmid per line (stdout only)
|
||||
pve_cluster_vmids() {
|
||||
need_cmd pvesh python3
|
||||
pvesh get /cluster/resources --type vm --output-format json \
|
||||
| python3 - <<'PY'
|
||||
import json,sys
|
||||
data=json.load(sys.stdin)
|
||||
ids=sorted({int(x["vmid"]) for x in data if "vmid" in x})
|
||||
for i in ids:
|
||||
print(i)
|
||||
PY
|
||||
}
|
||||
|
||||
########################################
|
||||
# Template handling
|
||||
########################################
|
||||
# Pick a free CTID cluster-wide. Default start=2000 to avoid collisions with "classic" ranges.
|
||||
pve_next_free_ctid() {
|
||||
need_cmd python3
|
||||
local start="${1:-2000}"
|
||||
local end="${2:-9999}"
|
||||
|
||||
local used
|
||||
used="$(pve_cluster_vmids || true)"
|
||||
|
||||
python3 - <<PY
|
||||
used=set()
|
||||
for line in """$used""".splitlines():
|
||||
line=line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
used.add(int(line))
|
||||
except:
|
||||
pass
|
||||
|
||||
start=int("$start"); end=int("$end")
|
||||
for vmid in range(start, end+1):
|
||||
if vmid not in used:
|
||||
print(vmid)
|
||||
raise SystemExit(0)
|
||||
raise SystemExit(1)
|
||||
PY
|
||||
}
|
||||
|
||||
# Ensure Debian 12 CT template is available.
|
||||
# Returns template ref like: local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst (stdout only)
|
||||
pve_template_ensure_debian12() {
|
||||
local tpl="debian-12-standard_12.12-1_amd64.tar.zst"
|
||||
local store="$1"
|
||||
need_cmd pveam awk grep
|
||||
|
||||
local store="$1"
|
||||
local tpl="debian-12-standard_12.12-1_amd64.tar.zst"
|
||||
|
||||
# pveam templates are usually on "local" (dir storage). If chosen storage doesn't support templates -> fallback.
|
||||
if ! pveam list "$store" >/dev/null 2>&1; then
|
||||
warn "pveam storage '$store' not available for templates; falling back to 'local'"
|
||||
store="local"
|
||||
fi
|
||||
|
||||
if ! pveam list "$store" | awk '{print $2}' | grep -qx "$tpl"; then
|
||||
info "Downloading CT template to $store: $tpl"
|
||||
pveam update
|
||||
pveam download "$store" "$tpl"
|
||||
# Update template list (quietly)
|
||||
pveam update >/dev/null 2>&1 || true
|
||||
|
||||
# Download if missing
|
||||
if ! pveam list "$store" 2>/dev/null | awk '{print $2}' | grep -qx "$tpl"; then
|
||||
info "Downloading CT template to ${store}: ${tpl}"
|
||||
pveam download "$store" "$tpl" >/dev/null
|
||||
fi
|
||||
|
||||
echo "$store:vztmpl/$tpl"
|
||||
# Print template ref for pct create
|
||||
echo "${store}:vztmpl/${tpl}"
|
||||
}
|
||||
|
||||
########################################
|
||||
# Cluster-wide CTID
|
||||
########################################
|
||||
|
||||
pve_next_free_ctid() {
|
||||
local used
|
||||
used="$(pvesh get /cluster/resources --type vm | awk 'NR>1 {print $1}' | sort -n)"
|
||||
|
||||
for id in $(seq 100 9999); do
|
||||
if ! echo "$used" | grep -qx "$id"; then
|
||||
echo "$id"
|
||||
return
|
||||
fi
|
||||
done
|
||||
|
||||
die "No free CTID found"
|
||||
}
|
||||
|
||||
########################################
|
||||
# Networking
|
||||
########################################
|
||||
|
||||
pve_build_net0() {
|
||||
local bridge="$1"
|
||||
local ip="$2"
|
||||
|
||||
if [[ "$ip" == "dhcp" ]]; then
|
||||
echo "name=eth0,bridge=$bridge,ip=dhcp"
|
||||
echo "name=eth0,bridge=${bridge},ip=dhcp"
|
||||
else
|
||||
echo "name=eth0,bridge=$bridge,ip=$ip"
|
||||
# expects CIDR e.g. 192.168.45.171/24 (no gateway here; can be set later if needed)
|
||||
echo "name=eth0,bridge=${bridge},ip=${ip}"
|
||||
fi
|
||||
}
|
||||
|
||||
pve_create_ct() {
|
||||
need_cmd pct
|
||||
local ctid="$1" template="$2" hostname="$3"
|
||||
local cores="$4" memory="$5" swap="$6"
|
||||
local storage="$7" disk="$8"
|
||||
local bridge="$9" ip="${10}" unpriv="${11}"
|
||||
|
||||
local net0 rootfs features
|
||||
net0="$(pve_build_net0 "$bridge" "$ip")"
|
||||
rootfs="${storage}:${disk}"
|
||||
features="nesting=1,keyctl=1,fuse=1"
|
||||
|
||||
info "Creating CT ${ctid} (${hostname}) from ${template}"
|
||||
pct create "$ctid" "$template" \
|
||||
--hostname "$hostname" \
|
||||
--cores "$cores" \
|
||||
--memory "$memory" \
|
||||
--swap "$swap" \
|
||||
--net0 "$net0" \
|
||||
--rootfs "$rootfs" \
|
||||
--unprivileged "$unpriv" \
|
||||
--features "$features" \
|
||||
--start 0
|
||||
}
|
||||
|
||||
pve_start_ct() {
|
||||
need_cmd pct
|
||||
local ctid="$1"
|
||||
info "Starting CT ${ctid}"
|
||||
pct start "$ctid" >/dev/null
|
||||
}
|
||||
|
||||
# Wait for an IPv4 (non-empty) from pct exec "hostname -I"
|
||||
pve_wait_ct_ip() {
|
||||
need_cmd pct awk
|
||||
local ctid="$1"
|
||||
local timeout="${2:-120}"
|
||||
local slept=0
|
||||
|
||||
while (( slept < timeout )); do
|
||||
# hostname -I may return multiple; pick first IPv4
|
||||
local ip
|
||||
ip="$(pct exec "$ctid" -- bash -lc "hostname -I 2>/dev/null | awk '{print \$1}'" || true)"
|
||||
ip="${ip//$'\r'/}"
|
||||
ip="${ip//$'\n'/}"
|
||||
if [[ -n "$ip" ]]; then
|
||||
echo "$ip"
|
||||
return 0
|
||||
fi
|
||||
sleep 2
|
||||
slept=$((slept+2))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Execute a command inside CT with proper shell
|
||||
ct_exec() {
|
||||
need_cmd pct
|
||||
local ctid="$1"; shift
|
||||
pct exec "$ctid" -- bash -lc "$*"
|
||||
}
|
||||
|
||||
# Upload a local file/stream into CT (uses pct push)
|
||||
ct_push() {
|
||||
need_cmd pct
|
||||
local ctid="$1" src="$2" dst="$3"
|
||||
pct push "$ctid" "$src" "$dst"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user