From 8aa196c6600f923cf69a3d1575379a99ac32224c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 00:26:36 -0500 Subject: [PATCH 01/27] Remove extra reload command --- plugins/nginx-vhosts/post-deploy | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index fe01ab77b..20302c06c 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -130,6 +130,5 @@ else sudo /sbin/service nginx reload > /dev/null ;; esac - sudo /etc/init.d/nginx reload > /dev/null fi fi From eeff3d52eb635655d15fc604531446d051a90f28 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 00:29:02 -0500 Subject: [PATCH 02/27] Combine calls to restart nginx into a single command --- plugins/nginx-vhosts/post-deploy | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 20302c06c..4ba2f8ba1 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -9,6 +9,18 @@ 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" fi @@ -103,15 +115,7 @@ EOF pluginhook nginx-pre-reload $APP $PORT echo " Reloading nginx" - case "$DOKKU_DISTRO" in - ubuntu) - sudo /etc/init.d/nginx reload > /dev/null - ;; - - opensuse) - sudo /sbin/service nginx reload > /dev/null - ;; - esac + restart_nginx else if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then echo "------> NO_VHOST set, deleting $APP/URL" @@ -121,14 +125,6 @@ else echo "------> NO_VHOST set, deleting $APP/nginx.conf" rm "$DOKKU_ROOT/$APP/nginx.conf" echo "------> NO_VHOST set, reloading nginx after nginx.conf deletion" - case "$DOKKU_DISTRO" in - ubuntu) - sudo /etc/init.d/nginx reload > /dev/null - ;; - - opensuse) - sudo /sbin/service nginx reload > /dev/null - ;; - esac + restart_nginx fi fi From 68a1d1af38fad5918c9f0a9f3e605e0867fd85b7 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 00:46:58 -0500 Subject: [PATCH 03/27] one-liner --- plugins/nginx-vhosts/post-deploy | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 4ba2f8ba1..e16fc1e80 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -5,9 +5,7 @@ APP="$1"; PORT="$2" WILDCARD_SSL="$DOKKU_ROOT/tls" SSL="$DOKKU_ROOT/$APP/tls" -set +e -NO_VHOST=$(dokku config:get $APP NO_VHOST) -set -e +set +e; NO_VHOST=$(dokku config:get $APP NO_VHOST); set -e restart_nginx () { case "$DOKKU_DISTRO" in From 8f0e794893f9f3363ab6ccc1ad0a80cf871073f2 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 00:52:58 -0500 Subject: [PATCH 04/27] Remove extra dash --- plugins/nginx-vhosts/post-deploy | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index e16fc1e80..18c8c20a6 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -116,13 +116,14 @@ EOF restart_nginx else if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then - echo "------> NO_VHOST set, deleting $APP/URL" + echo "-----> NO_VHOST set, deleting $APP/URL" rm "$DOKKU_ROOT/$APP/URL" fi if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then - echo "------> NO_VHOST set, deleting $APP/nginx.conf" + echo "-----> NO_VHOST set, deleting nginx.conf" rm "$DOKKU_ROOT/$APP/nginx.conf" - echo "------> NO_VHOST set, reloading nginx after nginx.conf deletion" + + echo "-----> NO_VHOST set, reloading nginx after nginx.conf deletion" restart_nginx fi fi From a6c34ddd6f470858cc1913a2bcaea9a0ad2a7159 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 00:56:26 -0500 Subject: [PATCH 05/27] Use a template to create an application's nginx.conf --- plugins/nginx-vhosts/post-deploy | 68 +++---------------- plugins/nginx-vhosts/templates/nginx.conf | 17 +++++ plugins/nginx-vhosts/templates/nginx.ssl.conf | 28 ++++++++ 3 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 plugins/nginx-vhosts/templates/nginx.conf create mode 100644 plugins/nginx-vhosts/templates/nginx.ssl.conf diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 18c8c20a6..7365bc915 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -47,68 +47,18 @@ EOF SSL_DIRECTIVES="" fi + NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.conf" + SCHEME="http" if [[ -n "$SSL_INUSE" ]]; then - echo "-----> Creating ssl nginx.conf" - cat< $DOKKU_ROOT/$APP/nginx.conf -upstream $APP { server 127.0.0.1:$PORT; } -server { - listen [::]:80; - listen 80; - server_name $hostname; - return 301 https://\$host\$request_uri; -} - -server { - listen [::]:443 ssl spdy; - listen 443 ssl spdy; - server_name $hostname; -$SSL_DIRECTIVES - - keepalive_timeout 70; - add_header Alternate-Protocol 443:npn-spdy/2; - - location / { - proxy_pass http://$APP; - proxy_http_version 1.1; - proxy_set_header Upgrade \$http_upgrade; - proxy_set_header Connection "upgrade"; - proxy_set_header Host \$http_host; - proxy_set_header X-Forwarded-Proto \$scheme; - proxy_set_header X-Forwarded-For \$remote_addr; - proxy_set_header X-Forwarded-Port \$server_port; - proxy_set_header X-Request-Start \$msec; - } - include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf; -} -EOF - - echo "https://$hostname" > "$DOKKU_ROOT/$APP/URL" - else - echo "-----> Creating non-ssl nginx.conf" - cat< $DOKKU_ROOT/$APP/nginx.conf -upstream $APP { server 127.0.0.1:$PORT; } -server { - listen [::]:80; - listen 80; - server_name $hostname; - location / { - proxy_pass http://$APP; - proxy_http_version 1.1; - proxy_set_header Upgrade \$http_upgrade; - proxy_set_header Connection "upgrade"; - proxy_set_header Host \$http_host; - proxy_set_header X-Forwarded-Proto \$scheme; - proxy_set_header X-Forwarded-For \$remote_addr; - proxy_set_header X-Forwarded-Port \$server_port; - proxy_set_header X-Request-Start \$msec; - } - include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf; -} -EOF - - echo "http://$hostname" > "$DOKKU_ROOT/$APP/URL" + NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.ssl.conf" + SCHEME="https" fi + 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 "$SCHEME://$hostname" > "$DOKKU_ROOT/$APP/URL" + echo "-----> Running nginx-pre-reload" pluginhook nginx-pre-reload $APP $PORT diff --git a/plugins/nginx-vhosts/templates/nginx.conf b/plugins/nginx-vhosts/templates/nginx.conf new file mode 100644 index 000000000..9b73e6964 --- /dev/null +++ b/plugins/nginx-vhosts/templates/nginx.conf @@ -0,0 +1,17 @@ +server { + listen [::]:80; + listen 80; + server_name $hostname; + location / { + proxy_pass http://$APP; + proxy_http_version 1.1; + proxy_set_header Upgrade \$http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host \$http_host; + proxy_set_header X-Forwarded-Proto \$scheme; + proxy_set_header X-Forwarded-For \$remote_addr; + proxy_set_header X-Forwarded-Port \$server_port; + proxy_set_header X-Request-Start \$msec; + } + include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf; +} diff --git a/plugins/nginx-vhosts/templates/nginx.ssl.conf b/plugins/nginx-vhosts/templates/nginx.ssl.conf new file mode 100644 index 000000000..a77e39c89 --- /dev/null +++ b/plugins/nginx-vhosts/templates/nginx.ssl.conf @@ -0,0 +1,28 @@ +server { + listen [::]:80; + listen 80; + server_name $hostname; + return 301 https://\$host\$request_uri; +} + +server { + listen [::]:443 ssl spdy; + listen 443 ssl spdy; + server_name $hostname; +$SSL_DIRECTIVES + + keepalive_timeout 70; + add_header Alternate-Protocol 443:npn-spdy/2; + location / { + proxy_pass http://$APP; + proxy_http_version 1.1; + proxy_set_header Upgrade \$http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host \$http_host; + proxy_set_header X-Forwarded-Proto \$scheme; + proxy_set_header X-Forwarded-For \$remote_addr; + proxy_set_header X-Forwarded-Port \$server_port; + proxy_set_header X-Request-Start \$msec; + } + include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf; +} From b967514c5514c03bdc08f43d853d1f009df312ec Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 01:41:05 -0500 Subject: [PATCH 06/27] Allow overriding of the nginx.conf template via nginx.conf.template --- plugins/nginx-vhosts/post-deploy | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 7365bc915..1bc802a92 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -54,6 +54,12 @@ EOF SCHEME="https" 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 + 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" From f23809e75f305715fdb580be90340cfc6cfc85de Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 01:46:42 -0500 Subject: [PATCH 07/27] Implement the ability to set multiple server names for a given application Adding a hostname to the $APP/VHOST file will enable it as a virtualhost for the application. In addition, any hostname set that matches an associated ssl certificate will also be set as an ssl host. Note that if a hostname does not have a matching SSL host, then it will result in an erroring application. For the moment, running `dokku url` on an app may not correctly display the current hostnames for said app. --- plugins/nginx-vhosts/post-deploy | 53 ++++++++++++++----- plugins/nginx-vhosts/templates/nginx.conf | 2 +- plugins/nginx-vhosts/templates/nginx.ssl.conf | 4 +- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 1bc802a92..f2905a905 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -2,6 +2,7 @@ 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" @@ -21,20 +22,31 @@ restart_nginx () { if [[ -n "$NO_VHOST" ]]; then echo "-----> NO_VHOST config detected" +elif [[ -f "$DOKKU_ROOT/VHOST" ]]; then + [ -f $VHOST_PATH ] || { + echo "-----> Creating new $VHOST_PATH..." + if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then + cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH + else + VHOST=$(< "$DOKKU_ROOT/VHOST") + SUBDOMAIN=${APP/%\.${VHOST}/} + hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) + if [[ ! -n $hostname ]]; then + if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then + hostname="${APP/\//-}" + else + hostname="${APP/\//-}.$VHOST" + fi + fi + + echo $hostname > $VHOST_PATH + fi + } fi -if [[ -f "$DOKKU_ROOT/VHOST" && ! -n "$NO_VHOST" ]]; then - VHOST=$(< "$DOKKU_ROOT/VHOST") - SUBDOMAIN=${APP/%\.${VHOST}/} - hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) - if [[ ! -n $hostname ]]; then - if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then - hostname="${APP/\//-}" - else - hostname="${APP/\//-}.$VHOST" - fi - 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" @@ -60,10 +83,14 @@ EOF 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 "$SCHEME://$hostname" > "$DOKKU_ROOT/$APP/URL" + echo $NOSSL_SERVER_NAME > "$DOKKU_ROOT/$APP/URL" echo "-----> Running nginx-pre-reload" pluginhook nginx-pre-reload $APP $PORT diff --git a/plugins/nginx-vhosts/templates/nginx.conf b/plugins/nginx-vhosts/templates/nginx.conf index 9b73e6964..8ed426275 100644 --- a/plugins/nginx-vhosts/templates/nginx.conf +++ b/plugins/nginx-vhosts/templates/nginx.conf @@ -1,7 +1,7 @@ server { listen [::]:80; listen 80; - server_name $hostname; + server_name $NOSSL_SERVER_NAME; location / { proxy_pass http://$APP; proxy_http_version 1.1; diff --git a/plugins/nginx-vhosts/templates/nginx.ssl.conf b/plugins/nginx-vhosts/templates/nginx.ssl.conf index a77e39c89..e30a6a82d 100644 --- a/plugins/nginx-vhosts/templates/nginx.ssl.conf +++ b/plugins/nginx-vhosts/templates/nginx.ssl.conf @@ -1,14 +1,14 @@ server { listen [::]:80; listen 80; - server_name $hostname; + server_name $NOSSL_SERVER_NAME; return 301 https://\$host\$request_uri; } server { listen [::]:443 ssl spdy; listen 443 ssl spdy; - server_name $hostname; + server_name $SSL_SERVER_NAME; $SSL_DIRECTIVES keepalive_timeout 70; From 66171551d89d3144e1009f2850550296820cd17a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 02:02:46 -0500 Subject: [PATCH 08/27] Add domains plugin as written in https://github.com/jtangelder/dokku-nginx-alt This plugin conforms to the heroku api for setting and removing domains. Note that it does not yet update the URL file for a given application. --- plugins/domains/commands | 82 ++++++++++++++++++++++++++++++++++++++++ plugins/domains/install | 7 ++++ 2 files changed, 89 insertions(+) create mode 100755 plugins/domains/commands create mode 100755 plugins/domains/install diff --git a/plugins/domains/commands b/plugins/domains/commands new file mode 100755 index 000000000..ee28de85e --- /dev/null +++ b/plugins/domains/commands @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +vhost_create () { + VHOST_PATH="$DOKKU_ROOT/$2/VHOST" + [ -f $VHOST_PATH ] || { + echo "-----> Creating new $VHOST_PATH..." + touch $VHOST_PATH + } +} + +case "$1" in + domains) + [[ -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 + APP="$2" + + vhost_create + echo "=== $APP Domain Names" + cat "$DOKKU_ROOT/$APP/VHOST" + ;; + + domains:add) + [[ -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 + APP="$2" + + if [[ -z $3 ]]; then + echo "Usage: dokku $1 $APP DOMAIN" + echo "Must specify DOMAIN." + exit 1 + fi + + vhost_create + echo "$3" >> "$DOKKU_ROOT/$APP/VHOST" + pluginhook post-domains-update $APP + echo "-----> Added $3 to $APP" + ;; + + domains:clear) + [[ -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 + APP="$2" + + vhost_create + echo '' > "$DOKKU_ROOT/$APP/VHOST" + pluginhook post-domains-update $APP + echo "-----> Cleared domains in $APP" + ;; + + domains:remove) + [[ -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 + APP="$2" + + if [[ -z $3 ]]; then + echo "Usage: dokku $1 $2 DOMAIN" + echo "Must specify DOMAIN." + exit 1 + fi + + vhost_create + sed -i "/^$3$/d" "$DOKKU_ROOT/$APP/VHOST" + pluginhook post-domains-update $APP + echo "-----> Removed $3 from $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 +EOF + ;; + + *) + exit $DOKKU_NOT_IMPLEMENTED_EXIT + ;; + +esac diff --git a/plugins/domains/install b/plugins/domains/install new file mode 100755 index 000000000..c93af0f66 --- /dev/null +++ b/plugins/domains/install @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +for app in $(ls -d $DOKKU_ROOT/*/CONTAINER); do + APP=$(basename $(dirname $app)); + touch "$DOKKU_ROOT/$APP/VHOST" +done From e84526490ec2820adbf541a9137e7393894fab20 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 24 Nov 2014 02:13:41 -0500 Subject: [PATCH 09/27] Move VHOST file creation to a single domains:setup command This helps remove duplication and also ensures that all hosts have a proper setup after running `make copyfiles` Note that this command is not exposed in help as it is not generally useful to developers --- plugins/domains/commands | 42 +++++++++++++++++++++++--------- plugins/domains/install | 2 +- plugins/nginx-vhosts/post-deploy | 20 +-------------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index ee28de85e..a06b85b67 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -1,25 +1,43 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -vhost_create () { - VHOST_PATH="$DOKKU_ROOT/$2/VHOST" - [ -f $VHOST_PATH ] || { - echo "-----> Creating new $VHOST_PATH..." - touch $VHOST_PATH - } -} - case "$1" in domains) [[ -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 APP="$2" - vhost_create + dokku domains:setup $APP echo "=== $APP Domain Names" cat "$DOKKU_ROOT/$APP/VHOST" ;; + domains:setup) + [[ -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 + APP="$2" + + [ -f $VHOST_PATH ] || { + echo "-----> Creating new $VHOST_PATH..." + if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then + cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH + else + VHOST=$(< "$DOKKU_ROOT/VHOST") + SUBDOMAIN=${APP/%\.${VHOST}/} + hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) + if [[ ! -n $hostname ]]; then + if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then + hostname="${APP/\//-}" + else + hostname="${APP/\//-}.$VHOST" + fi + fi + + echo $hostname > $VHOST_PATH + fi + } + ;; + domains:add) [[ -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 @@ -31,7 +49,7 @@ case "$1" in exit 1 fi - vhost_create + dokku domains:setup $APP echo "$3" >> "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Added $3 to $APP" @@ -42,7 +60,7 @@ case "$1" in [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" - vhost_create + dokku domains:setup $APP echo '' > "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Cleared domains in $APP" @@ -59,7 +77,7 @@ case "$1" in exit 1 fi - vhost_create + dokku domains:setup $APP sed -i "/^$3$/d" "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Removed $3 from $APP" diff --git a/plugins/domains/install b/plugins/domains/install index c93af0f66..18b7ae028 100755 --- a/plugins/domains/install +++ b/plugins/domains/install @@ -3,5 +3,5 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x for app in $(ls -d $DOKKU_ROOT/*/CONTAINER); do APP=$(basename $(dirname $app)); - touch "$DOKKU_ROOT/$APP/VHOST" + dokku domains:setup $APP done diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index f2905a905..7853d6931 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -23,25 +23,7 @@ restart_nginx () { if [[ -n "$NO_VHOST" ]]; then echo "-----> NO_VHOST config detected" elif [[ -f "$DOKKU_ROOT/VHOST" ]]; then - [ -f $VHOST_PATH ] || { - echo "-----> Creating new $VHOST_PATH..." - if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then - cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH - else - VHOST=$(< "$DOKKU_ROOT/VHOST") - SUBDOMAIN=${APP/%\.${VHOST}/} - hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) - if [[ ! -n $hostname ]]; then - if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then - hostname="${APP/\//-}" - else - hostname="${APP/\//-}.$VHOST" - fi - fi - - echo $hostname > $VHOST_PATH - fi - } + dokku domains:setup $APP fi NONSSL_VHOSTS=`cat $VHOST_PATH` From 0b4b2b29a872055b8b93834fe3c36e4f49030a5a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 01:36:06 -0500 Subject: [PATCH 10/27] Add missing env var --- plugins/domains/commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index a06b85b67..74ed91705 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -15,7 +15,7 @@ case "$1" in domains:setup) [[ -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 - APP="$2" + APP="$2"; VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" [ -f $VHOST_PATH ] || { echo "-----> Creating new $VHOST_PATH..." From 976dc3009c809d5f0b0e431d4df88ca1365cb5d7 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 01:36:13 -0500 Subject: [PATCH 11/27] Use a normal conditional --- plugins/domains/commands | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index 74ed91705..828c167c6 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -17,7 +17,7 @@ case "$1" in [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" - [ -f $VHOST_PATH ] || { + if [[ ! -f $VHOST_PATH ]]; then echo "-----> Creating new $VHOST_PATH..." if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH @@ -35,7 +35,7 @@ case "$1" in echo $hostname > $VHOST_PATH fi - } + fi ;; domains:add) From 4955f9c6007f77d672b5d0a55b95a27d8bb3e006 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 01:36:36 -0500 Subject: [PATCH 12/27] Fix test for file --- plugins/nginx-vhosts/post-deploy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 7853d6931..cb4c7d810 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -22,7 +22,7 @@ restart_nginx () { if [[ -n "$NO_VHOST" ]]; then echo "-----> NO_VHOST config detected" -elif [[ -f "$DOKKU_ROOT/VHOST" ]]; then +elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then dokku domains:setup $APP fi From 9111902542cf9fc0fc3d93f9d36e1ea9001118f9 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:09:54 -0500 Subject: [PATCH 13/27] Fix issue where VHOST was not properly being set The URL file is being rewritten on every deploy by `dokku deploy` and thus that file was incorrect. Instead, we just reuse the old logic from the nginx-vhosts plugin to handle creation of the VHOST file --- plugins/domains/commands | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index 828c167c6..16f931fbf 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -19,22 +19,18 @@ case "$1" in if [[ ! -f $VHOST_PATH ]]; then echo "-----> Creating new $VHOST_PATH..." - if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then - cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH - else - VHOST=$(< "$DOKKU_ROOT/VHOST") - SUBDOMAIN=${APP/%\.${VHOST}/} - hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) - if [[ ! -n $hostname ]]; then - if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then - hostname="${APP/\//-}" - else - hostname="${APP/\//-}.$VHOST" - fi + VHOST=$(< "$DOKKU_ROOT/VHOST") + SUBDOMAIN=${APP/%\.${VHOST}/} + hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) + if [[ ! -n $hostname ]]; then + if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then + hostname="${APP/\//-}" + else + hostname="${APP/\//-}.$VHOST" fi - - echo $hostname > $VHOST_PATH fi + + echo $hostname > $VHOST_PATH fi ;; From 8109c9c5696e6891e11fd6ff9a12cd188e7f4680 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:10:49 -0500 Subject: [PATCH 14/27] Delete the VHOST file, not the URL file, when NO_VHOST is set The URL file is useful for understanding what port docker decided to host our app internally on. --- plugins/nginx-vhosts/post-deploy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index cb4c7d810..9312056ba 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -80,9 +80,9 @@ EOF echo " Reloading nginx" restart_nginx else - if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then - echo "-----> NO_VHOST set, deleting $APP/URL" - rm "$DOKKU_ROOT/$APP/URL" + 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" From 605ba27eeba7549ad21924d8262f75ded3354694 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:12:46 -0500 Subject: [PATCH 15/27] Use a one-liner for the "Configuring" echo statement --- plugins/nginx-vhosts/post-deploy | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 9312056ba..40a4d8bb3 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -65,8 +65,7 @@ EOF NGINX_CONF=$APP_NGINX_TEMPLATE fi - cat $VHOST_PATH | xargs -i \ - echo "-----> Configuring {}..." + cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..." NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` echo "-----> Creating $SCHEME nginx.conf" From 11b126a84572ea2522494e148109f8a7ebc27399 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:14:52 -0500 Subject: [PATCH 16/27] Do not overwrite URL file --- plugins/nginx-vhosts/post-deploy | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 40a4d8bb3..9fa092920 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -71,7 +71,6 @@ EOF 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 $NOSSL_SERVER_NAME > "$DOKKU_ROOT/$APP/URL" echo "-----> Running nginx-pre-reload" pluginhook nginx-pre-reload $APP $PORT From 0865ff3f793bacb02a377cb996fb1a390226be91 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:16:52 -0500 Subject: [PATCH 17/27] Use updated VHOST file instead of URL file when outputting the current URL to an app Note that this may include more than one URL to an app --- plugins/00_dokku-standard/commands | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index 7fd4f9476..3cfc15fb1 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -39,8 +39,8 @@ case "$1" in [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; - if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then - echo $(< "$DOKKU_ROOT/$APP/URL") + if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then + echo $(< "$DOKKU_ROOT/$APP/VHOST") fi ;; From 01e7f1cdb6df4cf3e16df98fee2377d4de8e4d6c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:34:08 -0500 Subject: [PATCH 18/27] Output *all* hosts on multiple lines --- plugins/00_dokku-standard/commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index 3cfc15fb1..ac55d4c8a 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -40,7 +40,7 @@ case "$1" in APP="$2"; if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - echo $(< "$DOKKU_ROOT/$APP/VHOST") + cat "$DOKKU_ROOT/$APP/VHOST" fi ;; From b4c864bea7e44e4102a0be8e920081be26f913f3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:34:24 -0500 Subject: [PATCH 19/27] Use sed to indent output of `dokku url` command --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index 0246d8965..0dbeba176 100755 --- a/dokku +++ b/dokku @@ -36,7 +36,7 @@ case "$1" in echo "-----> Deploying $APP ..." dokku deploy $APP echo "=====> Application deployed:" - echo " $(dokku url $APP)" + echo "$(dokku url $APP | sed "s/^/ /")" echo ;; From 1d45ca0cdd313aba4515deab0a81bbc7d1cb3988 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:38:08 -0500 Subject: [PATCH 20/27] Add backup export/import hooks for associated VHOST files --- plugins/domains/backup-export | 8 ++++++++ plugins/domains/backup-import | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100755 plugins/domains/backup-export create mode 100755 plugins/domains/backup-import diff --git a/plugins/domains/backup-export b/plugins/domains/backup-export new file mode 100755 index 000000000..2306b1055 --- /dev/null +++ b/plugins/domains/backup-export @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +shopt -s nullglob +VERSION="$1" +BASE_DIR="$2" + +cat; for i in $BASE_DIR/*/VHOST; do echo $i; done diff --git a/plugins/domains/backup-import b/plugins/domains/backup-import new file mode 100755 index 000000000..05ace80ff --- /dev/null +++ b/plugins/domains/backup-import @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +shopt -s nullglob +VERSION="$1" +IMPORT_DIR="$2" +TARGET_DIR="$3" + +cd $IMPORT_DIR +for file in */VHOST; do + cp $file "$TARGET_DIR/$file" +done From ae61ef3f1e7027bf9156213260f84f24d928ee1c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Dec 2014 02:40:13 -0500 Subject: [PATCH 21/27] Add backup support to nginx.conf.template files --- plugins/domains/backup-export | 1 + plugins/domains/backup-import | 3 +++ 2 files changed, 4 insertions(+) diff --git a/plugins/domains/backup-export b/plugins/domains/backup-export index 2306b1055..e131dfbf9 100755 --- a/plugins/domains/backup-export +++ b/plugins/domains/backup-export @@ -6,3 +6,4 @@ VERSION="$1" BASE_DIR="$2" cat; for i in $BASE_DIR/*/VHOST; do echo $i; done +cat; for i in $BASE_DIR/*/nginx.conf.template; do echo $i; done diff --git a/plugins/domains/backup-import b/plugins/domains/backup-import index 05ace80ff..f16caaf8a 100755 --- a/plugins/domains/backup-import +++ b/plugins/domains/backup-import @@ -10,3 +10,6 @@ cd $IMPORT_DIR for file in */VHOST; do cp $file "$TARGET_DIR/$file" done +for file in */nginx.conf.template; do + cp $file "$TARGET_DIR/$file" +done From 4e0670f1ac2fd2772c6b3e7298e779c5c3fb064e Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 12 Dec 2014 17:53:30 -0800 Subject: [PATCH 22/27] 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 From 78d3921a6f62b50481344a4c5e38de2f55e5d60b Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 15 Dec 2014 13:15:40 -0800 Subject: [PATCH 23/27] don't break other nginx plugins like nginx-alt --- plugins/domains/commands | 3 --- plugins/nginx-vhosts/post-domains-update | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100755 plugins/nginx-vhosts/post-domains-update diff --git a/plugins/domains/commands b/plugins/domains/commands index 68e220ce2..80d1c1603 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -55,7 +55,6 @@ case "$1" in pluginhook post-domains-update $APP echo "-----> Added $3 to $APP" - dokku nginx:build-config $APP ;; domains:clear) @@ -68,7 +67,6 @@ case "$1" in pluginhook post-domains-update $APP echo "-----> Cleared domains in $APP" - dokku nginx:build-config $APP ;; domains:remove) @@ -87,7 +85,6 @@ case "$1" in pluginhook post-domains-update $APP echo "-----> Removed $3 from $APP" - dokku nginx:build-config $APP ;; help | domains:help) diff --git a/plugins/nginx-vhosts/post-domains-update b/plugins/nginx-vhosts/post-domains-update new file mode 100755 index 000000000..e2bb35ebd --- /dev/null +++ b/plugins/nginx-vhosts/post-domains-update @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +APP="$1" + +dokku nginx:build-config $APP From 50115d3640a89e107d42a5fe035a9c1fb659c0f8 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 15 Dec 2014 18:02:57 -0800 Subject: [PATCH 24/27] print out urls with scheme --- dokku | 2 +- plugins/00_dokku-standard/commands | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/dokku b/dokku index 0dbeba176..6c16f0b3b 100755 --- a/dokku +++ b/dokku @@ -36,7 +36,7 @@ case "$1" in echo "-----> Deploying $APP ..." dokku deploy $APP echo "=====> Application deployed:" - echo "$(dokku url $APP | sed "s/^/ /")" + echo "$(dokku urls $APP | sed "s/^/ /")" echo ;; diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index ac55d4c8a..403b40c15 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -34,13 +34,26 @@ case "$1" in docker run -i -t $DOCKER_ARGS $IMAGE /exec "$@" ;; - url) + url | urls) [[ -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 - APP="$2"; + APP="$2"; SCHEME="http"; SSL="$DOKKU_ROOT/$APP/tls" + + if [[ -e "$SSL/server.crt" ]] && [[ -e "$SSL/server.key" ]]; then + SCHEME="https" + fi if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - cat "$DOKKU_ROOT/$APP/VHOST" + case "$1" in + url) + echo "$SCHEME://$(head -n1 "$DOKKU_ROOT/$APP/VHOST")" + ;; + urls) + for vhost in $(< "$DOKKU_ROOT/$APP/VHOST"); do + echo "$SCHEME://$vhost" + done + ;; + esac fi ;; @@ -55,7 +68,8 @@ case "$1" in cat && cat< [-t] Show the last logs for an application (-t follows) run Run a command in the environment of an application - url Show the URL for an application + url Show the first URL for an application (compatibility) + urls Show all URLs for an application version Print dokku's version EOF ;; From a6cdae5abf2f2eda09d0bb955237ec1b6c00d350 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 15 Dec 2014 18:04:00 -0800 Subject: [PATCH 25/27] reset VHOST to default upon domains:clear --- plugins/domains/commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index 80d1c1603..14c7b01a8 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -62,8 +62,8 @@ case "$1" in [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" + rm -f "$DOKKU_ROOT/$APP/VHOST" dokku domains:setup $APP - echo '' > "$DOKKU_ROOT/$APP/VHOST" pluginhook post-domains-update $APP echo "-----> Cleared domains in $APP" From 1b208778f335a513aa6e33ce40e49983f54b3987 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 15 Dec 2014 18:10:14 -0800 Subject: [PATCH 26/27] bugfixes - don't match substrings in VHOSTS - handle case where NOSSL_SERVER_NAME is \' \' - rebuild nginx config on ssl import - prevent cwd errors after we rm the temp dir --- plugins/nginx-vhosts/commands | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 7052de158..64f88f947 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -43,8 +43,8 @@ EOF SSL_HOSTNAME=`openssl x509 -in $SSL_INUSE/server.crt -noout -subject | tr '/' '\n' | grep CN= | cut -c4-` SSL_HOSTNAME=`echo "$SSL_HOSTNAME" | sed 's|\.|\\.|g' | sed 's/\*/\.\*/g'` - SSL_VHOSTS=`grep "$SSL_HOSTNAME" $VHOST_PATH` - NONSSL_VHOSTS=`grep -v "$SSL_HOSTNAME" $VHOST_PATH` + SSL_VHOSTS=`egrep ^"$SSL_HOSTNAME"$ $VHOST_PATH` + NONSSL_VHOSTS=`egrep -v ^"$SSL_HOSTNAME"$ $VHOST_PATH || exit 0` while read line; do echo "-----> Configuring SSL for $line..." @@ -61,6 +61,8 @@ EOF cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..." NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` + # if NOSSL_SERVER_NAME is just ' ' then set NOSSL_SERVER_NAME to SSL_HOSTNAME since we redirect anyway + [[ "$NOSSL_SERVER_NAME" = ' ' ]] && NOSSL_SERVER_NAME=$(echo $SSL_VHOSTS | tr '\n' ' ') echo "-----> Creating $SCHEME nginx.conf" echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf @@ -101,7 +103,9 @@ EOF mkdir -p "$DOKKU_ROOT/$APP/tls" mv "$TEMP_DIR/server.crt" "$DOKKU_ROOT/$APP/tls/server.crt" mv "$TEMP_DIR/server.key" "$DOKKU_ROOT/$APP/tls/server.key" + cd $DOKKU_ROOT rm -rf $TEMP_DIR + dokku nginx:build-config $APP ;; help | nginx:help) From 59f047bb453e029c16235a62959d144c5093768a Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Tue, 16 Dec 2014 12:15:10 -0800 Subject: [PATCH 27/27] include SSL_VHOSTS in NOSSL_SERVER_NAME so we can redirect from http to https on the secure hostname as well --- plugins/nginx-vhosts/commands | 5 ++--- plugins/nginx-vhosts/templates/nginx.ssl.conf | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 64f88f947..c961bc5f0 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -60,9 +60,8 @@ EOF fi cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..." - NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` - # if NOSSL_SERVER_NAME is just ' ' then set NOSSL_SERVER_NAME to SSL_HOSTNAME since we redirect anyway - [[ "$NOSSL_SERVER_NAME" = ' ' ]] && NOSSL_SERVER_NAME=$(echo $SSL_VHOSTS | tr '\n' ' ') + # Include SSL_VHOSTS so we can redirect http to https on that hostname as well + NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS $SSL_VHOSTS| tr '\n' ' '` echo "-----> Creating $SCHEME nginx.conf" echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf diff --git a/plugins/nginx-vhosts/templates/nginx.ssl.conf b/plugins/nginx-vhosts/templates/nginx.ssl.conf index e30a6a82d..5cde7484a 100644 --- a/plugins/nginx-vhosts/templates/nginx.ssl.conf +++ b/plugins/nginx-vhosts/templates/nginx.ssl.conf @@ -2,7 +2,7 @@ server { listen [::]:80; listen 80; server_name $NOSSL_SERVER_NAME; - return 301 https://\$host\$request_uri; + return 301 https://$SSL_SERVER_NAME\$request_uri; } server {