refactored overlay class
overlay.GetOverlay(name) returns now an error if the overlay doesn't exist. This is the most canonical way to act if there is no overlay.
This commit is contained in:
committed by
Jonathon Anderson
parent
c17fe8d512
commit
6f4fd60d8f
@@ -5,10 +5,8 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/overlay"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
@@ -17,7 +15,6 @@ import (
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
var dest string
|
||||
|
||||
overlayName := args[0]
|
||||
source := args[1]
|
||||
|
||||
if len(args) == 3 {
|
||||
@@ -25,25 +22,23 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
} else {
|
||||
dest = source
|
||||
}
|
||||
|
||||
wwlog.Verbose("Copying '%s' into overlay '%s:%s'", source, overlayName, dest)
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
overlay_, err := overlay.GetOverlay(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !overlay_.IsSiteOverlay() {
|
||||
overlay_, err = overlay_.CloneSiteOverlay()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
if util.IsDir(overlay_.File(dest)) {
|
||||
dest = path.Join(dest, path.Base(source))
|
||||
}
|
||||
|
||||
if !OverwriteFile && util.IsFile(overlay_.File(dest)) {
|
||||
return fmt.Errorf("a file with that name already exists in the overlay: %s", overlayName)
|
||||
return fmt.Errorf("a file with that name already exists in the overlay")
|
||||
}
|
||||
|
||||
if CreateDirs {
|
||||
@@ -68,33 +63,5 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
return fmt.Errorf("could not copy file into overlay: %w", err)
|
||||
}
|
||||
|
||||
if !NoOverlayUpdate {
|
||||
n, err := node.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open node configuration: %s", err)
|
||||
}
|
||||
|
||||
nodes, err := n.FindAllNodes()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get node list: %s", err)
|
||||
}
|
||||
|
||||
var updateNodes []node.Node
|
||||
|
||||
for _, node := range nodes {
|
||||
if util.InSlice(node.SystemOverlay, overlayName) {
|
||||
updateNodes = append(updateNodes, node)
|
||||
} else if util.InSlice(node.RuntimeOverlay, overlayName) {
|
||||
updateNodes = append(updateNodes, node)
|
||||
}
|
||||
}
|
||||
|
||||
workers := Workers
|
||||
if workers <= 0 {
|
||||
workers = runtime.NumCPU()
|
||||
}
|
||||
return overlay.BuildSpecificOverlays(updateNodes, nodes, []string{overlayName}, workers)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ nodes: {}
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
baseCmd.SetArgs([]string{"-p", "-n", "test", file.Name()})
|
||||
baseCmd.SetArgs([]string{"-p", "test", file.Name()})
|
||||
baseCmd.SetOut(nil)
|
||||
baseCmd.SetErr(nil)
|
||||
err = baseCmd.Execute()
|
||||
|
||||
@@ -24,15 +24,13 @@ var (
|
||||
}
|
||||
},
|
||||
}
|
||||
OverwriteFile bool
|
||||
NoOverlayUpdate bool
|
||||
CreateDirs bool
|
||||
Workers int
|
||||
OverwriteFile bool
|
||||
CreateDirs bool
|
||||
Workers int
|
||||
)
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&OverwriteFile, "overwrite", "o", false, "Overwrite file if exists")
|
||||
baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays")
|
||||
baseCmd.PersistentFlags().BoolVarP(&CreateDirs, "parents", "p", false, "Create any necessary parent directories")
|
||||
baseCmd.PersistentFlags().IntVar(&Workers, "workers", 0, "The number of parallel workers building overlays (<=0 indicates 1 worker per CPU)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user