readded configure hostfile command
This commit is contained in:
10
internal/app/wwctl/configure/hostfile/main.go
Normal file
10
internal/app/wwctl/configure/hostfile/main.go
Normal 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()
|
||||||
|
}
|
||||||
21
internal/app/wwctl/configure/hostfile/root.go
Normal file
21
internal/app/wwctl/configure/hostfile/root.go
Normal 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
|
||||||
|
}
|
||||||
@@ -34,6 +34,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
|||||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
err = configure.Hostfile()
|
||||||
|
if err != nil {
|
||||||
|
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
_ = cmd.Help()
|
_ = cmd.Help()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package configure
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hpcng/warewulf/internal/app/wwctl/configure/dhcp"
|
"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/nfs"
|
||||||
"github.com/hpcng/warewulf/internal/app/wwctl/configure/ssh"
|
"github.com/hpcng/warewulf/internal/app/wwctl/configure/ssh"
|
||||||
"github.com/hpcng/warewulf/internal/app/wwctl/configure/tftp"
|
"github.com/hpcng/warewulf/internal/app/wwctl/configure/tftp"
|
||||||
@@ -25,6 +26,8 @@ func init() {
|
|||||||
baseCmd.AddCommand(tftp.GetCommand())
|
baseCmd.AddCommand(tftp.GetCommand())
|
||||||
baseCmd.AddCommand(ssh.GetCommand())
|
baseCmd.AddCommand(ssh.GetCommand())
|
||||||
baseCmd.AddCommand(nfs.GetCommand())
|
baseCmd.AddCommand(nfs.GetCommand())
|
||||||
|
baseCmd.AddCommand(hostfile.GetCommand())
|
||||||
|
|
||||||
baseCmd.PersistentFlags().BoolVarP(&allFunctions, "all", "a", false, "Configure all services")
|
baseCmd.PersistentFlags().BoolVarP(&allFunctions, "all", "a", false, "Configure all services")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
46
internal/pkg/configure/hostfile.go
Normal file
46
internal/pkg/configure/hostfile.go
Normal 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
|
||||||
|
}
|
||||||
@@ -260,7 +260,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir
|
|||||||
if len(filenameFromTemplate) != 0 {
|
if len(filenameFromTemplate) != 0 {
|
||||||
wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1])
|
wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1])
|
||||||
if foundFileComment {
|
if foundFileComment {
|
||||||
err = carefulWriteBuffer(path.Join(outputDir, destFileName),
|
err = CarefulWriteBuffer(path.Join(outputDir, destFileName),
|
||||||
fileBuffer, backupFile, info.Mode())
|
fileBuffer, backupFile, info.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "could not write file from template")
|
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)
|
_, _ = 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 {
|
if err != nil {
|
||||||
return errors.Wrap(err, "could not write file from template")
|
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.
|
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)
|
wwlog.Debug("Trying to careful write file (%d bytes): %s", buffer.Len(), destFile)
|
||||||
if backupFile {
|
if backupFile {
|
||||||
if !util.IsFile(destFile+".wwbackup") && util.IsFile(destFile) {
|
if !util.IsFile(destFile+".wwbackup") && util.IsFile(destFile) {
|
||||||
|
|||||||
Reference in New Issue
Block a user