From 1431724b858280f1a6cb9b9780d8c7c6044d9b9d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 8 Jul 2023 15:13:17 -0400 Subject: [PATCH] feat: use ports-clear plugn trigger to manage clearing the port map This removes any access to the underlying implementation for port map clearing, which will make it easier to refactor the actual clearing. --- docs/deployment/builders/dockerfiles.md | 2 +- .../builders/herokuish-buildpacks.md | 2 +- docs/development/plugin-triggers.md | 20 +++++++++++++++++++ plugins/20_events/ports-clear | 1 + plugins/nginx-vhosts/pre-disable-vhost | 3 ++- plugins/nginx-vhosts/pre-enable-vhost | 3 ++- plugins/ports/.gitignore | 1 + plugins/ports/Makefile | 2 +- plugins/ports/functions.go | 7 +++++++ plugins/ports/src/triggers/triggers.go | 3 +++ plugins/ports/subcommands.go | 4 +--- plugins/ports/triggers.go | 5 +++++ 12 files changed, 45 insertions(+), 8 deletions(-) create mode 120000 plugins/20_events/ports-clear diff --git a/docs/deployment/builders/dockerfiles.md b/docs/deployment/builders/dockerfiles.md index 7e060e40c..62a1cc590 100644 --- a/docs/deployment/builders/dockerfiles.md +++ b/docs/deployment/builders/dockerfiles.md @@ -29,7 +29,7 @@ Dokku will only select the `dockerfile` builder if both the `herokuish` and `pac If an application was previously deployed via buildpacks, the following commands should be run before a Dockerfile deploy will succeed: ```shell -dokku config:unset --no-restart node-js-app DOKKU_PROXY_PORT_MAP +dokku proxy:clear node-js-app ``` ### Changing the `Dockerfile` location diff --git a/docs/deployment/builders/herokuish-buildpacks.md b/docs/deployment/builders/herokuish-buildpacks.md index 947c36b97..66ca72ec3 100644 --- a/docs/deployment/builders/herokuish-buildpacks.md +++ b/docs/deployment/builders/herokuish-buildpacks.md @@ -301,7 +301,7 @@ false If an application was previously deployed via Dockerfile, the following commands should be run before a buildpack deploy will succeed: ```shell -dokku config:unset --no-restart node-js-app DOKKU_PROXY_PORT_MAP +dokku ports:clear node-js-app ``` ### Using a specific buildpack version diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 72364ae61..c31e389d9 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -1242,12 +1242,32 @@ nginx -t ``` ### `ports-get` + - Description: Returns a list of port mappings, newline delimited - Invoked by: Various networking plugins - Arguments `$APP` +- Example: + ```shell #!/usr/bin/env bash + set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + +### `ports-clear` + +- Description: Clears the ports for a given app without triggering further restarts or rebuilds +- Invoked by: internally +- Arguments: `$APP` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + # TODO ``` diff --git a/plugins/20_events/ports-clear b/plugins/20_events/ports-clear new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/ports-clear @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/nginx-vhosts/pre-disable-vhost b/plugins/nginx-vhosts/pre-disable-vhost index e786e5322..ecfba935c 100755 --- a/plugins/nginx-vhosts/pre-disable-vhost +++ b/plugins/nginx-vhosts/pre-disable-vhost @@ -10,7 +10,8 @@ trigger-nginx-vhosts-pre-disable-vhost() { declare APP="$1" if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then - DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$APP" DOKKU_PROXY_PORT DOKKU_PROXY_SSL_PORT DOKKU_PROXY_PORT_MAP + DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$APP" DOKKU_PROXY_PORT DOKKU_PROXY_SSL_PORT + plugn trigger ports-clear "$APP" fi } diff --git a/plugins/nginx-vhosts/pre-enable-vhost b/plugins/nginx-vhosts/pre-enable-vhost index bde11f963..64808eb3d 100755 --- a/plugins/nginx-vhosts/pre-enable-vhost +++ b/plugins/nginx-vhosts/pre-enable-vhost @@ -10,7 +10,8 @@ trigger-nginx-vhosts-pre-enable-vhost() { declare APP="$1" if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then - DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$APP" DOKKU_PROXY_PORT DOKKU_PROXY_SSL_PORT DOKKU_PROXY_PORT_MAP + DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$APP" DOKKU_PROXY_PORT DOKKU_PROXY_SSL_PORT + plugn trigger ports-clear "$APP" fi } diff --git a/plugins/ports/.gitignore b/plugins/ports/.gitignore index 291036bd4..3fc2c0d2f 100644 --- a/plugins/ports/.gitignore +++ b/plugins/ports/.gitignore @@ -3,5 +3,6 @@ /triggers/* /triggers /install +/ports-* /post-* /report diff --git a/plugins/ports/Makefile b/plugins/ports/Makefile index 8c519e82e..09ef51751 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-get triggers/post-certs-remove triggers/post-certs-update triggers/report +TRIGGERS = triggers/ports-clear triggers/ports-get triggers/post-certs-remove triggers/post-certs-update triggers/report BUILD = commands subcommands triggers PLUGIN_NAME = ports diff --git a/plugins/ports/functions.go b/plugins/ports/functions.go index 06fdc1641..4739c839e 100644 --- a/plugins/ports/functions.go +++ b/plugins/ports/functions.go @@ -20,6 +20,13 @@ func addPortMaps(appName string, portMaps []PortMap) error { return setPortMaps(appName, allPortMaps) } +func clearPorts(appName string) error { + return common.EnvWrap(func() error { + keys := []string{"DOKKU_PROXY_PORT_MAP"} + return config.UnsetMany(appName, keys, false) + }, map[string]string{"DOKKU_QUIET_OUTPUT": "1"}) +} + func filterAppPortMaps(appName string, scheme string, hostPort int) []PortMap { var filteredPortMaps []PortMap for _, portMap := range getPortMaps(appName) { diff --git a/plugins/ports/src/triggers/triggers.go b/plugins/ports/src/triggers/triggers.go index ae0f68c4d..d7a1f8c88 100644 --- a/plugins/ports/src/triggers/triggers.go +++ b/plugins/ports/src/triggers/triggers.go @@ -21,6 +21,9 @@ func main() { case "ports-get": appName := flag.Arg(0) err = ports.TriggerPortsGet(appName) + case "ports-clear": + appName := flag.Arg(0) + err = ports.TriggerPortsClear(appName) case "post-certs-remove": appName := flag.Arg(0) err = ports.TriggerPostCertsRemove(appName) diff --git a/plugins/ports/subcommands.go b/plugins/ports/subcommands.go index 70ebf9057..ce34e37a8 100644 --- a/plugins/ports/subcommands.go +++ b/plugins/ports/subcommands.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/dokku/dokku/plugins/common" - "github.com/dokku/dokku/plugins/config" ) // CommandList is a cmd wrapper to list port mappings for an app @@ -45,8 +44,7 @@ func CommandClear(appName string) error { return err } - keys := []string{"DOKKU_PROXY_PORT_MAP"} - if err := config.UnsetMany(appName, keys, false); err != nil { + if err := clearPorts(appName); err != nil { return err } diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index 0139ee3bd..a444a4a30 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -19,6 +19,11 @@ func TriggerPortsGet(appName string) error { return nil } +// TriggerPortsClear removes all ports for the specified app +func TriggerPortsClear(appName string) error { + return clearPorts(appName) +} + // TriggerPostCertsRemove unsets port config vars after SSL cert is added func TriggerPostCertsRemove(appName string) error { keys := []string{"DOKKU_PROXY_SSL_PORT"}