update-lxcs.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 -e
  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. while true; do
  26. read -p "This Will Update All LXC Containers. Proceed(y/n)?" yn
  27. case $yn in
  28. [Yy]*) break ;;
  29. [Nn]*) exit ;;
  30. *) echo "Please answer yes or no." ;;
  31. esac
  32. done
  33. clear
  34. exclude_container="$@"
  35. containers=$(pct list | tail -n +2 | cut -f1 -d' ' | grep -vE "^($exclude_container)$")
  36. function update_container() {
  37. container=$1
  38. header_info
  39. name=$(pct exec "$container" hostname)
  40. echo -e "${BL}[Info]${GN} Updating ${BL}$container${CL} : ${GN}$name${CL} \n"
  41. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  42. case "$os" in
  43. alpine) pct exec "$container" -- ash -c "apk update && apk upgrade" ;;
  44. archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm";;
  45. fedora|rocky|centos|alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
  46. ubuntu|debian|devuan) pct exec "$container" -- bash -c "apt-get update && apt-get -y dist-upgrade" ;;
  47. esac
  48. }
  49. header_info
  50. for container in $containers; do
  51. status=$(pct status $container)
  52. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  53. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  54. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  55. pct start $container
  56. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  57. sleep 5
  58. update_container $container
  59. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  60. pct shutdown $container &
  61. elif [ "$status" == "status: running" ]; then
  62. update_container $container
  63. fi
  64. done
  65. wait
  66. header_info
  67. echo -e "${GN} Finished, All Containers Updated. ${CL} \n"