Files
warewulf/internal/pkg/configure/hostfile.go
Jonathon Anderson 0b3e862bea Remove trailing newline from wwlog
I noticed that some wwlog calls included a trailing newline, but others
did not. I tested both in isolation and discovered that the behavior was
consistent regardless of whether a trailing newline was included. I
further confirmed in code that wwlog appends a trailing newline
automatically if it is not present; so a trailing newline is unnecessary
in individual calls.

This commit removes trailing newlines from all calls to make them
consistent. It also replaces two calls to wwlog.Printf. (see #534)

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
2022-09-15 12:38:03 -06:00

47 lines
1.2 KiB
Go

package configure
import (
"os"
"path"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/overlay"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
)
/*
Creates '/etc/hosts' from the host template.
*/
func Hostfile() error {
if !(util.IsFile(path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"))) {
wwlog.Error("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay")
os.Exit(1)
}
var nodeInfo node.NodeInfo
tstruct := overlay.InitStruct(nodeInfo)
hostname, _ := os.Hostname()
nodeInfo.Id.Set(hostname)
buffer, backupFile, writeFile, err := overlay.RenderTemplateFile(
path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"),
tstruct)
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
info, err := os.Stat(path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"))
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
if writeFile {
err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
}
}
return nil
}