Add ability to configure host file on the controller node
This commit is contained in:
14
etc/hosts.tmpl
Normal file
14
etc/hosts.tmpl
Normal file
@@ -0,0 +1,14 @@
|
||||
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
|
||||
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
|
||||
|
||||
|
||||
{{range $node := $.AllNodes}}
|
||||
# Entry for {{$node.Id.Get}}
|
||||
{{- range $devname, $netdev := $node.NetDevs}}
|
||||
{{- if $netdev.Default}}
|
||||
{{$netdev.Ipaddr.Get}} {{$node.Id.Get}}
|
||||
{{- else}}
|
||||
{{$netdev.Ipaddr.Get}} {{$node.Id.Get}}-{{$devname}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
{{end}}
|
||||
@@ -1 +1,74 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"github.com/hpcng/warewulf/internal/pkg/node"
|
||||
"github.com/hpcng/warewulf/internal/pkg/util"
|
||||
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type TemplateStruct struct {
|
||||
Ipaddr string
|
||||
Fqdn string
|
||||
AllNodes []node.NodeInfo
|
||||
}
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var replace TemplateStruct
|
||||
|
||||
if util.IsFile("/etc/warewulf/hosts.tmpl") == false {
|
||||
wwlog.Printf(wwlog.WARN, "Template not found, not updating host file\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
controller, err := warewulfconf.New()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
n, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles("/etc/warewulf/hosts.tmpl")
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not parse hosts template: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
w, err := os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
nodes, _ := n.FindAllNodes()
|
||||
|
||||
replace.AllNodes = nodes
|
||||
replace.Ipaddr = controller.Ipaddr
|
||||
replace.Fqdn = controller.Fqdn
|
||||
|
||||
if SetShow == false {
|
||||
err = tmpl.Execute(w, replace)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
err = tmpl.Execute(os.Stdout, replace)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1 +1,22 @@
|
||||
package hosts
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var (
|
||||
baseCmd = &cobra.Command{
|
||||
Use: "hosts",
|
||||
Short: "NFS configuration",
|
||||
Long: "NFS Config",
|
||||
RunE: CobraRunE,
|
||||
}
|
||||
SetShow bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&SetShow, "show", "s", false, "Show configuration (don't update)")
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
func GetCommand() *cobra.Command {
|
||||
return baseCmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user