compiling config_defaults, unmarshall all values

This commit is contained in:
Christian Goll
2022-04-04 15:41:26 +02:00
parent e090d30f19
commit e0681148e9
6 changed files with 81 additions and 91 deletions

1
.gitignore vendored
View File

@@ -10,6 +10,7 @@
/wwctl
/bash_completion
/man_page
/config_defaults
# other created files
/man_pages

View File

@@ -95,7 +95,7 @@ export GOPROXY
# built tags needed for wwbuild binary
WW_GO_BUILD_TAGS := containers_image_openpgp containers_image_ostree
all: config vendor wwctl wwclient bash_completion.d man_pages
all: config vendor wwctl wwclient bash_completion.d man_pages config_defaults
build: lint test-it vet all
@@ -225,6 +225,14 @@ man_pages: man_page
./man_page ./man_pages
cd man_pages; for i in wwctl*1; do echo "Compressing manpage: $$i"; gzip --force $$i; done
config_defaults:
cd cmd/config_defaults && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\
-X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\
-mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../config_defaults
warewulfconf: config_defaults
./config_defaults
dist: vendor config
rm -rf .dist/$(WAREWULF)-$(VERSION)
mkdir -p .dist/$(WAREWULF)-$(VERSION)
@@ -246,6 +254,7 @@ clean:
rm -f config
rm -f Defaults.mk
rm -rf $(TOOLS_DIR)
rm -f config_defaults
install: files install_wwclient

View File

@@ -1,28 +1,36 @@
WW_INTERNAL: 43
ipaddr: 192.168.200.1
netmask: 255.255.255.0
network: 192.168.200.0
warewulf:
port: 9873
secure: false
autobuild overlays: true
update interval: 60
autobuild overlays: true
host overlay: false
syslog: false
datastore: ""
dhcp:
enabled: true
template: default
range start: 192.168.200.50
range end: 192.168.200.99
template: default
systemd name: dhcpd
tftp:
enabled: true
tftproot: ""
systemd name: tftp
nfs:
systemd name: nfs-server
enabled: true
exports: []
export paths:
- path: /home
export options: rw,sync
mount options: defaults
mount: true
- path: /opt
export options: ro,sync,no_root_squash
mount: false
- path: /home
export options: rw,sync
mount options: defaults
mount: true
- path: /opt
export options: ro,sync,no_root_squash
mount options: ""
mount: false
systemd name: nfs-server

View File

