diff --git a/.gitignore b/.gitignore index 27667354..1262a976 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ /wwctl /bash_completion /man_page +/config_defaults # other created files /man_pages diff --git a/Makefile b/Makefile index a7421035..704337b1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/etc/warewulf.conf b/etc/warewulf.conf index ffd9aa5c..7ba927e5 100644 --- a/etc/warewulf.conf +++ b/etc/warewulf.conf @@ -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 + diff --git a/internal/pkg/warewulfconf/constructors.go b/internal/pkg/warewulfconf/constructors.go index b3d91d1f..57afeff9 100644 --- a/internal/pkg/warewulfconf/constructors.go +++ b/internal/pkg/warewulfconf/constructors.go @@ -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 diff --git a/internal/pkg/warewulfconf/datastructure.go b/internal/pkg/warewulfconf/datastructure.go index a87bd78e..887d11ef 100644 --- a/internal/pkg/warewulfconf/datastructure.go +++ b/internal/pkg/warewulfconf/datastructure.go @@ -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 } diff --git a/internal/pkg/warewulfconf/defaults.go b/internal/pkg/warewulfconf/defaults.go deleted file mode 100644 index e3a89296..00000000 --- a/internal/pkg/warewulfconf/defaults.go +++ /dev/null @@ -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, - } -}