moved dhcpd configuration to host templating
This commit is contained in:
@@ -3,40 +3,19 @@ package configure
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
|
||||
"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/warewulfconf"
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
/*
|
||||
Configures the dhcpd server, when show is set to false, else the
|
||||
dhcp configuration is checked.
|
||||
*/
|
||||
func configureDHCP(show bool) error {
|
||||
|
||||
var (
|
||||
d struct {
|
||||
Ipaddr string
|
||||
Port string
|
||||
RangeStart string
|
||||
RangeEnd string
|
||||
Network string
|
||||
Netmask string
|
||||
Nodes []node.NodeInfo
|
||||
}
|
||||
|
||||
templateFile string
|
||||
)
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
controller, err := warewulfconf.New()
|
||||
if err != nil {
|
||||
@@ -58,74 +37,18 @@ func configureDHCP(show bool) error {
|
||||
wwlog.Printf(wwlog.ERROR, "Configuration is not defined: `dhcpd range end`\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if controller.Dhcp.ConfigFile == "" {
|
||||
controller.Dhcp.ConfigFile = "/etc/dhcp/dhcpd.conf"
|
||||
}
|
||||
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not find all controllers: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
d.Nodes = append(d.Nodes, nodes...)
|
||||
|
||||
templateFile = dhcpTemplateFile(controller.Dhcp.Template)
|
||||
tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
d.Ipaddr = controller.Ipaddr
|
||||
d.Port = strconv.Itoa(controller.Warewulf.Port)
|
||||
d.Network = controller.Network
|
||||
d.Netmask = controller.Netmask
|
||||
d.RangeStart = controller.Dhcp.RangeStart
|
||||
d.RangeEnd = controller.Dhcp.RangeEnd
|
||||
|
||||
if !show {
|
||||
fmt.Printf("Writing the DHCP configuration file\n")
|
||||
configWriter, err := os.OpenFile(controller.Dhcp.ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer configWriter.Close()
|
||||
err = tmpl.Execute(configWriter, d)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = overlay.BuildHostOverlay()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.WARN, "host overlay could not be built: %s\n", err)
|
||||
}
|
||||
fmt.Printf("Enabling and restarting the DHCP services\n")
|
||||
err = util.SystemdStart(controller.Dhcp.SystemdName)
|
||||
err := util.SystemdStart(controller.Dhcp.SystemdName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to start")
|
||||
}
|
||||
} else {
|
||||
err = tmpl.Execute(os.Stdout, d)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dhcpTemplateFile returns the path of the warewulf dhcp template given controller.Dhcp.Template.
|
||||
func dhcpTemplateFile(controllerDhcpTemplate string) (templateFile string) {
|
||||
if controllerDhcpTemplate == "" {
|
||||
templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")
|
||||
} else {
|
||||
if strings.HasPrefix(controllerDhcpTemplate, "/") {
|
||||
templateFile = controllerDhcpTemplate
|
||||
} else {
|
||||
templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/"+controllerDhcpTemplate+"-dhcpd.conf")
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package configure
|
||||
|
||||
import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
|
||||
)
|
||||
|
||||
func TestDhcpTemplateFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
parameter string
|
||||
expected string
|
||||
}{
|
||||
{"", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")},
|
||||
{"default", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")},
|
||||
{"static", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/static-dhcpd.conf")},
|
||||
{"/test/absolute/path.conf", "/test/absolute/path.conf"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
actual := dhcpTemplateFile(tt.parameter)
|
||||
if actual != tt.expected {
|
||||
t.Errorf("dhcpTemplateFile(%v) expected: %v, actual: %v",
|
||||
tt.parameter, tt.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,5 @@ type TemplateStruct struct {
|
||||
Network string
|
||||
Dhcp warewulfconf.DhcpConf
|
||||
Nfs warewulfconf.NfsConf
|
||||
Warewulf warewulfconf.WarewulfConf
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ type DhcpConf struct {
|
||||
RangeStart string `yaml:"range start"`
|
||||
RangeEnd string `yaml:"range end"`
|
||||
SystemdName string `yaml:"systemd name"`
|
||||
ConfigFile string `yaml:"config file,omitempty"`
|
||||
}
|
||||
|
||||
type TftpConf struct {
|
||||
|
||||
@@ -19,7 +19,6 @@ func defaultConfig() *ControllerConf {
|
||||
RangeStart: "192.168.200.50",
|
||||
RangeEnd: "192.168.200.99",
|
||||
SystemdName: "dhcpd",
|
||||
ConfigFile: "/etc/dhcp/dhcpd.conf",
|
||||
}
|
||||
Tftp := &TftpConf{
|
||||
Enabled: true,
|
||||
|
||||
Reference in New Issue
Block a user