@@ -8,6 +8,7 @@ import (
"path"
"github.com/brotherpowers/ipsubnet"
"github.com/creasty/defaults"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
@@ -25,15 +26,26 @@ func init() {
}
func New() (ControllerConf, error) {
var ret ControllerConf = *defaultConfig()
var ret ControllerConf
var warewulfconf WarewulfConf
var dhpdconf DhcpConf
var tftpconf TftpConf
var nfsConf NfsConf
ret.Warewulf = &warewulfconf
ret.Dhcp = &dhpdconf
ret.Tftp = &tftpconf
ret.Nfs = &nfsConf
err := defaults.Set(&ret)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Coult initialize default variables\n")
return ret, err
}
// Check if cached config is old before re-reading config file
if !cachedConf.current {
wwlog.Printf(wwlog.DEBUG, "Opening Warewulf configuration file: %s\n", ConfigFile)
data, err := ioutil.ReadFile(ConfigFile)
if err != nil {
fmt.Printf("Error reading Warewulf configuration file\n")
return ret, err
wwlog.Printf(wwlog.WARN, "Error reading Warewulf configuration file\n")
}
wwlog.Printf(wwlog.DEBUG, "Unmarshaling the Warewulf configuration\n")
@@ -42,16 +54,22 @@ func New() (ControllerConf, error) {
return ret, err
}
// TODO: Need to add comprehensive config file validator
// TODO: Change function to guess default IP address and/or mask from local system
if ret.Ipaddr == "" {
wwlog.Printf(wwlog.ERROR, "IP address is not configured in warewulfd.conf\n")
return ret, errors.New("no IP Address")
}
if ret.Netmask == "" {
wwlog.Printf(wwlog.ERROR, "Netmask is not configured in warewulfd.conf\n")
return ret, errors.New("no netmask")
if ret.Ipaddr == "" || ret.Netmask == "" {
conn, error := net.Dial("udp", "8.8.8.8:80")
if error != nil {
return ret, err
}
defer conn.Close()
localIp := conn.LocalAddr().(*net.UDPAddr)
if ret.Ipaddr == "" {
ret.Ipaddr = localIp.IP.String()
wwlog.Printf(wwlog.WARN, "IP address is not configured in warewulfd.conf, using %s\n", ret.Ipaddr)
}
if ret.Netmask == "" {
mask := localIp.IP.DefaultMask()
ret.Netmask = fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])
wwlog.Printf(wwlog.WARN, "Netmask address is not configured in warewulfd.conf, using %s\n", ret.Netmask)
}
}
if ret.Network == "" {
@@ -74,9 +92,6 @@ func New() (ControllerConf, error) {
return ret, errors.New("invalid ipv6 network size")
}
}
if ret.Warewulf.Port == 0 {
ret.Warewulf.Port = defaultPort
}
wwlog.Printf(wwlog.DEBUG, "Returning warewulf config object\n")
cachedConf = ret

View File

@@ -23,52 +23,46 @@ type ControllerConf struct {
}
type WarewulfConf struct {
Port int `yaml:"port"`
Secure bool `yaml:"secure"`
UpdateInterval int `yaml:"update interval"`
AutobuildOverlays bool `yaml:"autobuild overlays"`
EnableHostOverlay bool `yaml:"host overlay"`
Syslog bool `yaml:"syslog"`
DataStore string `yaml:"datastore"`
Port int `yaml:"port" default:"9983"`
Secure bool `yaml:"secure" default:"true"`
UpdateInterval int `yaml:"update interval" default:"60"`
AutobuildOverlays bool `yaml:"autobuild overlays" default:"true"`
EnableHostOverlay bool `yaml:"host overlay" default:"true"`
Syslog bool `yaml:"syslog" default:"false"`
DataStore string `yaml:"datastore" default:"/var/lib/warewulf"`
}
type DhcpConf struct {
Enabled bool `yaml:"enabled"`
Template string `yaml:"template"`
RangeStart string `yaml:"range start"`
RangeEnd string `yaml:"range end"`
SystemdName string `yaml:"systemd name"`
Enabled bool `yaml:"enabled" default:"true"`
Template string `yaml:"template" default:"default"`
RangeStart string `yaml:"range start" default:"192.168.200.50"`
RangeEnd string `yaml:"range end" default:"192.168.200.99"`
SystemdName string `yaml:"systemd name" default:"dhcpd"`
}
type TftpConf struct {
Enabled bool `yaml:"enabled"`
TftpRoot string `yaml:"tftproot"`
SystemdName string `yaml:"systemd name"`
Enabled bool `yaml:"enabled" default:"true"`
TftpRoot string `yaml:"tftproot" default:"/var/lib/tftpboot"`
SystemdName string `yaml:"systemd name" default:"tftp"`
}
type NfsConf struct {
Enabled bool `default:"true" yaml:"enabled"`
ExportsExtended []*NfsExportConf `yaml:"export paths"`
SystemdName string `yaml:"systemd name"`
Enabled bool `yaml:"enabled" default:"true"`
ExportsExtended []*NfsExportConf `yaml:"export paths" default:"[{\"Path\": \"home\"}]"`
SystemdName string `yaml:"systemd name" default:"nfsd"`
}
type NfsExportConf struct {
Path string `yaml:"path"`
Path string `yaml:"path" default:"/home"`
ExportOptions string `default:"rw,sync,no_subtree_check" yaml:"export options"`
MountOptions string `default:"defaults" yaml:"mount options"`
Mount bool `default:"true" yaml:"mount"`
}
func (s *NfsConf) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error {
if err := defaults.Set(s); err != nil {
return err
}
type plain NfsConf
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
}

View File

@@ -1,37 +0,0 @@
package warewulfconf
import "github.com/hpcng/warewulf/internal/pkg/buildconfig"
const defaultPort int = 9983
func defaultConfig() *ControllerConf {
Warewulf := &WarewulfConf{
Port: defaultPort,
Secure: true,
UpdateInterval: 60,
AutobuildOverlays: true,
EnableHostOverlay: true,
Syslog: false,
DataStore: buildconfig.LOCALSTATEDIR(),
}
Dhcp := &DhcpConf{
Enabled: true,
Template: "default",
RangeStart: "192.168.200.50",
RangeEnd: "192.168.200.99",
SystemdName: "dhcpd",
}
Tftp := &TftpConf{
Enabled: true,
TftpRoot: buildconfig.TFTPDIR(),
SystemdName: "tftp",
}
return &ControllerConf{
WWInternal: buildconfig.WWVer,
Warewulf: Warewulf,
Dhcp: Dhcp,
Tftp: Tftp,
current: false,
}
}