functions.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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:'
  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 separate container
  68. echo "Reloading nginx proxy (using separate container ${NGINX_DOCKER_GEN_CONTAINER})..."
  69. docker_kill "$NGINX_DOCKER_GEN_CONTAINER" SIGHUP
  70. else
  71. if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
  72. echo "Reloading nginx proxy..."
  73. docker_exec "$NGINX_PROXY_CONTAINER" \
  74. '[ "sh", "-c", "/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload" ]'
  75. fi
  76. fi
  77. }
  78. # Convert argument to lowercase (bash 4 only)
  79. function lc() {
  80. echo "${@,,}"
  81. }