menu_functions.sh 35 KB

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