Merge pull request #3607 from dokku/herokuish-cache-purge

Purge cache using herokuish image
This commit is contained in:
Jose Diaz-Gonzalez
2019-07-05 18:25:37 -04:00
committed by GitHub
5 changed files with 71 additions and 49 deletions

View File

@@ -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 "$@"

View File

@@ -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/* .

36
plugins/repo/repo.go Normal file
View 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
}

View File

@@ -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())
}
}

View 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())
}
}