menu_functions.sh 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. # The options menu and all its associated functions
  2. # Options are only shown if relevant in that moment (e.g. "setup" only if service is already configured, "configure" only if service has not yet been configured, "reconfigure" if service is already configured, "destroy" only if it has been configured or installed etc...)
  3. # Services marked orange are configured but not installed.
  4. # Services marked green are installed and running
  5. # If containers of a service are currently stopped the services will say (stopped) behind the service name. This only works if the service has been stopped via the dockerbunker menu, because only then the service is marked as stopped in dockerbunker.env
  6. options_menu() {
  7. COLUMNS=12
  8. exitmenu=$(printf "\e[1;4;33mExit\e[0m")
  9. returntopreviousmenu=$(printf "\e[1;4;33mReturn to previous menu\e[0m")
  10. container_check=1
  11. # if service is marked as installed, make sure all containers exist and offer to run them if necessary
  12. if elementInArray "${PROPER_NAME}" "${INSTALLED_SERVICES[@]}";then
  13. for container in "${containers[@]}";do
  14. RUNNING=$(docker ps -a -q --filter name=^/${container}$)
  15. while [[ -z ${RUNNING} ]];do
  16. echo -e "\n\e[3m$container container missing\e[0m\n" \
  17. && prompt_confirm "Re-run container?" \
  18. && docker_run ${container//-/_}
  19. RUNNING=$(docker ps -a -q --filter name=^/${container}$)
  20. done
  21. done
  22. fi
  23. if [[ $RUNNING ]];then
  24. menu=( "Reconfigure service" "Reinstall service" "Backup Service" "Upgrade Image(s)" "Destroy \"${PROPER_NAME}\"" "$exitmenu" )
  25. add_ssl_menuentry menu 2
  26. if elementInArray "${PROPER_NAME}" "${STOPPED_SERVICES}";then
  27. insert menu "Start container(s)" 3
  28. else
  29. insert menu "Restart container(s)" 3
  30. insert menu "Stop container(s)" 4
  31. fi
  32. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  33. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  34. && insert menu "Restore Service" 6
  35. elif [[ $RUNNING == "false" ]];then
  36. menu=( "Reconfigure service" "Reinstall service" "Backup Service" "Start container(s)" "Destroy \"${PROPER_NAME}\"" "$exitmenu" )
  37. add_ssl_menuentry menu 2
  38. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  39. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  40. && insert menu "Restore Service" 3
  41. else
  42. if ! elementInArray "${PROPER_NAME}" "${CONFIGURED_SERVICES[@]}" \
  43. && ! elementInArray "${PROPER_NAME}" "${INSTALLED_SERVICES[@]}" \
  44. && [[ ! -f "${ENV_DIR}"/${SERVICE_NAME}.env ]];then
  45. [[ ${STATIC} \
  46. && $(ls -A "${ENV_DIR}"/static) ]] \
  47. && menu=( "Configure Site" "Manage Sites" "$exitmenu" ) \
  48. || menu=( "Configure Service" "$exitmenu" )
  49. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  50. && [[ "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  51. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  52. && insert menu "Restore Service" 1
  53. elif ! elementInArray "${PROPER_NAME}" "${CONFIGURED_SERVICES[@]}" \
  54. && ! elementInArray "${PROPER_NAME}" "${INSTALLED_SERVICES[@]}";then
  55. menu=( "Destroy \"${PROPER_NAME}\"" "$exitmenu" )
  56. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  57. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  58. && insert menu "Restore Service" 1
  59. error="Environment file found but ${PROPER_NAME} is not marked as configured or installed. Please destroy first!"
  60. elif elementInArray "${PROPER_NAME}" "${CONFIGURED_SERVICES[@]}" \
  61. && [[ ! -f "${ENV_DIR}"/${SERVICE_NAME}.env ]];then
  62. error="Service marked as configured, but configuration file is missing. Please destroy first."
  63. menu=( "Destroy \"${PROPER_NAME}\"" "$exitmenu" )
  64. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  65. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  66. && insert menu "Restore Service" 1
  67. elif elementInArray "${PROPER_NAME}" "${CONFIGURED_SERVICES[@]}" \
  68. && [[ -f "${ENV_DIR}"/${SERVICE_NAME}.env ]];then
  69. menu=( "Reconfigure service" "Setup service" "Destroy \"${PROPER_NAME}\"" "$exitmenu" )
  70. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  71. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]] \
  72. && insert menu "Restore Service" 2
  73. fi
  74. fi
  75. echo ""
  76. echo -e "\e[4m${PROPER_NAME}\e[0m"
  77. if [[ $error ]];then
  78. echo -e "\n\e[3m$error\e[0m\n"
  79. fi
  80. select choice in "${menu[@]}"
  81. do
  82. case $choice in
  83. "Configure Site")
  84. echo -e "\n\e[3m\xe2\x86\x92 Configure ${PROPER_NAME}\e[0m\n"
  85. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh configure
  86. say_done
  87. sleep 0.2
  88. break
  89. ;;
  90. "Configure Service")
  91. echo -e "\n\e[3m\xe2\x86\x92 Configure ${PROPER_NAME}\e[0m\n"
  92. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh configure
  93. sleep 0.2
  94. break
  95. ;;
  96. "Manage Sites")
  97. echo -e "\n\e[3m\xe2\x86\x92 Manage sites\e[0m"
  98. static_menu
  99. sleep 0.2
  100. break
  101. ;;
  102. "Reconfigure service")
  103. echo -e "\n\e[3m\xe2\x86\x92 Reconfigure ${PROPER_NAME}\e[0m"
  104. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh reconfigure
  105. break
  106. ;;
  107. "Setup service")
  108. # Set up nginx container if not yet present
  109. setup_nginx
  110. echo -e "\n\e[3m\xe2\x86\x92 Setup ${PROPER_NAME}\e[0m"
  111. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh setup
  112. sleep 0.2
  113. break
  114. ;;
  115. "Reinstall service")
  116. echo -e "\n\e[3m\xe2\x86\x92 Reinstall ${PROPER_NAME}\e[0m"
  117. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh reinstall
  118. say_done
  119. sleep 0.2
  120. break
  121. ;;
  122. "Upgrade Image(s)")
  123. echo -e "\n\e[3m\xe2\x86\x92 Upgrade ${PROPER_NAME} images\e[0m"
  124. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh upgrade
  125. say_done
  126. sleep 0.2
  127. break
  128. ;;
  129. "Backup Service")
  130. echo -e "\n\e[3m\xe2\x86\x92 Backup Service\e[0m"
  131. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh backup
  132. say_done
  133. sleep 0.2
  134. break
  135. ;;
  136. "Restore Service")
  137. echo -e "\n\e[3m\xe2\x86\x92 Restore Service\e[0m"
  138. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh restore
  139. say_done
  140. sleep 0.2
  141. break
  142. ;;
  143. "Generate self-signed certificate")
  144. generate_certificate
  145. restart_nginx
  146. say_done
  147. sleep 0.2
  148. break
  149. ;;
  150. "Obtain Let's Encrypt certificate")
  151. get_le_cert
  152. say_done
  153. sleep 0.2
  154. exit
  155. ;;
  156. "Renew Let's Encrypt certificate")
  157. get_le_cert renew
  158. say_done
  159. sleep 0.2
  160. exit
  161. ;;
  162. "Restart container(s)")
  163. echo -e "\n\e[3m\xe2\x86\x92 Restart ${PROPER_NAME} Containers\e[0m"
  164. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh restart_containers
  165. say_done
  166. sleep 0.2
  167. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh
  168. break
  169. ;;
  170. "Start container(s)")
  171. echo -e "\n\e[3m\xe2\x86\x92 Start ${PROPER_NAME} Containers\e[0m"
  172. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh start_containers
  173. say_done
  174. sleep 0.2
  175. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh
  176. break
  177. ;;
  178. "Stop container(s)")
  179. echo -e "\n\e[3m\xe2\x86\x92 Stop ${PROPER_NAME} Containers\e[0m"
  180. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh stop_containers
  181. say_done
  182. sleep 0.2
  183. ${SERVICES_DIR}/${SERVICE_NAME}/${SERVICE_NAME}.sh
  184. break
  185. ;;
  186. "Destroy \"${PROPER_NAME}\"")
  187. echo -e "\n\e[3m\xe2\x86\x92 Destroy ${PROPER_NAME}\e[0m"
  188. echo ""
  189. echo "The following will be removed:"
  190. echo ""
  191. for container in ${containers[@]};do
  192. [[ $(docker ps -a -q --filter name=^/${container}$) ]] \
  193. && containers_found+=( $container )
  194. done
  195. [[ ${containers_found[0]} ]] \
  196. && echo "- ${PROPER_NAME} container(s)"
  197. for volume in ${volumes[@]};do
  198. [[ $(docker volume ls -q --filter name=^${volume}$) ]] \
  199. && volumes_found+=( $volume )
  200. done
  201. [[ ${volumes_found[0]} ]] \
  202. && echo "- ${PROPER_NAME} volume(s)"
  203. [[ -f "${ENV_DIR}"/static/${SERVICE_DOMAIN[0]}.env \
  204. || -f "${ENV_DIR}"/${SERVICE_NAME}.env ]] \
  205. && echo "- ${PROPER_NAME} environment file(s)"
  206. [[ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf \
  207. || -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]] \
  208. && echo "- nginx configuration of ${SERVICE_DOMAIN[0]}"
  209. [[ ${SERVICE_DOMAIN[0]} \
  210. && -d "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]} ]] \
  211. && echo "- self-signed certificate for ${SERVICE_DOMAIN[0]}"
  212. echo ""
  213. prompt_confirm "Continue?" \
  214. && prompt_confirm "Are you sure?" \
  215. && . "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh destroy_service
  216. say_done
  217. sleep 0.2
  218. ${BASE_DIR}/dockerbunker.sh
  219. ;;
  220. "$exitmenu")
  221. exit 0
  222. ;;
  223. *)
  224. echo "Invalid option."
  225. ;;
  226. esac
  227. done
  228. }
  229. get_le_cert() {
  230. if ! [[ $1 == "renew" ]];then
  231. echo -e "\n\e[3m\xe2\x86\x92 Obtain Let's Encrypt certificate\e[0m"
  232. [[ -z ${LE_EMAIL} ]] && get_le_email
  233. if [[ ${STATIC} ]];then
  234. sed -i "s/SSL_CHOICE=.*/SSL_CHOICE=le/" "${ENV_DIR}"/static/${SERVICE_DOMAIN[0]}.env
  235. sed -i "s/LE_EMAIL=.*/LE_EMAIL="${LE_EMAIL}"/" "${ENV_DIR}"/static/${SERVICE_DOMAIN[0]}.env
  236. else
  237. sed -i "s/SSL_CHOICE=.*/SSL_CHOICE=le/" "${SERVICE_ENV}"
  238. sed -i "s/LE_EMAIL=.*/LE_EMAIL="${LE_EMAIL}"/" "${SERVICE_ENV}"
  239. fi
  240. elementInArray "${PROPER_NAME}" "${STOPPED_SERVICES[@]}" \
  241. && "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh start_containers
  242. if [[ ${SERVICE_DOMAIN[0]} && -d "${CONF_DIR}"/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} \
  243. && ! -L "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/cert.pem ]];then
  244. # Back up self-signed certificate
  245. mv "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/cert.{pem,pem.backup}
  246. mv "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/key.{pem,pem.backup}
  247. # Symlink letsencrypt certificate
  248. ln -sf "/etc/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]}/fullchain.pem" "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/cert.pem
  249. ln -sf "/etc/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]}/privkey.pem" "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/key.pem
  250. fi
  251. letsencrypt issue
  252. else
  253. echo -e "\n\e[3m\xe2\x86\x92 Renew Let's Encrypt certificate\e[0m"
  254. export prevent_nginx_restart=1
  255. bash "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh letsencrypt issue
  256. fi
  257. }
  258. # start/stop/restart nginx container
  259. restart_nginx() {
  260. echo -en "\n\e[1mRestarting nginx container\e[0m"
  261. docker exec -it nginx-dockerbunker nginx -t >/dev/null \
  262. && docker restart ${NGINX_CONTAINER} >/dev/null
  263. exit_response
  264. if [[ $? == 1 ]];then
  265. echo ""
  266. docker exec -it nginx-dockerbunker nginx -t
  267. echo -e "\n\e[3m\xe2\x86\x92 \e[3m\`nginx -t\` failed. Trying to add missing containers to dockerbunker-network.\e[0m"
  268. for container in ${CONTAINERS_IN_DOCKERBUNKER_NETWORK[@]};do
  269. connect_containers_to_network ${container}
  270. done
  271. echo -en "\n\e[1mRestarting nginx container\e[0m"
  272. docker exec -it nginx-dockerbunker nginx -t >/dev/null \
  273. && docker restart ${NGINX_CONTAINER} >/dev/null
  274. exit_response
  275. if [[ $? == 1 ]];then
  276. echo ""
  277. docker exec -it nginx-dockerbunker nginx -t
  278. echo -e "\n\`nginx -t\` failed again. Please resolve issue and try again."
  279. fi
  280. fi
  281. }
  282. start_nginx() {
  283. echo -en "\n\e[1mStarting nginx container\e[0m"
  284. docker start ${NGINX_CONTAINER} >/dev/null
  285. exit_response
  286. }
  287. stop_nginx() {
  288. echo -en "\n\e[1mStopping nginx container\e[0m"
  289. docker stop ${NGINX_CONTAINER} >/dev/null
  290. exit_response
  291. }
  292. # all functions starting/stopping/restarting containers of individual services. This is offered in every service specific menu.
  293. deactivate_nginx_conf() {
  294. if [[ ${SERVICE_NAME} == "nginx" ]] \
  295. || [[ -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]] \
  296. || elementInArray "${PROPER_NAME}" "${STOPPED_SERVICES[@]}" \
  297. || [[ ${FUNCNAME[2]} == "destroy_service" ]];then \
  298. return
  299. fi
  300. ! [[ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf ]] \
  301. && [[ -z $reconfigure ]] \
  302. && echo -e "\n\e[31mNginx configuration for ${PROPER_NAME} is not active or missing.\nPlease make sure ${PROPER_NAME} is properly configured.\e[0m\n" \
  303. && return
  304. ! [[ -d "${CONF_DIR}"/nginx/conf.inactive.d ]] \
  305. && mkdir "${CONF_DIR}"/nginx/conf.inactive.d
  306. if [[ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf ]];then
  307. echo -en "\n\e[1mDeactivating nginx configuration\e[0m"
  308. [[ -d "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]} ]] \
  309. && mv "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]} "${CONF_DIR}"/nginx/conf.inactive.d/
  310. mv "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf "${CONF_DIR}"/nginx/conf.inactive.d/
  311. exit_response
  312. fi
  313. if ! elementInArray "${PROPER_NAME}" "${STOPPED_SERVICES[@]}";then
  314. STOPPED_SERVICES+=( "${PROPER_NAME}" )
  315. sed -i '/STOPPED_SERVICES/d' "${ENV_DIR}"/dockerbunker.env
  316. declare -p STOPPED_SERVICES >> "${ENV_DIR}"/dockerbunker.env
  317. fi
  318. [[ -z $prevent_nginx_restart ]] && restart_nginx
  319. }
  320. activate_nginx_conf() {
  321. [[ ${SERVICE_NAME} == "nginx" ]] && return
  322. [[ ${FUNCNAME[1]} != "setup" ]] \
  323. && elementInArray "${PROPER_NAME}" "${STOPPED_SERVICES[@]}" \
  324. && ! [[ -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]] \
  325. && echo -e "\n\e[31mNginx configuration for ${PROPER_NAME} is not inactive or missing. Please make sure ${PROPER_NAME} is properly configured.\e[0m\n" \
  326. && return
  327. # activate nginx config
  328. [[ -d "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]} ]] \
  329. && mv "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]} "${CONF_DIR}"/nginx/conf.d/
  330. [[ -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]] \
  331. && mv "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf "${CONF_DIR}"/nginx/conf.d/
  332. }
  333. start_containers() {
  334. RUNNING=$(docker inspect --format="{{.State.Running}}" ${NGINX_CONTAINER} 2> /dev/null)
  335. [[ $RUNNING == "false" ]] || [[ -z $RUNNING ]] && bash -c "${SERVICES_DIR}"/nginx/nginx.sh setup
  336. echo -e "\n\e[1mStarting containers\e[0m"
  337. for container in "${containers[@]}";do
  338. [[ $(docker ps -q --filter "status=exited" --filter name=^/${container}$) ]] \
  339. && echo -en "- $container" \
  340. && docker start $container >/dev/null 2>&1 \
  341. && exit_response
  342. done
  343. remove_from_STOPPED_SERVICES
  344. activate_nginx_conf
  345. [[ -z $prevent_nginx_restart ]] && restart_nginx
  346. }
  347. stop_containers() {
  348. deactivate_nginx_conf
  349. echo -e "\n\e[1mStopping containers\e[0m"
  350. for container in "${containers[@]}";do
  351. [[ $(docker ps -q --filter name=^/${container}$) ]] \
  352. && echo -en "- $container" \
  353. && docker stop $container >/dev/null 2>&1 \
  354. && exit_response \
  355. || echo "- $container (not running)"
  356. done
  357. }
  358. remove_containers() {
  359. for container in ${containers[@]};do
  360. [[ $(docker ps -a -q --filter name=^/${container}$) ]] \
  361. && containers_found+=( $container )
  362. done
  363. if [[ -z ${containers_found[0]} ]];then
  364. echo -e "\n\e[1mRemoving containers\e[0m"
  365. for container in "${containers[@]}";do
  366. [[ $(docker ps -q --filter "status=exited" --filter name=^/${container}$) || $(docker ps -q --filter "status=restarting" --filter name=^/${container}$) ]] \
  367. && echo -en "- $container" \
  368. && docker rm -f $container >/dev/null 2>&1 \
  369. && exit_response \
  370. || echo "- $container (not found)"
  371. done
  372. elif [[ ${#containers_found[@]} > 0 ]];then
  373. echo -e "\n\e[1mRemoving containers\e[0m"
  374. for container in "${containers[@]}";do
  375. echo -en "- $container"
  376. docker rm -f $container >/dev/null 2>&1
  377. exit_response
  378. done
  379. else
  380. return
  381. fi
  382. }
  383. remove_volumes() {
  384. if [[ ${volumes[@]} && -z $keep_volumes ]] || [[ $destroy_all ]];then
  385. echo -e "\n\e[1mRemoving volumes\e[0m"
  386. for volume in "${!volumes[@]}";do
  387. [[ $(docker volume ls -q --filter name=^${volume}$) ]] \
  388. && echo -en "- ${volume}" \
  389. && docker volume rm ${volume} >/dev/null \
  390. && exit_response \
  391. || echo "- ${volume} (not found)"
  392. done
  393. fi
  394. }
  395. remove_networks() {
  396. if [[ ${networks[0]} ]];then
  397. echo -e "\n\e[1mRemoving networks\e[0m"
  398. for network in "${networks[@]}";do
  399. [[ $(docker network ls -q --filter name=^${network}$) ]] \
  400. && echo -en "- $network" \
  401. && docker network rm $network >/dev/null \
  402. && exit_response \
  403. || echo "- $network (not found)"
  404. done
  405. fi
  406. }
  407. restart_containers() {
  408. echo -e "\n\e[1mRestarting containers\e[0m"
  409. [[ $(docker ps -a -q --filter name=^/${NGINX_CONTAINER}$ 2> /dev/null) ]] \
  410. || bash -c "${SERVICES_DIR}"/nginx/nginx.sh setup
  411. for container in "${containers[@]}";do
  412. if [[ $(docker ps -q --filter name=^/${container}$) ]];then
  413. echo -en "- $container";docker restart $container >/dev/null 2>&1
  414. exit_response
  415. fi
  416. done
  417. [[ -z $prevent_nginx_restart ]] && restart_nginx
  418. }
  419. remove_images() {
  420. if [[ ${IMAGES[0]} ]];then
  421. prompt_confirm "Remove all images?"
  422. if [[ $? == 0 ]];then
  423. echo -e "\n\e[1mRemoving images\e[0m"
  424. for image in "${IMAGES[@]}";do
  425. if ! [[ $(docker container ls | awk '{print $2}' | grep "\<${image}\>") ]];then
  426. echo -en "- $image"
  427. docker rmi $image >/dev/null
  428. exit_response
  429. fi
  430. done
  431. fi
  432. fi
  433. }
  434. remove_service_conf() {
  435. [[ -d "${CONF_DIR}/${SERVICE_NAME}" ]] \
  436. && rm -r "${CONF_DIR}/${SERVICE_NAME}" \
  437. }
  438. remove_environment_files() {
  439. [[ -f "${ENV_DIR}"/${SERVICE_NAME}.env ]] \
  440. && echo -en "\n\e[1mRemoving ${SERVICE_NAME}.env\e[0m" \
  441. && rm "${ENV_DIR}"/${SERVICE_NAME}.env \
  442. && exit_response
  443. [[ ${SERVICE_SPECIFIC_MX} ]] \
  444. && [[ -f "${ENV_DIR}"/${SERVICE_SPECIFIC_MX}mx.env ]] \
  445. && rm "${ENV_DIR}"/${SERVICE_SPECIFIC_MX}mx.env
  446. [[ ${STATIC} \
  447. && -f "${ENV_DIR}"/static/${SERVICE_DOMAIN[0]}.env ]] \
  448. && echo -en "\n\e[1mRemoving ${SERVICE_DOMAIN[0]}.env\e[0m" \
  449. && rm "${ENV_DIR}"/static/${SERVICE_DOMAIN[0]}.env \
  450. && exit_response
  451. }
  452. remove_ssl_certificate() {
  453. if [[ ${SERVICE_DOMAIN[0]} ]] \
  454. && [[ -d "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]} ]];then
  455. echo -en "\n\e[1mRemoving SSL Certificate\e[0m"
  456. rm -r "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}
  457. exit_response
  458. fi
  459. }
  460. destroy_service() {
  461. if [[ -z ${STATIC} ]];then
  462. disconnect_from_dockerbunker_network
  463. stop_containers
  464. remove_containers
  465. remove_volumes
  466. remove_networks
  467. remove_images
  468. echo -en "\n\e[1mRemoving ${PROPER_NAME} from dockerbunker.env\e[0m"
  469. remove_from_WEB_SERVICES
  470. remove_from_CONFIGURED_SERVICES
  471. remove_from_INSTALLED_SERVICES
  472. remove_from_STOPPED_SERVICES
  473. remove_from_CONTAINERS_IN_DOCKERBUNKER_NETWORK
  474. else
  475. [[ -d ${STATIC_HOME} ]] \
  476. && prompt_confirm "Remove HTML directory [${STATIC_HOME}]" \
  477. && echo -en "\n\e[1mRemoving ${STATIC_HOME}\e[0m" \
  478. && rm -r ${STATIC_HOME} >/dev/null \
  479. && exit_response
  480. echo -en "\n\e[1mRemoving "${SERVICE_DOMAIN[0]}" from dockerbunker.env\e[0m"
  481. remove_from_STATIC_SITES
  482. fi
  483. exit_response
  484. remove_nginx_conf
  485. remove_environment_files
  486. remove_service_conf
  487. remove_ssl_certificate
  488. [[ -z $destroy_all ]] \
  489. && [[ -z ${INSTALLED_SERVICES[@]} ]] \
  490. && [[ $(docker ps -q --filter name=^/${NGINX_CONTAINER}) ]] \
  491. && [[ -z $restoring ]] \
  492. && echo -e "\nNo remaining services running.\n" \
  493. && prompt_confirm "Destroy nginx as well and completely reset dockerbunker?" \
  494. && bash "${SERVICES_DIR}"/nginx/nginx.sh destroy_service \
  495. && return
  496. [[ -z $prevent_nginx_restart ]] \
  497. && [[ ${SERVICE_NAME} != "nginx" ]] \
  498. && restart_nginx
  499. }
  500. # minimal setup routine. if more is needed add custom setup() in data/services/${SERVICE_NAME}/${SERVICE_NAME}.sh
  501. setup() {
  502. initial_setup_routine
  503. SUBSTITUTE=( "\${SERVICE_DOMAIN}" )
  504. basic_nginx
  505. docker_run_all
  506. post_setup_routine
  507. }
  508. # minimal upgrade routine. if more is needed add custom upgrade() in data/services/${SERVICE_NAME}/${SERVICE_NAME}.sh
  509. upgrade() {
  510. pull_and_compare
  511. stop_containers
  512. remove_containers
  513. docker_run_all
  514. delete_old_images
  515. }
  516. reinstall() {
  517. echo ""
  518. prompt_confirm "Keep volumes?" && export keep_volumes=1
  519. disconnect_from_dockerbunker_network
  520. stop_containers
  521. remove_containers
  522. remove_volumes
  523. remove_networks
  524. export reinstall=1
  525. setup
  526. }
  527. remove_nginx_conf() {
  528. if [[ ${SERVICE_DOMAIN[0]} ]];then
  529. if [[ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf || -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]];then
  530. echo -en "\n\e[1mRemoving nginx configuration\e[0m"
  531. [[ -d "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]} ]] \
  532. && rm -r "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]} \
  533. || true
  534. [[ -d "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]} ]] \
  535. && rm -r "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]} \
  536. || true
  537. [[ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf ]] \
  538. && rm "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf \
  539. || true
  540. [[ -f "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf ]] \
  541. && rm "${CONF_DIR}"/nginx/conf.inactive.d/${SERVICE_DOMAIN[0]}.conf \
  542. || true
  543. exit_response
  544. fi
  545. fi
  546. }
  547. disconnect_from_dockerbunker_network() {
  548. for container in ${add_to_network[@]};do
  549. [[ $container && $(docker ps -q --filter name=^/${container}$) ]] \
  550. && docker network disconnect ${NETWORK} $container >/dev/null
  551. done
  552. }
  553. reconfigure() {
  554. reconfigure=1
  555. if [[ $safe_to_keep_volumes_when_reconfiguring ]];then
  556. echo ""
  557. prompt_confirm "Keep volumes?" && keep_volumes=1
  558. else
  559. echo ""
  560. prompt_confirm "All volumes will be removed. Continue?" || exit 0
  561. fi
  562. disconnect_from_dockerbunker_network
  563. stop_containers
  564. remove_containers
  565. remove_volumes
  566. remove_networks
  567. remove_nginx_conf
  568. remove_ssl_certificate
  569. remove_environment_files
  570. remove_service_conf
  571. [[ $(grep "${PROPER_NAME}" "${ENV_DIR}"/dockerbunker.env) ]] && echo -en "\n\e[1mRemoving ${PROPER_NAME} from dockerbunker.env\e[0m"
  572. remove_from_WEB_SERVICES
  573. remove_from_CONFIGURED_SERVICES
  574. remove_from_INSTALLED_SERVICES
  575. remove_from_STOPPED_SERVICES
  576. remove_from_CONTAINERS_IN_DOCKERBUNKER_NETWORK
  577. exit_response
  578. echo ""
  579. configure
  580. }
  581. # all functions that manipulate all containers
  582. start_all() {
  583. start_nginx
  584. for PROPER_NAME in "${STOPPED_SERVICES[@]}";do
  585. SERVICE_NAME="$(echo -e "${service,,}" | tr -d '[:space:]')"
  586. source "${ENV_DIR}"/${SERVICE_NAME}.env
  587. source "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh start_containers
  588. done
  589. restart_nginx
  590. }
  591. restart_all() {
  592. for service in "${INSTALLED_SERVICES[@]}";do
  593. service="$(echo -e "${service,,}" | tr -d '[:space:]')"
  594. source "${SERVICE_ENV}"
  595. source "${SERVICES_DIR}"/${service}/${service}.sh restart_containers
  596. done
  597. restart_nginx
  598. }
  599. stop_all() {
  600. for service in "${INSTALLED_SERVICES[@]}";do
  601. service="$(echo -e "${service,,}" | tr -d '[:space:]')"
  602. if ! elementInArray "$service" "${STOPPED_SERVICES[@]}";then
  603. source "${SERVICE_ENV}"
  604. source "${SERVICES_DIR}"/${service}/${service}.sh stop_containers
  605. fi
  606. done
  607. stop_nginx
  608. export prevent_nginx_restart=1
  609. }
  610. destroy_all() {
  611. # destroy_service() is calling restart_nginx, we don't want this happening after each service is destroyed
  612. export prevent_nginx_restart=1
  613. export destroy_all=1
  614. all_services=( "${INSTALLED_SERVICES[@]}" "${CONFIGURED_SERVICES[@]}" )
  615. [[ $(docker ps -q --filter name=^/nginx-dockerbunker$) ]] && all_services+=( "nginx" )
  616. if [[ ${all_services[0]} ]];then
  617. printf "\nThe following Services will be removed: \
  618. $(for i in "${all_services[@]}";do \
  619. if [[ "$i" == ${all_services[-1]} ]];then \
  620. (printf "\"\e[33m%s\e[0m\" " "$i" )
  621. else
  622. (printf "\"\e[33m%s\e[0m\", " "$i" )
  623. fi
  624. done) \n\n"
  625. fi
  626. prompt_confirm "Continue?"
  627. [[ $? == 1 ]] && echo "Exiting..." && exit 0
  628. for service in "${all_services[@]}";do
  629. SERVICE_NAME="$(echo -e "${service,,}" | tr -d '[:space:]')"
  630. echo -e "\n\e[3m\xe2\x86\x92 Destroying $service\e[0m"
  631. [[ -f "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh ]] \
  632. && "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh destroy_service
  633. done
  634. [[ -d "${CONF_DIR}"/nginx/conf.inactive.d ]] \
  635. && [[ $(ls -A "${CONF_DIR}"/nginx/conf.inactive.d) ]] \
  636. && rm -r "${CONF_DIR}"/nginx/conf.inactive.d/*
  637. [[ -d "${CONF_DIR}"/nginx/conf.d ]] \
  638. && [[ $(ls -A "${CONF_DIR}"/nginx/conf.d) ]] \
  639. && rm -r "${CONF_DIR}"/nginx/conf.d/*
  640. for cert_dir in $(ls "${CONF_DIR}"/nginx/ssl/);do
  641. [[ $cert_dir != "letsencrypt" ]] \
  642. && rm -r "${CONF_DIR}"/nginx/ssl/$cert_dir
  643. done
  644. [[ -d "${ENV_DIR}"/static ]] \
  645. && [[ $(ls -A "${ENV_DIR}"/static) ]] \
  646. && rm "${ENV_DIR}"/static/*
  647. }
  648. add_ssl_menuentry() {
  649. if [[ $SSL_CHOICE == "le" ]] && [[ -d "${CONF_DIR}"/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} ]];then
  650. # in this case le cert has been obtained previously and everything is as expected
  651. insert $1 "Renew Let's Encrypt certificate" $2
  652. elif ! [[ -d "${CONF_DIR}"/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} ]] && [[ -L "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/cert.pem ]];then
  653. # in this case neither a self-signed nor a le cert could be found. nginx container will refuse to restart until it can find a certificate in /etc/nginx/ssl/${SERVICE_DOMAIN} - so offer to put one there either via LE or generate new self-signed
  654. insert $1 "Generate self-signed certificate" $2
  655. insert $1 "Obtain Let's Encrypt certificate" $2
  656. elif [[ -f "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}/cert.pem ]];then
  657. # in this case only a self-signed cert is found and a previous cert for the domain might be present in the le directories (if so it will be used and linked to)
  658. insert $1 "Obtain Let's Encrypt certificate" $2
  659. else
  660. # not sure when this should be the case, but if it does happen, bot options are available
  661. insert $1 "Generate self-signed certificate" $2
  662. insert $1 "Obtain Let's Encrypt certificate" $2
  663. fi
  664. }
  665. static_menu() {
  666. [[ -z ${STATIC_SITES[0]} ]] \
  667. && echo -e "\n\e[1mNo existing sites found\e[0m" \
  668. && exec "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh
  669. # Display all static sites in a menu
  670. # Option menu from directory listing, based on terdon's answer in https://askubuntu.com/a/682146
  671. ## Collect all sites in the array $staticsites
  672. staticsites=( "${BASE_DIR}"/data/env/static/* )
  673. # strip path from directory names
  674. staticsites=( "${staticsites[@]##*/}" )
  675. staticsites=( "${staticsites[@]%.*}" )
  676. ## Enable extended globbing. This lets us use @(foo|bar) to
  677. ## match either 'foo' or 'bar'.
  678. shopt -s extglob
  679. ## Start building the string to match against.
  680. string="@(${staticsites[0]}"
  681. ## Add the rest of the site names to the string
  682. for((i=1;i<${#staticsites[@]};i++))
  683. do
  684. string+="|${staticsites[$i]}"
  685. done
  686. ## Close the parenthesis. $string is now @(site1|site2|...|siteN)
  687. string+=")"
  688. echo ""
  689. ## Show the menu. This will list all Static Sites that have an active environment file
  690. select static in "${staticsites[@]}" "$returntopreviousmenu"
  691. do
  692. case $static in
  693. $string)
  694. if [[ -f "${BASE_DIR}"/data/env/static/${static}.env ]];then
  695. source "${BASE_DIR}"/data/env/static/${static}.env
  696. else
  697. echo "No environment file found for $static. Exiting."
  698. exit 1
  699. fi
  700. echo ""
  701. static_choices=( "Remove site" "$returntopreviousmenu" )
  702. add_ssl_menuentry static_choices 1
  703. select static_choice in "${static_choices[@]}"
  704. do
  705. case $static_choice in
  706. "Remove site")
  707. echo -e "\n\e[4mRemove site\e[0m"
  708. prompt_confirm "Remove $static" && prompt_confirm "Are you sure?" && destroy_service
  709. say_done
  710. sleep 0.2
  711. break
  712. ;;
  713. "Generate self-signed certificate")
  714. generate_certificate
  715. restart_nginx
  716. say_done
  717. sleep 0.2
  718. break
  719. ;;
  720. "Obtain Let's Encrypt certificate")
  721. get_le_cert
  722. say_done
  723. sleep 0.2
  724. break
  725. ;;
  726. "Renew Let's Encrypt certificate")
  727. get_le_cert renew
  728. say_done
  729. sleep 0.2
  730. break
  731. ;;
  732. "$returntopreviousmenu")
  733. static_menu
  734. ;;
  735. *)
  736. echo "Invalid option."
  737. ;;
  738. esac
  739. done
  740. break;
  741. ;;
  742. "$returntopreviousmenu")
  743. exec "${SERVICES_DIR}"/statichtmlsite/statichtmlsite.sh options_menu;;
  744. *)
  745. static=""
  746. echo "Please choose a number from 1 to $((${#staticsites[@]}+1))";;
  747. esac
  748. done
  749. }
  750. backup() {
  751. ! [[ -d ${BASE_DIR}/data/backup/${SERVICE_NAME} ]] && mkdir -p ${BASE_DIR}/data/backup/${SERVICE_NAME}
  752. NOW=$(date -d "today" +"%Y%m%d_%H%M")
  753. for volume in ${!volumes[@]};do
  754. docker run --rm -i -v ${volume}:/${volumes[$volume]##*/} -v ${BASE_DIR}/data/backup/${SERVICE_NAME}/${NOW}:/backup debian:jessie tar cvfz /backup/${volume}.tar.gz /${volumes[$volume]##*/} 2>/dev/null | cut -b1-$(tput cols) | sed -u 'i\\o033[2K' | stdbuf -o0 tr '\n' '\r';echo -e "\033[2K\c"
  755. echo -en "\n\e[1mCompressing $volume\e[0m"
  756. exit_response
  757. done
  758. if [ -d "${CONF_DIR}"/${SERVICE_NAME} ];then
  759. ! [ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/conf ] \
  760. && mkdir "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/conf
  761. echo -en "\n\e[1mBacking up configuration files\e[0m"
  762. sleep 0.2
  763. cp -r "${CONF_DIR}"/${SERVICE_NAME}/* "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/conf
  764. exit_response
  765. fi
  766. if [[ ${SERVICE_DOMAIN[0]} ]] && [ -d "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]} ];then
  767. ! [ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl ] \
  768. && mkdir "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl
  769. echo -en "\n\e[1mBacking up SSL certificate\e[0m"
  770. sleep 0.2
  771. [[ -d "${CONF_DIR}"/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} ]] \
  772. && mkdir -p \
  773. "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/live \
  774. "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/archive \
  775. "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/renewal \
  776. && cp -r "${CONF_DIR}"/nginx/ssl/letsencrypt/archive/${SERVICE_DOMAIN[0]} "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/archive \
  777. && cp -r "${CONF_DIR}"/nginx/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/live \
  778. && cp -r "${CONF_DIR}"/nginx/ssl/letsencrypt/renewal/${SERVICE_DOMAIN[0]}.conf "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl/letsencrypt/renewal
  779. cp -r "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]} "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/ssl
  780. exit_response
  781. fi
  782. if [ -f "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}.conf ];then
  783. ! [ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/nginx ] \
  784. && mkdir -p "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/nginx
  785. echo -en "\n\e[1mBacking up nginx configuration\e[0m"
  786. sleep 0.2
  787. cp -r "${CONF_DIR}"/nginx/conf.d/${SERVICE_DOMAIN[0]}* "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}/nginx
  788. exit_response
  789. fi
  790. if [ -f "${ENV_DIR}"/${SERVICE_NAME}.env ];then
  791. echo -en "\n\e[1mBacking up environemt file(s)\e[0m"
  792. sleep 0.2
  793. [[ -f "${ENV_DIR}"/${SERVICE_SPECIFIC_MX}mx.env ]] \
  794. && cp "${ENV_DIR}"/${SERVICE_SPECIFIC_MX}mx.env "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}
  795. cp "${ENV_DIR}"/${SERVICE_NAME}.env "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${NOW}
  796. exit_response
  797. else
  798. echo -e "\n\e[3mCould not find environment file(s) for ${PROPER_NAME}.\e[0m"
  799. fi
  800. }
  801. restore() {
  802. echo ${FUNCNAME[@]}
  803. restoring=1
  804. ## Collect the backups in the array $backups
  805. backups=( "${BASE_DIR}"/data/backup/${SERVICE_NAME}/* )
  806. # strip path from directory names
  807. backups=( "${backups[@]##*/}" )
  808. ## Enable extended globbing. This lets us use @(foo|bar) to
  809. ## match either 'foo' or 'bar'.
  810. shopt -s extglob
  811. ## Start building the string to match against.
  812. string="@(${backups[0]}"
  813. ## Add the rest of the backups to the string
  814. for((i=1;i<${#backups[@]};i++))
  815. do
  816. string+="|${backups[$i]}"
  817. done
  818. ## Close the parenthesis. $string is now @(backup1|backup2|...|backupN)
  819. string+=")"
  820. # only continue if backup directory is not empty
  821. if [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME} ]] \
  822. && [[ $(ls -A "${BASE_DIR}"/data/backup/${SERVICE_NAME}) ]];then
  823. echo ""
  824. echo -e "\e[4mPlease choose a backup\e[0m"
  825. ## Show the menu. This will list all backups and the string "back to previous menu"
  826. select backup in "${backups[@]}" "Back to previous menu"
  827. do
  828. case $backup in
  829. ## If the choice is one of the backups (if it matches $string)
  830. $string)
  831. ! [[ -f "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_NAME}.env ]] \
  832. && echo -e "\n\e[3mCould not find ${SERVICE_NAME}.env in ${backup}\e[0m" \
  833. && return
  834. # destroy current service if found
  835. if [[ $(docker ps -q -a --filter name=^/"${SERVICE_NAME}-service-dockerbunker"$) ]];then
  836. echo -e "\n\e[3m\xe2\x86\x92 Destroying ${PROPER_NAME}\e[0m"
  837. destroy_service
  838. fi
  839. source "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_NAME}.env
  840. ! [[ $(docker ps -q --filter name=^/nginx-dockerbunker$) ]] && setup_nginx
  841. echo -e "\n\e[3m\xe2\x86\x92 Restoring ${PROPER_NAME}\e[0m"
  842. for volume in ${!volumes[@]};do
  843. [[ $(docker volume ls --filter name=^${volume}$) ]] \
  844. && docker volume create $volume >/dev/null
  845. docker run --rm -i -v ${volume}:/${volumes[$volume]##*/} -v ${BASE_DIR}/data/backup/${SERVICE_NAME}/${backup}:/backup debian:jessie tar xvfz /backup/${volume}.tar.gz 2>/dev/null | cut -b1-$(tput cols) | sed -u 'i\\o033[2K' | stdbuf -o0 tr '\n' '\r';echo -e "\033[2K\c"
  846. echo -en "\n\e[1mDecompressing $volume\e[0m"
  847. exit_response
  848. done
  849. sleep 0.2
  850. if [ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/conf ];then
  851. ! [ -d "${CONF_DIR}"/${SERVICE_NAME} ] \
  852. && mkdir "${CONF_DIR}"/${SERVICE_NAME}
  853. echo -en "\n\e[1mRestoring configuration files\e[0m"
  854. sleep 0.2
  855. cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/conf/* "${CONF_DIR}"/${SERVICE_NAME}
  856. exit_response
  857. fi
  858. if [ -f "${BASE_DIR}"/data/backup/${SERVICE_NAME}/$backup/nginx/${SERVICE_DOMAIN}.conf ];then
  859. ! [[ -d "${CONF_DIR}"/nginx/conf.inactive.d ]] \
  860. && mkdir "${CONF_DIR}"/nginx/conf.inactive.d
  861. echo -en "\n\e[1mRestoring nginx configuration\e[0m"
  862. cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/$backup/nginx/${SERVICE_DOMAIN}* "${CONF_DIR}"/nginx/conf.inactive.d
  863. exit_response
  864. fi
  865. sleep 0.2
  866. if [[ ${SERVICE_DOMAIN[0]} ]] && [ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl ];then
  867. ! [ -d "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]} ] \
  868. && mkdir -p "${CONF_DIR}"/nginx/ssl/${SERVICE_DOMAIN[0]}
  869. echo -en "\n\e[1mRestoring SSL certificate\e[0m"
  870. sleep 0.2
  871. [[ -d "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} ]] \
  872. && mkdir -p \
  873. "${CONF_DIR}"/nginx/ssl/letsencrypt/live \
  874. "${CONF_DIR}"/nginx/ssl/letsencrypt/archive \
  875. "${CONF_DIR}"/nginx/ssl/letsencrypt/renewal \
  876. && cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl/letsencrypt/archive/${SERVICE_DOMAIN[0]} "${CONF_DIR}"/nginx/ssl/letsencrypt/archive \
  877. && cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl/letsencrypt/live/${SERVICE_DOMAIN[0]} "${CONF_DIR}"/nginx/ssl/letsencrypt/live \
  878. && cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl/letsencrypt/renewal/${SERVICE_DOMAIN[0]}.conf "${CONF_DIR}"/nginx/ssl/letsencrypt/renewal
  879. cp -r "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/ssl/${SERVICE_DOMAIN[0]} "${CONF_DIR}"/nginx/ssl
  880. exit_response
  881. fi
  882. if [ -f "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_NAME}.env ];then
  883. echo -en "\n\e[1mRestoring environemt file(s)\e[0m"
  884. sleep 0.2
  885. [[ -f "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_SPECIFIC_MX}mx.env ]] \
  886. && cp "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_SPECIFIC_MX}mx.env "${ENV_DIR}"
  887. cp "${BASE_DIR}"/data/backup/${SERVICE_NAME}/${backup}/${SERVICE_NAME}.env "${ENV_DIR}"
  888. exit_response
  889. fi
  890. create_networks
  891. docker_run_all
  892. activate_nginx_conf
  893. post_setup_routine
  894. exit 0
  895. ;;
  896. "Back to previous menu")
  897. "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh
  898. ;;
  899. *)
  900. backup=""
  901. echo "Please choose a number from 1 to $((${#backups[@]}+1))";;
  902. esac
  903. done
  904. else
  905. echo -e "\n\e[1mNo ${PROPER_NAME} backup found\e[0m"
  906. echo -e "\n\e[3m\xe2\x86\x92 Checking service status"
  907. exec "${SERVICES_DIR}"/${SERVICE_NAME}/${SERVICE_NAME}.sh
  908. fi
  909. }