Files
warewulf/internal/pkg/configure/hostfile.go
Jonathon Anderson 037c456bc4 Correct host overlay processing during configure
wwctl configure hostfile was using the /host/ path twice,
causing it to look in the wrong place for /etc/hosts.ww

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
2022-11-11 10:56:07 -06:00

48 lines
1.1 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 {
hostTemplate := path.Join(overlay.OverlaySourceDir("host"), "/etc/hosts.ww")
if !(util.IsFile(hostTemplate)) {
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(
hostTemplate,
tstruct)
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
info, err := os.Stat(hostTemplate)
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
}