update-lxcs.sh 2.0 KB

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