diff --git a/Makefile b/Makefile index 4a5b1148..cc070596 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/etc/dhcp/static-dhcpd.conf b/etc/examples/static-dhcpd.conf.ww similarity index 80% rename from etc/dhcp/static-dhcpd.conf rename to etc/examples/static-dhcpd.conf.ww index 6aa62d4a..f1d3d3d0 100644 --- a/etc/dhcp/static-dhcpd.conf +++ b/etc/examples/static-dhcpd.conf.ww @@ -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 }} \ No newline at end of file diff --git a/internal/app/wwctl/configure/dhcp/main.go b/internal/app/wwctl/configure/dhcp/main.go index 15b61bd4..8ea2d8ff 100644 --- a/internal/app/wwctl/configure/dhcp/main.go +++ b/internal/app/wwctl/configure/dhcp/main.go @@ -8,3 +8,4 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { return configure.Configure("DHCP", setShow) } + diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index bda1fbf7..1471df30 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -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 } diff --git a/internal/pkg/configure/dhcp_main_test.go b/internal/pkg/configure/dhcp_main_test.go deleted file mode 100644 index bf265141..00000000 --- a/internal/pkg/configure/dhcp_main_test.go +++ /dev/null @@ -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) - } - } -} diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index cc43ef43..760c5eed 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -35,4 +35,5 @@ type TemplateStruct struct { Network string Dhcp warewulfconf.DhcpConf Nfs warewulfconf.NfsConf + Warewulf warewulfconf.WarewulfConf } diff --git a/internal/pkg/warewulfconf/datastructure.go b/internal/pkg/warewulfconf/datastructure.go index 4e9cb44f..0ef1481e 100644 --- a/internal/pkg/warewulfconf/datastructure.go +++ b/internal/pkg/warewulfconf/datastructure.go @@ -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 { diff --git a/internal/pkg/warewulfconf/defaults.go b/internal/pkg/warewulfconf/defaults.go index 4086a28f..a08dc6e2 100644 --- a/internal/pkg/warewulfconf/defaults.go +++ b/internal/pkg/warewulfconf/defaults.go @@ -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, diff --git a/etc/dhcp/default-dhcpd.conf b/overlays/host/etc/dhcpd.conf.ww similarity index 75% rename from etc/dhcp/default-dhcpd.conf rename to overlays/host/etc/dhcpd.conf.ww index 781c0765..b7ac60e5 100644 --- a/etc/dhcp/default-dhcpd.conf +++ b/overlays/host/etc/dhcpd.conf.ww @@ -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 }} \ No newline at end of file