From 237aae9eec932bc786c4732e09766aecfd5c417e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 17 Aug 2022 18:37:32 -0400 Subject: [PATCH 01/24] feat: first pass at haproxy plugin implementation --- docs/appendices/0.28.0-migration-guide.md | 4 +- docs/networking/proxies/haproxy.md | 132 +++++++++++++++++ docs/template.html | 1 + plugins/haproxy-vhosts/command-functions | 135 ++++++++++++++++++ plugins/haproxy-vhosts/commands | 15 ++ plugins/haproxy-vhosts/core-post-deploy | 20 +++ .../haproxy-vhosts/docker-args-process-deploy | 86 +++++++++++ plugins/haproxy-vhosts/help-functions | 36 +++++ plugins/haproxy-vhosts/install | 17 +++ plugins/haproxy-vhosts/internal-functions | 45 ++++++ plugins/haproxy-vhosts/plugin.toml | 4 + plugins/haproxy-vhosts/subcommands/default | 6 + plugins/haproxy-vhosts/subcommands/logs | 6 + plugins/haproxy-vhosts/subcommands/report | 6 + plugins/haproxy-vhosts/subcommands/set | 37 +++++ .../haproxy-vhosts/subcommands/show-config | 7 + plugins/haproxy-vhosts/subcommands/start | 6 + plugins/haproxy-vhosts/subcommands/stop | 6 + .../templates/compose.yml.sigil | 20 +++ 19 files changed, 587 insertions(+), 2 deletions(-) create mode 100644 docs/networking/proxies/haproxy.md create mode 100755 plugins/haproxy-vhosts/command-functions create mode 100755 plugins/haproxy-vhosts/commands create mode 100755 plugins/haproxy-vhosts/core-post-deploy create mode 100755 plugins/haproxy-vhosts/docker-args-process-deploy create mode 100755 plugins/haproxy-vhosts/help-functions create mode 100755 plugins/haproxy-vhosts/install create mode 100755 plugins/haproxy-vhosts/internal-functions create mode 100644 plugins/haproxy-vhosts/plugin.toml create mode 100755 plugins/haproxy-vhosts/subcommands/default create mode 100755 plugins/haproxy-vhosts/subcommands/logs create mode 100755 plugins/haproxy-vhosts/subcommands/report create mode 100755 plugins/haproxy-vhosts/subcommands/set create mode 100755 plugins/haproxy-vhosts/subcommands/show-config create mode 100755 plugins/haproxy-vhosts/subcommands/start create mode 100755 plugins/haproxy-vhosts/subcommands/stop create mode 100644 plugins/haproxy-vhosts/templates/compose.yml.sigil diff --git a/docs/appendices/0.28.0-migration-guide.md b/docs/appendices/0.28.0-migration-guide.md index 34e68f4fe..e308cf388 100644 --- a/docs/appendices/0.28.0-migration-guide.md +++ b/docs/appendices/0.28.0-migration-guide.md @@ -19,7 +19,7 @@ Ubuntu 18.04 is now a deprecated installation target. The operating system will ## Additions -New in 0.28.0 are the Caddy and Traefik plugins. As community plugins wrapping these proxies exist, users should: +New in 0.28.0 are the Caddy, Haproxy and Traefik plugins. As community plugins wrapping these proxies exist, users should: - Recommended: Uninstall the community plugin in question and switch all config to the new plugins. -- Upgrade the community plugin to a version that does not use the `proxy:set` value of `caddy` or `traefik`. +- Upgrade the community plugin to a version that does not use the `proxy:set` value of `caddy`, `haproxy` or `traefik`. diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md new file mode 100644 index 000000000..198576d62 --- /dev/null +++ b/docs/networking/proxies/haproxy.md @@ -0,0 +1,132 @@ +# Haproxy Proxy + +> New as of 0.28.0 + +Dokku provides integration with the [Haproxy](http://www.haproxy.org) proxy service by utilizing the Docker label-based integration implemented by [EasyHaproxy](https://github.com/byjg/docker-easy-haproxy). + +``` +haproxy:report [] [] # Displays a haproxy report for one or more apps +haproxy:logs [--num num] [--tail] # Display haproxy log output +haproxy:set () # Set or clear an haproxy property for an app +haproxy:show-config # Display haproxy compose config +haproxy:start # Starts the haproxy server +haproxy:stop # Stops the haproxy server +``` + +## Usage + +> Warning: As using multiple proxy plugins on a single Dokku installation can lead to issues routing requests to apps, doing so should be avoided. As the default proxy implementation is nginx, users are encouraged to stop the nginx service before switching to Haproxy. + +The Haproxy plugin has specific rules for routing requests: + +- Haproxy integration is exposed via docker labels attached to containers. Changes in labels require either app deploys or rebuilds. +- While Haproxy will respect labels associated with other containers, only `web` containers have Haproxy labels injected by the plugin. +- Only `http:80` port mappings are supported at this time. +- Requests are routed as soon as the container is running and passing healthchecks. + +### Switching to Haproxy + +To use the Haproxy plugin, use the `proxy:set` command for the app in question: + +```shell +dokku proxy:set node-js-app haproxy +``` + +This will enable the docker label-based Haproxy integration. All future deploys will inject the correct labels for Haproxy to read and route requests to containers. Due to the docker label-based integration used by Haproxy, a single deploy or rebuild will be required before requests will route successfully. + +```shell +dokku ps:rebuild node-js-app +``` + +Any changes to domains or port mappings will also require either a deploy or rebuild. + +### Starting Haproxy container + +Haproxy can be started via the `haproxy:start` command. This will start a Haproxy container via the `docker compose up` command. + +```shell +dokku haproxy:start +``` + +### Stopping the Haproxy container + +Haproxy may be stopped via the `haproxy:stop` command. + +```shell +dokku haproxy:stop +``` + +The Haproxy container will be stopped and removed from the system. If the container is not running, this command will do nothing. + +### Showing the Haproxy compose config + +For debugging purposes, it may be useful to show the Haproxy compose config. This can be achieved via the `haproxy:show-config` command. + +```shell +dokku haproxy:show-config +``` + +### Customizing the Haproxy container image + +While the default Haproxy image is hardcoded, users may specify an alternative by setting the `image` property with the `--global` flag: + +```shell +dokku haproxy:set --global image byjg/easy-haproxy:4.0.0 +``` + +#### Checking the Haproxy container's logs + +It may be necessary to check the Haproxy container's logs to ensure that Haproxy is operating as expected. This can be performed with the `haproxy:logs` command. + +```shell +dokku haproxy:logs +``` + +This command also supports the following modifiers: + +```shell +--num NUM # the number of lines to display +--tail # continually stream logs +``` + +You can use these modifiers as follows: + +```shell +dokku haproxy:logs --tail --num 10 +``` + +The above command will show logs continually from the vector container, with an initial history of 10 log lines + +## Displaying Haproxy reports for an app + +You can get a report about the app's Haproxy config using the `haproxy:report` command: + +```shell +dokku haproxy:report +``` + +``` +=====> node-js-app haproxy information + Haproxy image: byjg/easy-haproxy:4.0.0 +=====> python-app haproxy information + Haproxy image: byjg/easy-haproxy:4.0.0 +=====> ruby-app haproxy information + Haproxy image: byjg/easy-haproxy:4.0.0 +``` + +You can run the command for a specific app also. + +```shell +dokku haproxy:report node-js-app +``` + +``` +=====> node-js-app haproxy information + Haproxy image: byjg/easy-haproxy:4.0.0 +``` + +You can pass flags which will output only the value of the specific information you want. For example: + +```shell +dokku haproxy:report node-js-app --haproxy-image +``` diff --git a/docs/template.html b/docs/template.html index d258aa950..df6b69c44 100644 --- a/docs/template.html +++ b/docs/template.html @@ -176,6 +176,7 @@ Proxy Management Caddy Proxy + Haproxy Proxy Nginx Proxy Traefik Proxy diff --git a/plugins/haproxy-vhosts/command-functions b/plugins/haproxy-vhosts/command-functions new file mode 100755 index 000000000..283416120 --- /dev/null +++ b/plugins/haproxy-vhosts/command-functions @@ -0,0 +1,135 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-haproxy-report() { + declare desc="displays a haproxy report for one or more apps" + declare cmd="haproxy:report" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" INFO_FLAG="$2" + + if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then + INFO_FLAG="$APP" + APP="" + fi + + if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then + INFO_FLAG="true" + fi + + if [[ -z "$APP" ]]; then + for app in $(dokku_apps); do + cmd-haproxy-report-single "$app" "$INFO_FLAG" | tee || true + done + else + cmd-haproxy-report-single "$APP" "$INFO_FLAG" + fi +} + +cmd-haproxy-report-single() { + declare APP="$1" INFO_FLAG="$2" + if [[ "$INFO_FLAG" == "true" ]]; then + INFO_FLAG="" + fi + verify_app_name "$APP" + local flag_map=( + "--haproxy-image: $(fn-haproxy-image)" + ) + + if [[ -z "$INFO_FLAG" ]]; then + dokku_log_info2_quiet "${APP} haproxy information" + for flag in "${flag_map[@]}"; do + key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')" + dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")" + done + else + local match=false + local value_exists=false + for flag in "${flag_map[@]}"; do + valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)" + if [[ "$flag" == "${INFO_FLAG}:"* ]]; then + value=${flag#*: } + size="${#value}" + if [[ "$size" -ne 0 ]]; then + echo "$value" && match=true && value_exists=true + else + match=true + fi + fi + done + [[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}" + fi +} + +cmd-haproxy-logs() { + declare desc="display haproxy logs from command line" + declare cmd="haproxy:logs" + [[ "$1" == "$cmd" ]] && shift 1 + local NUM="100" TAIL=false + + local TEMP=$(getopt -o htn: --long help,tail,num: -n 'dokku haproxy:logs' -- "$@") + local EXIT_CODE="$?" + if [[ "$EXIT_CODE" != 0 ]]; then + fn-haproxy-logs-usage >&2 + exit 1 + fi + eval set -- "$TEMP" + + while true; do + case "$1" in + -t | --tail) + local TAIL=true + shift + ;; + -n | --num) + local NUM="$2" + shift 2 + ;; + --) + shift + break + ;; + *) dokku_log_fail "Internal error" ;; + esac + done + + fn-haproxy-logs "$TAIL" "$NUM" +} + +cmd-haproxy-show-config() { + declare desc="display haproxy config" + declare cmd="haproxy:show-config" + [[ "$1" == "$cmd" ]] && shift 1 + + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") + trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT + + fn-haproxy-template-compose-file "$TMP_COMPOSE_FILE" + cat "$TMP_COMPOSE_FILE" +} + +cmd-haproxy-start() { + declare desc="Starts the haproxy server" + declare cmd="haproxy:start" + [[ "$1" == "$cmd" ]] && shift 1 + + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") + trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT + + fn-haproxy-template-compose-file "$TMP_COMPOSE_FILE" + "$DOCKER_BIN" compose -f "$TMP_COMPOSE_FILE" -p haproxy up -d --quiet-pull +} + +cmd-haproxy-stop() { + declare desc="Starts the haproxy server" + declare cmd="haproxy:start" + [[ "$1" == "$cmd" ]] && shift 1 + + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") + trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT + + fn-haproxy-template-compose-file "$TMP_COMPOSE_FILE" + "$DOCKER_BIN" compose -f "$TMP_COMPOSE_FILE" -p haproxy down --remove-orphans +} diff --git a/plugins/haproxy-vhosts/commands b/plugins/haproxy-vhosts/commands new file mode 100755 index 000000000..945869e42 --- /dev/null +++ b/plugins/haproxy-vhosts/commands @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +[[ " haproxy:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/help-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +case "$1" in + help | haproxy:help) + cmd-haproxy-help "$@" + ;; + + *) + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" + ;; +esac diff --git a/plugins/haproxy-vhosts/core-post-deploy b/plugins/haproxy-vhosts/core-post-deploy new file mode 100755 index 000000000..7b7c4c968 --- /dev/null +++ b/plugins/haproxy-vhosts/core-post-deploy @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +trigger-haproxy-vhosts-core-post-deploy() { + declare desc="haproxy-vhosts core-post-deploy plugin trigger" + declare trigger="core-post-deploy" + declare APP="$1" + local tls_internal + + if [[ "$(plugn trigger proxy-type "$APP")" != "haproxy" ]]; then + return + fi + + dokku_log_info1 "Routing app via haproxy" +} + +trigger-haproxy-vhosts-core-post-deploy "$@" diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy new file mode 100755 index 000000000..2918f252e --- /dev/null +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/internal-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +trigger-haproxy-vhosts-docker-args-process-deploy() { + declare desc="nginx-vhosts core-post-deploy plugin trigger" + declare trigger="docker-args-process-deploy" + declare APP="$1" IMAGE_SOURCE_TYPE="$2" IMAGE_TAG="$3" PROC_TYPE="$4" CONTAINER_INDEX="$5" + local app_domains haproxy_domains is_app_listening letsencrypt_email output proxy_container_port proxy_host_port port_map proxy_port_map proxy_scheme proxy_schemes scheme tls_internal + local proxy_container_http_port proxy_container_http_port_candidate proxy_host_http_port_candidate + local proxy_container_https_port proxy_container_https_port_candidate proxy_host_https_port_candidate + local app_urls_path="$DOKKU_ROOT/$APP/URLS" + local STDIN=$(cat) + + if [[ "$PROC_TYPE" != "web" ]]; then + return + fi + + if [[ "$(plugn trigger proxy-type "$APP")" != "haproxy" ]]; then + return + fi + + if [[ "$(plugn trigger proxy-is-enabled "$APP")" != "true" ]]; then + return + fi + + if ! plugn trigger domains-vhost-enabled "$APP" 2>/dev/null; then + return + fi + + # run this silently or the output will be set as a label + plugn trigger domains-setup "$APP" >/dev/null + + # ensure we have a port mapping + plugn trigger proxy-configure-ports "$APP" + + app_domains="$(plugn trigger domains-list "$APP")" + if [[ -n "$app_domains" ]]; then + haproxy_domains="$(echo "$app_domains" | xargs)" + haproxy_domains="${haproxy_domains// /,}" + fi + + output="" + # gather port mapping information + is_app_listening="false" + proxy_port_map="$(plugn trigger config-get "$APP" DOKKU_PROXY_PORT_MAP)" + for port_map in $proxy_port_map; do + proxy_scheme="$(awk -F ':' '{ print $1 }' <<<"$port_map")" + proxy_host_port="$(awk -F ':' '{ print $2 }' <<<"$port_map")" + proxy_container_port="$(awk -F ':' '{ print $3 }' <<<"$port_map")" + + # ignore https for now + if [[ "$proxy_scheme" != "http" ]] && [[ "$proxy_scheme" != "tcp" ]]; then + continue + fi + + # ignore 443 for now + if [[ "$proxy_host_port" == "443" ]]; then + continue + fi + + # ignore everything but 80 for now + if [[ "$proxy_host_port" != "80" ]]; then + continue + fi + + is_app_listening="true" + output="$output --label easyhaproxy.$APP-$PROC_TYPE.mode=$proxy_scheme" + output="$output --label easyhaproxy.$APP-$PROC_TYPE.port=$proxy_host_port" + output="$output --label easyhaproxy.$APP-$PROC_TYPE.localport=$proxy_container_port" + output="$output --label easyhaproxy.$APP-$PROC_TYPE.host=$haproxy_domains" + done + + # add the labels for haproxy here + # prefer the https:443 mapping to http:80 mapping + if [[ -n "$is_app_listening" ]] && [[ -n "$haproxy_domains" ]]; then + echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" >"$app_urls_path" + xargs -I{} echo "$scheme://{}" <<<"$(echo "${app_domains}" | tr ' ' '\n' | sort -u)" >>"$app_urls_path" + fi + + echo -n "$STDIN$output" +} + +trigger-haproxy-vhosts-docker-args-process-deploy "$@" diff --git a/plugins/haproxy-vhosts/help-functions b/plugins/haproxy-vhosts/help-functions new file mode 100755 index 000000000..80da34667 --- /dev/null +++ b/plugins/haproxy-vhosts/help-functions @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-haproxy-help() { + declare desc="help command" + declare CMD="$1" + local plugin_name="haproxy" + local plugin_description="Manage mounted volumes" + + if [[ "$CMD" == "${plugin_name}:help" ]]; then + echo -e "Usage: dokku ${plugin_name}[:COMMAND]" + echo '' + echo "$plugin_description" + echo '' + echo 'Additional commands:' + fn-help-content | sort | column -c2 -t -s, + elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then + fn-help-content + else + cat <] [], Displays an haproxy report for one or more apps + haproxy:set (), Set or clear an haproxy property for an app + haproxy:show-config , Display haproxy compose config + haproxy:start, Starts the haproxy server + haproxy:stop, Stops the haproxy server +help_content +} diff --git a/plugins/haproxy-vhosts/install b/plugins/haproxy-vhosts/install new file mode 100755 index 000000000..75e6c3b84 --- /dev/null +++ b/plugins/haproxy-vhosts/install @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +trigger-haproxy-install() { + declare desc="installs the haproxy plugin" + declare trigger="install" + + mkdir -p "${DOKKU_LIB_ROOT}/data/haproxy" + chown -R "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/data/haproxy" + + fn-plugin-property-setup "haproxy" +} + +trigger-caddy-install "$@" diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions new file mode 100755 index 000000000..cd14271ec --- /dev/null +++ b/plugins/haproxy-vhosts/internal-functions @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +fn-haproxy-logs() { + declare desc="shows the logs for the haproxy container" + declare TAIL="$1" NUM="$2" + local dokku_logs_args=("--tail" "$NUM") + + if [[ "$TAIL" == "true" ]]; then + dokku_logs_args+=("--follow") + fi + + "$DOCKER_BIN" logs haproxy-haproxy-1 "${dokku_logs_args[@]}" +} + +fn-haproxy-logs-usage() { + declare desc="logs specific usage" + echo "Usage: dokku haproxy:logs" + echo " display recent haproxy log output" + echo "" + echo " -n, --num NUM # the number of lines to display" + echo " -t, --tail # continually stream logs" +} + +fn-haproxy-template-compose-file() { + declare desc="templates out the compose file" + declare OUTPUT_PATH="$1" + local COMPOSE_TEMPLATE="$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/templates/compose.yml.sigil" + + CUSTOM_COMPOSE_TEMPLATE="$(plugn trigger haproxy-template-source "$APP")" + if [[ -n "$CUSTOM_COMPOSE_TEMPLATE" ]]; then + COMPOSE_TEMPLATE="$CUSTOM_COMPOSE_TEMPLATE" + fi + + local SIGIL_PARAMS=(HAPROXY_IMAGE="$(fn-haproxy-image)") + + sigil -f "$COMPOSE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >"$OUTPUT_PATH" +} + +fn-haproxy-image() { + fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.0.0" +} diff --git a/plugins/haproxy-vhosts/plugin.toml b/plugins/haproxy-vhosts/plugin.toml new file mode 100644 index 000000000..39b6df8eb --- /dev/null +++ b/plugins/haproxy-vhosts/plugin.toml @@ -0,0 +1,4 @@ +[plugin] +description = "dokku core caddy-vhosts plugin" +version = "0.27.10" +[plugin.config] diff --git a/plugins/haproxy-vhosts/subcommands/default b/plugins/haproxy-vhosts/subcommands/default new file mode 100755 index 000000000..dfdd058d7 --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/default @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/help-functions" + +cmd-haproxy-help "haproxy:help" diff --git a/plugins/haproxy-vhosts/subcommands/logs b/plugins/haproxy-vhosts/subcommands/logs new file mode 100755 index 000000000..2112086fd --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/logs @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-haproxy-logs "$@" diff --git a/plugins/haproxy-vhosts/subcommands/report b/plugins/haproxy-vhosts/subcommands/report new file mode 100755 index 000000000..0e483c438 --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/report @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-haproxy-report "$@" diff --git a/plugins/haproxy-vhosts/subcommands/set b/plugins/haproxy-vhosts/subcommands/set new file mode 100755 index 000000000..54f7388dc --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/set @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +cmd-haproxy-set() { + declare desc="set or clear an haproxy property for an app" + declare cmd="haproxy:set" + [[ "$1" == "$cmd" ]] && shift 1 + declare APP="$1" KEY="$2" VALUE="$3" + local VALID_KEYS=("image") + local GLOBAL_KEYS=("image") + + [[ -z "$KEY" ]] && dokku_log_fail "No key specified" + + if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then + dokku_log_fail "Invalid key specified, valid keys include: image" + fi + + if ! fn-in-array "$KEY" "${GLOBAL_KEYS[@]}"; then + if [[ "$APP" == "--global" ]]; then + dokku_log_fail "The key '$KEY' cannot be set globally" + fi + verify_app_name "$APP" + fi + + if [[ -n "$VALUE" ]]; then + dokku_log_info2_quiet "Setting ${KEY} to ${VALUE}" + fn-plugin-property-write "haproxy" "$APP" "$KEY" "$VALUE" + else + dokku_log_info2_quiet "Unsetting ${KEY}" + fn-plugin-property-delete "haproxy" "$APP" "$KEY" + fi +} + +cmd-haproxy-set "$@" diff --git a/plugins/haproxy-vhosts/subcommands/show-config b/plugins/haproxy-vhosts/subcommands/show-config new file mode 100755 index 000000000..0b58d12a4 --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/show-config @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions" + +cmd-haproxy-show-config "$@" diff --git a/plugins/haproxy-vhosts/subcommands/start b/plugins/haproxy-vhosts/subcommands/start new file mode 100755 index 000000000..7b73b1fc2 --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/start @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions" + +cmd-haproxy-start "$@" diff --git a/plugins/haproxy-vhosts/subcommands/stop b/plugins/haproxy-vhosts/subcommands/stop new file mode 100755 index 000000000..4e6119885 --- /dev/null +++ b/plugins/haproxy-vhosts/subcommands/stop @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions" + +cmd-haproxy-stop "$@" diff --git a/plugins/haproxy-vhosts/templates/compose.yml.sigil b/plugins/haproxy-vhosts/templates/compose.yml.sigil new file mode 100644 index 000000000..1b63072f8 --- /dev/null +++ b/plugins/haproxy-vhosts/templates/compose.yml.sigil @@ -0,0 +1,20 @@ +--- +version: "3.7" + +services: + caddy: + image: "{{ $.HAPROXY_IMAGE }}" + + environment: + - EASYHAPROXY_DISCOVER=docker + - EASYHAPROXY_LABEL_PREFIX=haproxy + + network_mode: bridge + + ports: + - "80:80" + + restart: unless-stopped + + volumes: + - "/var/run/docker.sock:/var/run/docker.sock:ro" From 49cd46df1d9fed413aba924d22d6350cc9cfd66e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 17 Aug 2022 18:54:20 -0400 Subject: [PATCH 02/24] fix: properly name things --- plugins/haproxy-vhosts/install | 2 +- plugins/haproxy-vhosts/plugin.toml | 2 +- plugins/haproxy-vhosts/templates/compose.yml.sigil | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/haproxy-vhosts/install b/plugins/haproxy-vhosts/install index 75e6c3b84..24d3be3a7 100755 --- a/plugins/haproxy-vhosts/install +++ b/plugins/haproxy-vhosts/install @@ -14,4 +14,4 @@ trigger-haproxy-install() { fn-plugin-property-setup "haproxy" } -trigger-caddy-install "$@" +trigger-haproxy-install "$@" diff --git a/plugins/haproxy-vhosts/plugin.toml b/plugins/haproxy-vhosts/plugin.toml index 39b6df8eb..3eb682a2f 100644 --- a/plugins/haproxy-vhosts/plugin.toml +++ b/plugins/haproxy-vhosts/plugin.toml @@ -1,4 +1,4 @@ [plugin] -description = "dokku core caddy-vhosts plugin" +description = "dokku core haproxy-vhosts plugin" version = "0.27.10" [plugin.config] diff --git a/plugins/haproxy-vhosts/templates/compose.yml.sigil b/plugins/haproxy-vhosts/templates/compose.yml.sigil index 1b63072f8..816c8efde 100644 --- a/plugins/haproxy-vhosts/templates/compose.yml.sigil +++ b/plugins/haproxy-vhosts/templates/compose.yml.sigil @@ -2,7 +2,7 @@ version: "3.7" services: - caddy: + haproxy: image: "{{ $.HAPROXY_IMAGE }}" environment: From 44543c979a9928d09bd601dfdb44260f618df7d1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 00:19:31 -0400 Subject: [PATCH 03/24] docs: add requirement for docker-compose-plugin --- docs/networking/proxies/haproxy.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md index 198576d62..497df7c6c 100644 --- a/docs/networking/proxies/haproxy.md +++ b/docs/networking/proxies/haproxy.md @@ -13,6 +13,10 @@ haproxy:start # Starts the haproxy server haproxy:stop # Stops the haproxy server ``` +## Requirements + +Using the `caddy` plugin integration requires the `docker-compose-plugin` for Docker. See [this document](https://docs.docker.com/compose/install/) from the Docker documentation for more information on the installation process for the `docker-compose-plugin`. + ## Usage > Warning: As using multiple proxy plugins on a single Dokku installation can lead to issues routing requests to apps, doing so should be avoided. As the default proxy implementation is nginx, users are encouraged to stop the nginx service before switching to Haproxy. From 3871a35de63557e9dd62456f25656898061556b9 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 00:19:53 -0400 Subject: [PATCH 04/24] fix: respect EASYHAPROXY_LABEL_PREFIX --- plugins/haproxy-vhosts/docker-args-process-deploy | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index 2918f252e..d2fa97683 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -67,10 +67,10 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { fi is_app_listening="true" - output="$output --label easyhaproxy.$APP-$PROC_TYPE.mode=$proxy_scheme" - output="$output --label easyhaproxy.$APP-$PROC_TYPE.port=$proxy_host_port" - output="$output --label easyhaproxy.$APP-$PROC_TYPE.localport=$proxy_container_port" - output="$output --label easyhaproxy.$APP-$PROC_TYPE.host=$haproxy_domains" + output="$output --label haproxy.$APP-$PROC_TYPE.mode=$proxy_scheme" + output="$output --label haproxy.$APP-$PROC_TYPE.port=$proxy_host_port" + output="$output --label haproxy.$APP-$PROC_TYPE.localport=$proxy_container_port" + output="$output --label haproxy.$APP-$PROC_TYPE.host=$haproxy_domains" done # add the labels for haproxy here From 8bd4f66bec7f61b582991cd3b94eb6d921483da9 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 00:21:02 -0400 Subject: [PATCH 05/24] chore: update to latest version --- plugins/haproxy-vhosts/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions index cd14271ec..34d44f534 100755 --- a/plugins/haproxy-vhosts/internal-functions +++ b/plugins/haproxy-vhosts/internal-functions @@ -41,5 +41,5 @@ fn-haproxy-template-compose-file() { } fn-haproxy-image() { - fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.0.0" + fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.2.0" } From fd09beaf4848e62e161866524b482eb258e5e946 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 00:26:48 -0400 Subject: [PATCH 06/24] feat: add the ability to set the haproxy log level --- docs/networking/proxies/haproxy.md | 10 ++++++++++ plugins/haproxy-vhosts/command-functions | 1 + plugins/haproxy-vhosts/internal-functions | 9 ++++++++- plugins/haproxy-vhosts/subcommands/set | 6 +++--- plugins/haproxy-vhosts/templates/compose.yml.sigil | 1 + 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md index 497df7c6c..9b7dfbc01 100644 --- a/docs/networking/proxies/haproxy.md +++ b/docs/networking/proxies/haproxy.md @@ -101,6 +101,16 @@ dokku haproxy:logs --tail --num 10 The above command will show logs continually from the vector container, with an initial history of 10 log lines +### Changing the Haproxy log level + +Haproxy log output is set to `ERROR` by default. It may be changed by setting the `log-level` property with the `--global` flag: + +```shell +dokku haproxy:set --global log-level DEBUG +``` + +After modifying, the Haproxy container will need to be restarted. + ## Displaying Haproxy reports for an app You can get a report about the app's Haproxy config using the `haproxy:report` command: diff --git a/plugins/haproxy-vhosts/command-functions b/plugins/haproxy-vhosts/command-functions index 283416120..05aceb4b5 100755 --- a/plugins/haproxy-vhosts/command-functions +++ b/plugins/haproxy-vhosts/command-functions @@ -36,6 +36,7 @@ cmd-haproxy-report-single() { verify_app_name "$APP" local flag_map=( "--haproxy-image: $(fn-haproxy-image)" + "--haproxy-log-level: $(fn-haproxy-log-level)" ) if [[ -z "$INFO_FLAG" ]]; then diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions index 34d44f534..28aecf6a1 100755 --- a/plugins/haproxy-vhosts/internal-functions +++ b/plugins/haproxy-vhosts/internal-functions @@ -35,7 +35,8 @@ fn-haproxy-template-compose-file() { COMPOSE_TEMPLATE="$CUSTOM_COMPOSE_TEMPLATE" fi - local SIGIL_PARAMS=(HAPROXY_IMAGE="$(fn-haproxy-image)") + local SIGIL_PARAMS=(HAPROXY_IMAGE="$(fn-haproxy-image)" + HAPROXY_LOG_LEVEL="$(fn-traefik-log-level)") sigil -f "$COMPOSE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >"$OUTPUT_PATH" } @@ -43,3 +44,9 @@ fn-haproxy-template-compose-file() { fn-haproxy-image() { fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.2.0" } + +fn-haproxy-log-level() { + local log_level + log_level="$(fn-plugin-property-get-default "haproxy" "--global" "log-level" "ERROR")" + echo "${log_level^^}" +} diff --git a/plugins/haproxy-vhosts/subcommands/set b/plugins/haproxy-vhosts/subcommands/set index 54f7388dc..d893c6c6e 100755 --- a/plugins/haproxy-vhosts/subcommands/set +++ b/plugins/haproxy-vhosts/subcommands/set @@ -9,13 +9,13 @@ cmd-haproxy-set() { declare cmd="haproxy:set" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" KEY="$2" VALUE="$3" - local VALID_KEYS=("image") - local GLOBAL_KEYS=("image") + local VALID_KEYS=("image" "log-level") + local GLOBAL_KEYS=("image" "log-level") [[ -z "$KEY" ]] && dokku_log_fail "No key specified" if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then - dokku_log_fail "Invalid key specified, valid keys include: image" + dokku_log_fail "Invalid key specified, valid keys include: image log-level" fi if ! fn-in-array "$KEY" "${GLOBAL_KEYS[@]}"; then diff --git a/plugins/haproxy-vhosts/templates/compose.yml.sigil b/plugins/haproxy-vhosts/templates/compose.yml.sigil index 816c8efde..49e714123 100644 --- a/plugins/haproxy-vhosts/templates/compose.yml.sigil +++ b/plugins/haproxy-vhosts/templates/compose.yml.sigil @@ -8,6 +8,7 @@ services: environment: - EASYHAPROXY_DISCOVER=docker - EASYHAPROXY_LABEL_PREFIX=haproxy + - EASYHAPROXY_LOG_LEVEL={{ $.HAPROXY_LOG_LEVEL }} network_mode: bridge From 1a54e2263b4fc0481e474c6889aea5ecda8ea295 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 00:39:15 -0400 Subject: [PATCH 07/24] fix: use correct variable name --- plugins/haproxy-vhosts/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions index 28aecf6a1..ff0595289 100755 --- a/plugins/haproxy-vhosts/internal-functions +++ b/plugins/haproxy-vhosts/internal-functions @@ -36,7 +36,7 @@ fn-haproxy-template-compose-file() { fi local SIGIL_PARAMS=(HAPROXY_IMAGE="$(fn-haproxy-image)" - HAPROXY_LOG_LEVEL="$(fn-traefik-log-level)") + HAPROXY_LOG_LEVEL="$(fn-haproxy-log-level)") sigil -f "$COMPOSE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >"$OUTPUT_PATH" } From a249dea217a7b84076ceb2296e365053ceaf1ece Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 01:12:38 -0400 Subject: [PATCH 08/24] fix: add alias --- dokku | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dokku b/dokku index 2ad0de76f..7a6b897db 100755 --- a/dokku +++ b/dokku @@ -141,6 +141,9 @@ execute_dokku_cmd() { caddy | caddy:*) local PLUGIN_NAME=${PLUGIN_NAME/caddy/caddy-vhosts} ;; + haproxy | haproxy:*) + local PLUGIN_NAME=${PLUGIN_NAME/haproxy/haproxy-vhosts} + ;; nginx | nginx:*) local PLUGIN_NAME=${PLUGIN_NAME/nginx/nginx-vhosts} ;; From 212389980e4cab72ec570e52df0f949f4797b82d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Nov 2022 01:12:48 -0400 Subject: [PATCH 09/24] feat: add initial tests for haproxy plugin --- tests/unit/haproxy.bats | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/unit/haproxy.bats diff --git a/tests/unit/haproxy.bats b/tests/unit/haproxy.bats new file mode 100644 index 000000000..440b9650e --- /dev/null +++ b/tests/unit/haproxy.bats @@ -0,0 +1,83 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + global_setup + dokku nginx:stop + dokku haproxy:start + create_app +} + +teardown() { + global_teardown + destroy_app + dokku haproxy:stop + dokku nginx:start +} + +@test "(haproxy) haproxy:help" { + run /bin/bash -c "dokku haproxy" + echo "output: $output" + echo "status: $status" + assert_output_contains "Manage the haproxy proxy integration" + help_output="$output" + + run /bin/bash -c "dokku haproxy:help" + echo "output: $output" + echo "status: $status" + assert_output_contains "Manage the haproxy proxy integration" + assert_output "$help_output" +} + +@test "(haproxy) single domain" { + run /bin/bash -c "dokku proxy:set $TEST_APP haproxy" + echo "output: $output" + echo "status: $status" + assert_success + + run deploy_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "curl --silent $(dokku url $TEST_APP)" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "python/http.server" +} + +@test "(haproxy) multiple domains" { + run /bin/bash -c "dokku proxy:set $TEST_APP haproxy" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku domains:add $TEST_APP $TEST_APP.dokku.me" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku domains:add $TEST_APP $TEST_APP-2.dokku.me" + echo "output: $output" + echo "status: $status" + assert_success + + run deploy_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "curl --silent $TEST_APP.dokku.me" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "python/http.server" + + run /bin/bash -c "curl --silent $TEST_APP-2.dokku.me" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "python/http.server" +} From e1f9e44e51af8d32af68e357d0f32ca99132670d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 7 Nov 2022 22:27:02 -0500 Subject: [PATCH 10/24] fix: set correct help output --- plugins/haproxy-vhosts/help-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/haproxy-vhosts/help-functions b/plugins/haproxy-vhosts/help-functions index 80da34667..b28fd2b1f 100755 --- a/plugins/haproxy-vhosts/help-functions +++ b/plugins/haproxy-vhosts/help-functions @@ -6,7 +6,7 @@ cmd-haproxy-help() { declare desc="help command" declare CMD="$1" local plugin_name="haproxy" - local plugin_description="Manage mounted volumes" + local plugin_description="Manage the haproxy proxy integration" if [[ "$CMD" == "${plugin_name}:help" ]]; then echo -e "Usage: dokku ${plugin_name}[:COMMAND]" From 40cb28287f51541f98dd4a1b0c7ae8415c9a80cb Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 7 Nov 2022 23:33:21 -0500 Subject: [PATCH 11/24] fix: gate compose usage behind compose check --- plugins/haproxy-vhosts/command-functions | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugins/haproxy-vhosts/command-functions b/plugins/haproxy-vhosts/command-functions index 05aceb4b5..286a3bec0 100755 --- a/plugins/haproxy-vhosts/command-functions +++ b/plugins/haproxy-vhosts/command-functions @@ -104,6 +104,10 @@ cmd-haproxy-show-config() { declare cmd="haproxy:show-config" [[ "$1" == "$cmd" ]] && shift 1 + if ! fn-is-compose-installed; then + dokku_log_fail "Required docker compose plugin is not installed" + fi + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT @@ -116,6 +120,10 @@ cmd-haproxy-start() { declare cmd="haproxy:start" [[ "$1" == "$cmd" ]] && shift 1 + if ! fn-is-compose-installed; then + dokku_log_fail "Required docker compose plugin is not installed" + fi + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT @@ -128,6 +136,10 @@ cmd-haproxy-stop() { declare cmd="haproxy:start" [[ "$1" == "$cmd" ]] && shift 1 + if ! fn-is-compose-installed; then + dokku_log_fail "Required docker compose plugin is not installed" + fi + local TMP_COMPOSE_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX") trap "rm -rf '$TMP_COMPOSE_FILE' >/dev/null" RETURN INT TERM EXIT From c14154e5f7c62454e8b768162f3b953d7c33185b Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 8 Nov 2022 23:51:47 -0500 Subject: [PATCH 12/24] feat: add a test for log levels --- tests/unit/haproxy.bats | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/haproxy.bats b/tests/unit/haproxy.bats index 440b9650e..ecf442c2a 100644 --- a/tests/unit/haproxy.bats +++ b/tests/unit/haproxy.bats @@ -30,6 +30,24 @@ teardown() { assert_output "$help_output" } +@test "(haproxy) log-level" { + run /bin/bash -c "dokku haproxy:set --global log-level DEBUG" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku haproxy:stop" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku haproxy:start" + echo "output: $output" + echo "status: $status" + assert_success +} + + @test "(haproxy) single domain" { run /bin/bash -c "dokku proxy:set $TEST_APP haproxy" echo "output: $output" From 35c6d61a04103f1e9f4cbac07c0da2156331fbf6 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 8 Nov 2022 23:52:38 -0500 Subject: [PATCH 13/24] feat: test all compose-based proxy implementations with a dockerfile app This allows the tests to run on arm systems. --- tests/apps/python/dockerfile.Dockerfile | 5 +++++ tests/unit/caddy.bats | 4 ++-- tests/unit/haproxy.bats | 4 ++-- tests/unit/test_helper.bash | 7 +++++++ tests/unit/traefik.bats | 4 ++-- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 tests/apps/python/dockerfile.Dockerfile diff --git a/tests/apps/python/dockerfile.Dockerfile b/tests/apps/python/dockerfile.Dockerfile new file mode 100644 index 000000000..383a6c616 --- /dev/null +++ b/tests/apps/python/dockerfile.Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.11.0-buster + +COPY . /app + +WORKDIR /app diff --git a/tests/unit/caddy.bats b/tests/unit/caddy.bats index 1ff68959c..2362ababd 100644 --- a/tests/unit/caddy.bats +++ b/tests/unit/caddy.bats @@ -38,7 +38,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success @@ -66,7 +66,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success diff --git a/tests/unit/haproxy.bats b/tests/unit/haproxy.bats index ecf442c2a..ef0df21a0 100644 --- a/tests/unit/haproxy.bats +++ b/tests/unit/haproxy.bats @@ -54,7 +54,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success @@ -82,7 +82,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 2c1f34a8b..7ca8aad7f 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -591,3 +591,10 @@ install_pack() { apt-get --yes install pack-cli fi } + +convert_to_dockerfile() { + local APP="$1" + local APP_REPO_DIR="$2" + [[ -z "$APP" ]] && local APP="$TEST_APP" + mv "$APP_REPO_DIR/dockerfile.Dockerfile" "$APP_REPO_DIR/Dockerfile" +} diff --git a/tests/unit/traefik.bats b/tests/unit/traefik.bats index 4c3b0e82c..a7a1ec1de 100644 --- a/tests/unit/traefik.bats +++ b/tests/unit/traefik.bats @@ -38,7 +38,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success @@ -66,7 +66,7 @@ teardown() { echo "status: $status" assert_success - run deploy_app + run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile echo "output: $output" echo "status: $status" assert_success From f02f19652ec55da8897ffbabb4d38f36ac51fd06 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 8 Nov 2022 23:53:13 -0500 Subject: [PATCH 14/24] fix: set default scheme correctly --- plugins/haproxy-vhosts/docker-args-process-deploy | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index d2fa97683..430fd188b 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -76,6 +76,7 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { # add the labels for haproxy here # prefer the https:443 mapping to http:80 mapping if [[ -n "$is_app_listening" ]] && [[ -n "$haproxy_domains" ]]; then + scheme="http" echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" >"$app_urls_path" xargs -I{} echo "$scheme://{}" <<<"$(echo "${app_domains}" | tr ' ' '\n' | sort -u)" >>"$app_urls_path" fi From ab9e2b101ee2d66949e40d0f17bc67e1ed815d89 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 8 Nov 2022 23:53:25 -0500 Subject: [PATCH 15/24] feat: install dev env tools correctly --- .../bin/{download-go-mod => setup-dev-env} | 21 +++++++++++++++++-- .devcontainer/devcontainer.json | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) rename .devcontainer/bin/{download-go-mod => setup-dev-env} (63%) diff --git a/.devcontainer/bin/download-go-mod b/.devcontainer/bin/setup-dev-env similarity index 63% rename from .devcontainer/bin/download-go-mod rename to .devcontainer/bin/setup-dev-env index 90973dfad..1ea3e9de2 100755 --- a/.devcontainer/bin/download-go-mod +++ b/.devcontainer/bin/setup-dev-env @@ -1,7 +1,11 @@ #!/usr/bin/env bash -main() { - declare PLUGIN_NAME="$1" +install-go-devtools() { + go install github.com/go-delve/delve/cmd/dlv@latest + go install honnef.co/go/tools/cmd/staticcheck@latest +} + +install-go-mods() { local plugin_root="/root/go/src/github.com/dokku/dokku/plugins" export GO111MODULE=off @@ -22,4 +26,17 @@ main() { popd >/dev/null || true } +setup-ci() { + local dokku_root="/root/go/src/github.com/dokku/dokku" + pushd "$plugin_root" >/dev/null || true + make ci-dependencies setup-deploy-tests + popd >/dev/null || true +} + +main () { + install-go-mods + install-go-devtools + setup-ci +} + main "$@" diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 0d5729f68..74304d1e7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -19,7 +19,7 @@ "source=${localWorkspaceFolder}/tmp/data/,target=/mnt/dokku/,type=bind" ], "overrideCommand": false, - "postCreateCommand": "download-go-mod && make setup-deploy-tests", + "postCreateCommand": "setup-dev-env", "runArgs": ["--init"], "workspaceFolder": "/root/go/src/github.com/dokku/dokku", "workspaceMount": "type=bind,source=${localWorkspaceFolder},target=/root/go/src/github.com/dokku/dokku,consistency=cached" From 62004eab564c2433384f6c5f2403f2c338c3ef10 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 06:11:10 -0500 Subject: [PATCH 16/24] chore: sync plugin version --- plugins/haproxy-vhosts/plugin.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/haproxy-vhosts/plugin.toml b/plugins/haproxy-vhosts/plugin.toml index 3eb682a2f..4784bbf16 100644 --- a/plugins/haproxy-vhosts/plugin.toml +++ b/plugins/haproxy-vhosts/plugin.toml @@ -1,4 +1,4 @@ [plugin] description = "dokku core haproxy-vhosts plugin" -version = "0.27.10" +version = "0.29.4" [plugin.config] From 537d8321e449a1f33dc25f750b65a47c7058f315 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 06:11:23 -0500 Subject: [PATCH 17/24] chore: update easy-haproxy version to latest --- plugins/haproxy-vhosts/internal-functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions index ff0595289..71bbcf114 100755 --- a/plugins/haproxy-vhosts/internal-functions +++ b/plugins/haproxy-vhosts/internal-functions @@ -42,7 +42,7 @@ fn-haproxy-template-compose-file() { } fn-haproxy-image() { - fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.2.0" + fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.3.0" } fn-haproxy-log-level() { From cbd3c43176c72b21ef564ea1268a94548cc64c19 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 06:22:02 -0500 Subject: [PATCH 18/24] feat: add letsencrypt support to haproxy plugin --- docs/networking/proxies/haproxy.md | 24 +++++++ plugins/haproxy-vhosts/command-functions | 2 + plugins/haproxy-vhosts/internal-functions | 12 +++- plugins/haproxy-vhosts/subcommands/set | 6 +- .../templates/compose.yml.sigil | 10 +++ tests/unit/haproxy.bats | 62 ++++++++++++++++++- 6 files changed, 111 insertions(+), 5 deletions(-) diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md index 9b7dfbc01..bf2291d1a 100644 --- a/docs/networking/proxies/haproxy.md +++ b/docs/networking/proxies/haproxy.md @@ -111,6 +111,30 @@ dokku haproxy:set --global log-level DEBUG After modifying, the Haproxy container will need to be restarted. +### SSL Configuration + +The haproxt plugin only supports automatic ssl certificates from it's letsencrypt integration. Managed certificates provided by the `certs` plugin are ignored. + +#### Enabling letsencrypt integration + +By default, letsencrypt is disabled and https port mappings are ignored. To enable, set the `letsencrypt-email` property with the `--global` flag: + +```shell +dokku haproxt:set --global letsencrypt-email automated@dokku.sh +``` + +After enabling, the Haproxy container will need to be restarted and apps will need to be rebuilt. All http requests will then be redirected to https. + +#### Customizing the letsencrypt server + +The letsencrypt integration is set to the production letsencrypt server by default. To change this, set the `letsencrypt-server` property with the `--global` flag: + +```shell +dokku haproxt:set --global letsencrypt-server https://acme-staging-v02.api.letsencrypt.org/directory +``` + +After enabling, the Haproxy container will need to be restarted and apps will need to be rebuilt to retrieve certificates from the new server. + ## Displaying Haproxy reports for an app You can get a report about the app's Haproxy config using the `haproxy:report` command: diff --git a/plugins/haproxy-vhosts/command-functions b/plugins/haproxy-vhosts/command-functions index 286a3bec0..1854569b7 100755 --- a/plugins/haproxy-vhosts/command-functions +++ b/plugins/haproxy-vhosts/command-functions @@ -36,6 +36,8 @@ cmd-haproxy-report-single() { verify_app_name "$APP" local flag_map=( "--haproxy-image: $(fn-haproxy-image)" + "--haproxy-letsencrypt-email: $(fn-haproxy-letsencrypt-email)" + "--haproxy-letsencrypt-server: $(fn-haproxy-letsencrypt-server)" "--haproxy-log-level: $(fn-haproxy-log-level)" ) diff --git a/plugins/haproxy-vhosts/internal-functions b/plugins/haproxy-vhosts/internal-functions index 71bbcf114..508869054 100755 --- a/plugins/haproxy-vhosts/internal-functions +++ b/plugins/haproxy-vhosts/internal-functions @@ -36,7 +36,9 @@ fn-haproxy-template-compose-file() { fi local SIGIL_PARAMS=(HAPROXY_IMAGE="$(fn-haproxy-image)" - HAPROXY_LOG_LEVEL="$(fn-haproxy-log-level)") + HAPROXY_LOG_LEVEL="$(fn-haproxy-log-level)" + HAPROXY_LETSENCRYPT_EMAIL="$(fn-haproxy-letsencrypt-email)" + HAPROXY_LETSENCRYPT_SERVER="$(fn-haproxy-letsencrypt-server)") sigil -f "$COMPOSE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >"$OUTPUT_PATH" } @@ -50,3 +52,11 @@ fn-haproxy-log-level() { log_level="$(fn-plugin-property-get-default "haproxy" "--global" "log-level" "ERROR")" echo "${log_level^^}" } + +fn-haproxy-letsencrypt-email() { + fn-plugin-property-get-default "haproxy" "--global" "letsencrypt-email" "" +} + +fn-haproxy-letsencrypt-server() { + fn-plugin-property-get-default "haproxy" "--global" "letsencrypt-server" "https://acme-v02.api.letsencrypt.org/directory" +} diff --git a/plugins/haproxy-vhosts/subcommands/set b/plugins/haproxy-vhosts/subcommands/set index d893c6c6e..6406ea025 100755 --- a/plugins/haproxy-vhosts/subcommands/set +++ b/plugins/haproxy-vhosts/subcommands/set @@ -9,13 +9,13 @@ cmd-haproxy-set() { declare cmd="haproxy:set" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" KEY="$2" VALUE="$3" - local VALID_KEYS=("image" "log-level") - local GLOBAL_KEYS=("image" "log-level") + local VALID_KEYS=("image" "log-level" "letsencrypt-email" "letsencrypt-server") + local GLOBAL_KEYS=("image" "log-level" "letsencrypt-email" "letsencrypt-server") [[ -z "$KEY" ]] && dokku_log_fail "No key specified" if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then - dokku_log_fail "Invalid key specified, valid keys include: image log-level" + dokku_log_fail "Invalid key specified, valid keys include: image log-level letsencrypt-email letsencrypt-server" fi if ! fn-in-array "$KEY" "${GLOBAL_KEYS[@]}"; then diff --git a/plugins/haproxy-vhosts/templates/compose.yml.sigil b/plugins/haproxy-vhosts/templates/compose.yml.sigil index 49e714123..6b9890a32 100644 --- a/plugins/haproxy-vhosts/templates/compose.yml.sigil +++ b/plugins/haproxy-vhosts/templates/compose.yml.sigil @@ -9,11 +9,21 @@ services: - EASYHAPROXY_DISCOVER=docker - EASYHAPROXY_LABEL_PREFIX=haproxy - EASYHAPROXY_LOG_LEVEL={{ $.HAPROXY_LOG_LEVEL }} + - CERTBOT_LOG_LEVEL={{ $.HAPROXY_LOG_LEVEL }} + - HAPROXY_LOG_LEVEL={{ $.HAPROXY_LOG_LEVEL }} + {{ if $.HAPROXY_LETSENCRYPT_EMAIL }} + - EASYHAPROXY_LETSENCRYPT_EMAIL={{ $.HAPROXY_LETSENCRYPT_EMAIL }}" + - EASYHAPROXY_LETSENCRYPT_SERVER={{ $.HAPROXY_LETSENCRYPT_SERVER }} + {{ end }} + - HAPROXY_STATS_PORT=false network_mode: bridge ports: - "80:80" + {{ if $.HAPROXY_LETSENCRYPT_EMAIL }} + - "443:443" + {{ end }} restart: unless-stopped diff --git a/tests/unit/haproxy.bats b/tests/unit/haproxy.bats index ef0df21a0..e63526f59 100644 --- a/tests/unit/haproxy.bats +++ b/tests/unit/haproxy.bats @@ -5,6 +5,8 @@ load test_helper setup() { global_setup dokku nginx:stop + dokku haproxy:set --global letsencrypt-server https://acme-staging-v02.api.letsencrypt.org/directory + dokku haproxy:set --global letsencrypt-email dokku haproxy:start create_app } @@ -47,7 +49,6 @@ teardown() { assert_success } - @test "(haproxy) single domain" { run /bin/bash -c "dokku proxy:set $TEST_APP haproxy" echo "output: $output" @@ -99,3 +100,62 @@ teardown() { assert_success assert_output "python/http.server" } + +@test "(haproxy) ssl" { + run /bin/bash -c "dokku builder-herokuish:set $TEST_APP allowed true" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku proxy:set $TEST_APP haproxy" + echo "output: $output" + echo "status: $status" + assert_success + + run deploy_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy\" }}'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$TEST_APP.dokku.me:80" + + run /bin/bash -c "dokku haproxy:set --global letsencrypt-email test@example.com" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku haproxy:stop" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku haproxy:start" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ps:rebuild $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ps:inspect $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy\" }}'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$TEST_APP.dokku.me" + + run /bin/bash -c "dokku proxy:report $TEST_APP --proxy-port-map" + echo "output: $output" + echo "status: $status" + assert_output "http:80:5000" +} From 203dfe249b7a81d0c7abd7fee5cd05c4298a561d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 06:24:27 -0500 Subject: [PATCH 19/24] fix: run shfmt --- .devcontainer/bin/setup-dev-env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/bin/setup-dev-env b/.devcontainer/bin/setup-dev-env index 1ea3e9de2..fab9f2b30 100755 --- a/.devcontainer/bin/setup-dev-env +++ b/.devcontainer/bin/setup-dev-env @@ -33,7 +33,7 @@ setup-ci() { popd >/dev/null || true } -main () { +main() { install-go-mods install-go-devtools setup-ci From 78d66dc68a908262432af21e2b6bf7e57650e04a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 17:40:05 -0500 Subject: [PATCH 20/24] docs: note that https:443 is now supported --- docs/networking/proxies/haproxy.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md index bf2291d1a..8de8fbc59 100644 --- a/docs/networking/proxies/haproxy.md +++ b/docs/networking/proxies/haproxy.md @@ -25,7 +25,7 @@ The Haproxy plugin has specific rules for routing requests: - Haproxy integration is exposed via docker labels attached to containers. Changes in labels require either app deploys or rebuilds. - While Haproxy will respect labels associated with other containers, only `web` containers have Haproxy labels injected by the plugin. -- Only `http:80` port mappings are supported at this time. +- Only `http:80` and `https:443` port mappings are supported at this time. - Requests are routed as soon as the container is running and passing healthchecks. ### Switching to Haproxy From 070f643a01fe3cc8a38bcc6d9780746fae4913a3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 17:40:41 -0500 Subject: [PATCH 21/24] refactor: drop url management and sync logic with traefik plugin --- .../haproxy-vhosts/docker-args-process-deploy | 105 +++++++++++++----- 1 file changed, 75 insertions(+), 30 deletions(-) diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index 430fd188b..f343879e0 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -10,8 +10,7 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { declare APP="$1" IMAGE_SOURCE_TYPE="$2" IMAGE_TAG="$3" PROC_TYPE="$4" CONTAINER_INDEX="$5" local app_domains haproxy_domains is_app_listening letsencrypt_email output proxy_container_port proxy_host_port port_map proxy_port_map proxy_scheme proxy_schemes scheme tls_internal local proxy_container_http_port proxy_container_http_port_candidate proxy_host_http_port_candidate - local proxy_container_https_port proxy_container_https_port_candidate proxy_host_https_port_candidate - local app_urls_path="$DOKKU_ROOT/$APP/URLS" + local proxy_container_https_port proxy_container_https_port_candidate proxy_host_https_port_candidate proxy_redirect_ssl local STDIN=$(cat) if [[ "$PROC_TYPE" != "web" ]]; then @@ -36,14 +35,10 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { # ensure we have a port mapping plugn trigger proxy-configure-ports "$APP" - app_domains="$(plugn trigger domains-list "$APP")" - if [[ -n "$app_domains" ]]; then - haproxy_domains="$(echo "$app_domains" | xargs)" - haproxy_domains="${haproxy_domains// /,}" - fi - - output="" # gather port mapping information + # we only support proxying a single port for http and https listeners + # so this block parses the port mappings and tries to find the correct + # mapping to expose is_app_listening="false" proxy_port_map="$(plugn trigger config-get "$APP" DOKKU_PROXY_PORT_MAP)" for port_map in $proxy_port_map; do @@ -51,34 +46,84 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { proxy_host_port="$(awk -F ':' '{ print $2 }' <<<"$port_map")" proxy_container_port="$(awk -F ':' '{ print $3 }' <<<"$port_map")" - # ignore https for now - if [[ "$proxy_scheme" != "http" ]] && [[ "$proxy_scheme" != "tcp" ]]; then - continue + if [[ "$proxy_scheme" == "http" ]]; then + is_app_listening="true" + if [[ -z "$proxy_container_http_port_candidate" ]]; then + proxy_container_http_port_candidate="$proxy_container_port" + proxy_host_http_port_candidate="$proxy_host_port" + fi + + if [[ "$proxy_host_port" == "80" ]] && [[ -z "$proxy_container_http_port" ]]; then + proxy_container_http_port="$proxy_container_port" + fi fi - # ignore 443 for now - if [[ "$proxy_host_port" == "443" ]]; then - continue - fi + if [[ "$proxy_scheme" == "https" ]]; then + is_app_listening="true" + if [[ -z "$proxy_container_https_port_candidate" ]]; then + proxy_container_https_port_candidate="$proxy_container_port" + proxy_host_https_port_candidate="$proxy_host_port" + fi - # ignore everything but 80 for now - if [[ "$proxy_host_port" != "80" ]]; then - continue + if [[ "$proxy_host_port" == "443" ]] && [[ -z "$proxy_container_https_port" ]]; then + proxy_container_https_port="$proxy_container_port" + fi fi - - is_app_listening="true" - output="$output --label haproxy.$APP-$PROC_TYPE.mode=$proxy_scheme" - output="$output --label haproxy.$APP-$PROC_TYPE.port=$proxy_host_port" - output="$output --label haproxy.$APP-$PROC_TYPE.localport=$proxy_container_port" - output="$output --label haproxy.$APP-$PROC_TYPE.host=$haproxy_domains" done + letsencrypt_email="$(fn-haproxy-letsencrypt-email)" + if [[ -n "$letsencrypt_email" ]] && [[ -z "$proxy_container_https_port" ]]; then + proxy_container_https_port_candidate="$proxy_container_http_port_candidate" + proxy_host_https_port_candidate="$proxy_host_http_port_candidate" + if [[ -n "$proxy_container_http_port" ]]; then + proxy_container_https_port_candidate="$proxy_container_http_port" + proxy_host_http_port_candidate=443 + fi + fi + # add the labels for haproxy here - # prefer the https:443 mapping to http:80 mapping - if [[ -n "$is_app_listening" ]] && [[ -n "$haproxy_domains" ]]; then - scheme="http" - echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" >"$app_urls_path" - xargs -I{} echo "$scheme://{}" <<<"$(echo "${app_domains}" | tr ' ' '\n' | sort -u)" >>"$app_urls_path" + # any `http:80` port mapping is treated as a `http` traefik entrypoint + # any `https:443` port mapping is treated as a `https` traefik entrypoint + if [[ -n "$is_app_listening" ]]; then + app_domains="$(plugn trigger domains-list "$APP")" + if [[ -n "$app_domains" ]]; then + haproxy_domains="$(echo "$app_domains" | xargs)" + haproxy_domains="${haproxy_domains// /,}" + fi + + if [[ -n "$proxy_container_http_port" ]] || [[ -n "$proxy_container_http_port_candidate" ]]; then + if [[ -z "$proxy_container_http_port" ]]; then + dokku_log_warn "Warning: http:80 port mapping not found" + dokku_log_warn "Utilizing first http port mapping, http:$proxy_host_http_port_candidate:$proxy_container_http_port_candidate" + proxy_container_http_port="$proxy_container_http_port_candidate" + fi + + output="$output --label haproxy.$APP-$PROC_TYPE.localport=$proxy_container_http_port" + output="$output --label haproxy.$APP-$PROC_TYPE.mode=http" + output="$output --label haproxy.$APP-$PROC_TYPE.port=80" + if [[ -n "$haproxy_domains" ]]; then + output="$output --label haproxy.$APP-$PROC_TYPE.host=$haproxy_domains" + fi + fi + + if [[ -n "$proxy_container_https_port" ]] || [[ -n "$proxy_container_https_port_candidate" ]]; then + if [[ -z "$proxy_container_https_port" ]]; then + dokku_log_warn "Warning: https:443 port mapping not found" + dokku_log_warn "Utilizing first https port mapping, http:$proxy_host_https_port_candidate:$proxy_container_https_port_candidate" + proxy_container_https_port="$proxy_container_https_port_candidate" + fi + + + output="$output --label haproxy.$APP-$PROC_TYPE.redirect_ssl=true" + output="$output --label haproxy.$APP-$PROC_TYPE-https.localport=$proxy_container_https_port" + output="$output --label haproxy.$APP-$PROC_TYPE-https.letsencrypt=true" + output="$output --label haproxy.$APP-$PROC_TYPE-https.mode=http" + output="$output --label haproxy.$APP-$PROC_TYPE-https.port=443" + + if [[ -n "$haproxy_domains" ]]; then + output="$output --label haproxy.$APP-$PROC_TYPE-https.host=$haproxy_domains" + fi + fi fi echo -n "$STDIN$output" From 710b1db6bea209a1471ec6178c3bcd2f255ac925 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 17:42:13 -0500 Subject: [PATCH 22/24] chore: run shfmt --- plugins/haproxy-vhosts/docker-args-process-deploy | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index f343879e0..2688b7cb5 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -113,7 +113,6 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { proxy_container_https_port="$proxy_container_https_port_candidate" fi - output="$output --label haproxy.$APP-$PROC_TYPE.redirect_ssl=true" output="$output --label haproxy.$APP-$PROC_TYPE-https.localport=$proxy_container_https_port" output="$output --label haproxy.$APP-$PROC_TYPE-https.letsencrypt=true" From 4ae64b23c6f07f91f83f66cd2716266f974b0fe7 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 20:55:20 -0500 Subject: [PATCH 23/24] fix: update test to get the correct label for testing if ssl redirects are working properly --- plugins/haproxy-vhosts/docker-args-process-deploy | 2 ++ tests/unit/haproxy.bats | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index 2688b7cb5..f08713a5e 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -122,6 +122,8 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { if [[ -n "$haproxy_domains" ]]; then output="$output --label haproxy.$APP-$PROC_TYPE-https.host=$haproxy_domains" fi + else + output="$output --label haproxy.$APP-$PROC_TYPE.redirect_ssl=false" fi fi diff --git a/tests/unit/haproxy.bats b/tests/unit/haproxy.bats index e63526f59..cf74e6f67 100644 --- a/tests/unit/haproxy.bats +++ b/tests/unit/haproxy.bats @@ -117,11 +117,11 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy\" }}'" + run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy.$TEST_APP-web.redirect_ssl\" }}'" echo "output: $output" echo "status: $status" assert_success - assert_output "$TEST_APP.dokku.me:80" + assert_output "false" run /bin/bash -c "dokku haproxy:set --global letsencrypt-email test@example.com" echo "output: $output" @@ -148,11 +148,11 @@ teardown() { echo "status: $status" assert_success - run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy\" }}'" + run /bin/bash -c "docker inspect $TEST_APP.web.1 --format '{{ index .Config.Labels \"haproxy.$TEST_APP-web.redirect_ssl\" }}'" echo "output: $output" echo "status: $status" assert_success - assert_output "$TEST_APP.dokku.me" + assert_output "true" run /bin/bash -c "dokku proxy:report $TEST_APP --proxy-port-map" echo "output: $output" From d1c4cc89bfc3f6c6ed305f52f228cdd087f17754 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 11 Feb 2023 20:55:59 -0500 Subject: [PATCH 24/24] docs: fix plugin name in haproxy docs --- docs/networking/proxies/haproxy.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/networking/proxies/haproxy.md b/docs/networking/proxies/haproxy.md index 8de8fbc59..d4e64d8c6 100644 --- a/docs/networking/proxies/haproxy.md +++ b/docs/networking/proxies/haproxy.md @@ -113,14 +113,14 @@ After modifying, the Haproxy container will need to be restarted. ### SSL Configuration -The haproxt plugin only supports automatic ssl certificates from it's letsencrypt integration. Managed certificates provided by the `certs` plugin are ignored. +The haproxy plugin only supports automatic ssl certificates from it's letsencrypt integration. Managed certificates provided by the `certs` plugin are ignored. #### Enabling letsencrypt integration By default, letsencrypt is disabled and https port mappings are ignored. To enable, set the `letsencrypt-email` property with the `--global` flag: ```shell -dokku haproxt:set --global letsencrypt-email automated@dokku.sh +dokku haproxy:set --global letsencrypt-email automated@dokku.sh ``` After enabling, the Haproxy container will need to be restarted and apps will need to be rebuilt. All http requests will then be redirected to https. @@ -130,7 +130,7 @@ After enabling, the Haproxy container will need to be restarted and apps will ne The letsencrypt integration is set to the production letsencrypt server by default. To change this, set the `letsencrypt-server` property with the `--global` flag: ```shell -dokku haproxt:set --global letsencrypt-server https://acme-staging-v02.api.letsencrypt.org/directory +dokku haproxy:set --global letsencrypt-server https://acme-staging-v02.api.letsencrypt.org/directory ``` After enabling, the Haproxy container will need to be restarted and apps will need to be rebuilt to retrieve certificates from the new server.