From b605de522c4a3ceca8587978f0d91a74ae58679d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 10 Aug 2022 04:12:01 -0400 Subject: [PATCH] refactor: split out proxy port configuration into it's own trigger This will allow it to be called by plugins other than the nginx-vhosts one. --- docs/development/plugin-triggers.md | 15 +++++++ plugins/nginx-vhosts/functions | 58 +------------------------ plugins/proxy/.gitignore | 1 + plugins/proxy/proxy-configure-ports | 65 +++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 57 deletions(-) create mode 100755 plugins/proxy/proxy-configure-ports diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 3e5564eee..347356920 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -1842,6 +1842,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `proxy-configure-ports` + +- Description: Configures the proxy port mapping +- Invoked by: `internally triggered by proxy plugins` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + ### `proxy-disable` - Description: Disables the configured proxy implementation for an app diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index f7514161d..426e91db7 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -155,62 +155,6 @@ nginx_logs() { tail "$NGINX_LOGS_ARGS" "$NGINX_LOGS_PATH" } -configure_nginx_ports() { - declare desc="configure nginx listening ports" - local APP=$1 - local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")" - local DOKKU_PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT) - local DOKKU_PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT) - local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP) - local IS_APP_VHOST_ENABLED=true - local UPSTREAM_PORT="5000" - plugn trigger domains-vhost-enabled "$APP" 2>/dev/null || IS_APP_VHOST_ENABLED=false - - if [[ -z "$DOKKU_PROXY_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then - if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then - dokku_log_info1 "No proxy port set, setting to random open high port" - local PROXY_PORT=$(get_available_port) - else - local PROXY_PORT=$(config_get --global DOKKU_PROXY_PORT) - PROXY_PORT=${PROXY_PORT:=80} - fi - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT="$PROXY_PORT" - fi - if [[ -z "$DOKKU_PROXY_SSL_PORT" ]]; then - if (is_ssl_enabled "$APP"); then - local PROXY_SSL_PORT=$(config_get --global DOKKU_PROXY_SSL_PORT) - PROXY_SSL_PORT=${PROXY_SSL_PORT:=443} - if [[ -z "$RAW_TCP_PORTS" ]] && [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then - dokku_log_info1 "No proxy ssl port set, setting to random open high port" - PROXY_SSL_PORT=$(get_available_port) - fi - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_SSL_PORT="$PROXY_SSL_PORT" - fi - fi - if [[ -z "$DOKKU_PROXY_PORT_MAP" ]]; then - if [[ -n "$RAW_TCP_PORTS" ]]; then - local RAW_TCP_PORT - for RAW_TCP_PORT in $RAW_TCP_PORTS; do - local PROXY_PORT_MAP+=" http:${RAW_TCP_PORT}:${RAW_TCP_PORT} " - done - else - local PROXY_PORT=${PROXY_PORT:-$DOKKU_PROXY_PORT} - local PROXY_SSL_PORT=${PROXY_SSL_PORT:-$DOKKU_PROXY_SSL_PORT} - [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]] && local UPSTREAM_PORT="$(<"$DOKKU_ROOT/$APP/PORT.web.1")" - if [[ -n "$PROXY_PORT" ]] && [[ -n "$PROXY_SSL_PORT" ]]; then - local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT https:${PROXY_SSL_PORT}:$UPSTREAM_PORT " - elif [[ -n "$PROXY_PORT" ]]; then - local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT " - fi - fi - if [[ -n "$PROXY_PORT_MAP" ]]; then - local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)" - local PROXY_PORT_MAP+=" $(merge_dedupe_list "$(remove_val_from_list "$PORT_MAP" "$DOKKU_PROXY_PORT_MAP" " ")" " ") " - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT_MAP="$PROXY_PORT_MAP" - fi - fi -} - validate_ssl_domains() { declare desc="check configured domains against SSL cert contents and show warning if mismatched" local APP=$1 @@ -384,7 +328,7 @@ nginx_build_config() { fi # setup nginx listen ports - configure_nginx_ports "$APP" + plugn trigger proxy-configure-ports "$APP" local PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT) local PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT) local PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP) diff --git a/plugins/proxy/.gitignore b/plugins/proxy/.gitignore index ce994fee0..3b20f2e1c 100644 --- a/plugins/proxy/.gitignore +++ b/plugins/proxy/.gitignore @@ -6,3 +6,4 @@ /install /post-* /report +!/proxy-configure-ports diff --git a/plugins/proxy/proxy-configure-ports b/plugins/proxy/proxy-configure-ports new file mode 100755 index 000000000..67754a7c4 --- /dev/null +++ b/plugins/proxy/proxy-configure-ports @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/certs/functions" +source "$PLUGIN_AVAILABLE_PATH/config/functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +trigger-proxy-proxy-configure-ports() { + declare desc="proxy proxy-configure-ports plugin trigger" + declare trigger="proxy-configure-ports" + declare APP="$1" + local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")" + local DOKKU_PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT) + local DOKKU_PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT) + local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP) + local IS_APP_VHOST_ENABLED=true + local UPSTREAM_PORT="5000" + + plugn trigger domains-vhost-enabled "$APP" 2>/dev/null || IS_APP_VHOST_ENABLED=false + + if [[ -z "$DOKKU_PROXY_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then + if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then + dokku_log_info1 "No proxy port set, setting to random open high port" + local PROXY_PORT=$(get_available_port) + else + local PROXY_PORT=$(config_get --global DOKKU_PROXY_PORT) + PROXY_PORT=${PROXY_PORT:=80} + fi + DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT="$PROXY_PORT" + fi + if [[ -z "$DOKKU_PROXY_SSL_PORT" ]]; then + if (is_ssl_enabled "$APP"); then + local PROXY_SSL_PORT=$(config_get --global DOKKU_PROXY_SSL_PORT) + PROXY_SSL_PORT=${PROXY_SSL_PORT:=443} + if [[ -z "$RAW_TCP_PORTS" ]] && [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then + dokku_log_info1 "No proxy ssl port set, setting to random open high port" + PROXY_SSL_PORT=$(get_available_port) + fi + DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_SSL_PORT="$PROXY_SSL_PORT" + fi + fi + if [[ -z "$DOKKU_PROXY_PORT_MAP" ]]; then + if [[ -n "$RAW_TCP_PORTS" ]]; then + local RAW_TCP_PORT + for RAW_TCP_PORT in $RAW_TCP_PORTS; do + local PROXY_PORT_MAP+=" http:${RAW_TCP_PORT}:${RAW_TCP_PORT} " + done + else + local PROXY_PORT=${PROXY_PORT:-$DOKKU_PROXY_PORT} + local PROXY_SSL_PORT=${PROXY_SSL_PORT:-$DOKKU_PROXY_SSL_PORT} + [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]] && local UPSTREAM_PORT="$(<"$DOKKU_ROOT/$APP/PORT.web.1")" + if [[ -n "$PROXY_PORT" ]] && [[ -n "$PROXY_SSL_PORT" ]]; then + local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT https:${PROXY_SSL_PORT}:$UPSTREAM_PORT " + elif [[ -n "$PROXY_PORT" ]]; then + local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT " + fi + fi + if [[ -n "$PROXY_PORT_MAP" ]]; then + local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)" + local PROXY_PORT_MAP+=" $(merge_dedupe_list "$(remove_val_from_list "$PORT_MAP" "$DOKKU_PROXY_PORT_MAP" " ")" " ") " + DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT_MAP="$PROXY_PORT_MAP" + fi + fi +} +trigger-proxy-proxy-configure-ports "$@"