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:
Jose Diaz-Gonzalez committed 2019-07-05 17:40:20 -04:00
1 parent 33423a0508
commit 69e4cd7c44
5 files changed
+71 -46

No files matched your search

+36
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
}