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..1a7d75cf9 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 @@ -50,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 "$@" 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) + } +} 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 "$@" 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 "$@" 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" {