feat: first pass at haproxy plugin implementation

This commit is contained in:
Jose Diaz-Gonzalez
2022-08-17 18:37:32 -04:00
parent 89cf4a5e76
commit 237aae9eec
19 changed files with 587 additions and 2 deletions

View File

@@ -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`.

View File

@@ -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 [<app>] [<flag>] # Displays a haproxy report for one or more apps
haproxy:logs [--num num] [--tail] # Display haproxy log output
haproxy:set <app> <property> (<value>) # Set or clear an haproxy property for an app
haproxy:show-config <app> # 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
```

View File

@@ -176,6 +176,7 @@
<a href="/{{NAME}}/networking/proxy-management/" class="list-group-item">Proxy Management</a>
<a href="/{{NAME}}/networking/proxies/caddy/" class="list-group-item">Caddy Proxy</a>
<a href="/{{NAME}}/networking/proxies/haproxy/" class="list-group-item">Haproxy Proxy</a>
<a href="/{{NAME}}/networking/proxies/nginx/" class="list-group-item">Nginx Proxy</a>
<a href="/{{NAME}}/networking/proxies/traefik/" class="list-group-item">Traefik Proxy</a>

View File

@@ -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
}

15
plugins/haproxy-vhosts/commands Executable file
View File

@@ -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

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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 <<help_desc
$plugin_name, $plugin_description
help_desc
fi
}
fn-help-content() {
declare desc="return help content"
cat <<help_content
haproxy:report [<app>] [<flag>], Displays an haproxy report for one or more apps
haproxy:set <app> <property> (<value>), Set or clear an haproxy property for an app
haproxy:show-config <app>, Display haproxy compose config
haproxy:start, Starts the haproxy server
haproxy:stop, Stops the haproxy server
help_content
}

17
plugins/haproxy-vhosts/install Executable file
View File

@@ -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 "$@"

View File

@@ -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"
}

View File

@@ -0,0 +1,4 @@
[plugin]
description = "dokku core caddy-vhosts plugin"
version = "0.27.10"
[plugin.config]

View File

@@ -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"

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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 "$@"

View File

@@ -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"