-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.sh
119 lines (111 loc) · 3.91 KB
/
functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
[[ -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'
add_location_configuration() {
local domain="${1:-}"
[[ -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
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() {
local old_shopt_options=$(shopt -p) # Backup shopt options
shopt -s nullglob
for file in "${VHOST_DIR}"/*; do
[[ -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "$file") ]] && \
sed -i "/$START_HEADER/,/$END_HEADER/d" "$file"
done
eval "$old_shopt_options" # Restore shopt options
}
## Docker API
docker_api() {
local version='v1.25'
local method=${2:-GET}
local host
local path="${1:-/}"
local data="${3:-}"
local curl_opts=(-s)
if [ "$method" = 'POST' ]; then
curl_opts+=(-d "$data")
if [ -n "$data" ]; then
curl_opts+=(-H 'Content-Type: application/json')
fi
elif [ -n "$data" ]; then
curl_opts+=(--get)
curl_opts+=(--data-urlencode "$data")
fi
if [ -z "$DOCKER_HOST" ]; then
echo "Error DOCKER_HOST variable not set" >&2
return 1
fi
if [ -n "${DOCKER_HOST#unix://}" ]; then
curl_opts+=(--unix-socket "${DOCKER_HOST#unix://}")
host='http://localhost'
else
host="http://${DOCKER_HOST#*://}"
fi
curl "${curl_opts[@]}" "${host}/${version}$path"
}
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
}
docker_kill() {
local id="${1?missing id}"
local signal="${2?missing signal}"
docker_api "/containers/$id/kill?signal=$signal" "POST"
}
## Nginx
reload_nginx_container() {
local id="${1?missing id}"
local method=${2:-kill}
echo "Reloading nginx proxy (${id})..."
if [ "$method" != 'exec' ]; then
docker_kill "${id}" SIGHUP
else
docker_exec "${id}" \
'[ "sh", "-c", "/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload" ]'
fi
}
reload_nginx() {
local filters
local container_ids
local container_id
if [ "$MODE" = 'swarm' ]; then
# Using separate nginx and docker-gen containers
filters='{"label": ["com.github.jrcs.docker_letsencrypt_nginx_proxy_companion.docker_gen"]}'
container_ids=$(docker_api "/containers/json" "GET" "filters=$filters" | jq -r '[.[] | .Id] | join(" ")')
for container_id in ${container_ids}; do
reload_nginx_container "${container_id}"
done
# Using combined nginx-proxy container
filters='{"label": ["com.github.jrcs.docker_letsencrypt_nginx_proxy_companion.nginx_proxy"]}'
container_ids=$(docker_api "/containers/json" "GET" "filters=$filters" | jq -r '[.[] | .Id] | join(" ")')
for container_id in ${container_ids}; do
reload_nginx_container "${container_id}" "exec"
done
elif [[ -n "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
# Using separate nginx and docker-gen containers
reload_nginx_container "${NGINX_DOCKER_GEN_CONTAINER}"
elif [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
# Using combined nginx-proxy container
reload_nginx_container "${NGINX_PROXY_CONTAINER}" "exec"
fi
}
# Convert argument to lowercase (bash 4 only)
lc() {
echo "${@,,}"
}