87 lines
2.8 KiB
Bash
87 lines
2.8 KiB
Bash
#!/bin/bash
|
||
|
||
# Colors
|
||
GREEN="\033[1;32m"
|
||
RED="\033[1;31m"
|
||
YELLOW="\033[1;33m"
|
||
NC="\033[0m"
|
||
|
||
echo -e "${GREEN}Proxmox Apt-Cacher-NG Setup${NC}"
|
||
|
||
# Prompt for target selection
|
||
while true; do
|
||
echo -e "${YELLOW}Select what to update:${NC}"
|
||
echo "1) LXC Containers"
|
||
echo "2) VMs"
|
||
echo "3) All (LXC + VMs)"
|
||
read -rp "Enter choice [1-3]: " TARGET_CHOICE
|
||
case $TARGET_CHOICE in
|
||
1|2|3) break;;
|
||
*) echo -e "${RED}Invalid choice. Please enter 1, 2, or 3.${NC}";;
|
||
esac
|
||
done
|
||
|
||
# Prompt for updating Proxmox node itself
|
||
while true; do
|
||
read -rp "Do you want to update the Proxmox node itself with the cache? (y/n): " UPDATE_NODE
|
||
case $UPDATE_NODE in
|
||
y|n) break;;
|
||
*) echo -e "${RED}Please enter y or n.${NC}";;
|
||
esac
|
||
done
|
||
|
||
# Ask for the apt-cacher-ng server IP and port
|
||
read -rp "Enter your apt-cacher-ng IP address (default: 192.168.1.10): " CACHE_IP
|
||
CACHE_IP="${CACHE_IP:-192.168.1.10}"
|
||
|
||
read -rp "Enter apt-cacher-ng port (default: 3142): " CACHE_PORT
|
||
CACHE_PORT="${CACHE_PORT:-3142}"
|
||
|
||
APT_PROXY="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
|
||
|
||
echo -e "\n${YELLOW}Using APT Proxy: ${APT_PROXY}${NC}\n"
|
||
|
||
# Confirm before proceeding
|
||
read -rp "Proceed to update selected targets with this proxy? (y/n): " CONFIRM
|
||
[[ "$CONFIRM" != "y" ]] && echo "Aborted." && exit 1
|
||
|
||
### Update LXC containers ###
|
||
if pct list &>/dev/null && { [ "$TARGET_CHOICE" = "1" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
|
||
echo -e "\n${GREEN}Updating LXC Containers...${NC}"
|
||
for CTID in $(pct list | awk 'NR>1 {print $1}'); do
|
||
echo -e "\n${YELLOW}Updating LXC: $CTID${NC}"
|
||
pct exec "$CTID" -- bash -c "
|
||
mkdir -p /etc/apt/apt.conf.d
|
||
echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy
|
||
"
|
||
echo -e "${GREEN}LXC $CTID updated successfully.${NC}"
|
||
done
|
||
fi
|
||
|
||
### Update VMs via QEMU Guest Agent ###
|
||
if qm list &>/dev/null && { [ "$TARGET_CHOICE" = "2" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
|
||
echo -e "\n${GREEN}Updating VMs via QEMU Agent...${NC}"
|
||
for VMID in $(qm list | awk 'NR>1 {print $1}'); do
|
||
if qm guest cmd "$VMID" get-osinfo &>/dev/null; then
|
||
echo -e "\n${YELLOW}Updating VM: $VMID${NC}"
|
||
qm guest exec "$VMID" -- bash -c "
|
||
mkdir -p /etc/apt/apt.conf.d
|
||
echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy
|
||
" &>/dev/null
|
||
echo -e "${GREEN}VM $VMID updated successfully.${NC}"
|
||
else
|
||
echo -e "${RED}Skipping VM $VMID – QEMU Guest Agent not available.${NC}"
|
||
fi
|
||
done
|
||
fi
|
||
|
||
### Update Proxmox Node itself ###
|
||
if [ "$UPDATE_NODE" = "y" ]; then
|
||
echo -e "\n${GREEN}Updating Proxmox Node itself...${NC}"
|
||
mkdir -p /etc/apt/apt.conf.d
|
||
echo "${APT_PROXY}" > /etc/apt/apt.conf.d/01proxy
|
||
echo -e "${GREEN}Proxmox Node updated successfully.${NC}"
|
||
fi
|
||
|
||
echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated as selected.${NC}"
|