From 13c426de1c39b24bd4b3a202f67328783d89fa1a Mon Sep 17 00:00:00 2001 From: griznog Date: Thu, 27 Jul 2023 07:27:20 -0500 Subject: [PATCH] Add an argument to the runtime/system overlay related functions to carry the context to getOverlayFile() for building the img name. --- internal/pkg/overlay/config.go | 24 ++++++++++++++++++++++-- internal/pkg/overlay/overlay.go | 25 +++++++++++++++++-------- internal/pkg/warewulfd/provision.go | 8 +++++++- internal/pkg/warewulfd/util.go | 5 +++-- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/internal/pkg/overlay/config.go b/internal/pkg/overlay/config.go index d2a9e924..8cc09d95 100644 --- a/internal/pkg/overlay/config.go +++ b/internal/pkg/overlay/config.go @@ -5,6 +5,7 @@ import ( "path" "strings" + "github.com/hpcng/warewulf/internal/pkg/wwlog" warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" ) @@ -30,7 +31,26 @@ func OverlaySourceDir(overlayName string) string { /* Returns the overlay name of the image for a given node */ -func OverlayImage(nodeName string, overlayName []string) string { +func OverlayImage(nodeName string, overlayName []string, img_context ...string) string { + var name string + var context string + + /* Check optional context argument. If missing, default to legacy. */ + if len(img_context) == 0 { + context = "legacy" + } else { + context = img_context[0] + } + conf := warewulfconf.Get() - return path.Join(conf.Paths.WWProvisiondir, "overlays/", nodeName, strings.Join(overlayName, "-")+".img") + + switch context { + case "legacy": + name = strings.Join(overlayName, "-")+".img" + default: + wwlog.Warn("Context %s passed to OverlayImage(), using %s to build image name.", context, "__" + strings.ToUpper(context) + "__") + name = "__" + strings.ToUpper(context) + "__.img" + } + + return path.Join(conf.Paths.WWProvisiondir, "overlays/", nodeName, name) } diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 0d441cc7..ec505c28 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -47,13 +47,13 @@ func BuildAllOverlays(nodes []node.NodeInfo) error { sysOverlays := n.SystemOverlay.GetSlice() wwlog.Info("Building system overlays for %s: [%s]", n.Id.Get(), strings.Join(sysOverlays, ", ")) - err := BuildOverlay(n, sysOverlays) + err := BuildOverlay(n, sysOverlays, "system") if err != nil { return errors.Wrapf(err, "could not build system overlays %v for node %s", sysOverlays, n.Id.Get()) } runOverlays := n.RuntimeOverlay.GetSlice() wwlog.Info("Building runtime overlays for %s: [%s]", n.Id.Get(), strings.Join(runOverlays, ", ")) - err = BuildOverlay(n, runOverlays) + err = BuildOverlay(n, runOverlays, "runtime") if err != nil { return errors.Wrapf(err, "could not build runtime overlays %v for node %s", runOverlays, n.Id.Get()) } @@ -66,11 +66,12 @@ func BuildAllOverlays(nodes []node.NodeInfo) error { func BuildSpecificOverlays(nodes []node.NodeInfo, overlayNames []string) error { for _, n := range nodes { - wwlog.Info("Building overlay for %s: %v", n.Id.Get(), overlayNames) - err := BuildOverlay(n, overlayNames) - if err != nil { - return errors.Wrapf(err, "could not build overlay for node %s: %v", n.Id.Get(), overlayNames) + for _, overlayName := range overlayNames { + err := BuildOverlay(n, []string{overlayName}) + if err != nil { + return errors.Wrapf(err, "could not build overlay %s for node %s", overlayName, n.Id.Get()) + } } } @@ -139,10 +140,18 @@ func OverlayInit(overlayName string) error { /* Build the given overlays for a node and create a Image for them */ -func BuildOverlay(nodeInfo node.NodeInfo, overlayNames []string) error { +func BuildOverlay(nodeInfo node.NodeInfo, overlayNames []string, img_context ...string) error { + var context string + /* Check optional context argument. If missing, default to legacy. */ + if len(img_context) == 0 { + context = "legacy" + } else { + context = img_context[0] + } + // create the dir where the overlay images will reside name := fmt.Sprintf("overlay %s/%v", nodeInfo.Id.Get(), overlayNames) - overlayImage := OverlayImage(nodeInfo.Id.Get(), overlayNames) + overlayImage := OverlayImage(nodeInfo.Id.Get(), overlayNames, context) overlayImageDir := path.Dir(overlayImage) err := os.MkdirAll(overlayImageDir, 0755) diff --git a/internal/pkg/warewulfd/provision.go b/internal/pkg/warewulfd/provision.go index 60457208..d83fd70d 100644 --- a/internal/pkg/warewulfd/provision.go +++ b/internal/pkg/warewulfd/provision.go @@ -59,6 +59,8 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) { status_stage := status_stages[rinfo.stage] var stage_overlays []string var stage_file string = "" + var img_context string = "legacy" /* Default to old image name behavior */ + // TODO: when module version is upgraded to go1.18, should be 'any' type var tmpl_data interface{} @@ -128,6 +130,7 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) { } else if rinfo.stage == "system" { if len(node.SystemOverlay.GetSlice()) != 0 { stage_overlays = node.SystemOverlay.GetSlice() + img_context = rinfo.stage } else { wwlog.Warn("No system overlay set for node %s", node.Id.Get()) } @@ -135,8 +138,10 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) { } else if rinfo.stage == "runtime" { if rinfo.overlay != "" { stage_overlays = []string{rinfo.overlay} + img_context = "legacy" } else if len(node.RuntimeOverlay.GetSlice()) != 0 { stage_overlays = node.RuntimeOverlay.GetSlice() + img_context = rinfo.stage } else { wwlog.Warn("No runtime overlay set for node %s", node.Id.Get()) } @@ -147,7 +152,8 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) { stage_file, err = getOverlayFile( node.Id.Get(), stage_overlays, - conf.Warewulf.AutobuildOverlays) + conf.Warewulf.AutobuildOverlays, + img_context ) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/pkg/warewulfd/util.go b/internal/pkg/warewulfd/util.go index 6b2275fe..4e497c6f 100644 --- a/internal/pkg/warewulfd/util.go +++ b/internal/pkg/warewulfd/util.go @@ -44,9 +44,10 @@ func sendFile( func getOverlayFile( nodeId string, stage_overlays []string, - autobuild bool ) (stage_file string, err error) { + autobuild bool, + img_context string) (stage_file string, err error) { - stage_file = overlay.OverlayImage(nodeId, stage_overlays) + stage_file = overlay.OverlayImage(nodeId, stage_overlays, img_context) err = nil build := !util.IsFile(stage_file)