menu_functions.sh 34 KB

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