update-lxcs.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. function header_info {
  3. cat <<"EOF"
  4. __ __ __ __ __ _ ________
  5. / / / /___ ____/ /___ _/ /____ / / | |/ / ____/
  6. / / / / __ \/ __ / __ `/ __/ _ \ / / | / /
  7. / /_/ / /_/ / /_/ / /_/ / /_/ __/ / /___/ / /___
  8. \____/ .___/\__,_/\__,_/\__/\___/ /_____/_/|_\____/
  9. /_/
  10. EOF
  11. }
  12. set -e
  13. YW=$(echo "\033[33m")
  14. BL=$(echo "\033[36m")
  15. RD=$(echo "\033[01;31m")
  16. CM='\xE2\x9C\x94\033'
  17. GN=$(echo "\033[1;92m")
  18. CL=$(echo "\033[m")
  19. clear
  20. header_info
  21. while true; do
  22. read -p "This Will Update All LXC Containers. Proceed(y/n)?" yn
  23. case $yn in
  24. [Yy]*) break ;;
  25. [Nn]*) exit ;;
  26. *) echo "Please answer yes or no." ;;
  27. esac
  28. done
  29. clear
  30. containers=$(pct list | tail -n +2 | cut -f1 -d' ')
  31. function update_container() {
  32. container=$1
  33. clear
  34. header_info
  35. name=`pct exec $container hostname`
  36. echo -e "${BL}[Info]${GN} Updating ${BL}$container${CL} : ${GN}$name${CL} \n"
  37. pct config $container > temp
  38. os=`awk '/^ostype/' temp | cut -d' ' -f2`
  39. if [ "$os" == "alpine" ]; then
  40. pct exec $container -- ash -c "apk update && apk upgrade"
  41. elif [ "$os" == "ubuntu" ] || [ "$PCT_OSTYPE" == "debian" ] || [ "$os" == "devuan" ]; then
  42. pct exec $container -- bash -c "apt-get update && apt-get -y upgrade"
  43. elif [ "$os" == "fedora" ] || [ "$os" == "rocky" ] || [ "$os" == "centos" ] || [ "$os" == "alma" ]; then
  44. pct exec $container -- bash -c "dnf -y update && dnf -y upgrade"
  45. elif [ "$os" == "archlinux" ]; then
  46. pct exec $container -- bash -c "pacman -Syyu --noconfirm"
  47. else
  48. pct exec $container -- bash -c "yum -y update"
  49. fi
  50. }
  51. header_info
  52. read -p "Skip stopped containers? [y/N]" -n 1 -r
  53. echo
  54. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  55. skip=no
  56. else
  57. skip=yes
  58. fi
  59. for container in $containers; do
  60. status=$(pct status $container)
  61. if [ "$skip" == "no" ]; then
  62. if [ "$status" == "status: stopped" ]; then
  63. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  64. pct start $container
  65. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  66. sleep 5
  67. update_container $container
  68. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  69. pct shutdown $container &
  70. elif [ "$status" == "status: running" ]; then
  71. update_container $container
  72. fi
  73. fi
  74. if [ "$skip" == "yes" ]; then
  75. if [ "$status" == "status: running" ]; then
  76. update_container $container
  77. fi
  78. fi
  79. done
  80. wait
  81. rm -rf temp
  82. clear
  83. header_info
  84. echo -e "${GN} Finished, All Containers Updated. ${CL} \n"