latest-update-menu.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env bash
  2. while true; do
  3. read -p "This will create a New Update Menu for Home Assistant Container LXC. Proceed(y/n)?" yn
  4. case $yn in
  5. [Yy]* ) break;;
  6. [Nn]* ) exit;;
  7. * ) echo "Please answer yes or no.";;
  8. esac
  9. done
  10. clear
  11. set -o errexit
  12. rm -rf /root/update.sh update update-containers.sh
  13. echo -e "\e[1;92m Creating New Update Menu Script... \e[0m"
  14. if [ -f /usr/local/lib/python3.9/dist-packages/runlike/runlike.py ]; then
  15. echo -e "\e[1;92m pip3/runlike Already Installed! \e[0m"
  16. else
  17. apt-get update &>/dev/null
  18. echo -e "\e[1;92m Installing pip3... \e[0m"
  19. apt-get install -y python3-pip &>/dev/null
  20. echo -e "\e[1;92m Installing runlike... \e[0m"
  21. pip3 install runlike &>/dev/null
  22. fi
  23. echo -e "\e[1;92m Creating Update Script... \e[0m"
  24. if [ -d /root/hass_config ]; then
  25. echo -e "\e[1;92m There's Already (hass_config) Folder! \e[0m"
  26. else
  27. mkdir /root/hass_config
  28. fi
  29. UPDATE_PATH='/root/update'
  30. UPDATE_CONTAINERS_PATH='/root/update-containers.sh'
  31. cat >$UPDATE_PATH <<'EOF'
  32. #!/bin/sh
  33. set -o errexit
  34. show_menu(){
  35. normal=`echo "\033[m"`
  36. safe=`echo "\033[32m"`
  37. menu=`echo "\033[36m"`
  38. number=`echo "\033[33m"`
  39. bgred=`echo "\033[41m"`
  40. fgred=`echo "\033[31m"`
  41. hostname -I
  42. printf "\n${menu}*********************************************${normal}\n"
  43. printf "${menu}**${number} 1)${safe} Switch to Stable Branch ${normal}\n"
  44. printf "${menu}**${number} 2)${number} Switch to Beta Branch ${normal}\n"
  45. printf "${menu}**${number} 3)${fgred} Switch to Dev Branch ${normal}\n"
  46. printf "${menu}**${number} 4)${safe} Backup Home Assistant Data (to root) ${normal}\n"
  47. printf "${menu}**${number} 5)${number} Restore Home Assistant Data ${normal}\n"
  48. printf "${menu}**${number} 6)${fgred} Edit Home Assistant Configuration ${normal}\n"
  49. printf "${menu}**${number} 7)${safe} Restart Home Assistant ${normal}\n"
  50. printf "${menu}**${number} 8)${safe} Just Update Containers ${normal}\n"
  51. printf "${menu}**${number} 9)${number} Remove Unused Images ${normal}\n"
  52. printf "${menu}**${number} 10)${safe} Update Host OS ${normal}\n"
  53. printf "${menu}**${number} 11)${safe} Reboot Host OS ${normal}\n"
  54. printf "${menu}*********************************************${normal}\n"
  55. printf "Please choose an option from the menu and enter or ${fgred}x to exit. ${normal}"
  56. read opt
  57. }
  58. option_picked(){
  59. msgcolor=`echo "\033[01;31m"`
  60. normal=`echo "\033[00;00m"`
  61. message=${@:-"${normal}Error: No message passed"}
  62. printf "${msgcolor}${message}${normal}\n"
  63. }
  64. clear
  65. show_menu
  66. while [ $opt != '' ]
  67. do
  68. if [ $opt = '' ]; then
  69. exit;
  70. else
  71. case $opt in
  72. 1) clear;
  73. option_picked "Switching to Stable Branch";
  74. TAG=stable
  75. break;
  76. ;;
  77. 2) clear;
  78. option_picked "Switching to Beta Branch";
  79. TAG=beta
  80. break;
  81. ;;
  82. 3) while true; do
  83. read -p "Are you sure you want to Switch to Dev Branch? Proceed(y/n)?" yn
  84. case $yn in
  85. [Yy]* ) break;;
  86. [Nn]* ) exit;;
  87. * ) echo "Please answer yes or no.";;
  88. esac
  89. done
  90. clear;
  91. option_picked "Switching to Dev Branch";
  92. TAG=dev
  93. break;
  94. ;;
  95. 4) clear;
  96. option_picked "Backing up Home Assistant Data to root (hass_config)";
  97. rm -r hass_config;
  98. cp -pR /var/lib/docker/volumes/hass_config/ /root/;
  99. sleep 2;
  100. clear;
  101. show_menu;
  102. ;;
  103. 5) while true; do
  104. read -p "Are you sure you want to Restore Home Assistant Data? Proceed(y/n)?" yn
  105. case $yn in
  106. [Yy]* ) break;;
  107. [Nn]* ) exit;;
  108. * ) echo "Please answer yes or no.";;
  109. esac
  110. done
  111. clear;
  112. option_picked "Restoring Home Assistant Data from root (hass_config)";
  113. rm -r /var/lib/docker/volumes/hass_config/_data;
  114. cp -pR /root/hass_config/_data /var/lib/docker/volumes/hass_config/;
  115. sleep 2;
  116. clear;
  117. show_menu;
  118. ;;
  119. 6) while true; do
  120. read -p "Are you sure you want to Edit Home Assistant Configuration? Proceed(y/n)?" yn
  121. case $yn in
  122. [Yy]* ) break;;
  123. [Nn]* ) exit;;
  124. * ) echo "Please answer yes or no.";;
  125. esac
  126. done
  127. clear;
  128. option_picked "Editing Home Assistant Configuration";
  129. nano /var/lib/docker/volumes/hass_config/_data/configuration.yaml;
  130. clear;
  131. show_menu;
  132. ;;
  133. 7) clear;
  134. option_picked "Restarting Home Assistant";
  135. docker restart homeassistant;
  136. exit;
  137. ;;
  138. 8) clear;
  139. option_picked "Just Updating Containers";
  140. ./update-containers.sh;
  141. sleep 2;
  142. clear;
  143. show_menu;
  144. ;;
  145. 9) clear;
  146. option_picked "Removing Unused Images";
  147. docker image prune -af;
  148. sleep 2;
  149. clear;
  150. show_menu;
  151. ;;
  152. 10) clear;
  153. option_picked "Updating Host OS";
  154. apt update && apt upgrade -y;
  155. sleep 2;
  156. clear;
  157. show_menu;
  158. ;;
  159. 11) clear;
  160. option_picked "Reboot Host OS";
  161. reboot;
  162. exit;
  163. ;;
  164. x)exit;
  165. ;;
  166. \n)exit;
  167. ;;
  168. *)clear;
  169. option_picked "Please choose an option from the menu";
  170. show_menu;
  171. ;;
  172. esac
  173. fi
  174. done
  175. docker pull homeassistant/home-assistant:$TAG
  176. docker rm --force homeassistant
  177. docker run -d \
  178. --name homeassistant \
  179. --privileged \
  180. --restart unless-stopped \
  181. -v /var/run/docker.sock:/var/run/docker.sock \
  182. -v /dev:/dev \
  183. -v hass_config:/config \
  184. -v /etc/localtime:/etc/localtime:ro \
  185. -v /etc/timezone:/etc/timezone:ro \
  186. --net=host \
  187. homeassistant/home-assistant:$TAG
  188. EOF
  189. sudo chmod +x /root/update
  190. cat >$UPDATE_CONTAINERS_PATH <<'EOF'
  191. #!/bin/bash
  192. set -o errexit
  193. CONTAINER_LIST="${1:-$(docker ps -q)}"
  194. for container in ${CONTAINER_LIST}; do
  195. CONTAINER_IMAGE="$(docker inspect --format "{{.Config.Image}}" --type container ${container})"
  196. RUNNING_IMAGE="$(docker inspect --format "{{.Image}}" --type container "${container}")"
  197. docker pull "${CONTAINER_IMAGE}"
  198. LATEST_IMAGE="$(docker inspect --format "{{.Id}}" --type image "${CONTAINER_IMAGE}")"
  199. if [[ "${RUNNING_IMAGE}" != "${LATEST_IMAGE}" ]]; then
  200. echo "Updating ${container} image ${CONTAINER_IMAGE}"
  201. DOCKER_COMMAND="$(runlike "${container}")"
  202. docker rm --force "${container}"
  203. eval ${DOCKER_COMMAND}
  204. fi
  205. done
  206. EOF
  207. sudo chmod +x /root/update-containers.sh
  208. echo -e "\e[1;92m Finished. Type ./update in the LXC console\e[0m"