Rename pkg/warewulfconf to pkg/config
Signed-off-by: Jonathon Anderson <janderson@ciq.co>
This commit is contained in:
18
internal/pkg/config/buildconfig.go.in
Normal file
18
internal/pkg/config/buildconfig.go.in
Normal file
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
type BuildConfig struct {
|
||||
Bindir string `default:"@BINDIR@"`
|
||||
Sysconfdir string `default:"@SYSCONFDIR@"`
|
||||
Datadir string `default:"@DATADIR@"`
|
||||
Localstatedir string `default:"@LOCALSTATEDIR@"`
|
||||
Srvdir string `default:"@SRVDIR@"`
|
||||
Tftpdir string `default:"@TFTPDIR@"`
|
||||
Firewallddir string `default:"@FIREWALLDDIR@"`
|
||||
Systemddir string `default:"@SYSTEMDDIR@"`
|
||||
WWOverlaydir string `default:"@WWOVERLAYDIR@"`
|
||||
WWChrootdir string `default:"@WWCHROOTDIR@"`
|
||||
WWProvisiondir string `default:"@WWPROVISIONDIR@"`
|
||||
Version string `default:"@VERSION@"`
|
||||
Release string `default:"@RELEASE@"`
|
||||
WWClientdir string `default:"@WWCLIENTDIR@"`
|
||||
}
|
||||
156
internal/pkg/config/constructors.go
Normal file
156
internal/pkg/config/constructors.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/creasty/defaults"
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var cachedConf ControllerConf
|
||||
|
||||
var ConfigFile string
|
||||
|
||||
/*
|
||||
Creates a new empty ControllerConf object, returns a cached
|
||||
one if called in a nother context.
|
||||
*/
|
||||
func New() (conf ControllerConf) {
|
||||
// NOTE: This function can be called before any log level is set
|
||||
// so using wwlog.Verbose or wwlog.Debug won't work
|
||||
if !cachedConf.current {
|
||||
conf.Warewulf = new(WarewulfConf)
|
||||
conf.Dhcp = new(DhcpConf)
|
||||
conf.Tftp = new(TftpConf)
|
||||
conf.Nfs = new(NfsConf)
|
||||
conf.Paths = new(BuildConfig)
|
||||
_ = defaults.Set(&conf)
|
||||
cachedConf = conf
|
||||
cachedConf.readConf = false
|
||||
cachedConf.current = true
|
||||
} else {
|
||||
// If cached struct isn't empty, use it as the return value
|
||||
conf = cachedConf
|
||||
}
|
||||
return conf
|
||||
}
|
||||
|
||||
/*
|
||||
Populate the configuration with the values from the configuration file.
|
||||
*/
|
||||
func (conf *ControllerConf) ReadConf(confFileName string) (err error) {
|
||||
wwlog.Debug("Reading warewulf.conf from: %s", confFileName)
|
||||
fileHandle, err := os.ReadFile(confFileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return conf.Read(fileHandle)
|
||||
}
|
||||
|
||||
/*
|
||||
Populate the configuration with the values from the given yaml information
|
||||
*/
|
||||
func (conf *ControllerConf) Read(data []byte) (err error) {
|
||||
// ipxe binaries are merged not overwritten, store defaults separate
|
||||
defIpxe := make(map[string]string)
|
||||
for k, v := range conf.Tftp.IpxeBinaries {
|
||||
defIpxe[k] = v
|
||||
delete(conf.Tftp.IpxeBinaries, k)
|
||||
}
|
||||
err = yaml.Unmarshal(data, &conf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = conf.SetDynamicDefaults()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(conf.Tftp.IpxeBinaries) == 0 {
|
||||
conf.Tftp.IpxeBinaries = defIpxe
|
||||
}
|
||||
cachedConf = *conf
|
||||
cachedConf.current = true
|
||||
cachedConf.readConf = true
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Set the runtime defaults like IP address of running system to the config
|
||||
*/
|
||||
func (conf *ControllerConf) SetDynamicDefaults() (err error) {
|
||||
if conf.Ipaddr == "" || conf.Netmask == "" || conf.Network == "" {
|
||||
var mask net.IPMask
|
||||
var network *net.IPNet
|
||||
var ipaddr net.IP
|
||||
|
||||
if conf.Ipaddr == "" {
|
||||
wwlog.Verbose("Configuration has no valid network, going to dynamic values")
|
||||
conn, _ := net.Dial("udp", "8.8.8.8:80")
|
||||
defer conn.Close()
|
||||
ipaddr = conn.LocalAddr().(*net.UDPAddr).IP
|
||||
mask = ipaddr.DefaultMask()
|
||||
sz, _ := mask.Size()
|
||||
conf.Ipaddr = ipaddr.String() + fmt.Sprintf("/%d", sz)
|
||||
}
|
||||
_, network, err = net.ParseCIDR(conf.Ipaddr)
|
||||
if err == nil {
|
||||
mask = network.Mask
|
||||
} else {
|
||||
return errors.Wrap(err, "Couldn't parse IP address")
|
||||
}
|
||||
if conf.Netmask == "" {
|
||||
conf.Netmask = fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])
|
||||
wwlog.Verbose("Netmask address is not configured in warewulf.conf, using %s", conf.Netmask)
|
||||
}
|
||||
if conf.Network == "" {
|
||||
conf.Network = network.IP.String()
|
||||
wwlog.Verbose("Network is not configured in warewulf.conf, using %s", conf.Network)
|
||||
}
|
||||
}
|
||||
if conf.Dhcp.RangeStart == "" && conf.Dhcp.RangeEnd == "" {
|
||||
start := net.ParseIP(conf.Network).To4()
|
||||
start[3] += 1
|
||||
if start.Equal(net.ParseIP(conf.Ipaddr)) {
|
||||
start[3] += 1
|
||||
}
|
||||
conf.Dhcp.RangeStart = start.String()
|
||||
wwlog.Verbose("dhpd start is not configured in warewulf.conf, using %s", conf.Dhcp.RangeStart)
|
||||
sz, _ := net.IPMask(net.ParseIP(conf.Netmask).To4()).Size()
|
||||
range_end := (1 << (32 - sz)) / 8
|
||||
if range_end > 127 {
|
||||
range_end = 127
|
||||
}
|
||||
end := net.ParseIP(conf.Network).To4()
|
||||
end[3] += byte(range_end)
|
||||
conf.Dhcp.RangeEnd = end.String()
|
||||
wwlog.Verbose("dhpd end is not configured in warewulf.conf, using %s", conf.Dhcp.RangeEnd)
|
||||
|
||||
}
|
||||
// check validity of ipv6 net
|
||||
if conf.Ipaddr6 != "" {
|
||||
_, ipv6net, err := net.ParseCIDR(conf.Ipaddr6)
|
||||
if err != nil {
|
||||
wwlog.Error("Invalid ipv6 address specified, mut be CIDR notation: %s", conf.Ipaddr6)
|
||||
return errors.New("invalid ipv6 network")
|
||||
}
|
||||
if msize, _ := ipv6net.Mask.Size(); msize > 64 {
|
||||
wwlog.Error("ipv6 mask size must be smaller than 64")
|
||||
return errors.New("invalid ipv6 network size")
|
||||
}
|
||||
}
|
||||
cachedConf = *conf
|
||||
cachedConf.current = true
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Return if configuration was read from disk
|
||||
*/
|
||||
func (conf *ControllerConf) Initialized() bool {
|
||||
return conf.readConf
|
||||
}
|
||||
87
internal/pkg/config/datastructure.go
Normal file
87
internal/pkg/config/datastructure.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/creasty/defaults"
|
||||
)
|
||||
|
||||
type ControllerConf struct {
|
||||
WWInternal int `yaml:"WW_INTERNAL"`
|
||||
Comment string `yaml:"comment,omitempty"`
|
||||
Ipaddr string `yaml:"ipaddr"`
|
||||
Ipaddr6 string `yaml:"ipaddr6,omitempty"`
|
||||
Netmask string `yaml:"netmask"`
|
||||
Network string `yaml:"network,omitempty"`
|
||||
Ipv6net string `yaml:"ipv6net,omitempty"`
|
||||
Fqdn string `yaml:"fqdn,omitempty"`
|
||||
Warewulf *WarewulfConf `yaml:"warewulf"`
|
||||
Dhcp *DhcpConf `yaml:"dhcp"`
|
||||
Tftp *TftpConf `yaml:"tftp"`
|
||||
Nfs *NfsConf `yaml:"nfs"`
|
||||
MountsContainer []*MountEntry `yaml:"container mounts" default:"[{\"source\": \"/etc/resolv.conf\", \"dest\": \"/etc/resolv.conf\"}]"`
|
||||
Paths *BuildConfig `yaml:"paths"`
|
||||
current bool
|
||||
readConf bool
|
||||
}
|
||||
|
||||
type WarewulfConf struct {
|
||||
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" default:"true"`
|
||||
Template string `yaml:"template" default:"default"`
|
||||
RangeStart string `yaml:"range start,omitempty"`
|
||||
RangeEnd string `yaml:"range end,omitempty"`
|
||||
SystemdName string `yaml:"systemd name" default:"dhcpd"`
|
||||
}
|
||||
|
||||
type TftpConf struct {
|
||||
Enabled bool `yaml:"enabled" default:"true"`
|
||||
TftpRoot string `yaml:"tftproot" default:"/var/lib/tftpboot"`
|
||||
SystemdName string `yaml:"systemd name" default:"tftp"`
|
||||
// Path is relative to buildconfig.DATADIR()
|
||||
IpxeBinaries map[string]string `yaml:"ipxe" default:"{\"00:09\": \"x86_64.efi\",\"00:00\": \"x86_64.kpxe\",\"00:0B\": \"arm64.efi\",\"00:07\": \"x86_64.efi\"}"`
|
||||
}
|
||||
|
||||
type NfsConf struct {
|
||||
Enabled bool `yaml:"enabled" default:"true"`
|
||||
ExportsExtended []*NfsExportConf `yaml:"export paths" default:"[]"`
|
||||
SystemdName string `yaml:"systemd name" default:"nfsd"`
|
||||
}
|
||||
|
||||
type NfsExportConf struct {
|
||||
Path string `yaml:"path" default:"/dev/null"`
|
||||
ExportOptions string `default:"rw,sync,no_subtree_check" yaml:"export options"`
|
||||
MountOptions string `default:"defaults" yaml:"mount options"`
|
||||
Mount bool `default:"true" yaml:"mount"`
|
||||
}
|
||||
|
||||
/*
|
||||
Describe a mount point for a container exec
|
||||
*/
|
||||
type MountEntry struct {
|
||||
Source string `yaml:"source" default:"/etc/resolv.conf"`
|
||||
Dest string `yaml:"dest,omitempty" default:"/etc/resolv.conf"`
|
||||
ReadOnly bool `yaml:"readonly,omitempty" default:"false"`
|
||||
Options string `yaml:"options,omitempty"` // ignored at the moment
|
||||
}
|
||||
|
||||
func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error {
|
||||
if err := defaults.Set(s); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Waste processor cycles to make code more readable
|
||||
|
||||
func DataStore() string {
|
||||
_ = New()
|
||||
return cachedConf.Warewulf.DataStore
|
||||
}
|
||||
32
internal/pkg/config/modifiers.go
Normal file
32
internal/pkg/config/modifiers.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func (controller *ControllerConf) Persist() error {
|
||||
|
||||
out, err := yaml.Marshal(controller)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
wwlog.Error("%s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(string(out)+"\n")
|
||||
if err != nil {
|
||||
wwlog.Error("Unable to write to warewulf.conf")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user