From 0498a707c8f0ff17a26afb8b3c8270bc5fb76ecd Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 27 Dec 2018 04:58:43 -0500 Subject: [PATCH 1/6] fix: Add a trigger to allow plugins to cleanup after a new clone is created --- docs/development/plugin-triggers.md | 15 +++++++++++++++ plugins/20_events/post-app-clone-setup | 1 + plugins/apps/subcommands/clone | 1 + 3 files changed, 17 insertions(+) create mode 120000 plugins/20_events/post-app-clone-setup diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 6bf29a48f..8c7986b04 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -559,6 +559,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # TODO ``` +### `post-app-clone-setup` + +- Description: Allows you to run commands after an app is setup, and before it is rebuild. This is useful for cleaning up tasks, or ensuring configuration from an old app is copied to the new app +- Invoked by: `dokku apps:clone` +- Arguments: `$OLD_APP_NAME $NEW_APP_NAME` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +# TODO +``` + ### `post-app-rename` - Description: Allows you to run commands after an app was renamed. diff --git a/plugins/20_events/post-app-clone-setup b/plugins/20_events/post-app-clone-setup new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/post-app-clone-setup @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/apps/subcommands/clone b/plugins/apps/subcommands/clone index 8076c0668..e31c828ff 100755 --- a/plugins/apps/subcommands/clone +++ b/plugins/apps/subcommands/clone @@ -39,6 +39,7 @@ apps_clone_cmd() { pushd "$DOKKU_ROOT/$OLD_APP/." > /dev/null find ./* -not \( -name .cache \) | grep -v "./cache" | cpio -pdmu --quiet "$DOKKU_ROOT/$NEW_APP" popd > /dev/null 2>&1 || pushd "/tmp" > /dev/null + plugn trigger post-app-clone-setup "$OLD_APP" "$NEW_APP" if [[ -d "$NEW_CACHE_DIR" ]] && ! rmdir "$NEW_CACHE_DIR"; then docker run "$DOKKU_GLOBAL_RUN_ARGS" --rm -v "$NEW_CACHE_HOST_DIR:/cache" "dokku/$OLD_APP" chmod 777 -R /cache From 34d248a28cefc76a431ee069ba9e503a59581198 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 27 Dec 2018 04:59:22 -0500 Subject: [PATCH 2/6] fix: remove old nginx.conf from a new app on clone --- plugins/nginx-vhosts/post-app-clone-setup | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100755 plugins/nginx-vhosts/post-app-clone-setup diff --git a/plugins/nginx-vhosts/post-app-clone-setup b/plugins/nginx-vhosts/post-app-clone-setup new file mode 100755 index 000000000..194e03106 --- /dev/null +++ b/plugins/nginx-vhosts/post-app-clone-setup @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +nginx_post_app_clone_setup() { + declare desc="removes nginx files when setting up a clone" + declare OLD_APP="$1" NEW_APP="$2" + local APP_DIR="$DOKKU_ROOT/$NEW_APP" + + rm -rf "$APP_DIR/nginx.conf" +} + +nginx_post_app_clone_setup "$@" From cfd8f237df6004660e82ed27357f71cb80ee04ad Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 27 Dec 2018 05:01:45 -0500 Subject: [PATCH 3/6] feat: cleanup IP and PORT files on app clone --- plugins/network/.gitignore | 3 ++- plugins/network/Makefile | 2 +- plugins/network/network.go | 23 +++++++++++++++++++ .../post-app-clone-setup.go | 19 +++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 plugins/network/src/triggers/post-app-clone-setup/post-app-clone-setup.go diff --git a/plugins/network/.gitignore b/plugins/network/.gitignore index 93636e7a5..7674a1147 100644 --- a/plugins/network/.gitignore +++ b/plugins/network/.gitignore @@ -3,6 +3,7 @@ /triggers/* /network-* /install +/post-app-clone-setup /post-create /post-delete -/report \ No newline at end of file +/report diff --git a/plugins/network/Makefile b/plugins/network/Makefile index dc1d9f7ce..671e88ae7 100644 --- a/plugins/network/Makefile +++ b/plugins/network/Makefile @@ -3,7 +3,7 @@ include ../../common.mk GO_ARGS ?= -a SUBCOMMANDS = subcommands/rebuild subcommands/rebuildall subcommands/report subcommands/set -TRIGGERS = triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-write-ipaddr triggers/network-write-port triggers/post-create triggers/post-delete triggers/report +TRIGGERS = triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-write-ipaddr triggers/network-write-port triggers/post-app-clone-setup triggers/post-create triggers/post-delete triggers/report build-in-docker: clean docker run --rm \ -v $$PWD/../..:$(GO_REPO_ROOT) \ diff --git a/plugins/network/network.go b/plugins/network/network.go index cab14abcd..af44450d3 100644 --- a/plugins/network/network.go +++ b/plugins/network/network.go @@ -180,6 +180,29 @@ func HasNetworkConfig(appName string) bool { return common.FileExists(ipfile) && common.FileExists(portfile) } +// PostAppCloneSetup removes old IP and PORT files for a newly cloned app +func PostAppCloneSetup(appName string) bool { + dokkuRoot := common.MustGetEnv("DOKKU_ROOT") + appRoot := strings.Join([]string{dokkuRoot, appName}, "/") + success := true + + ipFiles, _ := filepath.Glob(appRoot + "/IP.*") + for _, file := range ipFiles { + if err := os.Remove(file); err != nil { + common.LogWarn(fmt.Sprintf("Unable to remove file %s", file)) + success = false + } + } + portFiles, _ := filepath.Glob(appRoot + "/PORT.*") + for _, file := range portFiles { + if err := os.Remove(file); err != nil { + common.LogWarn(fmt.Sprintf("Unable to remove file %s", file)) + success = false + } + } + return success +} + // ReportSingleApp is an internal function that displays the app report for one or more apps func ReportSingleApp(appName, infoFlag string) { if err := common.VerifyAppName(appName); err != nil { diff --git a/plugins/network/src/triggers/post-app-clone-setup/post-app-clone-setup.go b/plugins/network/src/triggers/post-app-clone-setup/post-app-clone-setup.go new file mode 100644 index 000000000..70adb14ce --- /dev/null +++ b/plugins/network/src/triggers/post-app-clone-setup/post-app-clone-setup.go @@ -0,0 +1,19 @@ +package main + +import ( + "flag" + "os" + + "github.com/dokku/dokku/plugins/network" +) + +// cleanup network files for a new app clone +func main() { + flag.Parse() + appName := flag.Arg(1) + + success := network.PostAppCloneSetup(appName) + if !success { + os.Exit(1) + } +} From f3a33e74d76d207b6197b6072d4a3e2c6359d05d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 27 Dec 2018 05:02:23 -0500 Subject: [PATCH 4/6] fix: remove CONTAINER files on app clone This will ensure we don't accidentally destroy old applications if the new app isn't rebuilt or the rebuild fails. Closes #3249 --- plugins/scheduler-docker-local/post-app-clone-setup | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100755 plugins/scheduler-docker-local/post-app-clone-setup diff --git a/plugins/scheduler-docker-local/post-app-clone-setup b/plugins/scheduler-docker-local/post-app-clone-setup new file mode 100755 index 000000000..b45fb390c --- /dev/null +++ b/plugins/scheduler-docker-local/post-app-clone-setup @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +scheduler-docker-local-post-app-clone-setup() { + declare desc="removes docker-local files when setting up a clone" + declare OLD_APP="$1" NEW_APP="$2" + local APP_DIR="$DOKKU_ROOT/$NEW_APP" + + find "$APP_DIR" -type f -name 'CONTAINER.*' -exec rm {} \; +} + +scheduler-docker-local-post-app-clone-setup "$@" From a9d46ada5db5055cb57b3e0365c9cf768fc33e52 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 27 Dec 2018 05:06:21 -0500 Subject: [PATCH 5/6] feat: add test for old app during apps:clone --skip-deploy If you skip a deploy and then destroy the newly created app, the old application should continue functioning like normal. --- tests/unit/10_apps.bats | 8 ++++++++ 1 file changed, 8 insertions(+) mode change 100644 => 100755 tests/unit/10_apps.bats diff --git a/tests/unit/10_apps.bats b/tests/unit/10_apps.bats old mode 100644 new mode 100755 index 0cbddd8d9..71dd6b0f7 --- a/tests/unit/10_apps.bats +++ b/tests/unit/10_apps.bats @@ -152,6 +152,10 @@ teardown () { echo "output: $output" echo "status: $status" assert_success + run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url $TEST_APP` | grep 200" + echo "output: $output" + echo "status: $status" + assert_success run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url great-test-name` | grep 404" echo "output: $output" echo "status: $status" @@ -160,6 +164,10 @@ teardown () { echo "output: $output" echo "status: $status" assert_success + run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url $TEST_APP` | grep 200" + echo "output: $output" + echo "status: $status" + assert_success } @test "(apps) apps:clone --ignore-existing" { From c5143cb7229476fa14fb1076033a4995e4602c9e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 28 Dec 2018 16:52:59 -0500 Subject: [PATCH 6/6] chore: switch to dokku_log_info1_quiet --- plugins/apps/subcommands/clone | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/apps/subcommands/clone b/plugins/apps/subcommands/clone index e31c828ff..1a7d75cf9 100755 --- a/plugins/apps/subcommands/clone +++ b/plugins/apps/subcommands/clone @@ -51,7 +51,7 @@ apps_clone_cmd() { [[ -f "$DOKKU_ROOT/$NEW_APP/hooks/pre-receive" ]] && sed -i -e "s/git-hook $OLD_APP/git-hook $NEW_APP/g" "$DOKKU_ROOT/$NEW_APP/hooks/pre-receive" [[ "$SKIP_REBUILD" == "true" ]] || ps_rebuild "$NEW_APP" plugn trigger post-app-clone "$OLD_APP" "$NEW_APP" - echo "Cloning $OLD_APP to $NEW_APP... done" + dokku_log_info1_quiet "Cloning $OLD_APP to $NEW_APP... done" } apps_clone_cmd "$@"