Rename and refactor overlay constructor functions

- Reduces redundancy
- Adds clarity

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-12-18 06:56:43 -07:00
committed by Christian Goll
parent 717241aa18
commit ac49ecef70
14 changed files with 93 additions and 67 deletions

View File

@@ -21,11 +21,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return fmt.Errorf("could not convert requested mode: %s", err) return fmt.Errorf("could not convert requested mode: %s", err)
} }
err = overlay.CreateSiteOverlay(overlayName) err = overlay.CloneSiteOverlay(overlayName)
if err != nil { if err != nil {
return err return err
} }
overlaySourceDir, _ = overlay.OverlaySourceDir(overlayName) overlaySourceDir, _ = overlay.GetOverlay(overlayName)
if !util.IsDir(overlaySourceDir) { if !util.IsDir(overlaySourceDir) {
return fmt.Errorf("overlay does not exist: %s", overlayName) return fmt.Errorf("overlay does not exist: %s", overlayName)

View File

@@ -34,11 +34,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
} else { } else {
gid = -1 gid = -1
} }
err = overlay.CreateSiteOverlay(overlayName) err = overlay.CloneSiteOverlay(overlayName)
if err != nil { if err != nil {
return err return err
} }
overlaySourceDir, _ = overlay.OverlaySourceDir(overlayName) overlaySourceDir, _ = overlay.GetOverlay(overlayName)
if !util.IsDir(overlaySourceDir) { if !util.IsDir(overlaySourceDir) {
return fmt.Errorf("overlay does not exist: %s", overlayName) return fmt.Errorf("overlay does not exist: %s", overlayName)

View File

@@ -6,6 +6,6 @@ import (
) )
func CobraRunE(cmd *cobra.Command, args []string) (err error) { func CobraRunE(cmd *cobra.Command, args []string) (err error) {
err = overlay.OverlayInit(args[0]) _, err = overlay.CreateSiteOverlay(args[0])
return return err
} }

View File

@@ -22,7 +22,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
fileName = args[1] fileName = args[1]
} }
overlayPath, isSite := overlay.OverlaySourceDir(overlayName) overlayPath, isSite := overlay.GetOverlay(overlayName)
if !isSite { if !isSite {
return fmt.Errorf("distribution overlay can't deleted") return fmt.Errorf("distribution overlay can't deleted")

View File

@@ -36,9 +36,9 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
overlayName := args[0] overlayName := args[0]
fileName := args[1] fileName := args[1]
createdSite := false createdSite := false
overlaySourceDir, isSite := overlay.OverlaySourceDir(overlayName) overlaySourceDir, isSite := overlay.GetOverlay(overlayName)
if !isSite { if !isSite {
err = overlay.CreateSiteOverlay(overlayName) err = overlay.CloneSiteOverlay(overlayName)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -27,7 +27,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
} }
wwlog.Verbose("Copying '%s' into overlay '%s:%s'", source, overlayName, dest) wwlog.Verbose("Copying '%s' into overlay '%s:%s'", source, overlayName, dest)
overlaySource, _ = overlay.OverlaySourceDir(overlayName) overlaySource, _ = overlay.GetOverlay(overlayName)
if !util.IsDir(overlaySource) { if !util.IsDir(overlaySource) {
return fmt.Errorf("overlay does not exist: %s", overlayName) return fmt.Errorf("overlay does not exist: %s", overlayName)

View File

@@ -32,7 +32,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
for o := range overlays { for o := range overlays {
name := overlays[o] name := overlays[o]
path, isSite := overlay.OverlaySourceDir(name) path, isSite := overlay.GetOverlay(name)
if util.IsDir(path) { if util.IsDir(path) {
files := util.FindFiles(path) files := util.FindFiles(path)

View File

@@ -16,11 +16,11 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
overlayName := args[0] overlayName := args[0]
dirName := args[1] dirName := args[1]
err = overlay.CreateSiteOverlay(overlayName) err = overlay.CloneSiteOverlay(overlayName)
if err != nil { if err != nil {
return err return err
} }
overlaySourceDir, _ = overlay.OverlaySourceDir(overlayName) overlaySourceDir, _ = overlay.GetOverlay(overlayName)
if !util.IsDir(overlaySourceDir) { if !util.IsDir(overlaySourceDir) {
return fmt.Errorf("overlay does not exist: %s", overlayName) return fmt.Errorf("overlay does not exist: %s", overlayName)

View File

@@ -22,7 +22,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlayName := args[0] overlayName := args[0]
fileName := args[1] fileName := args[1]
overlaySourceDir, _ = overlay.OverlaySourceDir(overlayName) overlaySourceDir, _ = overlay.GetOverlay(overlayName)
if !util.IsDir(overlaySourceDir) { if !util.IsDir(overlaySourceDir) {
return fmt.Errorf("overlay dir: %s does not exist", overlaySourceDir) return fmt.Errorf("overlay dir: %s does not exist", overlaySourceDir)

View File

@@ -79,11 +79,11 @@ func (paths BuildConfig) OciBlobCachedir() string {
return path.Join(paths.Cachedir, "warewulf") return path.Join(paths.Cachedir, "warewulf")
} }
func (paths BuildConfig) SiteOverlaySourcedir() string { func (paths BuildConfig) SiteOverlaydir() string {
return paths.WWOverlaydir return paths.WWOverlaydir
} }
func (paths BuildConfig) DistributionOverlaySourcedir() string { func (paths BuildConfig) DistributionOverlaydir() string {
return path.Join(paths.Datadir, "warewulf", "overlays") return path.Join(paths.Datadir, "warewulf", "overlays")
} }

View File

@@ -14,7 +14,7 @@ import (
Creates '/etc/hosts' from the host template. Creates '/etc/hosts' from the host template.
*/ */
func Hostfile() (err error) { func Hostfile() (err error) {
overlaySourceDir, _ := overlay.OverlaySourceDir("host") overlaySourceDir, _ := overlay.GetOverlay("host")
hostTemplate := path.Join(overlaySourceDir, "/etc/hosts.ww") hostTemplate := path.Join(overlaySourceDir, "/etc/hosts.ww")
if !(util.IsFile(hostTemplate)) { if !(util.IsFile(hostTemplate)) {
return fmt.Errorf("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay") return fmt.Errorf("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay")

View File

@@ -13,28 +13,82 @@ import (
"github.com/warewulf/warewulf/internal/pkg/wwlog" "github.com/warewulf/warewulf/internal/pkg/wwlog"
) )
/* // GetOverlay returns the filesystem path of an overlay identified by its name,
Return the path for the base of the overlay, adds rootfs // along with a boolean indicating whether the returned overlayPath corresponds
prefix in the overlay dir if this it exists // to a site-specific overlay.
*/ func GetOverlay(name string) (overlayPath string, isSite bool) {
func OverlaySourceDir(overlayName string) (overlaypath string, isSite bool) { overlayPath = GetSiteOverlay(name)
if _, err := os.Stat(overlayPath); err == nil {
return overlayPath, true
}
overlayPath = GetDistributionOverlay(name)
return overlayPath, false
}
// GetDistributionOverlay returns the filesystem path of a distribution overlay
// identified by the given name.
func GetDistributionOverlay(name string) (overlayPath string) {
controller := warewulfconf.Get() controller := warewulfconf.Get()
return getOverlay(controller.Paths.DistributionOverlaydir(), name)
}
// GetSiteOverlay returns the filesystem path of a site-specific overlay
// identified by the given name.
func GetSiteOverlay(name string) (overlayPath string) {
controller := warewulfconf.Get()
return getOverlay(controller.Paths.SiteOverlaydir(), name)
}
// getOverlay constructs the filesystem path of an overlay based on the given
// overlay directory and overlay name.
//
// The returned path will include a "rootfs" directory if it exists.
func getOverlay(overlaydir, name string) (overlayPath string) {
/* Assume using old style overlay dir without rootfs */ /* Assume using old style overlay dir without rootfs */
overlaypath = path.Join(controller.Paths.SiteOverlaySourcedir(), overlayName) overlayPath = path.Join(overlaydir, name)
if _, err := os.Stat(path.Join(overlaypath, "rootfs")); err == nil { if _, err := os.Stat(path.Join(overlayPath, "rootfs")); err == nil {
/* rootfs exists, use it. */ /* rootfs exists, use it. */
overlaypath = path.Join(overlaypath, "rootfs") overlayPath = path.Join(overlayPath, "rootfs")
} }
if _, err := os.Stat(overlaypath); err == nil { return overlayPath
return overlaypath, true
} }
overlaypath = path.Join(controller.Paths.DistributionOverlaySourcedir(), overlayName)
if _, err := os.Stat(path.Join(overlaypath, "rootfs")); err == nil { // CreateSiteOverlay creates a new site overlay directory with the specified name.
/* rootfs exists, use it. */ //
overlaypath = path.Join(overlaypath, "rootfs") // This function constructs the path for the new overlay based on the site's overlay directory
// configuration and checks if the directory already exists. If the overlay already exists,
// it returns an error. Otherwise, it creates the necessary directory structure, including a
// rootfs directory.
//
// Parameters:
// - overlayName: The name of the site overlay to be created.
//
// Returns:
// - overlayPath: The full path to the created site overlay directory.
// - err: An error if the overlay already exists or if directory creation fails.
func CreateSiteOverlay(overlayName string) (overlayPath string, err error) {
controller := warewulfconf.Get()
overlayPath = path.Join(controller.Paths.SiteOverlaydir(), overlayName)
if util.IsDir(overlayPath) {
return overlayPath, fmt.Errorf("overlay already exists: %s", overlayName)
} }
wwlog.Debug("found overlay %s in path: %s", overlayName, overlaypath) overlayPath = path.Join(overlayPath, "rootfs")
return overlaypath, false err = os.MkdirAll(overlayPath, 0755)
return overlayPath, err
}
// Creates a site overlay from an existing distribution overlay.
//
// If the distribution overlay doesn't exist, return an OverlayDoesNotExist error.
func CloneSiteOverlay(name string) (err error) {
controller := warewulfconf.Get()
distroPath := path.Join(controller.Paths.DistributionOverlaydir(), name)
sitePath := path.Join(controller.Paths.SiteOverlaydir(), name)
if !util.IsDir(distroPath) {
return &OverlayDoesNotExist{Name: name}
}
err = copy.DirCopy(distroPath, sitePath, copy.Content, true)
return err
} }
// OverlayImage returns the full path to an overlay image based on the // OverlayImage returns the full path to an overlay image based on the
@@ -74,16 +128,3 @@ type OverlayDoesNotExist struct {
func (e *OverlayDoesNotExist) Error() string { func (e *OverlayDoesNotExist) Error() string {
return fmt.Sprintf("overlay %s does not exist", e.Name) return fmt.Sprintf("overlay %s does not exist", e.Name)
} }
// Creates a site overlay from an existing overlay and give back
// OverlayDoesNotExist error if distribution overlay doesn't exsist
func CreateSiteOverlay(name string) (err error) {
controller := warewulfconf.Get()
distroPath := path.Join(controller.Paths.DistributionOverlaySourcedir(), name)
sitePath := path.Join(controller.Paths.SiteOverlaySourcedir(), name)
if !util.IsDir(distroPath) {
return &OverlayDoesNotExist{Name: name}
}
err = copy.DirCopy(distroPath, sitePath, copy.Content, true)
return err
}

View File

@@ -71,7 +71,7 @@ func BuildHostOverlay() error {
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
hostData := node.NewNode(hostname) hostData := node.NewNode(hostname)
wwlog.Info("Building overlay for %s: host", hostname) wwlog.Info("Building overlay for %s: host", hostname)
hostdir, _ := OverlaySourceDir("host") hostdir, _ := GetOverlay("host")
stats, err := os.Stat(hostdir) stats, err := os.Stat(hostdir)
if err != nil { if err != nil {
return fmt.Errorf("could not build host overlay: %w ", err) return fmt.Errorf("could not build host overlay: %w ", err)
@@ -89,10 +89,10 @@ func FindOverlays() (overlayList []string, err error) {
dotfilecheck, _ := regexp.Compile(`^\..*`) dotfilecheck, _ := regexp.Compile(`^\..*`)
controller := warewulfconf.Get() controller := warewulfconf.Get()
var files []fs.DirEntry var files []fs.DirEntry
if distfiles, err := os.ReadDir(controller.Paths.DistributionOverlaySourcedir()); err == nil { if distfiles, err := os.ReadDir(controller.Paths.DistributionOverlaydir()); err == nil {
files = append(files, distfiles...) files = append(files, distfiles...)
} }
if sitefiles, err := os.ReadDir(path.Join(controller.Paths.SiteOverlaySourcedir())); err == nil { if sitefiles, err := os.ReadDir(path.Join(controller.Paths.SiteOverlaydir())); err == nil {
files = append(files, sitefiles...) files = append(files, sitefiles...)
} }
for _, file := range files { for _, file := range files {
@@ -107,21 +107,6 @@ func FindOverlays() (overlayList []string, err error) {
return overlayList, nil return overlayList, nil
} }
/*
Creates an empty overlay
*/
func OverlayInit(overlayName string) error {
controller := warewulfconf.Get()
overlayPath := path.Join(controller.Paths.Sysconfdir, "overlays", overlayName)
if util.IsDir(overlayPath) {
return fmt.Errorf("overlay already exists: %s", overlayName)
}
err := os.MkdirAll(path.Join(overlayPath, "rootfs"), 0755)
return err
}
/* /*
Build the given overlays for a node and create a Image for them Build the given overlays for a node and create a Image for them
*/ */
@@ -192,7 +177,7 @@ func BuildOverlayIndir(nodeData node.Node, overlayNames []string, outputDir stri
wwlog.Verbose("Processing node/overlay: %s/%s", nodeData.Id(), strings.Join(overlayNames, "-")) wwlog.Verbose("Processing node/overlay: %s/%s", nodeData.Id(), strings.Join(overlayNames, "-"))
for _, overlayName := range overlayNames { for _, overlayName := range overlayNames {
wwlog.Verbose("Building overlay %s for node %s in %s", overlayName, nodeData.Id(), outputDir) wwlog.Verbose("Building overlay %s for node %s in %s", overlayName, nodeData.Id(), outputDir)
overlaySourceDir, _ := OverlaySourceDir(overlayName) overlaySourceDir, _ := GetOverlay(overlayName)
wwlog.Debug("Changing directory to OverlayDir: %s", overlaySourceDir) wwlog.Debug("Changing directory to OverlayDir: %s", overlaySourceDir)
err := os.Chdir(overlaySourceDir) err := os.Chdir(overlaySourceDir)
if err != nil { if err != nil {
@@ -431,7 +416,7 @@ func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Get all the files as a string slice for a given overlay // Get all the files as a string slice for a given overlay
func OverlayGetFiles(name string) (files []string, err error) { func OverlayGetFiles(name string) (files []string, err error) {
baseDir, _ := OverlaySourceDir(name) baseDir, _ := GetOverlay(name)
if !util.IsDir(baseDir) { if !util.IsDir(baseDir) {
err = fmt.Errorf("overlay %s doesn't exist", name) err = fmt.Errorf("overlay %s doesn't exist", name)
return return

View File

@@ -54,7 +54,7 @@ func getOverlayFile(n node.Node, context string, stage_overlays []string, autobu
build = util.PathIsNewer(stage_file, config.Get().Paths.NodesConf()) build = util.PathIsNewer(stage_file, config.Get().Paths.NodesConf())
for _, overlayname := range stage_overlays { for _, overlayname := range stage_overlays {
overlayDir, _ := overlay.OverlaySourceDir(overlayname) overlayDir, _ := overlay.GetOverlay(overlayname)
build = build || util.PathIsNewer(stage_file, overlayDir) build = build || util.PathIsNewer(stage_file, overlayDir)
} }
} }