1
0

Create location configurations automatically

This commit is contained in:
JrCs 2016-01-01 14:32:40 +01:00
parent 0779129dd5
commit 459b1ed3c9
5 changed files with 41 additions and 4 deletions

@ -57,6 +57,7 @@ if [[ "$*" == "/bin/bash /app/start.sh" ]]; then
check_docker_socket check_docker_socket
get_nginx_proxy_cid get_nginx_proxy_cid
check_writable_directory '/etc/nginx/certs' check_writable_directory '/etc/nginx/certs'
check_writable_directory '/etc/nginx/vhost.d'
check_writable_directory '/usr/share/nginx/html' check_writable_directory '/usr/share/nginx/html'
fi fi

23
app/functions.lib Normal file

@ -0,0 +1,23 @@
declare -r VHOST_DIR=/etc/nginx/vhost.d
declare -r START_HEADER='## Start of configuration add by letsencrypt container'
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() {
for file in "${VHOST_DIR}"/*; do
[[ -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "$file") ]] && \
sed -i "/$START_HEADER/,/$END_HEADER/d" "$file"
done
}

@ -5,6 +5,13 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
seconds_to_wait=3600 seconds_to_wait=3600
acme_ca_uri="${ACME_CA_URI:-https://acme-v01.api.letsencrypt.org/directory}" acme_ca_uri="${ACME_CA_URI:-https://acme-v01.api.letsencrypt.org/directory}"
source /app/functions.lib
reload_nginx() {
/usr/bin/docker exec -t $NGINX_PROXY_CID \
sh -c '/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf;nginx -s reload'
}
update_certs() { update_certs() {
[[ ! -f "$DIR"/letsencrypt_service_data ]] && return [[ ! -f "$DIR"/letsencrypt_service_data ]] && return
@ -32,6 +39,8 @@ update_certs() {
for domain in "${!hosts_array}"; do for domain in "${!hosts_array}"; do
# Add all the domains to certificate # Add all the domains to certificate
params_d_str+=" -d $domain" params_d_str+=" -d $domain"
# Add location configuration for the domain
add_location_configuration "$domain" || reload_nginx
done done
echo "Creating/renewal $base_domain certificates... (${hosts_array_expanded[*]})" echo "Creating/renewal $base_domain certificates... (${hosts_array_expanded[*]})"
/usr/bin/simp_le \ /usr/bin/simp_le \
@ -52,10 +61,7 @@ update_certs() {
reload_nginx='true' reload_nginx='true'
fi fi
done done
if [[ "$reload_nginx" == 'true' ]]; then [[ "$reload_nginx" == 'true' ]] && reload_nginx
/usr/bin/docker exec -t $NGINX_PROXY_CID \
sh -c '/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf;nginx -s reload'
fi
} }
pid= pid=

4
app/nginx_location.conf Normal file

@ -0,0 +1,4 @@
location /.well-known/ {
root /usr/share/nginx/html;
try_files $uri =404;
}

@ -5,6 +5,9 @@ term_handler() {
[[ -n "$docker_gen_pid" ]] && kill $docker_gen_pid [[ -n "$docker_gen_pid" ]] && kill $docker_gen_pid
[[ -n "$letsencrypt_service_pid" ]] && kill $letsencrypt_service_pid [[ -n "$letsencrypt_service_pid" ]] && kill $letsencrypt_service_pid
source /app/functions.lib
remove_all_location_configurations
exit 143; # 128 + 15 -- SIGTERM exit 143; # 128 + 15 -- SIGTERM
} }