Files
scripts/lxc-toolbox.sh
T

199 lines
7.0 KiB
Bash
Raw Normal View History

2026-10-28 12:55:56 +01:00
#!/usr/bin/env bash
# Run on the Proxmox host. Requires Bash 4+, curl and pct.
set -uo pipefail
readonly BASE_URL='https://codealvin.xyz/Alvin-Zilverstand/scripts/raw/branch/main'
2026-10-29 05:38:44 +01:00
readonly -a FILES=(setup-locale-lxcs.sh setup-apt-cacher-lxcs.sh apt-http-lxcs.sh cleanup-apt-backups-lxcs.sh)
2026-10-28 12:55:56 +01:00
readonly -a KEYS=(locale proxy docker cleanup)
readonly -a LABELS=('Configure en_US.UTF-8 locale' 'Configure APT cacher (192.168.178.22:3142)' 'Change Docker APT repositories from HTTPS to HTTP' 'Delete legacy APT backup files')
selected=(0 0 0 0)
target_spec=''
all_lxcs=0
workdir=''
cleanup() { [[ -z "$workdir" ]] || rm -rf -- "$workdir"; }
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
usage() {
cat <<'HELP'
Usage: bash lxc-toolbox.sh [TASK ... | --all] [--ctids '101 102' | --all-lxcs]
bash lxc-toolbox.sh [--list | --help]
Without arguments, opens a simple selection menu.
Tasks: locale, proxy, docker, cleanup (or numbers 1 through 4).
Examples:
bash lxc-toolbox.sh
bash lxc-toolbox.sh locale proxy docker --ctids '101 102'
bash lxc-toolbox.sh --all --ctids 101,102
bash lxc-toolbox.sh --all --all-lxcs
Prompts for containers unless --ctids or --all-lxcs is supplied.
--all selects all tasks; --all-lxcs selects all containers in the current list.
Runs selected tasks in the order above; upstream scripts skip stopped/non-Debian LXCs.
The proxy script still asks whether to run apt update, including with --all.
Cleanup deletes legacy backup files; Docker conversion changes HTTPS to HTTP.
Selected scripts are fetched from the upstream main branch on each run.
HELP
}
list_tasks() {
local i
for i in "${!FILES[@]}"; do
printf ' %s) %-8s %s\n' "$((i + 1))" "${KEYS[i]}" "${LABELS[i]}"
done
}
select_task() {
case "$1" in
1|locale) selected[0]=1 ;;
2|proxy) selected[1]=1 ;;
3|docker) selected[2]=1 ;;
4|cleanup) selected[3]=1 ;;
a|all|--all) selected=(1 1 1 1) ;;
*) printf 'Unknown task: %s\n' "$1" >&2; return 2 ;;
esac
}
if (( $# == 1 )); then
case "$1" in
-h|--help) usage; exit 0 ;;
--list) list_tasks; exit 0 ;;
esac
fi
choices=()
while (( $# )); do
case "$1" in
--ctids)
if (( $# < 2 )) || [[ -z "$2" || -n "$target_spec" ]] || (( all_lxcs )); then
printf 'Supply --ctids once, with a nonempty list, without --all-lxcs.\n' >&2
exit 2
fi
target_spec=$2
shift 2 ;;
--all-lxcs)
[[ -z "$target_spec" ]] || { printf 'Use --ctids or --all-lxcs, not both.\n' >&2; exit 2; }
all_lxcs=1
shift ;;
*) choices+=("$1"); shift ;;
esac
done
if (( ${#choices[@]} == 0 )); then
printf '\nProxmox LXC toolbox\n\n'
list_tasks
printf '\n a) Run all four in order\n q) Quit\n\n'
printf 'Select tasks separated by spaces (for example: 1 2 3): '
IFS= read -r answer || exit 0
[[ "$answer" != q && "$answer" != Q ]] || exit 0
read -r -a choices <<< "$answer"
(( ${#choices[@]} > 0 )) || exit 0
fi
for choice in "${choices[@]}"; do
select_task "$choice" || exit 2
done
if (( EUID != 0 )); then
printf 'Run this script as root on the Proxmox host.\n' >&2
exit 1
fi
for dependency in pct curl bash mktemp; do
command -v "$dependency" >/dev/null 2>&1 || {
printf 'Missing required command: %s\n' "$dependency" >&2
exit 1
}
done
# Save the real executable before exporting a restricted pct function to the
# upstream Bash scripts. Their existing container loops remain unchanged.
LXC_TOOLBOX_PCT=$(type -P pct) || exit 1
inventory=$("$LXC_TOOLBOX_PCT" list) || { printf 'Unable to list containers.\n' >&2; exit 1; }
mapfile -t available < <(awk 'NR > 1 && $1 ~ /^[0-9]+$/ {print $1}' <<< "$inventory")
(( ${#available[@]} > 0 )) || { printf 'No containers found.\n'; exit 0; }
if [[ -z "$target_spec" ]] && (( ! all_lxcs )); then
printf '\nAvailable LXCs\n%s\n\n' "$inventory"
printf 'Enter CTIDs separated by spaces or commas, a for all, or q to quit: '
IFS= read -r target_spec || exit 0
case "$target_spec" in
q|Q|'') exit 0 ;;
a|all) all_lxcs=1 ;;
esac
fi
targets=()
if (( all_lxcs )); then
targets=("${available[@]}")
else
target_spec=${target_spec//,/ }
read -r -a requested <<< "$target_spec"
(( ${#requested[@]} > 0 )) || { printf 'No CTIDs supplied.\n' >&2; exit 2; }
for id in "${requested[@]}"; do
[[ "$id" =~ ^[0-9]+$ ]] || { printf 'Invalid CTID: %s\n' "$id" >&2; exit 2; }
found=0
for known in "${available[@]}"; do
[[ "$known" != "$id" ]] || found=1
done
(( found )) || { printf 'Unknown CTID: %s. No tasks were started.\n' "$id" >&2; exit 2; }
case " ${targets[*]} " in
*" $id "*) ;;
*) targets+=("$id") ;;
esac
done
fi
LXC_TOOLBOX_IDS=" ${targets[*]} "
export LXC_TOOLBOX_PCT LXC_TOOLBOX_IDS
pct() {
# Limit both enumeration and direct calls to the selected container IDs.
# This is a compatibility adapter, not a sandbox for untrusted scripts.
if [[ "${1:-}" == list && $# == 1 ]]; then
local listing
listing=$("$LXC_TOOLBOX_PCT" list) || return $?
awk -v ids="$LXC_TOOLBOX_IDS" 'NR == 1 || index(ids, " " $1 " ") > 0' <<< "$listing"
return
fi
case "${1:-}" in
status|config|exec)
if [[ "${2:-}" =~ ^[0-9]+$ && "$LXC_TOOLBOX_IDS" == *" $2 "* ]]; then
"$LXC_TOOLBOX_PCT" "$@"
return $?
fi ;;
esac
printf 'Blocked pct call outside selected containers or supported commands: %s\n' "$*" >&2
return 1
}
export -f pct
printf '\nSelected CTIDs: %s\n' "${targets[*]}"
workdir=$(mktemp -d "${TMPDIR:-/tmp}/lxc-toolbox.XXXXXXXX") || exit 1
printf '\nDownloading and syntax-checking selected scripts...\n'
for i in "${!FILES[@]}"; do
(( selected[i] )) || continue
file=${FILES[i]}
if ! curl --fail --show-error --silent --location --retry 2 \
--connect-timeout 15 --max-time 120 --proto '=https' --proto-redir '=https' \
"$BASE_URL/$file" -o "$workdir/$file"; then
printf 'Download failed: %s. No tasks were started.\n' "$file" >&2
exit 1
fi
if [[ ! -s "$workdir/$file" ]] || ! bash -n "$workdir/$file"; then
printf 'Empty or invalid Bash script: %s. No tasks were started.\n' "$file" >&2
exit 1
fi
done
for i in "${!FILES[@]}"; do
(( selected[i] )) || continue
printf '\n============================================================\n'
printf 'Running: %s\n' "${LABELS[i]}"
printf '============================================================\n'
# Keep stdin attached so upstream interactive prompts work normally.
bash "$workdir/${FILES[i]}"
status=$?
if (( status != 0 )); then
printf '\n%s exited with code %s; remaining tasks were not run.\n' "${FILES[i]}" "$status" >&2
exit "$status"
fi
done
printf '\nSelected scripts finished. Check their output for per-container errors.\n'