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.
This commit is contained in:
@@ -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 "$@"
|
|
||||||
@@ -3,7 +3,7 @@ include ../../common.mk
|
|||||||
GO_ARGS ?= -a
|
GO_ARGS ?= -a
|
||||||
|
|
||||||
SUBCOMMANDS = subcommands/gc subcommands/purge-cache
|
SUBCOMMANDS = subcommands/gc subcommands/purge-cache
|
||||||
|
TRIGGERS = triggers/pre-delete
|
||||||
build-in-docker: clean
|
build-in-docker: clean
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-v $$PWD/../..:$(GO_REPO_ROOT) \
|
-v $$PWD/../..:$(GO_REPO_ROOT) \
|
||||||
@@ -11,7 +11,8 @@ build-in-docker: clean
|
|||||||
$(BUILD_IMAGE) \
|
$(BUILD_IMAGE) \
|
||||||
bash -c "GO_ARGS='$(GO_ARGS)' make -j4 build" || exit $$?
|
bash -c "GO_ARGS='$(GO_ARGS)' make -j4 build" || exit $$?
|
||||||
|
|
||||||
build: commands subcommands
|
build: commands subcommands triggers
|
||||||
|
$(MAKE) triggers-copy
|
||||||
|
|
||||||
commands: **/**/commands.go
|
commands: **/**/commands.go
|
||||||
go build $(GO_ARGS) -o commands src/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 $@ $<
|
go build $(GO_ARGS) -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf commands subcommands
|
rm -rf commands subcommands triggers pre-delete
|
||||||
|
|
||||||
src-clean:
|
src-clean:
|
||||||
rm -rf .gitignore src triggers vendor Makefile *.go glide.*
|
rm -rf .gitignore src triggers vendor Makefile *.go glide.*
|
||||||
|
|
||||||
|
triggers: $(TRIGGERS)
|
||||||
|
|
||||||
|
triggers/%: src/triggers/*/%.go
|
||||||
|
go build $(GO_ARGS) -o $@ $<
|
||||||
|
|
||||||
|
triggers-copy:
|
||||||
|
cp triggers/* .
|
||||||
|
|||||||
36
plugins/repo/repo.go
Normal file
36
plugins/repo/repo.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -2,11 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"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
|
// deletes the contents of the build cache stored in the repository
|
||||||
@@ -16,25 +14,9 @@ func main() {
|
|||||||
if appName == "" {
|
if appName == "" {
|
||||||
common.LogFail("Please specify an app to run the command on")
|
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"}, "/")
|
err := repo.PurgeCache(appName)
|
||||||
cacheHostDir := strings.Join([]string{common.MustGetEnv("DOKKU_HOST_ROOT"), appName, "cache"}, "/")
|
if err != nil {
|
||||||
dokkuGlobalRunArgs := common.MustGetEnv("DOKKU_GLOBAL_RUN_ARGS")
|
common.LogWarn(err.Error())
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
plugins/repo/src/triggers/pre-delete/pre-delete.go
Normal file
19
plugins/repo/src/triggers/pre-delete/pre-delete.go
Normal file
@@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user