menu_functions.sh 35 KB

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