Skip to content

Commit

Permalink
delete old stale plugin from cache folder (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani authored May 2, 2024
1 parent 5f0d80b commit 471d7b2
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 56 deletions.
12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,15 @@ preferences: {}
// since /tmp will be mounted on PV we need to do manual clean up
// on restart. appData will be excluded from this clean up
func cleanupTmpDir() {
fileDescriptors, err := os.ReadDir(os.TempDir())
err := sysutil.RemoveDirContentsIf(
os.TempDir(),
func(path string, fi os.FileInfo) (bool, error) {
return fi.Name() != appData, nil
})
if err != nil {
fmt.Printf("unable to cleanup %s Error: %v\n", os.TempDir(), err)
return
}

for _, fd := range fileDescriptors {
if fd.Name() != appData {
sysutil.RemoveAll(path.Join(os.TempDir(), fd.Name()))
}
}
}

func applyGitDefaults(c *cli.Context, mirrorConf mirror.RepoPoolConfig) mirror.RepoPoolConfig {
Expand Down
17 changes: 17 additions & 0 deletions runner/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"os"
"path"
"sync"
"time"

"github.com/utilitywarehouse/terraform-applier/sysutil"
)

var pluginCacheMain = "plugin-cache-main"
var stalePluginTimeout = 7 * 24 * time.Hour

// The plugin cache directory is not guaranteed to be concurrency safe.
// https://github.com/hashicorp/terraform/issues/31964
Expand All @@ -29,10 +31,25 @@ type pluginCache struct {
func newPluginCache(log *slog.Logger, root string) (*pluginCache, error) {
main := path.Join(root, pluginCacheMain)

// crate main plugin cache dir if not exit
if err := os.MkdirAll(main, defaultDirMode); err != nil {
return nil, fmt.Errorf("unable to create main cache dir err:%w", err)
}

// clean up plugin cache dir in case its an old one
err := sysutil.RemoveDirContentsRecursiveIf(main,
func(path string, fi os.FileInfo) (bool, error) {
// delete all plugin/dir older then stalePluginTimeout
if time.Since(fi.ModTime()) > stalePluginTimeout {
log.Info("clearing stale plugin path", "path", path)
return true, nil
}
return false, nil
})
if err != nil {
log.Error("unable to clean up main plugin cache dir", "err", err)
}

return &pluginCache{
&sync.RWMutex{},
log,
Expand Down
72 changes: 72 additions & 0 deletions sysutil/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"sync"
)

Expand Down Expand Up @@ -114,3 +115,74 @@ func CopyDir(src string, dst string, withReplace bool) error {

return nil
}

func IsDirEmpty(path string) (bool, error) {
dirents, err := os.ReadDir(path)
if err != nil {
return false, err
}
return len(dirents) == 0, nil
}

func RemoveDirContentsRecursiveIf(dir string, fn func(path string, fi os.FileInfo) (bool, error)) error {
var errs []error

// check if any file/dir needs to be removed from current dir
if err := RemoveDirContentsIf(dir, fn); err != nil {
errs = append(errs, err)
}

// read current dir and check sub directories
dirEnts, err := os.ReadDir(dir)
if err != nil {
return err
}

for _, fi := range dirEnts {
if !fi.IsDir() {
continue
}
p := filepath.Join(dir, fi.Name())
if err := RemoveDirContentsRecursiveIf(p, fn); err != nil {
errs = append(errs, err)
}
}

if len(errs) != 0 {
return fmt.Errorf("%s", errs)
}

return nil
}

// RemoveDirContentsIf iterated the specified dir and removes entries
// if given function returns true for the given entry
func RemoveDirContentsIf(dir string, fn func(path string, fi os.FileInfo) (bool, error)) error {
dirEnts, err := os.ReadDir(dir)
if err != nil {
return err
}

// Save errors until the end.
var errs []error
for _, fi := range dirEnts {
p := filepath.Join(dir, fi.Name())
stat, err := os.Stat(p)
if err != nil {
return err
}
if shouldDelete, err := fn(p, stat); err != nil {
return err
} else if !shouldDelete {
continue
}
if err := RemoveAll(p); err != nil {
errs = append(errs, err)
}
}

if len(errs) != 0 {
return fmt.Errorf("%s", errs)
}
return nil
}
Loading

0 comments on commit 471d7b2

Please sign in to comment.