diff --git a/lxc-toolbox-README.md b/lxc-toolbox-README.md new file mode 100644 index 0000000..57cc6a0 --- /dev/null +++ b/lxc-toolbox-README.md @@ -0,0 +1,52 @@ +# Proxmox LXC toolbox + +A Bash menu for choosing maintenance tasks and the containers to run them on. +Run as root on the Proxmox host, with Bash 4+, curl and pct installed. + +## Publish on your Gitea server + +1. Open https://codealvin.xyz/Alvin-Zilverstand/scripts and select the `main` branch. +2. Use the repository's upload-file action and upload `lxc-toolbox.sh` to the repository root. If it already exists, replace/edit that file instead. +3. Commit the file to `main`. The four existing maintenance scripts stay in place. + +After publishing, open the menu with: + +```bash +bash <(curl -fsSL https://codealvin.xyz/Alvin-Zilverstand/scripts/raw/branch/main/lxc-toolbox.sh) +``` + +Choose tasks by number (for example `1 2 3`) or `a` for all four. Then choose container IDs separated by spaces or commas (for example `101 105`), or `a` for all listed containers. Enter `q` to quit. + +## Command-line examples + +Run every task on containers 101 and 105: + +```bash +bash <(curl -fsSL https://codealvin.xyz/Alvin-Zilverstand/scripts/raw/branch/main/lxc-toolbox.sh) --all --ctids 101,105 +``` + +Run selected tasks on selected containers: + +```bash +bash lxc-toolbox.sh locale proxy docker --ctids '101 105' +``` + +Run every task on all currently listed containers: + +```bash +bash lxc-toolbox.sh --all --all-lxcs +``` + +`--all` selects tasks, while `--all-lxcs` selects containers. Omitting the container option opens the container prompt. The APT cacher script still asks whether to run `apt update`, so these commands are interactive. + +## Behavior + +- Tasks run in this order: locale, APT cacher, Docker repository conversion, backup cleanup. +- Selected scripts are downloaded from this repository's `main` branch and syntax-checked before any task starts. +- An exported Bash `pct` function filters `pct list` to the selected IDs and permits `status`, `config`, and `exec` only for those IDs. This supports the current upstream scripts without editing them; it is not a security sandbox. Future upstream scripts must continue to use ordinary `pct` calls, rather than bypassing the function with an absolute executable path or `command pct`. +- Unknown IDs are rejected before downloading/running tasks; duplicates are removed. The existing scripts skip stopped containers and non-Debian containers. +- The existing proxy address remains `192.168.178.22:3142`, and the locale remains `en_US.UTF-8`. Change those in their respective upstream scripts if needed. +- Docker conversion changes repository URLs from HTTPS to HTTP. Cleanup uses the existing script's deletion rules and does not remove backups stored under `/root/apt-backups/docker`. +- A nonzero script exit stops the sequence. Some upstream scripts print per-container errors but return success; inspect their output. Already-completed changes are not rolled back. + +Validation used mocked Proxmox commands with the current upstream scripts, covering selection, multiple IDs, skip behavior, invalid inputs, interactive selection, all-container mode, download failure, and direct calls to unselected IDs. No live containers were modified during testing. diff --git a/lxc-toolbox.sh b/lxc-toolbox.sh new file mode 100644 index 0000000..2ad58a7 --- /dev/null +++ b/lxc-toolbox.sh @@ -0,0 +1,199 @@ +#!/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' +readonly -a FILES=(setup-locale-lxcs.sh setup-apt-cacher-lxcs.sh docker-apt-http-lxcs.sh cleanup-apt-backups-lxcs.sh) +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'