From 33423a0508ffcdc0ac3cb966c9bd4a7371013d50 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 5 Jul 2019 16:07:13 -0400 Subject: [PATCH 1/2] feat: purge cache using herokuish image In some cases, the cache directory may exist but there may be no app image to use for cache purging. This may be the case if the app was never deployed. Instead of failing, we can simply use the configured herokuish image for clearing cache, as that image is the base image for anything that would have modified cache. --- plugins/apps/pre-delete | 8 ++------ plugins/repo/src/subcommands/purge-cache/purge-cache.go | 3 ++- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/plugins/apps/pre-delete b/plugins/apps/pre-delete index 92a2ae159..24f924071 100755 --- a/plugins/apps/pre-delete +++ b/plugins/apps/pre-delete @@ -8,17 +8,13 @@ apps_pre_delete() { 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 + DOKKU_IMAGE="$(config_get "$APP" DOKKU_IMAGE || echo "$DOKKU_IMAGE")" + "$DOCKER_BIN" run "$DOKKU_GLOBAL_RUN_ARGS" --rm -v "$CACHE_HOST_DIR:/cache" "$DOKKU_IMAGE" find /cache -depth -mindepth 1 -maxdepth 1 -exec rm -Rf {} \; || true fi } diff --git a/plugins/repo/src/subcommands/purge-cache/purge-cache.go b/plugins/repo/src/subcommands/purge-cache/purge-cache.go index a1cf19728..2b5732b7e 100644 --- a/plugins/repo/src/subcommands/purge-cache/purge-cache.go +++ b/plugins/repo/src/subcommands/purge-cache/purge-cache.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/dokku/dokku/plugins/common" + "github.com/dokku/dokku/plugins/config" ) // deletes the contents of the build cache stored in the repository @@ -23,7 +24,7 @@ func main() { 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, "", "") + 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(), From 69e4cd7c442a313392ae43c15ee19974748d668f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 5 Jul 2019 16:12:22 -0400 Subject: [PATCH 2/2] refactor: use only a single implementation for cache purging This change moves cache clearing into the repo plugin, and standardizes on a single way of clearing the cache. --- plugins/apps/pre-delete | 21 ----------- plugins/repo/Makefile | 15 ++++++-- plugins/repo/repo.go | 36 +++++++++++++++++++ .../subcommands/purge-cache/purge-cache.go | 26 +++----------- .../src/triggers/pre-delete/pre-delete.go | 19 ++++++++++ 5 files changed, 71 insertions(+), 46 deletions(-) delete mode 100755 plugins/apps/pre-delete create mode 100644 plugins/repo/repo.go create mode 100644 plugins/repo/src/triggers/pre-delete/pre-delete.go diff --git a/plugins/apps/pre-delete b/plugins/apps/pre-delete deleted file mode 100755 index 24f924071..000000000 --- a/plugins/apps/pre-delete +++ /dev/null @@ -1,21 +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 CACHE_DIR="$DOKKU_ROOT/$APP/cache" - local CACHE_HOST_DIR="$DOKKU_HOST_ROOT/$APP/cache" - verify_app_name "$APP" - - if [[ -d $CACHE_DIR ]]; then - DOKKU_IMAGE="$(config_get "$APP" DOKKU_IMAGE || echo "$DOKKU_IMAGE")" - "$DOCKER_BIN" run "$DOKKU_GLOBAL_RUN_ARGS" --rm -v "$CACHE_HOST_DIR:/cache" "$DOKKU_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 2b5732b7e..63665ca18 100644 --- a/plugins/repo/src/subcommands/purge-cache/purge-cache.go +++ b/plugins/repo/src/subcommands/purge-cache/purge-cache.go @@ -2,11 +2,9 @@ package main import ( "flag" - "os" - "strings" "github.com/dokku/dokku/plugins/common" - "github.com/dokku/dokku/plugins/config" + "github.com/dokku/dokku/plugins/repo" ) // deletes the contents of the build cache stored in the repository @@ -16,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 := 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 { - 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()) + } +}