From 4e0670f1ac2fd2772c6b3e7298e779c5c3fb064e Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 12 Dec 2014 17:53:30 -0800 Subject: [PATCH] rebuild nginx config on domain change --- plugins/domains/commands | 12 ++++- plugins/nginx-vhosts/commands | 85 ++++++++++++++++++++++++++++++++ plugins/nginx-vhosts/post-deploy | 80 +----------------------------- 3 files changed, 97 insertions(+), 80 deletions(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index 16f931fbf..68e220ce2 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -45,10 +45,17 @@ case "$1" in exit 1 fi + if [[ $(grep $3 "$DOKKU_ROOT/$APP/VHOST" > /dev/null 2>&1; echo $?) -eq 0 ]]; then + echo "$3 is already defined for $APP" + exit 1 + fi + dokku domains:setup $APP echo "$3" >> "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Added $3 to $APP" + + dokku nginx:build-config $APP ;; domains:clear) @@ -60,6 +67,8 @@ case "$1" in echo '' > "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Cleared domains in $APP" + + dokku nginx:build-config $APP ;; domains:remove) @@ -77,12 +86,13 @@ case "$1" in sed -i "/^$3$/d" "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Removed $3 from $APP" + + dokku nginx:build-config $APP ;; help | domains:help) cat && cat< List custom domains for app - domains:help This help message domains:add DOMAIN Add a custom domain to app domains:clear Clear all custom domains for app domains:remove DOMAIN Remove a custom domain from app diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index eb6719a23..7052de158 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -1,7 +1,91 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +restart_nginx () { + case "$DOKKU_DISTRO" in + ubuntu) + sudo /etc/init.d/nginx reload > /dev/null + ;; + + opensuse) + sudo /sbin/service nginx reload > /dev/null + ;; + esac +} + case "$1" in + nginx:build-config) + APP="$2"; PORT="$3" + [[ -z "$PORT" ]] && PORT=$(< "$DOKKU_ROOT/$APP/PORT") + VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" + WILDCARD_SSL="$DOKKU_ROOT/tls" + SSL="$DOKKU_ROOT/$APP/tls" + NONSSL_VHOSTS=`cat $VHOST_PATH` + + if [[ ! -n "$NO_VHOST" ]]; then + if [[ -e "$SSL/server.crt" ]] && [[ -e "$SSL/server.key" ]]; then + SSL_INUSE="$SSL" + SSL_DIRECTIVES=$(cat < Configuring SSL for $line..." + SSL_SERVER_NAME=$line + eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" + done <<< "$SSL_VHOSTS" + fi + + APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" + if [[ -f $APP_NGINX_TEMPLATE ]]; then + echo "-----> Overriding default nginx.conf with detected nginx.conf.template" + NGINX_CONF=$APP_NGINX_TEMPLATE + fi + + cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..." + NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` + + echo "-----> Creating $SCHEME nginx.conf" + echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf + eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" + + echo "-----> Running nginx-pre-reload" + pluginhook nginx-pre-reload $APP $PORT + + echo " Reloading nginx" + restart_nginx + else + if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then + echo "-----> NO_VHOST set, deleting $APP/VHOST" + rm "$DOKKU_ROOT/$APP/VHOST" + fi + if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then + echo "-----> NO_VHOST set, deleting nginx.conf" + rm "$DOKKU_ROOT/$APP/nginx.conf" + + echo "-----> NO_VHOST set, reloading nginx after nginx.conf deletion" + restart_nginx + fi + fi + ;; + nginx:import-ssl) [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 @@ -23,6 +107,7 @@ case "$1" in help | nginx:help) cat && cat< Imports a tarball from stdin; should contain server.crt and server.key + nginx:build-config (Re)builds nginx config for given app EOF ;; diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 9fa092920..cdf81d452 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -2,91 +2,13 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x APP="$1"; PORT="$2" -VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" -WILDCARD_SSL="$DOKKU_ROOT/tls" -SSL="$DOKKU_ROOT/$APP/tls" set +e; NO_VHOST=$(dokku config:get $APP NO_VHOST); set -e -restart_nginx () { - case "$DOKKU_DISTRO" in - ubuntu) - sudo /etc/init.d/nginx reload > /dev/null - ;; - - opensuse) - sudo /sbin/service nginx reload > /dev/null - ;; - esac -} - if [[ -n "$NO_VHOST" ]]; then echo "-----> NO_VHOST config detected" elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then dokku domains:setup $APP fi -NONSSL_VHOSTS=`cat $VHOST_PATH` - -if [[ ! -n "$NO_VHOST" ]]; then - if [[ -e "$SSL/server.crt" ]] && [[ -e "$SSL/server.key" ]]; then - SSL_INUSE="$SSL" - SSL_DIRECTIVES=$(cat < Configuring SSL for $line..." - SSL_SERVER_NAME=$line - eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" - done <<< "$SSL_VHOSTS" - fi - - APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" - if [[ -f $APP_NGINX_TEMPLATE ]]; then - echo "-----> Overriding default nginx.conf with detected nginx.conf.template" - NGINX_CONF=$APP_NGINX_TEMPLATE - fi - - cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..." - NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` - - echo "-----> Creating $SCHEME nginx.conf" - echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf - eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" - - echo "-----> Running nginx-pre-reload" - pluginhook nginx-pre-reload $APP $PORT - - echo " Reloading nginx" - restart_nginx -else - if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - echo "-----> NO_VHOST set, deleting $APP/VHOST" - rm "$DOKKU_ROOT/$APP/VHOST" - fi - if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then - echo "-----> NO_VHOST set, deleting nginx.conf" - rm "$DOKKU_ROOT/$APP/nginx.conf" - - echo "-----> NO_VHOST set, reloading nginx after nginx.conf deletion" - restart_nginx - fi -fi +dokku nginx:build-config $APP $PORT