diff --git a/plugins/apps/pre-delete b/plugins/apps/pre-delete deleted file mode 100755 index 92a2ae159..000000000 --- a/plugins/apps/pre-delete +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail -[[ $DOKKU_TRACE ]] && set -x -source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -apps_pre_delete() { - declare desc="apps pre-delete plugin trigger" - local trigger="apps_pre_delete" - local APP="$1" - local IMAGE_TAG="$2" - local IMAGE=$(get_deploying_app_image_name "$APP" "$IMAGE_TAG") - local CACHE_DIR="$DOKKU_ROOT/$APP/cache" - local CACHE_HOST_DIR="$DOKKU_HOST_ROOT/$APP/cache" - verify_app_name "$APP" - - if ! is_image_herokuish_based "$IMAGE"; then - return - fi - - if [[ -d $CACHE_DIR ]]; then - "$DOCKER_BIN" run "$DOKKU_GLOBAL_RUN_ARGS" --rm -v "$CACHE_HOST_DIR:/cache" "$IMAGE" find /cache -depth -mindepth 1 -maxdepth 1 -exec rm -Rf {} \; || true - fi -} - -apps_pre_delete "$@" diff --git a/plugins/repo/Makefile b/plugins/repo/Makefile index 2b7be509a..706a28900 100644 --- a/plugins/repo/Makefile +++ b/plugins/repo/Makefile @@ -3,7 +3,7 @@ include ../../common.mk GO_ARGS ?= -a SUBCOMMANDS = subcommands/gc subcommands/purge-cache - +TRIGGERS = triggers/pre-delete build-in-docker: clean docker run --rm \ -v $$PWD/../..:$(GO_REPO_ROOT) \ @@ -11,7 +11,8 @@ build-in-docker: clean $(BUILD_IMAGE) \ bash -c "GO_ARGS='$(GO_ARGS)' make -j4 build" || exit $$? -build: commands subcommands +build: commands subcommands triggers + $(MAKE) triggers-copy commands: **/**/commands.go go build $(GO_ARGS) -o commands src/commands/commands.go @@ -22,7 +23,15 @@ subcommands/%: src/subcommands/*/%.go go build $(GO_ARGS) -o $@ $< clean: - rm -rf commands subcommands + rm -rf commands subcommands triggers pre-delete src-clean: rm -rf .gitignore src triggers vendor Makefile *.go glide.* + +triggers: $(TRIGGERS) + +triggers/%: src/triggers/*/%.go + go build $(GO_ARGS) -o $@ $< + +triggers-copy: + cp triggers/* . diff --git a/plugins/repo/repo.go b/plugins/repo/repo.go new file mode 100644 index 000000000..c317e9380 --- /dev/null +++ b/plugins/repo/repo.go @@ -0,0 +1,36 @@ +package repo + +import ( + "os" + "strings" + + "github.com/dokku/dokku/plugins/common" + "github.com/dokku/dokku/plugins/config" +) + +// PurgeCache deletes the contents of the build cache stored in the repository +func PurgeCache(appName string) error { + err := common.VerifyAppName(appName) + if err != nil { + return err + } + + cacheDir := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName, "cache"}, "/") + cacheHostDir := strings.Join([]string{common.MustGetEnv("DOKKU_HOST_ROOT"), appName, "cache"}, "/") + dokkuGlobalRunArgs := common.MustGetEnv("DOKKU_GLOBAL_RUN_ARGS") + image := config.GetWithDefault(appName, "DOKKU_IMAGE", os.Getenv("DOKKU_IMAGE")) + if info, _ := os.Stat(cacheDir); info != nil && info.IsDir() { + purgeCacheCmd := common.NewShellCmd(strings.Join([]string{ + common.DockerBin(), + "run --rm", dokkuGlobalRunArgs, + "-v", strings.Join([]string{cacheHostDir, ":/cache"}, ""), image, + `find /cache -depth -mindepth 1 -maxdepth 1 -exec rm -Rf {} ;`}, " ")) + purgeCacheCmd.Execute() + err := os.MkdirAll(cacheDir, 0644) + if err != nil { + return err + } + } + + return nil +} diff --git a/plugins/repo/src/subcommands/purge-cache/purge-cache.go b/plugins/repo/src/subcommands/purge-cache/purge-cache.go index a1cf19728..63665ca18 100644 --- a/plugins/repo/src/subcommands/purge-cache/purge-cache.go +++ b/plugins/repo/src/subcommands/purge-cache/purge-cache.go @@ -2,10 +2,9 @@ package main import ( "flag" - "os" - "strings" "github.com/dokku/dokku/plugins/common" + "github.com/dokku/dokku/plugins/repo" ) // deletes the contents of the build cache stored in the repository @@ -15,25 +14,9 @@ func main() { if appName == "" { common.LogFail("Please specify an app to run the command on") } - err := common.VerifyAppName(appName) - if err != nil { - common.LogFail(err.Error()) - } - cacheDir := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName, "cache"}, "/") - cacheHostDir := strings.Join([]string{common.MustGetEnv("DOKKU_HOST_ROOT"), appName, "cache"}, "/") - dokkuGlobalRunArgs := common.MustGetEnv("DOKKU_GLOBAL_RUN_ARGS") - image := common.GetDeployingAppImageName(appName, "", "") - if info, _ := os.Stat(cacheDir); info != nil && info.IsDir() { - purgeCacheCmd := common.NewShellCmd(strings.Join([]string{ - common.DockerBin(), - "run --rm", dokkuGlobalRunArgs, - "-v", strings.Join([]string{cacheHostDir, ":/cache"}, ""), image, - `find /cache -depth -mindepth 1 -maxdepth 1 -exec rm -Rf {} ;`}, " ")) - purgeCacheCmd.Execute() - err := os.MkdirAll(cacheDir, 0644) - if err != nil { - common.LogFail(err.Error()) - } + err := repo.PurgeCache(appName) + if err != nil { + common.LogWarn(err.Error()) } } diff --git a/plugins/repo/src/triggers/pre-delete/pre-delete.go b/plugins/repo/src/triggers/pre-delete/pre-delete.go new file mode 100644 index 000000000..c43f5fba0 --- /dev/null +++ b/plugins/repo/src/triggers/pre-delete/pre-delete.go @@ -0,0 +1,19 @@ +package main + +import ( + "flag" + + "github.com/dokku/dokku/plugins/common" + "github.com/dokku/dokku/plugins/repo" +) + +// destroys the buildpacks property for a given app container +func main() { + flag.Parse() + appName := flag.Arg(0) + + err := repo.PurgeCache(appName) + if err != nil { + common.LogFail(err.Error()) + } +}