menu_functions.sh 37 KB

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