From 3d1964f9330537ec8871771a31b82ce0e0e47a2d Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Tue, 3 Nov 2020 23:42:47 -0800 Subject: [PATCH] More updates including #WW... declarations in overlay configs --- Makefile | 5 ++-- cmd/wwbuild/overlay.go | 43 +++++++++++++++++++++++++++++++---- dhcpd.conf => etc/dhcpd.conf | 0 nodes.yaml => etc/nodes.conf | 0 etc/warewulf.conf | 4 ++++ go.mod | 5 +++- go.sum | 2 ++ internal/pkg/assets/assets.go | 20 ++++++++-------- internal/pkg/config/config.go | 35 ++++++++++++++++++++++++++++ 9 files changed, 96 insertions(+), 18 deletions(-) rename dhcpd.conf => etc/dhcpd.conf (100%) rename nodes.yaml => etc/nodes.conf (100%) create mode 100644 etc/warewulf.conf create mode 100644 internal/pkg/config/config.go diff --git a/Makefile b/Makefile index 949c0096..5fc220e8 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,9 @@ files: all install -d -m 0755 /var/warewulf/ install -d -m 0755 /etc/warewulf/ install -d -m 0755 /var/lib/tftpboot/warewulf/ipxe/ - install -m 0644 dhcpd.conf /etc/dhcp/dhcpd.conf - install -m 0644 nodes.yaml /etc/warewulf/nodes.yaml + install -m 0640 etc/dhcpd.conf /etc/dhcp/dhcpd.conf + install -m 0640 etc/nodes.conf /etc/warewulf/nodes.conf + install -m 0640 etc/warewulf.conf /etc/warewulf/warewulf.conf cp -r tftpboot/* /var/lib/tftpboot/warewulf/ipxe/ cp -r overlays /var/warewulf/ chmod +x /var/warewulf/overlays/system/default/init diff --git a/cmd/wwbuild/overlay.go b/cmd/wwbuild/overlay.go index 435590e0..b48302cf 100644 --- a/cmd/wwbuild/overlay.go +++ b/cmd/wwbuild/overlay.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "github.com/hpcng/warewulf/internal/pkg/util" + "io" "os" "path/filepath" "strings" @@ -29,6 +30,7 @@ func BuildOverlayDir(sourceDir string, destDir string, replace map[string]string } else { if filepath.Ext(path) == ".in" { destFile := strings.TrimSuffix(path, ".in") + skip := false sourceFD, err := os.Open(sourceDir + "/" + path) if err != nil { @@ -45,18 +47,49 @@ func BuildOverlayDir(sourceDir string, destDir string, replace map[string]string for scanner.Scan() { newLine := scanner.Text() + for k, v := range replace { replaceString := fmt.Sprintf("@%s@", strings.ToUpper(k)) newLine = strings.ReplaceAll(newLine, replaceString, v) } - //TODO: Support directives like '#include ' and + //TODO: Support directives like '#INCLUDE ' and // conditionals like '#IFDEF ....` - _, err := w.WriteString(newLine + "\n") - if err != nil { - return err - } + if strings.HasPrefix(newLine, "#WWENDIF") { + skip = false + } else if skip == true { + } else if strings.HasPrefix(newLine, "#WWIFDEF") { + line := strings.Split(newLine, " ") + if len(line) > 0 && line[1] != "false" { + skip = true + } + } else if strings.HasPrefix(newLine, "#WWIF ") { + line := strings.Split(newLine, " ") + if len(line) == 2 && line[1] != "false" { + skip = true + } else if len(line) > 3 { + + if line[2] == "==" { + if line[1] != line[3] { + skip = true + } + } + } + } else if strings.HasPrefix(newLine, "#WWINCLUDE ") { + line := strings.Split(newLine, " ") + includeFD, err := os.Open(line[1]) + if err != nil { + return err + } + io.Copy(w, includeFD) + includeFD.Close() + } else { + _, err := w.WriteString(newLine + "\n") + if err != nil { + return err + } + } } w.Flush() sourceFD.Close() diff --git a/dhcpd.conf b/etc/dhcpd.conf similarity index 100% rename from dhcpd.conf rename to etc/dhcpd.conf diff --git a/nodes.yaml b/etc/nodes.conf similarity index 100% rename from nodes.yaml rename to etc/nodes.conf diff --git a/etc/warewulf.conf b/etc/warewulf.conf new file mode 100644 index 00000000..236fa86b --- /dev/null +++ b/etc/warewulf.conf @@ -0,0 +1,4 @@ +Port: 9873 +Ipaddr: 192.168.1.1 +Secure: true +Debug: false diff --git a/go.mod b/go.mod index a6c2066a..bed59179 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/hpcng/warewulf go 1.15 -require gopkg.in/yaml.v2 v2.3.0 +require ( + github.com/kelseyhightower/envconfig v1.4.0 + gopkg.in/yaml.v2 v2.3.0 +) diff --git a/go.sum b/go.sum index 8fabe8da..abc16155 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/pkg/assets/assets.go b/internal/pkg/assets/assets.go index fafee644..ef1363df 100644 --- a/internal/pkg/assets/assets.go +++ b/internal/pkg/assets/assets.go @@ -8,7 +8,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/errors" ) -const ConfigFile = "/etc/warewulf/nodes.yaml" +const ConfigFile = "/etc/warewulf/nodes.conf" func init() { //TODO: Check to make sure nodes.yaml is found @@ -22,21 +22,21 @@ type nodeYaml struct { type nodeGroup struct { Comment string Vnfs string - SystemOverlay string `yaml:"system overlay""` - RuntimeOverlay string `yaml:"runtime overlay""` - DomainSuffix string `yaml:"domain suffix"` - KernelVersion string `yaml:"kernel version"` + SystemOverlay string `yaml:"system overlay""` + RuntimeOverlay string `yaml:"runtime overlay""` + DomainSuffix string `yaml:"domain suffix"` + KernelVersion string `yaml:"kernel version"` Nodes map[string]nodeEntry } type nodeEntry struct { Hostname string Vnfs string - SystemOverlay string `yaml:"system overlay"` - RuntimeOverlay string `yaml:"runtime overlay"` - DomainSuffix string `yaml:"domain suffix"` - KernelVersion string `yaml:"kernel version"` - IpmiIpaddr string `yaml:"ipmi ipaddr"` + SystemOverlay string `yaml:"system overlay"` + RuntimeOverlay string `yaml:"runtime overlay"` + DomainSuffix string `yaml:"domain suffix"` + KernelVersion string `yaml:"kernel version"` + IpmiIpaddr string `yaml:"ipmi ipaddr"` NetDevs map[string]netDevs } diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go new file mode 100644 index 00000000..63168025 --- /dev/null +++ b/internal/pkg/config/config.go @@ -0,0 +1,35 @@ +package config + +import ( + "gopkg.in/yaml.v2" + "github.com/kelseyhightower/envconfig" + "io/ioutil" +) + +type Config struct { + Port int `yaml:"port", envconfig:"WAREWULFD_PORT"` + Ipaddr string `yaml:"ipaddr", envconfig:"WAREWULFD_IPADDR"` + Secure bool `yaml:"secure port"` + Debug bool `yaml:"debug"` +} + +func New() (Config, error) { + var c Config + + fd, err := ioutil.ReadFile("/etc/warewulf/warewulf.conf") + if err != nil { + return c, err + } + + err = yaml.Unmarshal(fd, &c) + if err != nil { + return c, err + } + + err = envconfig.Process("", &c) + if err != nil { + return c, err + } + + return c, nil +}