From 628381a8a37ffd0db1d74c0de945b345c55154e1 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Tue, 1 Dec 2020 13:05:57 -0800 Subject: [PATCH] Startings of the `wwctl service dhcp` integration --- etc/dhcp/default-dhcpd.conf | 10 +++--- etc/dhcp/static-dhcpd.conf | 42 ++++++++++++++++++++++++ internal/app/wwctl/services/dhcp/main.go | 35 +++++++++++++++----- internal/app/wwctl/services/dhcp/root.go | 4 ++- 4 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 etc/dhcp/static-dhcpd.conf diff --git a/etc/dhcp/default-dhcpd.conf b/etc/dhcp/default-dhcpd.conf index ea5e9f72..17ae45f9 100644 --- a/etc/dhcp/default-dhcpd.conf +++ b/etc/dhcp/default-dhcpd.conf @@ -1,3 +1,7 @@ +# 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 services dhcp) + allow booting; allow bootp; ddns-update-style interim; @@ -34,9 +38,3 @@ subnet {{.Ipaddr}} netmask {{.Netmask}} { next-server 192.168.1.1; } -{{range .Nodes}} -#host {{.Fqdn}} { -# hardware ethernet {{.NetDevs.eth0.Hwaddr}} -# filed-address {{.NetDevs.eth0.Ipaddr}} -#} -{{end}} diff --git a/etc/dhcp/static-dhcpd.conf b/etc/dhcp/static-dhcpd.conf new file mode 100644 index 00000000..5056f064 --- /dev/null +++ b/etc/dhcp/static-dhcpd.conf @@ -0,0 +1,42 @@ +# 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 services dhcp) + +allow booting; +allow bootp; +ddns-update-style interim; +authoritative; + +option space ipxe; + +# Tell iPXE to not wait for ProxyDHCP requests to speed up boot. +option ipxe.no-pxedhcp code 176 = unsigned integer 8; +option ipxe.no-pxedhcp 1; + +option architecture-type code 93 = unsigned integer 16; + +if exists user-class and option user-class = "iPXE" { + filename "http://192.168.1.1:9873/ipxe/${mac:hexhyp}"; +} else { + if option architecture-type = 00:0B { + filename "/warewulf/ipxe/bin-arm64-efi/snp.efi"; + } elsif option architecture-type = 00:0A { + filename "/warewulf/ipxe/bin-arm32-efi/placeholder.efi"; + } elsif option architecture-type = 00:09 { + filename "/warewulf/ipxe/bin-x86_64-efi/snp.efi"; + } elsif option architecture-type = 00:07 { + filename "/warewulf/ipxe/bin-x86_64-efi/snp.efi"; + } elsif option architecture-type = 00:06 { + filename "/warewulf/ipxe/bin-i386-efi/snp.efi"; + } elsif option architecture-type = 00:00 { + filename "/warewulf/ipxe/bin-i386-pcbios/undionly.kpxe"; + } +} + + +{{range .Nodes}} +host {{.Fqdn}} { + hardware ethernet {{.NetDevs.eth0.Hwaddr}} + filed-address {{.NetDevs.eth0.Ipaddr}} +} +{{end}} diff --git a/internal/app/wwctl/services/dhcp/main.go b/internal/app/wwctl/services/dhcp/main.go index 88d9b7e5..2d950561 100644 --- a/internal/app/wwctl/services/dhcp/main.go +++ b/internal/app/wwctl/services/dhcp/main.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" "net" "os" + "path" + "strings" "text/template" ) @@ -33,6 +35,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } for _, controller := range controllers { + var templateFile string + var configWriter *os.File var d dhcpTemplate var configured bool @@ -51,7 +55,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error { d.RangeStart = controller.Services.Dhcp.RangeStart d.RangeEnd = controller.Services.Dhcp.RangeEnd configured = true - fmt.Printf("%#v\n", d) break } } @@ -82,20 +85,34 @@ func CobraRunE(cmd *cobra.Command, args []string) error { d.Nodes = append(d.Nodes, node) } - tmpl, err := template.New("default-dhcpd.conf").ParseFiles("/etc/warewulf/dhcp/default-dhcpd.conf") + if controller.Services.Dhcp.Template == "" { + templateFile = "/etc/warewulf/dhcp/default-dhcpd.conf" + } else { + if strings.HasPrefix(controller.Services.Dhcp.Template, "/") { + templateFile = controller.Services.Dhcp.Template + } else { + templateFile = fmt.Sprintf("/etc/warewulf/dhcp/%s-dhcpd.conf", controller.Services.Dhcp.Template) + } + } + + tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) } - // w, err := os.OpenFile(controller.Services.Dhcp.ConfigFile, os.O_RDWR|os.O_CREATE, 0640) - // if err != nil { - // wwlog.Printf(wwlog.ERROR, "%s\n", err) - // os.Exit(1) - // } - // defer w.Close() + if ShowConfig == true { + configWriter = os.Stdout + } else { + configWriter, err = os.OpenFile(controller.Services.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(os.Stdout, d) + err = tmpl.Execute(configWriter, d) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) diff --git a/internal/app/wwctl/services/dhcp/root.go b/internal/app/wwctl/services/dhcp/root.go index 189f85db..cadb2171 100644 --- a/internal/app/wwctl/services/dhcp/root.go +++ b/internal/app/wwctl/services/dhcp/root.go @@ -11,10 +11,12 @@ var ( Long: "DHCP Config", RunE: CobraRunE, } - test bool + ShowConfig bool ) func init() { + baseCmd.PersistentFlags().BoolVarP(&ShowConfig, "show", "s", false, "Show configuration rather than writing to files") + } // GetRootCommand returns the root cobra.Command for the application.