Startings of the wwctl service dhcp integration

This commit is contained in:
Gregory Kurtzer
2020-12-01 13:05:57 -08:00
parent 6968744e55
commit 628381a8a3
4 changed files with 75 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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