Merge pull request #5308 from dokku/haproxy-plugin

This commit is contained in:
josegonzalez
2023-02-12 00:02:09 -05:00
committed by GitHub
27 changed files with 915 additions and 9 deletions

View File

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

View File

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

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,170 @@
# 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
```
## 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.
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` 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
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
### 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.
### SSL Configuration
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 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.
#### 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 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.
## 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>

3
dokku
View File

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

View File

@@ -0,0 +1,150 @@
#!/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)"
"--haproxy-letsencrypt-email: $(fn-haproxy-letsencrypt-email)"
"--haproxy-letsencrypt-server: $(fn-haproxy-letsencrypt-server)"
"--haproxy-log-level: $(fn-haproxy-log-level)"
)
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
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
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
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
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
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
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,133 @@
#!/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 proxy_redirect_ssl
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"
# 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
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")"
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
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
if [[ "$proxy_host_port" == "443" ]] && [[ -z "$proxy_container_https_port" ]]; then
proxy_container_https_port="$proxy_container_port"
fi
fi
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
# 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
else
output="$output --label haproxy.$APP-$PROC_TYPE.redirect_ssl=false"
fi
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 the haproxy proxy integration"
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-haproxy-install "$@"

View File

@@ -0,0 +1,62 @@
#!/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)"
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"
}
fn-haproxy-image() {
fn-plugin-property-get-default "haproxy" "--global" "image" "byjg/easy-haproxy:4.3.0"
}
fn-haproxy-log-level() {
local 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"
}

View File

@@ -0,0 +1,4 @@
[plugin]
description = "dokku core haproxy-vhosts plugin"
version = "0.29.4"
[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" "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 letsencrypt-email letsencrypt-server"
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,31 @@
---
version: "3.7"
services:
haproxy:
image: "{{ $.HAPROXY_IMAGE }}"
environment:
- 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
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"

View File

@@ -0,0 +1,5 @@
FROM python:3.11.0-buster
COPY . /app
WORKDIR /app

View File

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

161
tests/unit/haproxy.bats Normal file
View File

@@ -0,0 +1,161 @@
#!/usr/bin/env bats
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
}
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) 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"
echo "status: $status"
assert_success
run deploy_app python dokku@dokku.me:$TEST_APP convert_to_dockerfile
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 python dokku@dokku.me:$TEST_APP convert_to_dockerfile
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"
}
@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.$TEST_APP-web.redirect_ssl\" }}'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "false"
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.$TEST_APP-web.redirect_ssl\" }}'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "true"
run /bin/bash -c "dokku proxy:report $TEST_APP --proxy-port-map"
echo "output: $output"
echo "status: $status"
assert_output "http:80:5000"
}

View File

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

View File

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