From 64dffa74e7d26772e106f59add7363de4888b40a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 2 Jul 2023 19:29:05 -0400 Subject: [PATCH 1/6] feat: expose get_available_port as a hook for use in the ports plugin While this should ideally live in the ports plugin, this is the smallest lift we can do to expose the functionality --- plugins/common/ports-get-available | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 plugins/common/ports-get-available diff --git a/plugins/common/ports-get-available b/plugins/common/ports-get-available new file mode 100755 index 00000000000..f7d9893334a --- /dev/null +++ b/plugins/common/ports-get-available @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +set -eo pipefail +[[ $DOKKU_TRACE ]] && set -x + +get_available_port "$@" From 26b4462a1f4db2177d2596278bc8c33017d33dbc Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 2 Jul 2023 19:37:50 -0400 Subject: [PATCH 2/6] refactor: deprecate proxy-configure-ports plugin trigger in favor of ports-configure This also rewrites the logic to be in golang, which will make it easier to refactor portions of it to use the property system going forward. --- docs/appendices/0.31.0-migration-guide.md | 5 +- docs/development/plugin-triggers.md | 17 +++ plugins/20_events/ports-configure | 1 + .../caddy-vhosts/docker-args-process-deploy | 2 +- .../haproxy-vhosts/docker-args-process-deploy | 2 +- plugins/nginx-vhosts/functions | 2 +- plugins/ports/Makefile | 2 +- plugins/ports/proxy-configure-ports | 54 +------- plugins/ports/src/triggers/triggers.go | 3 + plugins/ports/triggers.go | 119 ++++++++++++++++++ .../traefik-vhosts/docker-args-process-deploy | 2 +- 11 files changed, 150 insertions(+), 59 deletions(-) create mode 120000 plugins/20_events/ports-configure diff --git a/docs/appendices/0.31.0-migration-guide.md b/docs/appendices/0.31.0-migration-guide.md index dd66f2196cb..b07d22ca8a1 100644 --- a/docs/appendices/0.31.0-migration-guide.md +++ b/docs/appendices/0.31.0-migration-guide.md @@ -7,12 +7,13 @@ - The `traefik` integration now mounts config to `/data` instead of the path `/acme.json`, fixing permissions issues under certain architectures. To take advantage of the new functionality, the traefik container should be stopped (via `dokku traefik:stop`) and then started (via `dokku traefik:start`). - Users no longer need to clear the `source-image` git property when transitioning from image-based deploys (`git:from-image` and `git:load-image`) to other deployment methods (git push, `git:from-archive`, `git:sync`). - For deploys via the `git:from-image` and `git:load-image` commands, the `CHECKS` file is now extracted from the configured `WORKDIR` property of the image. For all other deploys - git push, `git:from-archive`, `git:sync` - will have the `CHECKS` extracted directly from the source code. The filename in both cases is `CHECKS` and cannot be modified. -- The `common#get_app_raw_tcp_ports()` function has been deprecated and will be removed in the next release. Users should avoid interacting with this function for dockerfile ports and instead use the `ports-get` plugin trigger for fetching ports for an app. -- The `common#get_available_port()` function has been deprecated and will be removed in the next release. Users should avoid interacting with this function and instead use the `ports-get-available` plugin trigger for fetching an available port. ## Deprecations - The `proxy:ports*` commands have been replaced with the new `ports` plugin. Users will be able to use the old `proxy:ports*` commands for a single minor release, and they will be removed in the next minor release. +- The `common#get_app_raw_tcp_ports()` function has been deprecated and will be removed in the next release. Users should avoid interacting with this function for dockerfile ports and instead use the `ports-get` plugin trigger for fetching ports for an app. +- The `common#get_available_port()` function has been deprecated and will be removed in the next release. Users should avoid interacting with this function and instead use the `ports-get-available` plugin trigger for fetching an available port. +- The `proxy-configure-ports` plugin trigger has been deprecated and will be removed in the next release. Users should instead trigger the `ports-configure` plugin trigger. ## Un-Deprecations diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index f1fc0c5f6ce..a3ddf285f53 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -1256,6 +1256,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `ports-configure` + +- Description: Configures the port mapping +- Invoked by: `internally triggered by proxy plugins` +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + ### `ports-dockerfile-raw-tcp-ports` > Warning: This trigger is for internal use and will be removed in a future release. Do not use it in your codebase. @@ -1934,6 +1949,8 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x ### `proxy-configure-ports` +> Warning: Deprecated, please use `ports-configure` instead + - Description: Configures the port mapping - Invoked by: `internally triggered by proxy plugins` - Arguments: `$APP` diff --git a/plugins/20_events/ports-configure b/plugins/20_events/ports-configure new file mode 120000 index 00000000000..5178a749ff6 --- /dev/null +++ b/plugins/20_events/ports-configure @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/caddy-vhosts/docker-args-process-deploy b/plugins/caddy-vhosts/docker-args-process-deploy index 1d1fdd28ede..65a9c97963c 100755 --- a/plugins/caddy-vhosts/docker-args-process-deploy +++ b/plugins/caddy-vhosts/docker-args-process-deploy @@ -30,7 +30,7 @@ trigger-caddy-vhosts-docker-args-process-deploy() { fi # ensure we have a port mapping - plugn trigger proxy-configure-ports "$APP" + plugn trigger ports-configure "$APP" # gather port mapping information # we only support proxying a single port for http and https listeners diff --git a/plugins/haproxy-vhosts/docker-args-process-deploy b/plugins/haproxy-vhosts/docker-args-process-deploy index 9546d7280c6..321b403fe23 100755 --- a/plugins/haproxy-vhosts/docker-args-process-deploy +++ b/plugins/haproxy-vhosts/docker-args-process-deploy @@ -33,7 +33,7 @@ trigger-haproxy-vhosts-docker-args-process-deploy() { plugn trigger domains-setup "$APP" >/dev/null # ensure we have a port mapping - plugn trigger proxy-configure-ports "$APP" + plugn trigger ports-configure "$APP" # gather port mapping information # we only support proxying a single port for http and https listeners diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 687a09f6bd7..9f65f1be207 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -318,7 +318,7 @@ nginx_build_config() { fi # setup nginx listen ports - plugn trigger proxy-configure-ports "$APP" + plugn trigger ports-configure "$APP" local PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT) local PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT) local PROXY_PORT_MAP=$(plugn trigger ports-get "$APP") diff --git a/plugins/ports/Makefile b/plugins/ports/Makefile index 962d514703e..189f7b8991b 100644 --- a/plugins/ports/Makefile +++ b/plugins/ports/Makefile @@ -1,5 +1,5 @@ SUBCOMMANDS = subcommands/list subcommands/add subcommands/clear subcommands/remove subcommands/set subcommands/report -TRIGGERS = triggers/ports-clear triggers/ports-dockerfile-raw-tcp-ports triggers/ports-get triggers/ports-get-available triggers/post-certs-remove triggers/post-certs-update triggers/report +TRIGGERS = triggers/ports-clear triggers/ports-configure triggers/ports-dockerfile-raw-tcp-ports triggers/ports-get triggers/ports-get-available triggers/post-certs-remove triggers/post-certs-update triggers/report BUILD = commands subcommands triggers PLUGIN_NAME = ports diff --git a/plugins/ports/proxy-configure-ports b/plugins/ports/proxy-configure-ports index 4f56e11db13..64910658803 100755 --- a/plugins/ports/proxy-configure-ports +++ b/plugins/ports/proxy-configure-ports @@ -1,6 +1,5 @@ #!/usr/bin/env bash source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" -source "$PLUGIN_AVAILABLE_PATH/config/functions" set -eo pipefail [[ $DOKKU_TRACE ]] && set -x @@ -8,58 +7,9 @@ trigger-ports-proxy-configure-ports() { declare desc="ports proxy-configure-ports plugin trigger" declare trigger="proxy-configure-ports" declare APP="$1" - local RAW_TCP_PORTS="$(plugn trigger ports-dockerfile-raw-tcp-ports "$APP" | xargs)" - local DOKKU_PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT) - local DOKKU_PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT) - local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP) - local IS_APP_VHOST_ENABLED=true - local UPSTREAM_PORT="5000" - plugn trigger domains-vhost-enabled "$APP" 2>/dev/null || IS_APP_VHOST_ENABLED=false - - if [[ -z "$DOKKU_PROXY_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then - if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then - dokku_log_info1 "No port set, setting to random open high port" - local PROXY_PORT=$(plugn trigger ports-get-available) - else - local PROXY_PORT=$(config_get --global DOKKU_PROXY_PORT) - PROXY_PORT=${PROXY_PORT:=80} - fi - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT="$PROXY_PORT" - fi - if [[ -z "$DOKKU_PROXY_SSL_PORT" ]]; then - if [[ "$(plugn trigger certs-exists "$APP")" == "true" ]]; then - local PROXY_SSL_PORT=$(config_get --global DOKKU_PROXY_SSL_PORT) - PROXY_SSL_PORT=${PROXY_SSL_PORT:=443} - if [[ -z "$RAW_TCP_PORTS" ]] && [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then - dokku_log_info1 "No ssl port set, setting to random open high port" - PROXY_SSL_PORT=$(plugn trigger ports-get-available) - fi - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_SSL_PORT="$PROXY_SSL_PORT" - fi - fi - if [[ -z "$DOKKU_PROXY_PORT_MAP" ]]; then - if [[ -n "$RAW_TCP_PORTS" ]]; then - local RAW_TCP_PORT - for RAW_TCP_PORT in $RAW_TCP_PORTS; do - local PROXY_PORT_MAP+=" http:${RAW_TCP_PORT}:${RAW_TCP_PORT} " - done - else - local PROXY_PORT=${PROXY_PORT:-$DOKKU_PROXY_PORT} - local PROXY_SSL_PORT=${PROXY_SSL_PORT:-$DOKKU_PROXY_SSL_PORT} - [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]] && local UPSTREAM_PORT="$(<"$DOKKU_ROOT/$APP/PORT.web.1")" - if [[ -n "$PROXY_PORT" ]] && [[ -n "$PROXY_SSL_PORT" ]]; then - local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT https:${PROXY_SSL_PORT}:$UPSTREAM_PORT " - elif [[ -n "$PROXY_PORT" ]]; then - local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT " - fi - fi - if [[ -n "$PROXY_PORT_MAP" ]]; then - local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)" - local PROXY_PORT_MAP+=" $(merge_dedupe_list "$(remove_val_from_list "$PORT_MAP" "$DOKKU_PROXY_PORT_MAP" " ")" " ") " - DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT_MAP="$PROXY_PORT_MAP" - fi - fi + dokku_log_warn "Deprecated: please use the 'ports-configure' plugin trigger instead" + plugn trigger ports-configure "$APP" } trigger-ports-proxy-configure-ports "$@" diff --git a/plugins/ports/src/triggers/triggers.go b/plugins/ports/src/triggers/triggers.go index bb125943c04..6551298da45 100644 --- a/plugins/ports/src/triggers/triggers.go +++ b/plugins/ports/src/triggers/triggers.go @@ -21,6 +21,9 @@ func main() { case "ports-clear": appName := flag.Arg(0) err = ports.TriggerPortsClear(appName) + case "ports-configure": + appName := flag.Arg(0) + err = ports.TriggerPortsConfigure(appName) case "ports-dockerfile-raw-tcp-ports": appName := flag.Arg(0) err = ports.TriggerPortsDockerfileRawTCPPorts(appName) diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index ea842f83a45..e3fb05ae8c9 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -2,6 +2,8 @@ package ports import ( "fmt" + "path/filepath" + "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" @@ -12,6 +14,123 @@ func TriggerPortsClear(appName string) error { return clearPorts(appName) } +// TriggerPortsConfigure ensures we have a port mapping +func TriggerPortsConfigure(appName string) error { + rawTCPPorts := getDockerfileRawTCPPorts(appName) + + b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT"}...) + dokkuProxyPort := strings.TrimSpace(string(b[:])) + + b, _ = common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_SSL_PORT"}...) + dokkuProxySSLPort := strings.TrimSpace(string(b[:])) + + b, _ = common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT_MAP"}...) + portMapString := strings.TrimSpace(string(b[:])) + + isAppVhostEnabled := true + upstreamPort := "5000" + + if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil { + isAppVhostEnabled = false + } + + if dokkuProxyPort == "" && len(rawTCPPorts) == 0 { + proxyPort := "80" + if !isAppVhostEnabled { + common.LogInfo1("No port set, setting to random open high port") + b, _ = common.PlugnTriggerOutput("ports-get-available", []string{}...) + proxyPort = strings.TrimSpace(string(b[:])) + } else { + b, _ = common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_PORT"}...) + proxyPort = strings.TrimSpace(string(b[:])) + } + + if proxyPort == "" { + proxyPort = "80" + } + + dokkuProxyPort = proxyPort + err := common.EnvWrap(func() error { + entries := map[string]string{ + "DOKKU_PROXY_PORT": proxyPort, + } + return config.SetMany(appName, entries, false) + }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) + if err != nil { + return err + } + } + + if dokkuProxySSLPort == "" { + b, _ = common.PlugnTriggerOutput("certs-exists", []string{appName}...) + certsExists := strings.TrimSpace(string(b[:])) + if certsExists == "true" { + b, _ = common.PlugnTriggerOutput("config-get-global", []string{"PROXY_SSL_PORT"}...) + proxySSLPort := strings.TrimSpace(string(b[:])) + if proxySSLPort == "" { + proxySSLPort = "443" + } + + if len(rawTCPPorts) == 0 && !isAppVhostEnabled { + common.LogInfo1("No ssl port set, setting to random open high port") + b, _ = common.PlugnTriggerOutput("ports-get-available", []string{}...) + proxySSLPort = strings.TrimSpace(string(b[:])) + } + + dokkuProxySSLPort = proxySSLPort + err := common.EnvWrap(func() error { + entries := map[string]string{ + "DOKKU_PROXY_SSL_PORT": proxySSLPort, + } + return config.SetMany(appName, entries, false) + }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) + if err != nil { + return err + } + } + } + + if len(portMapString) == 0 { + if len(rawTCPPorts) > 0 { + for _, rawTcpPort := range rawTCPPorts { + if rawTcpPort == 0 { + continue + } + + portMapString = fmt.Sprintf("%s http:%s:%s", portMapString, rawTcpPort, rawTcpPort) + } + } else { + portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1") + if common.FileExists(portFile) { + upstreamPort = common.ReadFirstLine(portFile) + } + + if dokkuProxyPort != "" { + portMapString = fmt.Sprintf("%s http:%s:%s", portMapString, dokkuProxyPort, upstreamPort) + } + if dokkuProxySSLPort != "" { + portMapString = fmt.Sprintf("%s https:%s:%s", portMapString, dokkuProxySSLPort, upstreamPort) + } + } + + portMapString = strings.TrimSpace(portMapString) + if len(portMapString) > 0 { + portMaps, err := parsePortMapString(portMapString) + if err != nil { + return err + } + + if err := setPortMaps(appName, portMaps); err != nil { + return err + } + } + + return nil + } + + return nil +} + // TriggerRawTCPPorts extracts raw tcp port numbers from DOCKERFILE_PORTS config variable func TriggerPortsDockerfileRawTCPPorts(appName string) error { ports := getDockerfileRawTCPPorts(appName) diff --git a/plugins/traefik-vhosts/docker-args-process-deploy b/plugins/traefik-vhosts/docker-args-process-deploy index 4f09b45cdb3..f9d94ffa46b 100755 --- a/plugins/traefik-vhosts/docker-args-process-deploy +++ b/plugins/traefik-vhosts/docker-args-process-deploy @@ -30,7 +30,7 @@ trigger-traefik-vhosts-docker-args-process-deploy() { fi # ensure we have a port mapping - plugn trigger proxy-configure-ports "$APP" + plugn trigger ports-configure "$APP" # gather port mapping information # we only support proxying a single port for http and https listeners From 5f203c43260bab2ebf00d1d37de70f5c2439af9d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 3 Jul 2023 00:19:29 -0400 Subject: [PATCH 3/6] fix: silence setting the port map value This can be removed once we migrate to properties, but otherwise this breaks deploys as somehow it gets triggered in the process of fetching the docker run arguments... --- plugins/ports/triggers.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index e3fb05ae8c9..9e56e168b4a 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -120,7 +120,10 @@ func TriggerPortsConfigure(appName string) error { return err } - if err := setPortMaps(appName, portMaps); err != nil { + err = common.EnvWrap(func() error { + return setPortMaps(appName, portMaps) + }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) + if err != nil { return err } } From 67cc54a9910931b1d0c981ced02c8dd829278d3b Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 9 Jul 2023 01:47:52 -0400 Subject: [PATCH 4/6] refactor: use correct type for raw tcp ports We have a typed programming language, lets use it! --- plugins/ports/triggers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index 9e56e168b4a..98c8d78a5ff 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -97,7 +97,7 @@ func TriggerPortsConfigure(appName string) error { continue } - portMapString = fmt.Sprintf("%s http:%s:%s", portMapString, rawTcpPort, rawTcpPort) + portMapString = fmt.Sprintf("%s http:%d:%d", portMapString, rawTcpPort, rawTcpPort) } } else { portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1") From d0726cf96dd226d43b4d23c85c466936dca8c6f8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 9 Jul 2023 02:14:50 -0400 Subject: [PATCH 5/6] refactor: use wrapper functions for interacting with plugin triggers --- plugins/ports/functions.go | 65 +++++++++++++++++++++-- plugins/ports/triggers.go | 106 ++++++++++++++++--------------------- 2 files changed, 105 insertions(+), 66 deletions(-) diff --git a/plugins/ports/functions.go b/plugins/ports/functions.go index 05c087d7377..3771693f6e3 100644 --- a/plugins/ports/functions.go +++ b/plugins/ports/functions.go @@ -28,6 +28,12 @@ func clearPorts(appName string) error { }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) } +func doesCertExist(appName string) bool { + b, _ := common.PlugnTriggerOutput("certs-exists", []string{appName}...) + certsExists := strings.TrimSpace(string(b[:])) + return certsExists == "true" +} + func filterAppPortMaps(appName string, scheme string, hostPort int) []PortMap { var filteredPortMaps []PortMap for _, portMap := range getPortMaps(appName) { @@ -90,12 +96,59 @@ func getDockerfileRawTCPPorts(appName string) []int { return ports } +func getGlobalProxyPort() int { + port := 80 + b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_PORT"}...) + if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { + port = intVar + } + + return port +} + +func getGlobalProxySSLPort() int { + port := 443 + b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_SSL_PORT"}...) + if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { + port = intVar + } + + return port +} + func getPortMaps(appName string) []PortMap { value := config.GetWithDefault(appName, "DOKKU_PROXY_PORT_MAP", "") portMaps, _ := parsePortMapString(value) return portMaps } +func getProxyPort(appName string) int { + port := 80 + b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT"}...) + if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { + port = intVar + } + + return port +} + +func getProxySSLPort(appName string) int { + port := 443 + b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_SSL_PORT"}...) + if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { + port = intVar + } + + return port +} + +func isAppVhostEnabled(appName string) bool { + if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil { + return false + } + return true +} + func inRange(value int, min int, max int) bool { return min < value && value < max } @@ -223,11 +276,13 @@ func setPortMaps(appName string, portMaps []PortMap) error { value = append(value, portMap.String()) } - sort.Strings(value) - entries := map[string]string{ - "DOKKU_PROXY_PORT_MAP": strings.Join(value, " "), - } - return config.SetMany(appName, entries, false) + return common.EnvWrap(func() error { + sort.Strings(value) + entries := map[string]string{ + "DOKKU_PROXY_PORT_MAP": strings.Join(value, " "), + } + return config.SetMany(appName, entries, false) + }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) } func uniquePortMaps(portMaps []PortMap) []PortMap { diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index 98c8d78a5ff..9da826bf20a 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -3,7 +3,7 @@ package ports import ( "fmt" "path/filepath" - "strings" + "strconv" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" @@ -18,41 +18,29 @@ func TriggerPortsClear(appName string) error { func TriggerPortsConfigure(appName string) error { rawTCPPorts := getDockerfileRawTCPPorts(appName) - b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT"}...) - dokkuProxyPort := strings.TrimSpace(string(b[:])) - - b, _ = common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_SSL_PORT"}...) - dokkuProxySSLPort := strings.TrimSpace(string(b[:])) - - b, _ = common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT_MAP"}...) - portMapString := strings.TrimSpace(string(b[:])) - - isAppVhostEnabled := true - upstreamPort := "5000" + dokkuProxyPort := getProxyPort(appName) + dokkuProxySSLPort := getProxySSLPort(appName) + portMaps := getPortMaps(appName) - if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil { - isAppVhostEnabled = false - } + vhostEnabled := isAppVhostEnabled(appName) - if dokkuProxyPort == "" && len(rawTCPPorts) == 0 { - proxyPort := "80" - if !isAppVhostEnabled { + if dokkuProxyPort == 0 && len(rawTCPPorts) == 0 { + proxyPort := 80 + if !vhostEnabled { common.LogInfo1("No port set, setting to random open high port") - b, _ = common.PlugnTriggerOutput("ports-get-available", []string{}...) - proxyPort = strings.TrimSpace(string(b[:])) + proxyPort = getAvailablePort() } else { - b, _ = common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_PORT"}...) - proxyPort = strings.TrimSpace(string(b[:])) + proxyPort = getGlobalProxyPort() } - if proxyPort == "" { - proxyPort = "80" + if proxyPort == 0 { + proxyPort = 80 } dokkuProxyPort = proxyPort err := common.EnvWrap(func() error { entries := map[string]string{ - "DOKKU_PROXY_PORT": proxyPort, + "DOKKU_PROXY_PORT": fmt.Sprint(proxyPort), } return config.SetMany(appName, entries, false) }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) @@ -61,26 +49,22 @@ func TriggerPortsConfigure(appName string) error { } } - if dokkuProxySSLPort == "" { - b, _ = common.PlugnTriggerOutput("certs-exists", []string{appName}...) - certsExists := strings.TrimSpace(string(b[:])) - if certsExists == "true" { - b, _ = common.PlugnTriggerOutput("config-get-global", []string{"PROXY_SSL_PORT"}...) - proxySSLPort := strings.TrimSpace(string(b[:])) - if proxySSLPort == "" { - proxySSLPort = "443" + if dokkuProxySSLPort == 0 { + if doesCertExist(appName) { + proxySSLPort := getGlobalProxySSLPort() + if proxySSLPort == 0 { + proxySSLPort = 443 } - if len(rawTCPPorts) == 0 && !isAppVhostEnabled { + if len(rawTCPPorts) == 0 && !vhostEnabled { common.LogInfo1("No ssl port set, setting to random open high port") - b, _ = common.PlugnTriggerOutput("ports-get-available", []string{}...) - proxySSLPort = strings.TrimSpace(string(b[:])) + proxySSLPort = getAvailablePort() } dokkuProxySSLPort = proxySSLPort err := common.EnvWrap(func() error { entries := map[string]string{ - "DOKKU_PROXY_SSL_PORT": proxySSLPort, + "DOKKU_PROXY_SSL_PORT": fmt.Sprint(proxySSLPort), } return config.SetMany(appName, entries, false) }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) @@ -90,42 +74,42 @@ func TriggerPortsConfigure(appName string) error { } } - if len(portMapString) == 0 { + if len(portMaps) == 0 { if len(rawTCPPorts) > 0 { for _, rawTcpPort := range rawTCPPorts { - if rawTcpPort == 0 { - continue - } - - portMapString = fmt.Sprintf("%s http:%d:%d", portMapString, rawTcpPort, rawTcpPort) + portMaps = append(portMaps, PortMap{ + ContainerPort: rawTcpPort, + HostPort: rawTcpPort, + Scheme: "http", + }) } } else { + upstreamPort := 5000 portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1") if common.FileExists(portFile) { - upstreamPort = common.ReadFirstLine(portFile) + if port, err := strconv.Atoi(common.ReadFirstLine(portFile)); err == nil { + upstreamPort = port + } } - if dokkuProxyPort != "" { - portMapString = fmt.Sprintf("%s http:%s:%s", portMapString, dokkuProxyPort, upstreamPort) + if dokkuProxyPort != 0 { + portMaps = append(portMaps, PortMap{ + ContainerPort: upstreamPort, + HostPort: dokkuProxyPort, + Scheme: "http", + }) } - if dokkuProxySSLPort != "" { - portMapString = fmt.Sprintf("%s https:%s:%s", portMapString, dokkuProxySSLPort, upstreamPort) + if dokkuProxySSLPort != 0 { + portMaps = append(portMaps, PortMap{ + ContainerPort: upstreamPort, + HostPort: dokkuProxySSLPort, + Scheme: "https", + }) } } - portMapString = strings.TrimSpace(portMapString) - if len(portMapString) > 0 { - portMaps, err := parsePortMapString(portMapString) - if err != nil { - return err - } - - err = common.EnvWrap(func() error { - return setPortMaps(appName, portMaps) - }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) - if err != nil { - return err - } + if len(portMaps) > 0 { + return setPortMaps(appName, portMaps) } return nil From 44c67e5baf92996a640949f2c0a6291c26f48e3a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 9 Jul 2023 04:15:21 -0400 Subject: [PATCH 6/6] fix: set empty default port --- plugins/ports/functions.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/ports/functions.go b/plugins/ports/functions.go index 3771693f6e3..d01d5dcbbaa 100644 --- a/plugins/ports/functions.go +++ b/plugins/ports/functions.go @@ -97,7 +97,7 @@ func getDockerfileRawTCPPorts(appName string) []int { } func getGlobalProxyPort() int { - port := 80 + port := 0 b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_PORT"}...) if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { port = intVar @@ -107,7 +107,7 @@ func getGlobalProxyPort() int { } func getGlobalProxySSLPort() int { - port := 443 + port := 0 b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_SSL_PORT"}...) if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { port = intVar @@ -123,7 +123,7 @@ func getPortMaps(appName string) []PortMap { } func getProxyPort(appName string) int { - port := 80 + port := 0 b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_PORT"}...) if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { port = intVar @@ -133,7 +133,7 @@ func getProxyPort(appName string) int { } func getProxySSLPort(appName string) int { - port := 443 + port := 0 b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_PROXY_SSL_PORT"}...) if intVar, err := strconv.Atoi(strings.TrimSpace(string(b[:]))); err == nil { port = intVar