Skip to content

Commit

Permalink
try to clean up cache if updating grypeDB fails
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Jul 13, 2023
1 parent 3c1acf1 commit e3590f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion adapters/v1/grype.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"context"
"os"
"path"
"sync"
"time"
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tools
import (
"encoding/json"
"os"
"path"
"regexp"
"runtime/debug"
"testing"
Expand Down Expand Up @@ -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
}

0 comments on commit e3590f7

Please sign in to comment.