update-lxcs.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. function header_info {
  7. clear
  8. cat <<"EOF"
  9. __ __ __ __ __ _ ________
  10. / / / /___ ____/ /___ _/ /____ / / | |/ / ____/
  11. / / / / __ \/ __ / __ `/ __/ _ \ / / | / /
  12. / /_/ / /_/ / /_/ / /_/ / /_/ __/ / /___/ / /___
  13. \____/ .___/\__,_/\__,_/\__/\___/ /_____/_/|_\____/
  14. /_/
  15. EOF
  16. }
  17. set -eEuo pipefail
  18. YW=$(echo "\033[33m")
  19. BL=$(echo "\033[36m")
  20. RD=$(echo "\033[01;31m")
  21. CM='\xE2\x9C\x94\033'
  22. GN=$(echo "\033[1;92m")
  23. CL=$(echo "\033[m")
  24. header_info
  25. echo "Loading..."
  26. whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Updater" --yesno "This Will Update LXC Containers. Proceed?" 10 58 || exit
  27. NODE=$(hostname)
  28. CTID_MENU=()
  29. MSG_MAX_LENGTH=0
  30. while read -r TAG ITEM; do
  31. OFFSET=2
  32. ((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET
  33. CTID_MENU+=("$TAG" "$ITEM " "OFF")
  34. done < <(pct list | awk 'NR>1')
  35. excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist \
  36. "\nSelect containers to skip from updates:\n" \
  37. 16 $((MSG_MAX_LENGTH + 23)) 6 "${CTID_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || clear; exit
  38. function update_container() {
  39. container=$1
  40. header_info
  41. name=$(pct exec "$container" hostname)
  42. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  43. if [[ "$os" == "ubuntu" || "$os" == "debian" ]]; then
  44. disk_info=$(pct exec "$container" df /boot | awk 'NR==2{gsub("%","",$5); printf "%s %.1fG %.1fG %.1fG", $5, $3/1024/1024, $2/1024/1024, $4/1024/1024 }')
  45. read -ra disk_info_array <<<"$disk_info"
  46. echo -e "${BL}[Info]${GN} Updating ${BL}$container${CL} : ${GN}$name${CL} - ${YW}Boot Disk: ${disk_info_array[0]}% full [${disk_info_array[1]}/${disk_info_array[2]} used, ${disk_info_array[3]} free]${CL}\n"
  47. else
  48. echo -e "${BL}[Info]${GN} Updating ${BL}$container${CL} : ${GN}$name${CL} - ${YW}[No disk info for ${os}]${CL}\n"
  49. fi
  50. case "$os" in
  51. alpine) pct exec "$container" -- ash -c "apk update && apk upgrade" ;;
  52. archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
  53. fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
  54. ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get update 2>/dev/null | grep 'packages.*upgraded'; apt list --upgradable && apt-get -y dist-upgrade" ;;
  55. esac
  56. }
  57. header_info
  58. for container in $(pct list | awk '{if(NR>1) print $1}'); do
  59. if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
  60. header_info
  61. echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
  62. sleep 1
  63. else
  64. status=$(pct status $container)
  65. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  66. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  67. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  68. pct start $container
  69. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  70. sleep 5
  71. update_container $container
  72. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  73. pct shutdown $container &
  74. elif [ "$status" == "status: running" ]; then
  75. update_container $container
  76. fi
  77. fi
  78. done
  79. wait
  80. header_info
  81. echo -e "${GN} Finished, Selected Containers Updated. ${CL} \n"