From 5c23a4cdded5aabd8478bd7027a54e1a226d8687 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 24 Jan 2024 11:15:45 +0100 Subject: [PATCH 1/2] added {{ .Overlay }} to templates {{ .Overlay }} contains the overlay name in which the template is located, or an empty string if RenderTempalte is called outside the warewulf overlay tree. - Closes #1052 Signed-off-by: Christian Goll --- CHANGELOG.md | 1 + internal/app/wwctl/overlay/show/main_test.go | 21 +++++++++++++++++--- internal/pkg/overlay/datastructure.go | 1 + internal/pkg/overlay/overlay.go | 12 +++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0ba95bc..84db043e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Document warewulf.conf:paths. #635 +- New "Overlay" template variable contains the name of the overlay being built. #1052 ### Changed diff --git a/internal/app/wwctl/overlay/show/main_test.go b/internal/app/wwctl/overlay/show/main_test.go index 8b32eef0..edca7005 100644 --- a/internal/app/wwctl/overlay/show/main_test.go +++ b/internal/app/wwctl/overlay/show/main_test.go @@ -14,8 +14,11 @@ import ( ) var ( - overlayCont = ` + overlayEmail = ` {{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }} +` + overlayOverlay = ` +overlay name {{ .Overlay }} ` ) @@ -38,7 +41,8 @@ nodes: - empty `) - env.WriteFile(t, path.Join(testenv.WWOverlaydir, "testoverlay/email.ww"), overlayCont) + env.WriteFile(t, path.Join(testenv.WWOverlaydir, "testoverlay/email.ww"), overlayEmail) + env.WriteFile(t, path.Join(testenv.WWOverlaydir, "testoverlay/overlay.ww"), overlayOverlay) defer env.RemoveAll(t) warewulfd.SetNoDaemon() t.Run("overlay show raw", func(t *testing.T) { @@ -50,7 +54,7 @@ nodes: wwlog.SetLogWriter(buf) err := baseCmd.Execute() assert.NoError(t, err) - assert.Contains(t, buf.String(), overlayCont) + assert.Contains(t, buf.String(), overlayEmail) }) t.Run("overlay show rendered node tag", func(t *testing.T) { baseCmd.SetArgs([]string{"-r", "node1", "testoverlay", "email.ww"}) @@ -85,6 +89,17 @@ nodes: assert.NoError(t, err) assert.Contains(t, buf.String(), "noMail") }) + t.Run("overlay shows overlay", func(t *testing.T) { + baseCmd.SetArgs([]string{"-r", "node1", "testoverlay", "overlay.ww"}) + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) + err := baseCmd.Execute() + assert.NoError(t, err) + assert.Contains(t, buf.String(), "testoverlay") + }) } func TestShowServerTemplate(t *testing.T) { diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index be813a8a..43f018a9 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -27,6 +27,7 @@ type TemplateStruct struct { Netmask string Network string NetworkCIDR string + Overlay string Ipv6 bool Dhcp warewulfconf.DHCPConf Nfs warewulfconf.NFSConf diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 9bb98aff..c0f93a24 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -352,6 +352,18 @@ func RenderTemplateFile(fileName string, data TemplateStruct) ( err error) { backupFile = true writeFile = true + // parse the overlay name out of the absolute path + overlayPath, _ := filepath.Rel(OverlaySourceTopDir(), fileName) + withoutRootfs, err := filepath.Rel("rootfs", overlayPath) + if err != nil { + overlayPath = withoutRootfs + } + overlayPathElem := strings.Split(overlayPath, "/") + if len(overlayPathElem) > 1 { + data.Overlay = overlayPathElem[0] + } else { + data.Overlay = "" + } // Build our FuncMap funcMap := template.FuncMap{ From ab881dbb37ab7a574cbd9c164522756a9bbe7dfc Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 5 Apr 2024 19:11:46 -0600 Subject: [PATCH 2/2] Add Overlay variable to debug template Signed-off-by: Jonathon Anderson --- overlays/debug/rootfs/warewulf/template-variables.md.ww | 1 + 1 file changed, 1 insertion(+) diff --git a/overlays/debug/rootfs/warewulf/template-variables.md.ww b/overlays/debug/rootfs/warewulf/template-variables.md.ww index 8c92c4f5..1a2940eb 100644 --- a/overlays/debug/rootfs/warewulf/template-variables.md.ww +++ b/overlays/debug/rootfs/warewulf/template-variables.md.ww @@ -72,6 +72,7 @@ data from other structures. - BuildTime: {{ .BuildTime }} - BuildTimeUnix: {{ .BuildTimeUnix }} - BuildSource: {{ .BuildSource }} +- Overlay: {{ .Overlay }} ## Network