diff --git a/adapters/v1/grype.go b/adapters/v1/grype.go index 879c2f9..0653182 100644 --- a/adapters/v1/grype.go +++ b/adapters/v1/grype.go @@ -2,6 +2,7 @@ package v1 import ( "context" + "os" "path" "sync" "time" @@ -90,7 +91,11 @@ func (g *GrypeAdapter) Ready(ctx context.Context) bool { g.store, g.dbStatus, g.dbCloser, err = grype.LoadVulnerabilityDB(g.dbConfig, true) if err != nil { logger.L().Ctx(ctx).Error("failed to update grype DB", helpers.Error(err)) - return false + err := tools.DeleteContents(g.dbConfig.DBRootDir) + logger.L().Debug("cleaned up cache", helpers.Error(err), + helpers.String("DBRootDir", g.dbConfig.DBRootDir)) + logger.L().Info("restarting to release previous grype DB") + os.Exit(0) } g.lastDbUpdate = now logger.L().Info("grype DB updated") diff --git a/internal/tools/tools.go b/internal/tools/tools.go index 9f61eaf..0dc153c 100644 --- a/internal/tools/tools.go +++ b/internal/tools/tools.go @@ -3,6 +3,7 @@ package tools import ( "encoding/json" "os" + "path" "regexp" "runtime/debug" "testing" @@ -83,3 +84,17 @@ func FileToCVEManifest(path string) domain.CVEManifest { } return cve } + +func DeleteContents(dir string) error { + d, err := os.ReadDir(dir) + if err != nil { + return err + } + for _, c := range d { + err := os.RemoveAll(path.Join([]string{dir, c.Name()}...)) + if err != nil { + return err + } + } + return nil +}