Add an Overlay type with helper methods

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-12-18 11:35:57 -07:00
committed by Christian Goll
parent 7d4b7ab432
commit c03dc9436b
14 changed files with 307 additions and 176 deletions

View File

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

View File

@@ -8,7 +8,7 @@ import (
"github.com/containers/storage/drivers/copy"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -16,79 +16,59 @@ import (
// GetOverlay returns the filesystem path of an overlay identified by its name,
// along with a boolean indicating whether the returned overlayPath corresponds
// to a site-specific overlay.
func GetOverlay(name string) (overlayPath string, isSite bool) {
overlayPath = GetSiteOverlay(name)
if _, err := os.Stat(overlayPath); err == nil {
return overlayPath, true
func GetOverlay(name string) (overlay Overlay) {
overlay = GetSiteOverlay(name)
if overlay.Exists() {
return overlay
}
overlayPath = GetDistributionOverlay(name)
return overlayPath, false
overlay = GetDistributionOverlay(name)
if overlay.Exists() {
return overlay
}
return GetSiteOverlay(name)
}
// GetDistributionOverlay returns the filesystem path of a distribution overlay
// identified by the given name.
func GetDistributionOverlay(name string) (overlayPath string) {
controller := warewulfconf.Get()
return getOverlay(controller.Paths.DistributionOverlaydir(), name)
func GetDistributionOverlay(name string) (overlay Overlay) {
return getOverlay(config.Get().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)
func GetSiteOverlay(name string) (overlay Overlay) {
return getOverlay(config.Get().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 */
overlayPath = path.Join(overlaydir, name)
if _, err := os.Stat(path.Join(overlayPath, "rootfs")); err == nil {
/* rootfs exists, use it. */
overlayPath = path.Join(overlayPath, "rootfs")
}
return overlayPath
// getOverlay constructs an overlay based on the given overlay directory and
// overlay name. The overlay does not necessarily exist.
func getOverlay(overlaydir, name string) (overlay Overlay) {
return Overlay(path.Join(overlaydir, name))
}
// CreateSiteOverlay creates a new site overlay directory with the specified name.
// Create creates a new overlay directory for the given overlay
//
// 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)
// Returns an error if the overlay already exists or if directory creation fails.
func (this Overlay) Create() error {
if util.IsDir(this.Path()) {
return fmt.Errorf("overlay already exists: %s", this)
}
overlayPath = path.Join(overlayPath, "rootfs")
err = os.MkdirAll(overlayPath, 0755)
return overlayPath, err
return os.MkdirAll(this.Rootfs(), 0755)
}
// Creates a site overlay from an existing distribution overlay.
//
// If the distribution overlay doesn't exist, return an 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 fmt.Errorf("overlay %s does not exist", name)
func (this Overlay) CloneSiteOverlay() (siteOverlay Overlay, err error) {
siteOverlay = GetSiteOverlay(this.Name())
if !util.IsDir(this.Path()) {
return siteOverlay, fmt.Errorf("source overlay does not exist: %s", this.Name())
}
err = copy.DirCopy(distroPath, sitePath, copy.Content, true)
return err
if siteOverlay.Exists() {
return siteOverlay, fmt.Errorf("site overlay already exists: %s", siteOverlay.Name())
}
err = copy.DirCopy(this.Path(), siteOverlay.Path(), copy.Content, true)
return siteOverlay, err
}
// OverlayImage returns the full path to an overlay image based on the
@@ -117,6 +97,5 @@ func OverlayImage(nodeName string, context string, overlayNames []string) string
return ""
}
conf := warewulfconf.Get()
return path.Join(conf.Paths.OverlayProvisiondir(), nodeName, name)
return path.Join(config.Get().Paths.OverlayProvisiondir(), nodeName, name)
}

View File

@@ -14,9 +14,9 @@ import (
"text/template"
"github.com/Masterminds/sprig/v3"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/pkg/errors"
"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"
@@ -26,6 +26,82 @@ var (
ErrDoesNotExist = errors.New("overlay does not exist")
)
// Overlay represents an overlay directory path.
type Overlay string
// Name returns the base name of the overlay directory.
//
// This is derived from the full path of the overlay.
func (this Overlay) Name() string {
return path.Base(this.Path())
}
// Path returns the string representation of the overlay path.
//
// This method allows the Overlay type to be easily converted back to its
// underlying string representation.
func (this Overlay) Path() string {
return string(this)
}
// Rootfs returns the path to the root filesystem (rootfs) within the overlay.
//
// If the "rootfs" directory exists inside the overlay path, it returns the
// path to the "rootfs" directory. Otherwise, it checks if the overlay path
// itself is a directory and returns that. If neither exists, it defaults to
// returning the "rootfs" path.
func (this Overlay) Rootfs() string {
rootfs := path.Join(this.Path(), "rootfs")
if util.IsDir(rootfs) {
return rootfs
} else if util.IsDir(this.Path()) {
return this.Path()
} else {
return rootfs
}
}
// File constructs a full path to a file within the overlay's root filesystem.
//
// Parameters:
// - filePath: The relative path of the file within the overlay.
//
// Returns:
// - The full path to the specified file in the overlay's rootfs.
func (this Overlay) File(filePath string) string {
return path.Join(this.Rootfs(), filePath)
}
// Exists checks whether the overlay path exists and is a directory.
//
// Returns:
// - true if the overlay path exists and is a directory; false otherwise.
func (this Overlay) Exists() bool {
return util.IsDir(this.Path())
}
// IsSiteOverlay determines whether the overlay is a site overlay.
//
// A site overlay is identified by its parent directory matching the configured
// site overlay directory path.
//
// Returns:
// - true if the overlay is a site overlay; false otherwise.
func (this Overlay) IsSiteOverlay() bool {
return path.Dir(this.Path()) == config.Get().Paths.SiteOverlaydir()
}
// IsDistributionOverlay determines whether the overlay is a distribution overlay.
//
// A distribution overlay is identified by its parent directory matching the configured
// distribution overlay directory path.
//
// Returns:
// - true if the overlay is a distribution overlay; false otherwise.
func (this Overlay) IsDistributionOverlay() bool {
return path.Dir(this.Path()) == config.Get().Paths.DistributionOverlaydir()
}
/*
Build all overlays for a node
*/
@@ -71,7 +147,7 @@ func BuildHostOverlay() error {
hostname, _ := os.Hostname()
hostData := node.NewNode(hostname)
wwlog.Info("Building overlay for %s: host", hostname)
hostdir, _ := GetOverlay("host")
hostdir := GetOverlay("host").Rootfs()
stats, err := os.Stat(hostdir)
if err != nil {
return fmt.Errorf("could not build host overlay: %w ", err)
@@ -87,7 +163,7 @@ Get all overlays present in warewulf
*/
func FindOverlays() (overlayList []string, err error) {
dotfilecheck, _ := regexp.Compile(`^\..*`)
controller := warewulfconf.Get()
controller := config.Get()
var files []fs.DirEntry
if distfiles, err := os.ReadDir(controller.Paths.DistributionOverlaydir()); err == nil {
files = append(files, distfiles...)
@@ -177,7 +253,7 @@ func BuildOverlayIndir(nodeData node.Node, overlayNames []string, outputDir stri
wwlog.Verbose("Processing node/overlay: %s/%s", nodeData.Id(), strings.Join(overlayNames, "-"))
for _, overlayName := range overlayNames {
wwlog.Verbose("Building overlay %s for node %s in %s", overlayName, nodeData.Id(), outputDir)
overlaySourceDir, _ := GetOverlay(overlayName)
overlaySourceDir := GetOverlay(overlayName).Rootfs()
wwlog.Debug("Changing directory to OverlayDir: %s", overlaySourceDir)
err := os.Chdir(overlaySourceDir)
if err != nil {
@@ -416,7 +492,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
func OverlayGetFiles(name string) (files []string, err error) {
baseDir, _ := GetOverlay(name)
baseDir := GetOverlay(name).Rootfs()
if !util.IsDir(baseDir) {
err = fmt.Errorf("overlay %s doesn't exist", name)
return

View File

@@ -10,10 +10,106 @@ import (
"github.com/stretchr/testify/assert"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/testenv"
"github.com/warewulf/warewulf/internal/pkg/util"
)
func Test_OverlayMethods(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll(t)
// Setup test data
sitedir := "/var/lib/warewulf/overlays/"
distdir := "/usr/share/warewulf/overlays/"
env.WriteFile(t, path.Join(sitedir, "siteonly/rootfs/testfile"), "a site overlay")
env.WriteFile(t, path.Join(distdir, "distonly/rootfs/testfile"), "a distribution overlay")
env.WriteFile(t, path.Join(sitedir, "legacy/testfile"), "a legacy overlay")
env.WriteFile(t, path.Join(sitedir, "both/rootfs/testfile"), "the site version")
env.WriteFile(t, path.Join(distdir, "both/rootfs/testfile"), "the distribution version")
var tests = map[string]struct {
name string
path string
rootfs string
file string
content string
exists bool
isSite bool
isDist bool
}{
"site overlay": {
name: "siteonly",
path: path.Join(sitedir, "siteonly"),
rootfs: path.Join(sitedir, "siteonly/rootfs"),
file: path.Join(sitedir, "siteonly/rootfs/testfile"),
content: "a site overlay",
exists: true,
isSite: true,
isDist: false,
},
"distribution overlay": {
name: "distonly",
path: path.Join(distdir, "distonly"),
rootfs: path.Join(distdir, "distonly/rootfs"),
file: path.Join(distdir, "distonly/rootfs/testfile"),
content: "a distribution overlay",
exists: true,
isSite: false,
isDist: true,
},
"overlapping overlay": {
name: "both",
path: path.Join(sitedir, "both"),
rootfs: path.Join(sitedir, "both/rootfs"),
file: path.Join(sitedir, "both/rootfs/testfile"),
content: "the site version",
exists: true,
isSite: true,
isDist: false,
},
"legacy overlay": {
name: "legacy",
path: path.Join(sitedir, "legacy"),
rootfs: path.Join(sitedir, "legacy"),
file: path.Join(sitedir, "legacy/testfile"),
content: "",
exists: true,
isSite: true,
isDist: false,
},
"missing overlay": {
name: "absent",
path: path.Join(sitedir, "absent"),
rootfs: path.Join(sitedir, "absent/rootfs"),
file: path.Join(sitedir, "absent/rootfs/testfile"),
content: "",
exists: false,
isSite: true,
isDist: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
overlay := GetOverlay(tt.name)
assert.Equal(t, tt.name, overlay.Name())
assert.Equal(t, env.GetPath(tt.path), overlay.Path())
assert.Equal(t, env.GetPath(tt.rootfs), overlay.Rootfs())
assert.Equal(t, env.GetPath(tt.file), overlay.File("testfile"))
if tt.content != "" {
buffer, err := os.ReadFile(overlay.File("testfile"))
assert.NoError(t, err)
assert.Equal(t, tt.content, string(buffer))
}
assert.Equal(t, tt.exists, overlay.Exists())
assert.Equal(t, tt.isSite, overlay.IsSiteOverlay())
assert.Equal(t, tt.isDist, overlay.IsDistributionOverlay())
})
}
}
var buildOverlayTests = []struct {
description string
nodeName string

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())
for _, overlayname := range stage_overlays {
overlayDir, _ := overlay.GetOverlay(overlayname)
overlayDir := overlay.GetOverlay(overlayname).Rootfs()
build = build || util.PathIsNewer(stage_file, overlayDir)
}
}