commit 5ff19242938b92ded120b6ad5ecc561697d77877 Author: alvin Date: Tue Sep 15 12:49:49 2026 +0200 Add setup-apt-cacher-lxcs.sh diff --git a/setup-apt-cacher-lxcs.sh b/setup-apt-cacher-lxcs.sh new file mode 100644 index 0000000..e1165ad --- /dev/null +++ b/setup-apt-cacher-lxcs.sh @@ -0,0 +1,99 @@ +cat > /root/setup-apt-cacher-lxcs.sh <<'EOF' +#!/usr/bin/env bash + +set -u + +# ============================== +# Configuration +# ============================== + +ACNG_SERVER="192.168.178.22" +ACNG_PORT="3142" + +PROXY_FILE="/etc/apt/apt.conf.d/01proxy" + +echo +echo "========================================" +echo " Apt-Cacher-NG LXC Configuration Script" +echo "========================================" +echo +echo "Proxy: http://${ACNG_SERVER}:${ACNG_PORT}" +echo + +read -rp "Run apt update after configuring each container? [y/N]: " RUN_UPDATE +echo + +for CTID in $(pct list | awk 'NR>1 {print $1}'); do + + STATUS=$(pct status "$CTID" 2>/dev/null | awk '{print $2}') + + echo "----------------------------------------" + echo "Container: $CTID" + echo "Status: $STATUS" + + if [[ "$STATUS" != "running" ]]; then + echo "SKIP: Container is not running." + continue + fi + + # Detect operating system + OS_ID=$(pct exec "$CTID" -- sh -c \ + '. /etc/os-release 2>/dev/null && echo "$ID"' \ + 2>/dev/null || true) + + OS_NAME=$(pct exec "$CTID" -- sh -c \ + '. /etc/os-release 2>/dev/null && echo "$PRETTY_NAME"' \ + 2>/dev/null || true) + + echo "OS: ${OS_NAME:-Unknown}" + + if [[ "$OS_ID" != "debian" ]]; then + echo "SKIP: Not Debian." + continue + fi + + echo "Configuring Apt-Cacher-NG..." + + # Backup existing configuration if there is one + pct exec "$CTID" -- sh -c " + if [ -f '$PROXY_FILE' ]; then + cp '$PROXY_FILE' '${PROXY_FILE}.backup-\$(date +%Y%m%d-%H%M%S)' + fi + " + + # Create proxy configuration + pct exec "$CTID" -- sh -c "cat > '$PROXY_FILE' <<'APTCONFIG' +Acquire::http::Proxy \"http://${ACNG_SERVER}:${ACNG_PORT}\"; +Acquire::https::Proxy \"DIRECT\"; +APTCONFIG +" + + if [[ $? -ne 0 ]]; then + echo "ERROR: Failed to configure container $CTID." + continue + fi + + echo "OK: Proxy configuration installed." + + echo + echo "Current configuration:" + pct exec "$CTID" -- cat "$PROXY_FILE" + + if [[ "$RUN_UPDATE" =~ ^[Yy]$ ]]; then + echo + echo "Running apt update..." + pct exec "$CTID" -- apt-get update + + if [[ $? -eq 0 ]]; then + echo "OK: apt update completed." + else + echo "WARNING: apt update failed in container $CTID." + fi + fi + + echo +done + +echo "========================================" +echo " Finished" +echo "========================================" \ No newline at end of file