moved dhcpd configuration to host templating

This commit is contained in:
Christian Goll
2022-02-15 12:14:57 +01:00
committed by jcsiadal
parent afc94ebd42
commit eeb062cf85
9 changed files with 29 additions and 129 deletions

View File

@@ -150,7 +150,7 @@ files: all
test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/warewulf.conf || install -m 644 etc/warewulf.conf $(DESTDIR)$(SYSCONFDIR)/warewulf/
test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/hosts.tmpl || install -m 644 etc/hosts.tmpl $(DESTDIR)$(SYSCONFDIR)/warewulf/
test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/nodes.conf || install -m 644 etc/nodes.conf $(DESTDIR)$(SYSCONFDIR)/warewulf/
cp -r etc/dhcp $(DESTDIR)$(SYSCONFDIR)/warewulf/
cp -r etc/examples $(DESTDIR)$(SYSCONFDIR)/warewulf/
cp -r etc/ipxe $(DESTDIR)$(SYSCONFDIR)/warewulf/
cp -r overlays/* $(DESTDIR)$(WWOVERLAYDIR)/
chmod 755 $(DESTDIR)$(WWOVERLAYDIR)/wwinit/init

View File

@@ -1,7 +1,8 @@
# This file is created and was written by a Warewulf template. It is strongly
# suggested that you do not edit this file, but rather, edit the template and
# recreate this file using Warewulf (wwctl configure dhcp)
{{- if .Dhcp.Enabled }}
# This file is autogenerated by warewulf
# Host: {{.BuildHost}}
# Time: {{.BuildTime}}
# Source: {{.BuildSource}}
allow booting;
allow bootp;
ddns-update-style interim;
@@ -16,7 +17,7 @@ option ipxe.no-pxedhcp 1;
option architecture-type code 93 = unsigned integer 16;
if exists user-class and option user-class = "iPXE" {
filename "http://{{$.Ipaddr}}:{{.Port}}/ipxe/${mac:hexhyp}";
filename "http://{{$.Ipaddr}}:{{.Warewulf.Port}}/ipxe/${mac:hexhyp}";
} else {
if option architecture-type = 00:0B {
filename "/warewulf/arm64.efi";
@@ -44,3 +45,5 @@ host {{$nodes.Id.Get}} {
fixed-address {{$nodes.NetDevs.eth0.Ipaddr.Get}};
}
{{end}}
{{- end }}

View File

@@ -8,3 +8,4 @@ import (
func CobraRunE(cmd *cobra.Command, args []string) error {
return configure.Configure("DHCP", setShow)
}

View File

@@ -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
}

View File

@@ -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)
}
}
}

View File

@@ -35,4 +35,5 @@ type TemplateStruct struct {
Network string
Dhcp warewulfconf.DhcpConf
Nfs warewulfconf.NfsConf
Warewulf warewulfconf.WarewulfConf
}

View File

@@ -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 {

View File

@@ -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,

View File

@@ -1,7 +1,8 @@
# This file is created and was written by a Warewulf template. It is strongly
# suggested that you do not edit this file, but rather, edit the template and
# recreate this file using Warewulf (wwctl configure dhcp)
{{- if .Dhcp.Enabled }}
# This file is autogenerated by warewulf
# Host: {{.BuildHost}}
# Time: {{.BuildTime}}
# Source: {{.BuildSource}}
allow booting;
allow bootp;
ddns-update-style interim;
@@ -16,7 +17,7 @@ option ipxe.no-pxedhcp 1;
option architecture-type code 93 = unsigned integer 16;
if exists user-class and option user-class = "iPXE" {
filename "http://{{$.Ipaddr}}:{{.Port}}/ipxe/${mac:hexhyp}";
filename "http://{{$.Ipaddr}}:{{.Warewulf.Port}}/ipxe/${mac:hexhyp}";
} else {
if option architecture-type = 00:0B {
filename "/warewulf/arm64.efi";
@@ -36,7 +37,7 @@ if exists user-class and option user-class = "iPXE" {
subnet {{$.Network}} netmask {{$.Netmask}} {
max-lease-time 120;
range {{$.RangeStart}} {{$.RangeEnd}};
range {{$.Dhcp.RangeStart}} {{$.Dhcp.RangeEnd}};
next-server {{$.Ipaddr}};
}
{{- end }}