From becf7c1f86b0e7f5d07f871fcfcee468085d3e7d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Sep 2020 13:57:35 -0400 Subject: [PATCH] feat: allow customizing the various nginx templates This will let custom distributions of Dokku to override the internal templates without needing to repackage Dokku or overwrite core files. --- docs/development/plugin-triggers.md | 30 +++++++++++++++++++++ plugins/20_events/nginx-app-template-source | 1 + plugins/nginx-vhosts/functions | 12 +++++---- plugins/nginx-vhosts/internal-functions | 10 +++++++ 4 files changed, 48 insertions(+), 5 deletions(-) create mode 120000 plugins/20_events/nginx-app-template-source diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 660b0696e..2a9f42e67 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -854,6 +854,36 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `nginx-app-template-source` + +- Description: Return the path to a `sigil` template that should be used to generate a given nginx configuration file. +- Invoked by: `nginx-vhosts#build-config` +- Arguments: `$APP $TEMPLATE_TYPE` + - The `TEMPLATE_TYPE` argument can be one of: `[app-config, hsts-config, validate-config]` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" + +APP="$1" +TEMPLATE_TYPE="$2" +case "$TEMPLATE_TYPE" in + app-config) + echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/app.conf.sigil";; + hsts-config) + echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hsts.conf.sigil";; + validate-config) + echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/validate.conf.sigil";; + *) + dokku_log_fail "Invalid template type: ${TEMPLATE_TYPE}" +esac +``` + +The default templates are viewable here: [plugins/nginx-vhosts/templates/](https://github.com/dokku/dokku/tree/master/plugins/nginx-vhosts/templates) + ### `nginx-dokku-template-source` - Description: Return the path to a `sigil` template that should be used to generate the `dokku.conf` nginx configuration file. diff --git a/plugins/20_events/nginx-app-template-source b/plugins/20_events/nginx-app-template-source new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/nginx-app-template-source @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 1ee8868a9..d7dfbb502 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -315,21 +315,23 @@ is_grpc_enabled() { nginx_build_config() { declare desc="build nginx config to proxy app containers using sigil" - local APP="$1" + declare APP="$1" DOKKU_APP_LISTEN_PORT="$2" DOKKU_APP_LISTEN_IP="$3" verify_app_name "$APP" - local DOKKU_APP_LISTEN_PORT="$2" - local DOKKU_APP_LISTEN_IP="$3" local VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" local APP_URLS_PATH="$DOKKU_ROOT/$APP/URLS" local NGINX_TEMPLATE_NAME="nginx.conf.sigil" - local DEFAULT_NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/$NGINX_TEMPLATE_NAME" - local NGINX_TEMPLATE="$DEFAULT_NGINX_TEMPLATE" + local NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/$NGINX_TEMPLATE_NAME" local SCHEME=http local NGINX_TEMPLATE_SOURCE="built-in" local APP_SSL_PATH="$DOKKU_ROOT/$APP/tls" local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")" local DOKKU_APP_LISTENERS + CUSTOM_NGINX_TEMPLATE="$(plugn trigger nginx-app-template-source "$APP" "app-config")" + if [[ -n "$CUSTOM_NGINX_TEMPLATE" ]]; then + NGINX_TEMPLATE="$CUSTOM_NGINX_TEMPLATE" + fi + local IS_APP_VHOST_ENABLED=true plugn trigger domains-vhost-enabled "$APP" || IS_APP_VHOST_ENABLED=false diff --git a/plugins/nginx-vhosts/internal-functions b/plugins/nginx-vhosts/internal-functions index 0528144df..3d3f8c0b1 100755 --- a/plugins/nginx-vhosts/internal-functions +++ b/plugins/nginx-vhosts/internal-functions @@ -22,6 +22,11 @@ fn-nginx-vhosts-manage-hsts() { local NGINX_HSTS_CONF="$DOKKU_ROOT/$APP/nginx.conf.d/hsts.conf" local HSTS_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/hsts.conf.sigil" + CUSTOM_HSTS_TEMPLATE="$(plugn trigger nginx-app-template-source "$APP" "hsts-config")" + if [[ -n "$CUSTOM_HSTS_TEMPLATE" ]]; then + HSTS_TEMPLATE="$CUSTOM_HSTS_TEMPLATE" + fi + if [[ "$HSTS" == "false" ]] || [[ "$SSL_ENABLED" != "true" ]]; then rm -rf "$NGINX_HSTS_CONF" return @@ -137,6 +142,11 @@ nginx_vhosts_is_valid_nginx_config_func() { local TMP_OUTPUT=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") trap "rm -rf '$TMP_OUTPUT' >/dev/null" RETURN INT TERM EXIT + CUSTOM_VALIDATE_TEMPLATE="$(plugn trigger nginx-app-template-source "$APP" "validate-config")" + if [[ -n "$CUSTOM_VALIDATE_TEMPLATE" ]]; then + VALIDATE_TEMPLATE="$CUSTOM_VALIDATE_TEMPLATE" + fi + sigil -f "$VALIDATE_TEMPLATE" NGINX_CONF="$DOKKU_ROOT/$APP/nginx.conf" | cat -s >"$TMP_OUTPUT" sudo "$NGINX_LOCATION" -t -c "$TMP_OUTPUT" 2>/dev/null }