2016-01-07 11:11:05 +01:00
|
|
|
[[ -z "${VHOST_DIR:-}" ]] && \
|
|
|
|
declare -r VHOST_DIR=/etc/nginx/vhost.d
|
|
|
|
[[ -z "${START_HEADER:-}" ]] && \
|
|
|
|
declare -r START_HEADER='## Start of configuration add by letsencrypt container'
|
|
|
|
[[ -z "${END_HEADER:-}" ]] && \
|
|
|
|
declare -r END_HEADER='## End of configuration add by letsencrypt container'
|
2016-01-01 14:32:40 +01:00
|
|
|
|
|
|
|
add_location_configuration() {
|
2016-01-07 11:11:05 +01:00
|
|
|
local domain="${1:-}"
|
2016-01-05 14:02:15 +01:00
|
|
|
[[ -z "$domain" || ! -f "${VHOST_DIR}/${domain}" ]] && domain=default
|
|
|
|
[[ -f "${VHOST_DIR}/${domain}" && \
|
|
|
|
-n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "${VHOST_DIR}/${domain}") ]] && return 0
|
2016-01-01 14:32:40 +01:00
|
|
|
echo "$START_HEADER" > "${VHOST_DIR}/${domain}".new
|
|
|
|
cat /app/nginx_location.conf >> "${VHOST_DIR}/${domain}".new
|
|
|
|
echo "$END_HEADER" >> "${VHOST_DIR}/${domain}".new
|
|
|
|
[[ -f "${VHOST_DIR}/${domain}" ]] && cat "${VHOST_DIR}/${domain}" >> "${VHOST_DIR}/${domain}".new
|
|
|
|
mv -f "${VHOST_DIR}/${domain}".new "${VHOST_DIR}/${domain}"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_all_location_configurations() {
|
2016-01-07 11:11:05 +01:00
|
|
|
local old_shopt_options=$(shopt -p) # Backup shopt options
|
|
|
|
shopt -s nullglob
|
2016-01-01 14:32:40 +01:00
|
|
|
for file in "${VHOST_DIR}"/*; do
|
|
|
|
[[ -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "$file") ]] && \
|
|
|
|
sed -i "/$START_HEADER/,/$END_HEADER/d" "$file"
|
|
|
|
done
|
2016-01-07 11:11:05 +01:00
|
|
|
eval "$old_shopt_options" # Restore shopt options
|
2016-01-01 14:32:40 +01:00
|
|
|
}
|
2016-01-05 14:02:15 +01:00
|
|
|
|
2016-02-11 21:18:20 +01:00
|
|
|
## Docker API
|
2016-01-06 19:33:16 +01:00
|
|
|
function docker_api {
|
|
|
|
local scheme
|
|
|
|
local curl_opts=(-s)
|
|
|
|
local method=${2:-GET}
|
|
|
|
# data to POST
|
|
|
|
if [[ -n "${3:-}" ]]; then
|
|
|
|
curl_opts+=(-d "$3")
|
|
|
|
fi
|
2016-02-11 21:18:20 +01:00
|
|
|
if [[ -z "$DOCKER_HOST" ]];then
|
|
|
|
echo "Error DOCKER_HOST variable not set" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
2016-01-06 19:33:16 +01:00
|
|
|
if [[ $DOCKER_HOST == unix://* ]]; then
|
|
|
|
curl_opts+=(--unix-socket ${DOCKER_HOST#unix://})
|
|
|
|
scheme='http:'
|
|
|
|
else
|
2016-01-08 14:31:45 +01:00
|
|
|
scheme="http://${DOCKER_HOST#*://}"
|
2016-01-06 19:33:16 +01:00
|
|
|
fi
|
|
|
|
[[ $method = "POST" ]] && curl_opts+=(-H 'Content-Type: application/json')
|
|
|
|
curl "${curl_opts[@]}" -X${method} ${scheme}$1
|
|
|
|
}
|
|
|
|
|
|
|
|
function docker_exec {
|
|
|
|
local id="${1?missing id}"
|
|
|
|
local cmd="${2?missing command}"
|
|
|
|
local data=$(printf '{ "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Tty":false,"Cmd": %s }' "$cmd")
|
|
|
|
exec_id=$(docker_api "/containers/$id/exec" "POST" "$data" | jq -r .Id)
|
|
|
|
if [[ -n "$exec_id" ]]; then
|
|
|
|
docker_api /exec/$exec_id/start "POST" '{"Detach": false, "Tty":false}'
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-11 21:18:20 +01:00
|
|
|
function docker_kill {
|
|
|
|
local id="${1?missing id}"
|
|
|
|
local signal="${2?missing signal}"
|
|
|
|
docker_api "/containers/$id/kill?signal=$signal" "POST"
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:33:16 +01:00
|
|
|
## Nginx
|
2016-01-05 14:02:15 +01:00
|
|
|
reload_nginx() {
|
2016-02-11 21:18:20 +01:00
|
|
|
if [[ -n "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
|
|
|
|
# Using docker-gen separate container
|
|
|
|
echo "Reloading nginx proxy (using separate container ${NGINX_DOCKER_GEN_CONTAINER})..."
|
|
|
|
docker_kill "$NGINX_DOCKER_GEN_CONTAINER" SIGHUP
|
|
|
|
else
|
|
|
|
if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
|
|
|
|
echo "Reloading nginx proxy..."
|
|
|
|
docker_exec "$NGINX_PROXY_CONTAINER" \
|
|
|
|
'[ "sh", "-c", "/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload" ]'
|
|
|
|
fi
|
2016-01-05 14:02:15 +01:00
|
|
|
fi
|
|
|
|
}
|
2016-03-27 16:44:02 +02:00
|
|
|
|
|
|
|
# Convert argument to lowercase (bash 4 only)
|
|
|
|
function lc() {
|
|
|
|
echo "${@,,}"
|
|
|
|
}
|