From 3d785360e1673bcf86a40710d95467b96d63ca74 Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Mon, 18 Sep 2017 19:46:47 +0200 Subject: [PATCH 1/7] fix: Check the location of nginx in a central way - Now there's only a "single source of truth" for the location of the nginx binary. - It is checked if it's there before it's needed. --- plugins/nginx-vhosts/functions | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index b41a69921..036d06eca 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -6,14 +6,29 @@ source "$PLUGIN_AVAILABLE_PATH/config/functions" source "$PLUGIN_AVAILABLE_PATH/domains/functions" source "$PLUGIN_AVAILABLE_PATH/ps/functions" +get_nginx_location() { + declare desc="check that nginx is at the expected location and return it" + local NGINX_LOCATION="/usr/sbin/nginx" + if ! [ -x "$NGINX_LOCATION" ]; then + dokku_log_fail "Could not find nginx binary at ${NGINX_LOCATION}." + exit 1; + fi + echo "$NGINX_LOCATION" +} + validate_nginx() { declare desc="validate entire nginx config" + local NGINX_LOCATION=$(get_nginx_location) + if [ -z "$NGINX_LOCATION" ]; then + exit 1; + fi + set +e - sudo /usr/sbin/nginx -t > /dev/null 2>&1 + sudo "$NGINX_LOCATION" -t > /dev/null 2>&1 local exit_code=$? set -e if [[ "$exit_code" -ne "0" ]]; then - sudo /usr/sbin/nginx -t + sudo "$NGINX_LOCATION" -t shopt -s nullglob local conf_file for conf_file in $DOKKU_ROOT/*/nginx.conf; do @@ -152,11 +167,6 @@ is_spdy_enabled() { local MAJOR_VERSION MINOR_VERSION PATCH_VERSION local HAS_SUPPORT=true - if ! which nginx > /dev/null 2>&1; then - echo $HAS_SUPPORT - return - fi - MAJOR_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[1]}') MINOR_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[2]}') PATCH_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[3]}') @@ -179,11 +189,6 @@ is_http2_enabled() { local MAJOR_VERSION MINOR_VERSION PATCH_VERSION local HAS_SUPPORT=false - if ! which nginx > /dev/null 2>&1; then - echo $HAS_SUPPORT - return - fi - MAJOR_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[1]}') MINOR_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[2]}') PATCH_VERSION=$(echo "$NGINX_VERSION" | awk '{split($0,a,"."); print a[3]}') @@ -281,7 +286,11 @@ nginx_build_config() { done fi - local NGINX_VERSION="$(nginx -v 2>&1 | cut -d'/' -f 2)" + local NGINX_LOCATION=$(get_nginx_location) + if [ -z "$NGINX_LOCATION" ]; then + exit 1; + fi + local NGINX_VERSION="$("$NGINX_LOCATION" -v 2>&1 | cut -d'/' -f 2)" local SPDY_SUPPORTED="$(is_spdy_enabled "$NGINX_VERSION")" local HTTP2_SUPPORTED="$(is_http2_enabled "$NGINX_VERSION")" From bada02a12248ec98a6a54863b0f3a5a239e81a19 Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Mon, 18 Sep 2017 21:09:03 +0200 Subject: [PATCH 2/7] Use double brackets instead of single ones --- plugins/nginx-vhosts/functions | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 036d06eca..70cd0588e 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -9,7 +9,7 @@ source "$PLUGIN_AVAILABLE_PATH/ps/functions" get_nginx_location() { declare desc="check that nginx is at the expected location and return it" local NGINX_LOCATION="/usr/sbin/nginx" - if ! [ -x "$NGINX_LOCATION" ]; then + if ! [[ -x "$NGINX_LOCATION" ]]; then dokku_log_fail "Could not find nginx binary at ${NGINX_LOCATION}." exit 1; fi @@ -19,7 +19,7 @@ get_nginx_location() { validate_nginx() { declare desc="validate entire nginx config" local NGINX_LOCATION=$(get_nginx_location) - if [ -z "$NGINX_LOCATION" ]; then + if [[ -z "$NGINX_LOCATION" ]]; then exit 1; fi @@ -287,7 +287,7 @@ nginx_build_config() { fi local NGINX_LOCATION=$(get_nginx_location) - if [ -z "$NGINX_LOCATION" ]; then + if [[ -z "$NGINX_LOCATION" ]]; then exit 1; fi local NGINX_VERSION="$("$NGINX_LOCATION" -v 2>&1 | cut -d'/' -f 2)" From c49f81cd8b656ce9ef0d904df82a5256b715629a Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Mon, 18 Sep 2017 21:19:30 +0200 Subject: [PATCH 3/7] fix: Check first in $PATH for nginx, use sbin location as backup --- plugins/nginx-vhosts/functions | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 70cd0588e..dc4195df2 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -8,10 +8,14 @@ source "$PLUGIN_AVAILABLE_PATH/ps/functions" get_nginx_location() { declare desc="check that nginx is at the expected location and return it" - local NGINX_LOCATION="/usr/sbin/nginx" - if ! [[ -x "$NGINX_LOCATION" ]]; then - dokku_log_fail "Could not find nginx binary at ${NGINX_LOCATION}." - exit 1; + local NGINX_LOCATION=$(which nginx 2>/dev/null) + + if [[ -z "$NGINX_LOCATION" ]]; then + NGINX_LOCATION="/usr/sbin/nginx" + if ! [[ -x "$NGINX_LOCATION" ]]; then + dokku_log_fail "Could not find nginx binary in \$PATH or at '${NGINX_LOCATION}'." + exit 1; + fi fi echo "$NGINX_LOCATION" } From deace7a03abe168960bf4c43a80b92ff7881de19 Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Tue, 19 Sep 2017 08:26:55 +0200 Subject: [PATCH 4/7] fix: Remove unnecessary nesting --- plugins/nginx-vhosts/functions | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index dc4195df2..263e7d663 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -12,11 +12,13 @@ get_nginx_location() { if [[ -z "$NGINX_LOCATION" ]]; then NGINX_LOCATION="/usr/sbin/nginx" - if ! [[ -x "$NGINX_LOCATION" ]]; then - dokku_log_fail "Could not find nginx binary in \$PATH or at '${NGINX_LOCATION}'." - exit 1; - fi fi + + if ! [[ -x "$NGINX_LOCATION" ]]; then + dokku_log_fail "Could not find nginx binary in \$PATH or at '${NGINX_LOCATION}'." + exit 1; + fi + echo "$NGINX_LOCATION" } From 4f17392bf1188ba4ed79335c3a65e364782ee7e6 Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Tue, 19 Sep 2017 08:27:21 +0200 Subject: [PATCH 5/7] fix: Move operator inside the brackets --- plugins/nginx-vhosts/functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 263e7d663..b979b800c 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -14,7 +14,7 @@ get_nginx_location() { NGINX_LOCATION="/usr/sbin/nginx" fi - if ! [[ -x "$NGINX_LOCATION" ]]; then + if [[ ! -x "$NGINX_LOCATION" ]]; then dokku_log_fail "Could not find nginx binary in \$PATH or at '${NGINX_LOCATION}'." exit 1; fi From 441e864ea1214b29a29954c42c3fa93dbfc3e1b0 Mon Sep 17 00:00:00 2001 From: Manuel Colmenero Date: Tue, 19 Sep 2017 19:14:48 +0200 Subject: [PATCH 6/7] fix: Declare and assign separately to avoid masking return values Fixes the introduced CI warnings. --- plugins/nginx-vhosts/functions | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index b979b800c..f135fc964 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -8,8 +8,9 @@ source "$PLUGIN_AVAILABLE_PATH/ps/functions" get_nginx_location() { declare desc="check that nginx is at the expected location and return it" - local NGINX_LOCATION=$(which nginx 2>/dev/null) + local NGINX_LOCATION + NGINX_LOCATION=$(which nginx 2>/dev/null) if [[ -z "$NGINX_LOCATION" ]]; then NGINX_LOCATION="/usr/sbin/nginx" fi @@ -24,7 +25,8 @@ get_nginx_location() { validate_nginx() { declare desc="validate entire nginx config" - local NGINX_LOCATION=$(get_nginx_location) + local NGINX_LOCATION + NGINX_LOCATION=$(get_nginx_location) if [[ -z "$NGINX_LOCATION" ]]; then exit 1; fi @@ -292,13 +294,14 @@ nginx_build_config() { done fi - local NGINX_LOCATION=$(get_nginx_location) + local NGINX_LOCATION NGINX_VERSION SPDY_SUPPORTED HTTP2_SUPPORTED + NGINX_LOCATION=$(get_nginx_location) if [[ -z "$NGINX_LOCATION" ]]; then exit 1; fi - local NGINX_VERSION="$("$NGINX_LOCATION" -v 2>&1 | cut -d'/' -f 2)" - local SPDY_SUPPORTED="$(is_spdy_enabled "$NGINX_VERSION")" - local HTTP2_SUPPORTED="$(is_http2_enabled "$NGINX_VERSION")" + NGINX_VERSION="$("$NGINX_LOCATION" -v 2>&1 | cut -d'/' -f 2)" + SPDY_SUPPORTED="$(is_spdy_enabled "$NGINX_VERSION")" + HTTP2_SUPPORTED="$(is_http2_enabled "$NGINX_VERSION")" eval "$(config_export app "$APP")" local SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT" From 689f0443f39d125e4e04c61534d5d757d41df1cd Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 19 Sep 2017 13:53:00 -0400 Subject: [PATCH 7/7] chore: remove unnecessary dokku_log_fail [ci skip] --- plugins/nginx-vhosts/functions | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index f135fc964..4792550df 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -17,7 +17,6 @@ get_nginx_location() { if [[ ! -x "$NGINX_LOCATION" ]]; then dokku_log_fail "Could not find nginx binary in \$PATH or at '${NGINX_LOCATION}'." - exit 1; fi echo "$NGINX_LOCATION"