Merge pull request #477 from mslacken/configure-hostfile

readded configure hostfile command
This commit is contained in:
Christian Goll
2022-09-13 11:26:23 +02:00
committed by GitHub
6 changed files with 89 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
package hostfile
import (
"github.com/hpcng/warewulf/internal/pkg/configure"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
return configure.Hostfile()
}

View File

@@ -0,0 +1,21 @@
package hostfile
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "hostfile [OPTIONS]",
Short: "update hostfile on master",
Long: "Manage the hostfile on the master node\n",
RunE: CobraRunE,
}
)
func init() {
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -34,6 +34,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
err = configure.Hostfile()
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
} else {
_ = cmd.Help()
os.Exit(0)

View File

@@ -2,6 +2,7 @@ package configure
import (
"github.com/hpcng/warewulf/internal/app/wwctl/configure/dhcp"
"github.com/hpcng/warewulf/internal/app/wwctl/configure/hostfile"
"github.com/hpcng/warewulf/internal/app/wwctl/configure/nfs"
"github.com/hpcng/warewulf/internal/app/wwctl/configure/ssh"
"github.com/hpcng/warewulf/internal/app/wwctl/configure/tftp"
@@ -25,6 +26,8 @@ func init() {
baseCmd.AddCommand(tftp.GetCommand())
baseCmd.AddCommand(ssh.GetCommand())
baseCmd.AddCommand(nfs.GetCommand())
baseCmd.AddCommand(hostfile.GetCommand())
baseCmd.PersistentFlags().BoolVarP(&allFunctions, "all", "a", false, "Configure all services")
}

View File

@@ -0,0 +1,46 @@
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\n")
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.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
info, err := os.Stat(path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"))
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", 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
}

View File

@@ -260,7 +260,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir
if len(filenameFromTemplate) != 0 {
wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1])
if foundFileComment {
err = carefulWriteBuffer(path.Join(outputDir, destFileName),
err = CarefulWriteBuffer(path.Join(outputDir, destFileName),
fileBuffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
@@ -277,7 +277,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir
_, _ = fileBuffer.WriteString(line)
}
}
err = carefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode())
err = CarefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
}
@@ -335,7 +335,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir
/*
Writes buffer to the destination file. If wwbackup is set a wwbackup will be created.
*/
func carefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, perm fs.FileMode) error {
func CarefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, perm fs.FileMode) error {
wwlog.Debug("Trying to careful write file (%d bytes): %s", buffer.Len(), destFile)
if backupFile {
if !util.IsFile(destFile+".wwbackup") && util.IsFile(destFile) {