Recommended refactors

- make sure spec sets new cache dir
- move clean out of api
- capture warewulfconf.Paths.Cachedir+"/warewulf" in once place
- split Clean() into multiple functions
- remove WriteFileAbs

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-10-25 17:46:42 -06:00
parent ff84974506
commit fbd5ca9895
11 changed files with 54 additions and 64 deletions

View File

@@ -1,47 +0,0 @@
package clean
import (
"os"
"path"
_ "golang.org/x/exp/slices"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
/*
Cleans up the OCI cache and remains of deleted nodes
*/
func Clean() (err error) {
warewulfconf := warewulfconf.Get()
wwlog.Verbose("removing oci cache dir: %s", path.Join(warewulfconf.Paths.Cachedir+"/warewulf"))
err = os.RemoveAll(path.Join(warewulfconf.Paths.Cachedir + "/warewulf"))
if err != nil {
return err
}
nodeDB, err := node.New()
if err != nil {
return err
}
nodes := nodeDB.ListAllNodes()
dirList, err := os.ReadDir(path.Join(warewulfconf.Paths.WWProvisiondir, "overlays/"))
if err != nil {
return err
}
for _, item := range dirList {
if !item.IsDir() {
continue
}
if !util.InSlice(nodes, item.Name()) {
wwlog.Verbose("removing overlays of delete node: %s", item.Name())
err = os.RemoveAll(path.Join(warewulfconf.Paths.WWProvisiondir, "overlays/", item.Name()))
if err != nil {
return err
}
}
}
return
}