functions.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. [[ -z "${VHOST_DIR:-}" ]] && \
  2. declare -r VHOST_DIR=/etc/nginx/vhost.d
  3. [[ -z "${START_HEADER:-}" ]] && \
  4. declare -r START_HEADER='## Start of configuration add by letsencrypt container'
  5. [[ -z "${END_HEADER:-}" ]] && \
  6. declare -r END_HEADER='## End of configuration add by letsencrypt container'
  7. add_location_configuration() {
  8. local domain="${1:-}"
  9. [[ -z "$domain" || ! -f "${VHOST_DIR}/${domain}" ]] && domain=default
  10. [[ -f "${VHOST_DIR}/${domain}" && \
  11. -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "${VHOST_DIR}/${domain}") ]] && return 0
  12. echo "$START_HEADER" > "${VHOST_DIR}/${domain}".new
  13. cat /app/nginx_location.conf >> "${VHOST_DIR}/${domain}".new
  14. echo "$END_HEADER" >> "${VHOST_DIR}/${domain}".new
  15. [[ -f "${VHOST_DIR}/${domain}" ]] && cat "${VHOST_DIR}/${domain}" >> "${VHOST_DIR}/${domain}".new
  16. mv -f "${VHOST_DIR}/${domain}".new "${VHOST_DIR}/${domain}"
  17. return 1
  18. }
  19. remove_all_location_configurations() {
  20. local old_shopt_options=$(shopt -p) # Backup shopt options
  21. shopt -s nullglob
  22. for file in "${VHOST_DIR}"/*; do
  23. [[ -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "$file") ]] && \
  24. sed -i "/$START_HEADER/,/$END_HEADER/d" "$file"
  25. done
  26. eval "$old_shopt_options" # Restore shopt options
  27. }
  28. ## Docker API
  29. function docker_api {
  30. local scheme
  31. local curl_opts=(-s)
  32. local method=${2:-GET}
  33. # data to POST
  34. if [[ -n "${3:-}" ]]; then
  35. curl_opts+=(-d "$3")
  36. fi
  37. if [[ -z "$DOCKER_HOST" ]];then
  38. echo "Error DOCKER_HOST variable not set" >&2
  39. return 1
  40. fi
  41. if [[ $DOCKER_HOST == unix://* ]]; then
  42. curl_opts+=(--unix-socket ${DOCKER_HOST#unix://})
  43. scheme='http://localhost'
  44. else
  45. scheme="http://${DOCKER_HOST#*://}"
  46. fi
  47. [[ $method = "POST" ]] && curl_opts+=(-H 'Content-Type: application/json')
  48. curl "${curl_opts[@]}" -X${method} ${scheme}$1
  49. }
  50. function docker_exec {
  51. local id="${1?missing id}"
  52. local cmd="${2?missing command}"
  53. local data=$(printf '{ "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Tty":false,"Cmd": %s }' "$cmd")
  54. exec_id=$(docker_api "/containers/$id/exec" "POST" "$data" | jq -r .Id)
  55. if [[ -n "$exec_id" ]]; then
  56. docker_api /exec/$exec_id/start "POST" '{"Detach": false, "Tty":false}'
  57. fi
  58. }
  59. function docker_kill {
  60. local id="${1?missing id}"
  61. local signal="${2?missing signal}"
  62. docker_api "/containers/$id/kill?signal=$signal" "POST"
  63. }
  64. ## Nginx
  65. reload_nginx() {
  66. if [[ -n "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
  67. # Using docker-gen and nginx in separate container
  68. echo "Reloading nginx docker-gen (using separate container ${NGINX_DOCKER_GEN_CONTAINER})..."
  69. docker_kill "$NGINX_DOCKER_GEN_CONTAINER" SIGHUP
  70. if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
  71. # Reloading nginx in case only certificates had been renewed
  72. echo "Reloading nginx (using separate container ${NGINX_PROXY_CONTAINER})..."
  73. docker_kill "$NGINX_PROXY_CONTAINER" SIGHUP
  74. fi
  75. else
  76. if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
  77. echo "Reloading nginx proxy..."
  78. docker_exec "$NGINX_PROXY_CONTAINER" \
  79. '[ "sh", "-c", "/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload" ]'
  80. fi
  81. fi
  82. }
  83. # Convert argument to lowercase (bash 4 only)
  84. function lc() {
  85. echo "${@,,}"
  86. }