Merge branch 'main' into tm-wwid-raspberry-pi

This commit is contained in:
Timothy Middelkoop
2024-03-12 12:51:54 -05:00
committed by GitHub
23 changed files with 534 additions and 643 deletions

View File

@@ -118,15 +118,26 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
ExpectContinueTimeout: 1 * time.Second,
},
}
smbiosDump, err := smbios.New()
if err != nil {
wwlog.Error("Could not get SMBIOS info: %s", err)
os.Exit(1)
var localUUID uuid.UUID
var tag string
smbiosDump, smbiosErr := smbios.New()
if smbiosErr == nil {
sysinfoDump := smbiosDump.SystemInformation()
localUUID, _ = sysinfoDump.UUID()
x := smbiosDump.SystemEnclosure()
tag = strings.ReplaceAll(x.AssetTagNumber(), " ", "_")
} else {
// Raspberry Pi serial and DUID locations
// /sys/firmware/devicetree/base/serial-number
// /sys/firmware/devicetree/base/chosen/rpi-duid
piSerial, err := os.ReadFile("/sys/firmware/devicetree/base/serial-number")
if err != nil {
wwlog.Error("Could not get SMBIOS info: %s", smbiosErr)
os.Exit(1)
}
localUUID = uuid.NewSHA1(uuid.NameSpaceURL, []byte("http://raspberrypi.com/serial-number/"+string(piSerial)))
tag = "Unknown"
}
sysinfoDump := smbiosDump.SystemInformation()
localUUID, _ := sysinfoDump.UUID()
x := smbiosDump.SystemEnclosure()
tag := strings.ReplaceAll(x.AssetTagNumber(), " ", "_")
cmdline, err := os.ReadFile("/proc/cmdline")
if err != nil {

View File

@@ -26,9 +26,9 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
}
if len(profileInfo.Output) > 0 {
ph := helper.NewPrintHelper(strings.Split(profileInfo.Output[0], "="))
ph := helper.NewPrintHelper(strings.Split(profileInfo.Output[0], ":=:"))
for _, val := range profileInfo.Output[1:] {
ph.Append(strings.Split(val, "="))
ph.Append(strings.Split(val, ":=:"))
}
ph.Render()
}

View File

@@ -32,20 +32,20 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
if ShowOpt.ShowAll || ShowOpt.ShowFullAll {
for _, p := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%s=%s=%s=%s", "PROFILE", "FIELD", "PROFILE", "VALUE"))
fmt.Sprintf("%s:=:%s:=:%s", "PROFILE", "FIELD", "VALUE"))
fields := p.GetFields(ShowOpt.ShowFullAll)
for _, f := range fields {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), f.Field, f.Source, f.Value))
fmt.Sprintf("%s:=:%s:=:%s", p.Id.Print(), f.Field, f.Value))
}
}
} else {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%s=%s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
fmt.Sprintf("%s:=:%s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
for _, profile := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%s=%s", profile.Id.Print(), profile.Comment.Print()))
fmt.Sprintf("%s:=:%s", profile.Id.Print(), profile.Comment.Print()))
}
}
return

View File

@@ -13,6 +13,8 @@ import (
"syscall"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -350,8 +352,9 @@ func RenderTemplateFile(fileName string, data TemplateStruct) (
err error) {
backupFile = true
writeFile = true
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(template.FuncMap{
// TODO: Fix for missingkey=zero
// Build our FuncMap
funcMap := template.FuncMap{
"Include": templateFileInclude,
"IncludeFrom": templateContainerFileInclude,
"IncludeBlock": templateFileBlock,
@@ -377,21 +380,20 @@ func RenderTemplateFile(fileName string, data TemplateStruct) (
backupFile = false
return ""
},
"split": func(s string, d string) []string {
return strings.Split(s, d)
},
"tr": func(source, old, new string) string {
return strings.Replace(source, old, new, -1)
},
"replace": func(source, old, new string) string {
return strings.Replace(source, old, new, -1)
},
// }).ParseGlob(path.Join(OverlayDir, destFile+".ww*"))
}).ParseGlob(fileName)
}
// Merge sprig.FuncMap with our FuncMap
for key, value := range sprig.TxtFuncMap() {
funcMap[key] = value
}
// Create the template with the merged FuncMap
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(funcMap).ParseGlob(fileName)
if err != nil {
err = errors.Wrap(err, "could not parse template "+fileName)
return
}
err = tmpl.Execute(&buffer, data)
if err != nil {
err = errors.Wrap(err, "could not execute template")

View File

@@ -6,10 +6,12 @@ import (
"fmt"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/container"
"github.com/warewulf/warewulf/internal/pkg/kernel"
@@ -226,7 +228,11 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) {
return
}
tmpl, err := template.ParseFiles(stage_file)
// Create a template with the Sprig functions.
tmpl := template.New(filepath.Base(stage_file)).Funcs(sprig.TxtFuncMap())
// Parse the template.
parsedTmpl, err := tmpl.ParseFiles(stage_file)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
wwlog.ErrorExc(err, "")
@@ -236,7 +242,7 @@ func ProvisionSend(w http.ResponseWriter, req *http.Request) {
// template engine writes file to buffer in case rendering fails
var buf bytes.Buffer
err = tmpl.Execute(&buf, tmpl_data)
err = parsedTmpl.Execute(&buf, tmpl_data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
wwlog.ErrorExc(err, "")