From c461e65a98a4179f3da8820f8576737634f33dcc Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 14:23:18 -0600 Subject: [PATCH 01/13] Rename pkg/warewulfconf to pkg/config Signed-off-by: Jonathon Anderson --- .gitignore | 3 ++- Makefile | 8 ++++---- internal/app/api/wwapic/wwapic.go | 2 +- internal/app/api/wwapid/wwapid.go | 4 ++-- internal/app/api/wwapird/wwapird.go | 2 +- internal/app/wwclient/root.go | 2 +- internal/app/wwctl/container/exec/child/main.go | 2 +- internal/app/wwctl/container/list/main_test.go | 2 +- internal/app/wwctl/genconf/warewulfconf/print/main.go | 2 +- internal/app/wwctl/node/add/main_test.go | 2 +- internal/app/wwctl/node/list/main_test.go | 2 +- internal/app/wwctl/node/status/main.go | 2 +- internal/app/wwctl/overlay/build/main.go | 2 +- internal/app/wwctl/profile/list/main_test.go | 2 +- internal/app/wwctl/root.go | 2 +- internal/app/wwctl/server/start/main.go | 2 +- internal/pkg/api/node/node.go | 2 +- internal/pkg/{warewulfconf => config}/buildconfig.go.in | 2 +- internal/pkg/{warewulfconf => config}/constructors.go | 2 +- internal/pkg/{warewulfconf => config}/datastructure.go | 2 +- internal/pkg/{warewulfconf => config}/modifiers.go | 2 +- internal/pkg/configure/dhcp.go | 2 +- internal/pkg/configure/nfs.go | 2 +- internal/pkg/configure/ssh.go | 2 +- internal/pkg/configure/tftp.go | 2 +- internal/pkg/container/config.go | 2 +- internal/pkg/container/imprt.go | 2 +- internal/pkg/container/mountpoints.go | 2 +- internal/pkg/kernel/kernel.go | 2 +- internal/pkg/node/constructors.go | 2 +- internal/pkg/oci/defaults.go | 8 +++++--- internal/pkg/overlay/config.go | 2 +- internal/pkg/overlay/datastructure.go | 2 +- internal/pkg/overlay/funcmap.go | 2 +- internal/pkg/version/version.go | 2 +- internal/pkg/warewulfd/daemon.go | 2 +- internal/pkg/warewulfd/provision.go | 2 +- internal/pkg/warewulfd/warewulfd.go | 2 +- 38 files changed, 47 insertions(+), 44 deletions(-) rename internal/pkg/{warewulfconf => config}/buildconfig.go.in (97%) rename internal/pkg/{warewulfconf => config}/constructors.go (99%) rename internal/pkg/{warewulfconf => config}/datastructure.go (99%) rename internal/pkg/{warewulfconf => config}/modifiers.go (96%) diff --git a/.gitignore b/.gitignore index 18b9454f..894fa86d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,10 +25,11 @@ /etc/bash_completion.d/ warewulf.spec internal/pkg/buildconfig/setconfigs.go -internal/pkg/warewulfconf/buildconfig.go +internal/pkg/config/buildconfig.go include/systemd/warewulfd.service _dist/ config +!internal/pkg/config warewulf-*.tar.gz Defaults.mk /etc/wwapid.config diff --git a/Makefile b/Makefile index 36e36d98..a9fc7dd2 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ vet: go vet ./... test-it: - go test -v ./... -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=$(shell pwd)/etc/warewulf.conf'" + go test -v ./... -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=$(shell pwd)/etc/warewulf.conf'" # Generate test coverage test-cover: ## Run test coverage and generate html report @@ -219,13 +219,13 @@ init: wwctl: $(WWCTL_DEPS) @echo Building "$@" @cd cmd/wwctl; GOOS=linux go build -mod vendor -tags "$(WW_GO_BUILD_TAGS)" \ - -ldflags "-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=$(SYSCONFDIR)/warewulf/warewulf.conf'" \ + -ldflags "-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=$(SYSCONFDIR)/warewulf/warewulf.conf'" \ -o ../../wwctl wwclient: $(WWCLIENT_DEPS) @echo Building "$@" @cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags -static \ - -X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=/etc/warewulf/warewulf.conf'" -o ../../wwclient + -X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=/etc/warewulf/warewulf.conf'" -o ../../wwclient man_pages: wwctl @install -d man_pages @@ -235,7 +235,7 @@ man_pages: wwctl @cd man_pages; for i in wwctl*1 *.5; do gzip --force $$i; echo -n "$$i "; done; echo update_configuration: vendor cmd/update_configuration/update_configuration.go - cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\ + cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=./etc/warewulf.conf'\ -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../update_configuration diff --git a/internal/app/api/wwapic/wwapic.go b/internal/app/api/wwapic/wwapic.go index 36cfb002..fc34659c 100644 --- a/internal/app/api/wwapic/wwapic.go +++ b/internal/app/api/wwapic/wwapic.go @@ -12,7 +12,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/api/apiconfig" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "google.golang.org/grpc" "google.golang.org/grpc/credentials" diff --git a/internal/app/api/wwapid/wwapid.go b/internal/app/api/wwapid/wwapid.go index ce7457e5..33de9db6 100644 --- a/internal/app/api/wwapid/wwapid.go +++ b/internal/app/api/wwapid/wwapid.go @@ -16,7 +16,7 @@ import ( apinode "github.com/hpcng/warewulf/internal/pkg/api/node" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/version" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + "github.com/hpcng/warewulf/internal/pkg/config" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -34,7 +34,7 @@ var apiVersion string func main() { log.Println("Server running") - conf := warewulfconf.New() + conf := config.New() // Read the config file. config, err := apiconfig.NewServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapid.conf")) if err != nil { diff --git a/internal/app/api/wwapird/wwapird.go b/internal/app/api/wwapird/wwapird.go index e9b210ac..c71f56e8 100644 --- a/internal/app/api/wwapird/wwapird.go +++ b/internal/app/api/wwapird/wwapird.go @@ -18,7 +18,7 @@ import ( "google.golang.org/grpc/credentials/insecure" "github.com/hpcng/warewulf/internal/pkg/api/apiconfig" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" gw "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" diff --git a/internal/app/wwclient/root.go b/internal/app/wwclient/root.go index b63f3f41..70167b2f 100644 --- a/internal/app/wwclient/root.go +++ b/internal/app/wwclient/root.go @@ -17,7 +17,7 @@ import ( "github.com/coreos/go-systemd/daemon" "github.com/google/uuid" "github.com/hpcng/warewulf/internal/pkg/pidfile" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/spf13/cobra" "github.com/talos-systems/go-smbios/smbios" diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index 9b62b507..3693e03f 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -15,7 +15,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" "github.com/spf13/cobra" diff --git a/internal/app/wwctl/container/list/main_test.go b/internal/app/wwctl/container/list/main_test.go index f87702e9..160a48dc 100644 --- a/internal/app/wwctl/container/list/main_test.go +++ b/internal/app/wwctl/container/list/main_test.go @@ -10,7 +10,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/stretchr/testify/assert" ) diff --git a/internal/app/wwctl/genconf/warewulfconf/print/main.go b/internal/app/wwctl/genconf/warewulfconf/print/main.go index 2efca477..26cab821 100644 --- a/internal/app/wwctl/genconf/warewulfconf/print/main.go +++ b/internal/app/wwctl/genconf/warewulfconf/print/main.go @@ -3,7 +3,7 @@ package print import ( "fmt" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/spf13/cobra" "gopkg.in/yaml.v2" ) diff --git a/internal/app/wwctl/node/add/main_test.go b/internal/app/wwctl/node/add/main_test.go index 6750cedf..95b14873 100644 --- a/internal/app/wwctl/node/add/main_test.go +++ b/internal/app/wwctl/node/add/main_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/stretchr/testify/assert" ) diff --git a/internal/app/wwctl/node/list/main_test.go b/internal/app/wwctl/node/list/main_test.go index 7d0e5751..4178f2b0 100644 --- a/internal/app/wwctl/node/list/main_test.go +++ b/internal/app/wwctl/node/list/main_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/stretchr/testify/assert" ) diff --git a/internal/app/wwctl/node/status/main.go b/internal/app/wwctl/node/status/main.go index 5d8cdef7..1d0dc6f9 100644 --- a/internal/app/wwctl/node/status/main.go +++ b/internal/app/wwctl/node/status/main.go @@ -9,7 +9,7 @@ import ( "github.com/fatih/color" apinode "github.com/hpcng/warewulf/internal/pkg/api/node" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/hpcng/warewulf/pkg/hostlist" "github.com/spf13/cobra" diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index 556833c4..bb61b131 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -7,7 +7,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/overlay" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/hpcng/warewulf/pkg/hostlist" "github.com/spf13/cobra" diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go index 46f29d30..4f4efad3 100644 --- a/internal/app/wwctl/profile/list/main_test.go +++ b/internal/app/wwctl/profile/list/main_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/stretchr/testify/assert" ) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 42f9e4e9..2717c72e 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -15,7 +15,7 @@ import ( "github.com/hpcng/warewulf/internal/app/wwctl/ssh" "github.com/hpcng/warewulf/internal/app/wwctl/version" "github.com/hpcng/warewulf/internal/pkg/help" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/spf13/cobra" ) diff --git a/internal/app/wwctl/server/start/main.go b/internal/app/wwctl/server/start/main.go index 00429177..076e1695 100644 --- a/internal/app/wwctl/server/start/main.go +++ b/internal/app/wwctl/server/start/main.go @@ -1,7 +1,7 @@ package start import ( - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/pkg/errors" "github.com/spf13/cobra" diff --git a/internal/pkg/api/node/node.go b/internal/pkg/api/node/node.go index 2c63d8db..8c5581ab 100644 --- a/internal/pkg/api/node/node.go +++ b/internal/pkg/api/node/node.go @@ -10,7 +10,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/hpcng/warewulf/pkg/hostlist" "github.com/pkg/errors" diff --git a/internal/pkg/warewulfconf/buildconfig.go.in b/internal/pkg/config/buildconfig.go.in similarity index 97% rename from internal/pkg/warewulfconf/buildconfig.go.in rename to internal/pkg/config/buildconfig.go.in index cd7ac2f4..eae1071f 100644 --- a/internal/pkg/warewulfconf/buildconfig.go.in +++ b/internal/pkg/config/buildconfig.go.in @@ -1,4 +1,4 @@ -package warewulfconf +package config type BuildConfig struct { Bindir string `default:"@BINDIR@"` diff --git a/internal/pkg/warewulfconf/constructors.go b/internal/pkg/config/constructors.go similarity index 99% rename from internal/pkg/warewulfconf/constructors.go rename to internal/pkg/config/constructors.go index 2a1e0ff0..6c1192e0 100644 --- a/internal/pkg/warewulfconf/constructors.go +++ b/internal/pkg/config/constructors.go @@ -1,4 +1,4 @@ -package warewulfconf +package config import ( "fmt" diff --git a/internal/pkg/warewulfconf/datastructure.go b/internal/pkg/config/datastructure.go similarity index 99% rename from internal/pkg/warewulfconf/datastructure.go rename to internal/pkg/config/datastructure.go index 1958a2b5..1e92866e 100644 --- a/internal/pkg/warewulfconf/datastructure.go +++ b/internal/pkg/config/datastructure.go @@ -1,4 +1,4 @@ -package warewulfconf +package config import ( "github.com/creasty/defaults" diff --git a/internal/pkg/warewulfconf/modifiers.go b/internal/pkg/config/modifiers.go similarity index 96% rename from internal/pkg/warewulfconf/modifiers.go rename to internal/pkg/config/modifiers.go index 457ed7f0..0c4209f7 100644 --- a/internal/pkg/warewulfconf/modifiers.go +++ b/internal/pkg/config/modifiers.go @@ -1,4 +1,4 @@ -package warewulfconf +package config import ( "os" diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index 4d2f42df..c5b1c207 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -6,7 +6,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index d7e18469..14f13e36 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -5,7 +5,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index 10186bc2..48d3818a 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -6,7 +6,7 @@ import ( "path" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 74bed889..4988ddfc 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -6,7 +6,7 @@ import ( "path" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/container/config.go b/internal/pkg/container/config.go index ecd751fa..f0babc66 100644 --- a/internal/pkg/container/config.go +++ b/internal/pkg/container/config.go @@ -3,7 +3,7 @@ package container import ( "path" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" ) func SourceParentDir() string { diff --git a/internal/pkg/container/imprt.go b/internal/pkg/container/imprt.go index 7a050f49..620991e7 100644 --- a/internal/pkg/container/imprt.go +++ b/internal/pkg/container/imprt.go @@ -9,7 +9,7 @@ import ( "github.com/containers/storage/drivers/copy" "github.com/pkg/errors" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/oci" "github.com/hpcng/warewulf/internal/pkg/util" ) diff --git a/internal/pkg/container/mountpoints.go b/internal/pkg/container/mountpoints.go index 727ff35c..d7281758 100644 --- a/internal/pkg/container/mountpoints.go +++ b/internal/pkg/container/mountpoints.go @@ -3,7 +3,7 @@ package container import ( "strings" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/kernel/kernel.go b/internal/pkg/kernel/kernel.go index 32a1da47..4611eb3a 100644 --- a/internal/pkg/kernel/kernel.go +++ b/internal/pkg/kernel/kernel.go @@ -12,7 +12,7 @@ import ( "github.com/pkg/errors" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 0619e912..86c19b49 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -8,7 +8,7 @@ import ( "sort" "strings" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "gopkg.in/yaml.v2" diff --git a/internal/pkg/oci/defaults.go b/internal/pkg/oci/defaults.go index ee6dc0e6..74bfb859 100644 --- a/internal/pkg/oci/defaults.go +++ b/internal/pkg/oci/defaults.go @@ -1,11 +1,13 @@ package oci -import "github.com/hpcng/warewulf/internal/pkg/warewulfconf" -import "path/filepath" +import ( + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" + "path/filepath" +) var defaultCachePath = filepath.Join(warewulfconf.DataStore(), "/container-cache/oci/") const ( blobPrefix = "blobs" rootfsPrefix = "rootfs" -) \ No newline at end of file +) diff --git a/internal/pkg/overlay/config.go b/internal/pkg/overlay/config.go index 91452e4a..59f268fc 100644 --- a/internal/pkg/overlay/config.go +++ b/internal/pkg/overlay/config.go @@ -5,7 +5,7 @@ import ( "path" "strings" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" ) func OverlaySourceTopDir() string { diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index c7da9a70..2cd50b01 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -7,7 +7,7 @@ import ( "time" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/overlay/funcmap.go b/internal/pkg/overlay/funcmap.go index ebdb1b1a..793f8d9c 100644 --- a/internal/pkg/overlay/funcmap.go +++ b/internal/pkg/overlay/funcmap.go @@ -8,7 +8,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/container" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/version/version.go b/internal/pkg/version/version.go index 6b4771bb..0632e2e6 100644 --- a/internal/pkg/version/version.go +++ b/internal/pkg/version/version.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" ) /* diff --git a/internal/pkg/warewulfd/daemon.go b/internal/pkg/warewulfd/daemon.go index 34a989b6..5d3610c2 100644 --- a/internal/pkg/warewulfd/daemon.go +++ b/internal/pkg/warewulfd/daemon.go @@ -11,7 +11,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/version" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) diff --git a/internal/pkg/warewulfd/provision.go b/internal/pkg/warewulfd/provision.go index c96267cc..230f7eb3 100644 --- a/internal/pkg/warewulfd/provision.go +++ b/internal/pkg/warewulfd/provision.go @@ -10,7 +10,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/container" "github.com/hpcng/warewulf/internal/pkg/kernel" "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) diff --git a/internal/pkg/warewulfd/warewulfd.go b/internal/pkg/warewulfd/warewulfd.go index d69aa210..43c24965 100644 --- a/internal/pkg/warewulfd/warewulfd.go +++ b/internal/pkg/warewulfd/warewulfd.go @@ -7,7 +7,7 @@ import ( "strconv" "syscall" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) From f78e6b73b15bd38cc9435915f5d865115275ef26 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 16:59:09 -0600 Subject: [PATCH 02/13] Rename ControllerConf to BaseConfig Also moved BaseConfig to a new source file, co-located with its constructors and methods, and added doc comments. Signed-off-by: Jonathon Anderson --- internal/pkg/config/datastructure.go | 19 ---- internal/pkg/config/modifiers.go | 32 ------ .../pkg/config/{constructors.go => root.go} | 99 ++++++++++++++----- 3 files changed, 76 insertions(+), 74 deletions(-) delete mode 100644 internal/pkg/config/modifiers.go rename internal/pkg/config/{constructors.go => root.go} (57%) diff --git a/internal/pkg/config/datastructure.go b/internal/pkg/config/datastructure.go index 1e92866e..dedc762f 100644 --- a/internal/pkg/config/datastructure.go +++ b/internal/pkg/config/datastructure.go @@ -4,25 +4,6 @@ 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"` diff --git a/internal/pkg/config/modifiers.go b/internal/pkg/config/modifiers.go deleted file mode 100644 index 0c4209f7..00000000 --- a/internal/pkg/config/modifiers.go +++ /dev/null @@ -1,32 +0,0 @@ -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 -} diff --git a/internal/pkg/config/constructors.go b/internal/pkg/config/root.go similarity index 57% rename from internal/pkg/config/constructors.go rename to internal/pkg/config/root.go index 6c1192e0..d56dbe27 100644 --- a/internal/pkg/config/constructors.go +++ b/internal/pkg/config/root.go @@ -1,5 +1,12 @@ +// Package config reads, parses, and represents the warewulf.conf +// config file. +// +// warewulf.conf is a yaml-formatted configuration file that includes +// configuration for the Warewulf daemon and commands, as well as the +// DHCP, TFTP and NFS services that Warewulf manages. package config + import ( "fmt" "net" @@ -12,15 +19,39 @@ import ( "gopkg.in/yaml.v2" ) -var cachedConf ControllerConf -var ConfigFile string +var ( + cachedConf RootConf + ConfigFile string +) -/* -Creates a new empty ControllerConf object, returns a cached -one if called in a nother context. -*/ -func New() (conf ControllerConf) { + +// RootConf is the main Warewulf configuration structure. It stores +// some information about the Warewulf server locally, and has +// [WarewulfConf], [DhcpConf], [TftpConf], and [NfsConf] sub-sections. +type RootConf 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 +} + + +// New returns a [RootConf] which may have been cached from a previous +// call. +func New() (conf RootConf) { // 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 { @@ -40,10 +71,9 @@ func New() (conf ControllerConf) { return conf } -/* -Populate the configuration with the values from the configuration file. -*/ -func (conf *ControllerConf) ReadConf(confFileName string) (err error) { +// ReadConf populates the configuration with the values from a +// configuration file. +func (conf *RootConf) ReadConf(confFileName string) (err error) { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) fileHandle, err := os.ReadFile(confFileName) if err != nil { @@ -52,10 +82,9 @@ func (conf *ControllerConf) ReadConf(confFileName string) (err error) { return conf.Read(fileHandle) } -/* -Populate the configuration with the values from the given yaml information -*/ -func (conf *ControllerConf) Read(data []byte) (err error) { +// Read populates the configuration with the values from a yaml +// document. +func (conf *RootConf) 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 { @@ -79,10 +108,9 @@ func (conf *ControllerConf) Read(data []byte) (err error) { return } -/* -Set the runtime defaults like IP address of running system to the config -*/ -func (conf *ControllerConf) SetDynamicDefaults() (err error) { +// SetDynamicDefaults populates the configuration with plausible +// defaults for the runtime environment. +func (conf *RootConf) SetDynamicDefaults() (err error) { if conf.Ipaddr == "" || conf.Netmask == "" || conf.Network == "" { var mask net.IPMask var network *net.IPNet @@ -148,9 +176,34 @@ func (conf *ControllerConf) SetDynamicDefaults() (err error) { return } -/* -Return if configuration was read from disk -*/ -func (conf *ControllerConf) Initialized() bool { +// Initialized returns true if the configuration in memory was read +// from disk, or false otherwise. +func (conf *RootConf) Initialized() bool { return conf.readConf } + + +// Persist writes the configuration to a file as a yaml document. +func (controller *RootConf) 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 +} From 70292276e2b389646ee9a1f1578cde27de409f6d Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 17:29:35 -0600 Subject: [PATCH 03/13] Rename BaseConf.New to BaseConf.Get Since BaseConf.New could return a cached BaseConf, rather than always constructing a new struct, I've renamed it to Get to more accurately reflect its use. A new New() method is called by Get and always initializes a new struct. Signed-off-by: Jonathon Anderson --- internal/app/api/wwapic/wwapic.go | 2 +- internal/app/api/wwapid/wwapid.go | 4 +- internal/app/api/wwapird/wwapird.go | 2 +- internal/app/wwclient/root.go | 2 +- .../app/wwctl/container/exec/child/main.go | 2 +- .../app/wwctl/container/list/main_test.go | 2 +- .../wwctl/genconf/warewulfconf/print/main.go | 2 +- internal/app/wwctl/node/status/main.go | 2 +- internal/app/wwctl/overlay/build/main.go | 2 +- internal/app/wwctl/root.go | 2 +- internal/app/wwctl/server/start/main.go | 2 +- internal/pkg/api/node/node.go | 2 +- internal/pkg/config/datastructure.go | 2 +- internal/pkg/config/root.go | 65 ++++++++++--------- internal/pkg/configure/dhcp.go | 2 +- internal/pkg/configure/nfs.go | 2 +- internal/pkg/configure/ssh.go | 2 +- internal/pkg/configure/tftp.go | 2 +- internal/pkg/container/config.go | 4 +- internal/pkg/kernel/kernel.go | 2 +- internal/pkg/node/constructors.go | 2 +- internal/pkg/overlay/config.go | 4 +- internal/pkg/overlay/datastructure.go | 2 +- internal/pkg/overlay/funcmap.go | 4 +- internal/pkg/version/version.go | 2 +- internal/pkg/warewulfd/daemon.go | 2 +- internal/pkg/warewulfd/provision.go | 2 +- internal/pkg/warewulfd/warewulfd.go | 2 +- 28 files changed, 65 insertions(+), 62 deletions(-) diff --git a/internal/app/api/wwapic/wwapic.go b/internal/app/api/wwapic/wwapic.go index fc34659c..8eca0814 100644 --- a/internal/app/api/wwapic/wwapic.go +++ b/internal/app/api/wwapic/wwapic.go @@ -24,7 +24,7 @@ import ( func main() { log.Println("Client running") - conf := warewulfconf.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewClient(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapic.conf")) diff --git a/internal/app/api/wwapid/wwapid.go b/internal/app/api/wwapid/wwapid.go index 33de9db6..5042b8c7 100644 --- a/internal/app/api/wwapid/wwapid.go +++ b/internal/app/api/wwapid/wwapid.go @@ -16,7 +16,7 @@ import ( apinode "github.com/hpcng/warewulf/internal/pkg/api/node" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/version" - "github.com/hpcng/warewulf/internal/pkg/config" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -34,7 +34,7 @@ var apiVersion string func main() { log.Println("Server running") - conf := config.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapid.conf")) if err != nil { diff --git a/internal/app/api/wwapird/wwapird.go b/internal/app/api/wwapird/wwapird.go index c71f56e8..354b82dd 100644 --- a/internal/app/api/wwapird/wwapird.go +++ b/internal/app/api/wwapird/wwapird.go @@ -29,7 +29,7 @@ func run() error { log.Println("test0") - conf := warewulfconf.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewClientServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapird.conf")) if err != nil { diff --git a/internal/app/wwclient/root.go b/internal/app/wwclient/root.go index 70167b2f..5af79840 100644 --- a/internal/app/wwclient/root.go +++ b/internal/app/wwclient/root.go @@ -49,7 +49,7 @@ func GetRootCommand() *cobra.Command { } func CobraRunE(cmd *cobra.Command, args []string) error { - conf := warewulfconf.New() + conf := warewulfconf.Get() pid, err := pidfile.Write(PIDFile) if err != nil && pid == -1 { diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index 3693e03f..ee2b97b9 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -33,7 +33,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { wwlog.Error("Unknown Warewulf container: %s", containerName) os.Exit(1) } - conf := warewulfconf.New() + conf := warewulfconf.Get() mountPts := conf.MountsContainer mountPts = append(container.InitMountPnts(binds), mountPts...) // check for valid mount points diff --git a/internal/app/wwctl/container/list/main_test.go b/internal/app/wwctl/container/list/main_test.go index 160a48dc..2d5afc3a 100644 --- a/internal/app/wwctl/container/list/main_test.go +++ b/internal/app/wwctl/container/list/main_test.go @@ -55,7 +55,7 @@ nodes: WW_INTERNAL: 0 ` - conf := warewulfconf.New() + conf := warewulfconf.Get() err := conf.Read([]byte(conf_yml)) assert.NoError(t, err) warewulfd.SetNoDaemon() diff --git a/internal/app/wwctl/genconf/warewulfconf/print/main.go b/internal/app/wwctl/genconf/warewulfconf/print/main.go index 26cab821..e18ea611 100644 --- a/internal/app/wwctl/genconf/warewulfconf/print/main.go +++ b/internal/app/wwctl/genconf/warewulfconf/print/main.go @@ -9,7 +9,7 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - conf := warewulfconf.New() + conf := warewulfconf.Get() buffer, err := yaml.Marshal(&conf) if err != nil { return diff --git a/internal/app/wwctl/node/status/main.go b/internal/app/wwctl/node/status/main.go index 1d0dc6f9..d1675cb9 100644 --- a/internal/app/wwctl/node/status/main.go +++ b/internal/app/wwctl/node/status/main.go @@ -18,7 +18,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) (err error) { - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Ipaddr == "" { return fmt.Errorf("warewulf Server IP Address is not properly configured") diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index bb61b131..75c7cf82 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -14,7 +14,7 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - controller := warewulfconf.New() + controller := warewulfconf.Get() nodeDB, err := node.New() if err != nil { wwlog.Error("Could not open node configuration: %s", err) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 2717c72e..c9d56480 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -76,7 +76,7 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { if LogLevel != wwlog.INFO { wwlog.SetLogLevel(LogLevel) } - conf := warewulfconf.New() + conf := warewulfconf.Get() if !AllowEmptyConf && !conf.Initialized() { if WarewulfConfArg != "" { err = conf.ReadConf(WarewulfConfArg) diff --git a/internal/app/wwctl/server/start/main.go b/internal/app/wwctl/server/start/main.go index 076e1695..01c9a19f 100644 --- a/internal/app/wwctl/server/start/main.go +++ b/internal/app/wwctl/server/start/main.go @@ -10,7 +10,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { if SetForeground { - conf := warewulfconf.New() + conf := warewulfconf.Get() conf.Warewulf.Syslog = false return errors.Wrap(warewulfd.RunServer(), "failed to start Warewulf server") } else { diff --git a/internal/pkg/api/node/node.go b/internal/pkg/api/node/node.go index 8c5581ab..13a10c15 100644 --- a/internal/pkg/api/node/node.go +++ b/internal/pkg/api/node/node.go @@ -317,7 +317,7 @@ func NodeStatus(nodeNames []string) (nodeStatusResponse *wwapiv1.NodeStatusRespo Nodes map[string]*nodeStatusInternal `json:"nodes"` } - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Ipaddr == "" { err = fmt.Errorf("the Warewulf Server IP Address is not properly configured") diff --git a/internal/pkg/config/datastructure.go b/internal/pkg/config/datastructure.go index dedc762f..92ada6ee 100644 --- a/internal/pkg/config/datastructure.go +++ b/internal/pkg/config/datastructure.go @@ -63,6 +63,6 @@ func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error { // Waste processor cycles to make code more readable func DataStore() string { - _ = New() + _ = Get() return cachedConf.Warewulf.DataStore } diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index d56dbe27..e654f291 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -11,6 +11,7 @@ import ( "fmt" "net" "os" + "reflect" "github.com/pkg/errors" @@ -44,35 +45,37 @@ type RootConf struct { 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 } -// New returns a [RootConf] which may have been cached from a previous -// call. +// New returns a [RootConf] initialized with empty values. func New() (conf RootConf) { - // 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 + conf.Warewulf = new(WarewulfConf) + conf.Dhcp = new(DhcpConf) + conf.Tftp = new(TftpConf) + conf.Nfs = new(NfsConf) + conf.Paths = new(BuildConfig) + _ = defaults.Set(&conf) + return } -// ReadConf populates the configuration with the values from a -// configuration file. + +// Get returns a [RootConf] which may have been cached from a previous +// call. +func Get() (RootConf) { + // NOTE: This function can be called before any log level is set + // so using wwlog.Verbose or wwlog.Debug won't work + if reflect.ValueOf(cachedConf).IsZero() { + cachedConf = New() + cachedConf.readConf = false + } + return cachedConf +} + + +// ReadConf populates [RootConf] with the values from a configuration +// file. func (conf *RootConf) ReadConf(confFileName string) (err error) { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) fileHandle, err := os.ReadFile(confFileName) @@ -82,8 +85,8 @@ func (conf *RootConf) ReadConf(confFileName string) (err error) { return conf.Read(fileHandle) } -// Read populates the configuration with the values from a yaml -// document. + +// Read populates [RootConf] with the values from a yaml document. func (conf *RootConf) Read(data []byte) (err error) { // ipxe binaries are merged not overwritten, store defaults separate defIpxe := make(map[string]string) @@ -103,13 +106,13 @@ func (conf *RootConf) Read(data []byte) (err error) { conf.Tftp.IpxeBinaries = defIpxe } cachedConf = *conf - cachedConf.current = true cachedConf.readConf = true return } -// SetDynamicDefaults populates the configuration with plausible -// defaults for the runtime environment. + +// SetDynamicDefaults populates [RootConf] with plausible defaults for +// the runtime environment. func (conf *RootConf) SetDynamicDefaults() (err error) { if conf.Ipaddr == "" || conf.Netmask == "" || conf.Network == "" { var mask net.IPMask @@ -172,18 +175,18 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { } } cachedConf = *conf - cachedConf.current = true return } -// Initialized returns true if the configuration in memory was read -// from disk, or false otherwise. + +// Initialized returns true if [RootConf] memory was read from disk, +// or false otherwise. func (conf *RootConf) Initialized() bool { return conf.readConf } -// Persist writes the configuration to a file as a yaml document. +// Persist writes [RootConf] to a file as a yaml document. func (controller *RootConf) Persist() error { out, err := yaml.Marshal(controller) diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index c5b1c207..d7ef2e69 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -17,7 +17,7 @@ dhcp configuration is checked. */ func Dhcp() (err error) { - controller := warewulfconf.New() + controller := warewulfconf.Get() if !controller.Dhcp.Enabled { wwlog.Info("This system is not configured as a Warewulf DHCP controller") diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index 14f13e36..343d59c8 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -16,7 +16,7 @@ nfs server. */ func NFS() error { - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Nfs.Enabled { if controller.Warewulf.EnableHostOverlay { diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index 48d3818a..1c24a3b2 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -14,7 +14,7 @@ import ( func SSH() error { if os.Getuid() == 0 { fmt.Printf("Updating system keys\n") - conf := warewulfconf.New() + conf := warewulfconf.Get() wwkeydir := path.Join(conf.Paths.Sysconfdir, "warewulf/keys") + "/" err := os.MkdirAll(path.Join(conf.Paths.Sysconfdir, "warewulf/keys"), 0755) diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 4988ddfc..16a28bbb 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -11,7 +11,7 @@ import ( ) func TFTP() error { - controller := warewulfconf.New() + controller := warewulfconf.Get() var tftpdir string = path.Join(controller.Paths.Tftpdir, "warewulf") err := os.MkdirAll(tftpdir, 0755) diff --git a/internal/pkg/container/config.go b/internal/pkg/container/config.go index f0babc66..f166e643 100644 --- a/internal/pkg/container/config.go +++ b/internal/pkg/container/config.go @@ -7,7 +7,7 @@ import ( ) func SourceParentDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return conf.Paths.WWChrootdir } @@ -20,7 +20,7 @@ func RootFsDir(name string) string { } func ImageParentDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "container/") } diff --git a/internal/pkg/kernel/kernel.go b/internal/pkg/kernel/kernel.go index 4611eb3a..a735f384 100644 --- a/internal/pkg/kernel/kernel.go +++ b/internal/pkg/kernel/kernel.go @@ -28,7 +28,7 @@ var ( ) func KernelImageTopDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "kernel") } diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 86c19b49..c77ba038 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -40,7 +40,7 @@ defaultnode: netmask: 255.255.255.0` func init() { - conf := warewulfconf.New() + conf := warewulfconf.Get() if ConfigFile == "" { ConfigFile = path.Join(conf.Paths.Sysconfdir, "warewulf/nodes.conf") } diff --git a/internal/pkg/overlay/config.go b/internal/pkg/overlay/config.go index 59f268fc..d2a9e924 100644 --- a/internal/pkg/overlay/config.go +++ b/internal/pkg/overlay/config.go @@ -9,7 +9,7 @@ import ( ) func OverlaySourceTopDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return conf.Paths.WWOverlaydir } @@ -31,6 +31,6 @@ func OverlaySourceDir(overlayName string) string { Returns the overlay name of the image for a given node */ func OverlayImage(nodeName string, overlayName []string) string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "overlays/", nodeName, strings.Join(overlayName, "-")+".img") } diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index 2cd50b01..760d3f42 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -44,7 +44,7 @@ Initialize an TemplateStruct with the given node.NodeInfo */ func InitStruct(nodeInfo node.NodeInfo) TemplateStruct { var tstruct TemplateStruct - controller := warewulfconf.New() + controller := warewulfconf.Get() nodeDB, err := node.New() if err != nil { wwlog.Error("%s", err) diff --git a/internal/pkg/overlay/funcmap.go b/internal/pkg/overlay/funcmap.go index 793f8d9c..4acf6705 100644 --- a/internal/pkg/overlay/funcmap.go +++ b/internal/pkg/overlay/funcmap.go @@ -17,7 +17,7 @@ Reads a file file from the host fs. If the file has nor '/' prefix the path is relative to Paths.SysconfdirTemplates in the file are no evaluated. */ func templateFileInclude(inc string) string { - conf := warewulfconf.New() + conf := warewulfconf.Get() if !strings.HasPrefix(inc, "/") { inc = path.Join(conf.Paths.Sysconfdir, "warewulf", inc) } @@ -35,7 +35,7 @@ is the file to read, the second the abort string Templates in the file are no evaluated. */ func templateFileBlock(inc string, abortStr string) (string, error) { - conf := warewulfconf.New() + conf := warewulfconf.Get() if !strings.HasPrefix(inc, "/") { inc = path.Join(conf.Paths.Sysconfdir, "warewulf", inc) } diff --git a/internal/pkg/version/version.go b/internal/pkg/version/version.go index 0632e2e6..d4b5fb85 100644 --- a/internal/pkg/version/version.go +++ b/internal/pkg/version/version.go @@ -11,7 +11,7 @@ import ( Return the version of wwctl */ func GetVersion() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return fmt.Sprintf("%s-%s", conf.Paths.Version, conf.Paths.Release) } diff --git a/internal/pkg/warewulfd/daemon.go b/internal/pkg/warewulfd/daemon.go index 5d3610c2..bfa16f9d 100644 --- a/internal/pkg/warewulfd/daemon.go +++ b/internal/pkg/warewulfd/daemon.go @@ -61,7 +61,7 @@ func DaemonInitLogging() error { wwlog.SetLogLevel(wwlog.SERV) } - conf := warewulfconf.New() + conf := warewulfconf.Get() if conf.Warewulf.Syslog { diff --git a/internal/pkg/warewulfd/provision.go b/internal/pkg/warewulfd/provision.go index 230f7eb3..60457208 100644 --- a/internal/pkg/warewulfd/provision.go +++ b/internal/pkg/warewulfd/provision.go @@ -30,7 +30,7 @@ type iPxeTemplate struct { } func ProvisionSend(w http.ResponseWriter, req *http.Request) { - conf := warewulfconf.New() + conf := warewulfconf.Get() rinfo, err := parseReq(req) if err != nil { diff --git a/internal/pkg/warewulfd/warewulfd.go b/internal/pkg/warewulfd/warewulfd.go index 43c24965..796054b0 100644 --- a/internal/pkg/warewulfd/warewulfd.go +++ b/internal/pkg/warewulfd/warewulfd.go @@ -58,7 +58,7 @@ func RunServer() error { http.HandleFunc("/overlay-runtime/", ProvisionSend) http.HandleFunc("/status", StatusSend) - conf := warewulfconf.New() + conf := warewulfconf.Get() daemonPort := conf.Warewulf.Port wwlog.Serv("Starting HTTPD REST service on port %d", daemonPort) From af6c91ade3faeb3522795827b707aa6e2af778ea Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 17:34:36 -0600 Subject: [PATCH 04/13] Rename BaseConf.Initialized to BaseConf.InitializedFromFile Initialized could be interpreted to mean dynamically initialized as well. This new name removes that ambiguity. Signed-off-by: Jonathon Anderson --- internal/app/wwctl/root.go | 2 +- internal/pkg/config/root.go | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index c9d56480..4d40ccde 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -77,7 +77,7 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { wwlog.SetLogLevel(LogLevel) } conf := warewulfconf.Get() - if !AllowEmptyConf && !conf.Initialized() { + if !AllowEmptyConf && !conf.InitializedFromFile() { if WarewulfConfArg != "" { err = conf.ReadConf(WarewulfConfArg) } else if os.Getenv("WAREWULFCONF") != "" { diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index e654f291..374f7d7c 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -45,7 +45,8 @@ type RootConf struct { Nfs *NfsConf `yaml:"nfs"` MountsContainer []*MountEntry `yaml:"container mounts" default:"[{\"source\": \"/etc/resolv.conf\", \"dest\": \"/etc/resolv.conf\"}]"` Paths *BuildConfig `yaml:"paths"` - readConf bool + + fromFile bool } @@ -68,7 +69,7 @@ func Get() (RootConf) { // so using wwlog.Verbose or wwlog.Debug won't work if reflect.ValueOf(cachedConf).IsZero() { cachedConf = New() - cachedConf.readConf = false + cachedConf.fromFile = false } return cachedConf } @@ -106,7 +107,7 @@ func (conf *RootConf) Read(data []byte) (err error) { conf.Tftp.IpxeBinaries = defIpxe } cachedConf = *conf - cachedConf.readConf = true + cachedConf.fromFile = true return } @@ -179,10 +180,10 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { } -// Initialized returns true if [RootConf] memory was read from disk, -// or false otherwise. -func (conf *RootConf) Initialized() bool { - return conf.readConf +// InitializedFromFile returns true if [RootConf] memory was read from +// a file, or false otherwise. +func (conf *RootConf) InitializedFromFile() bool { + return conf.fromFile } From a10fdb3808a7f505fa3d5ab8847bb3af610f3b47 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 17:39:20 -0600 Subject: [PATCH 05/13] Remove config.DataStore() Comments in the code were already disparaging of config.DataStore(), but implied that its presence made other code more readable. It was only used in two places and, imo, how I've refactored it is more explicit and clear than it was. Signed-off-by: Jonathon Anderson --- internal/pkg/config/datastructure.go | 7 ------- internal/pkg/container/imprt.go | 2 +- internal/pkg/oci/defaults.go | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/internal/pkg/config/datastructure.go b/internal/pkg/config/datastructure.go index 92ada6ee..4d5cefd1 100644 --- a/internal/pkg/config/datastructure.go +++ b/internal/pkg/config/datastructure.go @@ -59,10 +59,3 @@ func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error { } return nil } - -// Waste processor cycles to make code more readable - -func DataStore() string { - _ = Get() - return cachedConf.Warewulf.DataStore -} diff --git a/internal/pkg/container/imprt.go b/internal/pkg/container/imprt.go index 620991e7..2f62b438 100644 --- a/internal/pkg/container/imprt.go +++ b/internal/pkg/container/imprt.go @@ -15,7 +15,7 @@ import ( ) func ImportDocker(uri string, name string, sCtx *types.SystemContext) error { - OciBlobCacheDir := warewulfconf.DataStore() + "/oci" + OciBlobCacheDir := warewulfconf.Get().Warewulf.DataStore + "/oci" err := os.MkdirAll(OciBlobCacheDir, 0755) if err != nil { diff --git a/internal/pkg/oci/defaults.go b/internal/pkg/oci/defaults.go index 74bfb859..f7158a6f 100644 --- a/internal/pkg/oci/defaults.go +++ b/internal/pkg/oci/defaults.go @@ -5,7 +5,7 @@ import ( "path/filepath" ) -var defaultCachePath = filepath.Join(warewulfconf.DataStore(), "/container-cache/oci/") +var defaultCachePath = filepath.Join(warewulfconf.Get().Warewulf.DataStore, "/container-cache/oci/") const ( blobPrefix = "blobs" From 387cd0f7be70e90711e9a472d5a96cf443366444 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 18:34:03 -0600 Subject: [PATCH 06/13] Refactor BaseConf Read and Parse Also refactor New and Get to return pointers to match BaseConf methods. This makes calling the methods immediately on the return values of the constructors easier. Also move config.ConfigFile to buildconfig.go.in. ConfigFile is still used by wwctl as a default config file to reference; but it is removed from other locations now. Removed Persist, as nothing called it. Signed-off-by: Jonathon Anderson --- Makefile | 9 +- .../app/wwctl/container/list/main_test.go | 2 +- internal/app/wwctl/node/add/main_test.go | 2 +- internal/app/wwctl/node/list/main_test.go | 2 +- internal/app/wwctl/profile/list/main_test.go | 2 +- internal/app/wwctl/root.go | 6 +- internal/pkg/config/buildconfig.go.in | 2 + internal/pkg/config/root.go | 85 +++++++------------ 8 files changed, 42 insertions(+), 68 deletions(-) diff --git a/Makefile b/Makefile index a9fc7dd2..1abd6869 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ vet: go vet ./... test-it: - go test -v ./... -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=$(shell pwd)/etc/warewulf.conf'" + go test -v ./... # Generate test coverage test-cover: ## Run test coverage and generate html report @@ -219,13 +219,12 @@ init: wwctl: $(WWCTL_DEPS) @echo Building "$@" @cd cmd/wwctl; GOOS=linux go build -mod vendor -tags "$(WW_GO_BUILD_TAGS)" \ - -ldflags "-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=$(SYSCONFDIR)/warewulf/warewulf.conf'" \ -o ../../wwctl wwclient: $(WWCLIENT_DEPS) @echo Building "$@" - @cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags -static \ - -X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=/etc/warewulf/warewulf.conf'" -o ../../wwclient + @cd cmd/wwclient; CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags -static" \ + -o ../../wwclient man_pages: wwctl @install -d man_pages @@ -235,7 +234,7 @@ man_pages: wwctl @cd man_pages; for i in wwctl*1 *.5; do gzip --force $$i; echo -n "$$i "; done; echo update_configuration: vendor cmd/update_configuration/update_configuration.go - cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/config.ConfigFile=./etc/warewulf.conf'\ + cd cmd/update_configuration && go build \ -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../update_configuration diff --git a/internal/app/wwctl/container/list/main_test.go b/internal/app/wwctl/container/list/main_test.go index 2d5afc3a..ab3c880f 100644 --- a/internal/app/wwctl/container/list/main_test.go +++ b/internal/app/wwctl/container/list/main_test.go @@ -56,7 +56,7 @@ WW_INTERNAL: 0 ` conf := warewulfconf.Get() - err := conf.Read([]byte(conf_yml)) + err := conf.Parse([]byte(conf_yml)) assert.NoError(t, err) warewulfd.SetNoDaemon() for _, tt := range tests { diff --git a/internal/app/wwctl/node/add/main_test.go b/internal/app/wwctl/node/add/main_test.go index 95b14873..f0c7e7df 100644 --- a/internal/app/wwctl/node/add/main_test.go +++ b/internal/app/wwctl/node/add/main_test.go @@ -243,7 +243,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.ConfigFile = tempWarewulfConf.Name() + warewulfconf.New().Read(tempWarewulfConf.Name()) nodes_yml := `WW_INTERNAL: 43` tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") diff --git a/internal/app/wwctl/node/list/main_test.go b/internal/app/wwctl/node/list/main_test.go index 4178f2b0..ebd1ac3b 100644 --- a/internal/app/wwctl/node/list/main_test.go +++ b/internal/app/wwctl/node/list/main_test.go @@ -42,7 +42,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.ConfigFile = tempWarewulfConf.Name() + warewulfconf.New().Read(tempWarewulfConf.Name()) tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") assert.NoError(t, nodesConfErr) diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go index 4f4efad3..00f6144f 100644 --- a/internal/app/wwctl/profile/list/main_test.go +++ b/internal/app/wwctl/profile/list/main_test.go @@ -43,7 +43,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.ConfigFile = tempWarewulfConf.Name() + warewulfconf.New().Read(tempWarewulfConf.Name()) tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") assert.NoError(t, nodesConfErr) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 4d40ccde..282615b2 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -79,11 +79,11 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { conf := warewulfconf.Get() if !AllowEmptyConf && !conf.InitializedFromFile() { if WarewulfConfArg != "" { - err = conf.ReadConf(WarewulfConfArg) + err = conf.Read(WarewulfConfArg) } else if os.Getenv("WAREWULFCONF") != "" { - err = conf.ReadConf(os.Getenv("WAREWULFCONF")) + err = conf.Read(os.Getenv("WAREWULFCONF")) } else { - err = conf.ReadConf(warewulfconf.ConfigFile) + err = conf.Read(warewulfconf.ConfigFile) } } else { err = conf.SetDynamicDefaults() diff --git a/internal/pkg/config/buildconfig.go.in b/internal/pkg/config/buildconfig.go.in index eae1071f..5316e518 100644 --- a/internal/pkg/config/buildconfig.go.in +++ b/internal/pkg/config/buildconfig.go.in @@ -1,5 +1,7 @@ package config +var ConfigFile = "@SYSCONFDIR@/warewulf/warewulf.conf" + type BuildConfig struct { Bindir string `default:"@BINDIR@"` Sysconfdir string `default:"@SYSCONFDIR@"` diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index 374f7d7c..89e418e0 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -21,10 +21,7 @@ import ( ) -var ( - cachedConf RootConf - ConfigFile string -) +var cachedConf RootConf // RootConf is the main Warewulf configuration structure. It stores @@ -50,65 +47,67 @@ type RootConf struct { } -// New returns a [RootConf] initialized with empty values. -func New() (conf RootConf) { - conf.Warewulf = new(WarewulfConf) - conf.Dhcp = new(DhcpConf) - conf.Tftp = new(TftpConf) - conf.Nfs = new(NfsConf) - conf.Paths = new(BuildConfig) - _ = defaults.Set(&conf) - return +// New caches and returns a new [RootConf] initialized with empty +// values, clearing replacing any previously cached value. +func New() (*RootConf) { + cachedConf = RootConf{} + cachedConf.fromFile = false + cachedConf.Warewulf = new(WarewulfConf) + cachedConf.Dhcp = new(DhcpConf) + cachedConf.Tftp = new(TftpConf) + cachedConf.Nfs = new(NfsConf) + cachedConf.Paths = new(BuildConfig) + if err := defaults.Set(&cachedConf); err != nil { + panic(err) + } + return &cachedConf } -// Get returns a [RootConf] which may have been cached from a previous -// call. -func Get() (RootConf) { +// Get returns a previously cached [RootConf] if it exists, or returns +// a new RootConf. +func Get() (*RootConf) { // NOTE: This function can be called before any log level is set // so using wwlog.Verbose or wwlog.Debug won't work if reflect.ValueOf(cachedConf).IsZero() { - cachedConf = New() - cachedConf.fromFile = false + cachedConf = *New() } - return cachedConf + return &cachedConf } // ReadConf populates [RootConf] with the values from a configuration // file. -func (conf *RootConf) ReadConf(confFileName string) (err error) { +func (conf *RootConf) Read(confFileName string) (error) { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) - fileHandle, err := os.ReadFile(confFileName) + data, err := os.ReadFile(confFileName) if err != nil { return err } - return conf.Read(fileHandle) + return conf.Parse(data) } // Read populates [RootConf] with the values from a yaml document. -func (conf *RootConf) Read(data []byte) (err error) { +func (conf *RootConf) Parse(data []byte) (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 + if err := yaml.Unmarshal(data, &conf); err != nil { + return err } - err = conf.SetDynamicDefaults() - if err != nil { - return + if err := conf.SetDynamicDefaults(); err != nil { + return err } if len(conf.Tftp.IpxeBinaries) == 0 { conf.Tftp.IpxeBinaries = defIpxe } cachedConf = *conf cachedConf.fromFile = true - return + return nil } @@ -185,29 +184,3 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { func (conf *RootConf) InitializedFromFile() bool { return conf.fromFile } - - -// Persist writes [RootConf] to a file as a yaml document. -func (controller *RootConf) 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 -} From cfdf179e167b2cf972c9ae43e672de81292e0b3c Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 18:59:39 -0600 Subject: [PATCH 07/13] Remove SetDynamicDefaults from Parse Signed-off-by: Jonathon Anderson --- internal/app/wwctl/root.go | 6 ++++-- internal/pkg/config/root.go | 19 ++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 282615b2..e694b09f 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -85,8 +85,10 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { } else { err = conf.Read(warewulfconf.ConfigFile) } - } else { - err = conf.SetDynamicDefaults() } + if err != nil { + return + } + err = conf.SetDynamicDefaults() return } diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index 89e418e0..822df5c0 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -76,19 +76,22 @@ func Get() (*RootConf) { } -// ReadConf populates [RootConf] with the values from a configuration +// Read populates [RootConf] with the values from a configuration // file. func (conf *RootConf) Read(confFileName string) (error) { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) - data, err := os.ReadFile(confFileName) - if err != nil { + if data, err := os.ReadFile(confFileName); err != nil { return err + } else if err := conf.Parse(data); err != nil { + return err + } else { + conf.fromFile = true + return nil } - return conf.Parse(data) } -// Read populates [RootConf] with the values from a yaml document. +// Parse populates [RootConf] with the values from a yaml document. func (conf *RootConf) Parse(data []byte) (error) { // ipxe binaries are merged not overwritten, store defaults separate defIpxe := make(map[string]string) @@ -99,14 +102,9 @@ func (conf *RootConf) Parse(data []byte) (error) { if err := yaml.Unmarshal(data, &conf); err != nil { return err } - if err := conf.SetDynamicDefaults(); err != nil { - return err - } if len(conf.Tftp.IpxeBinaries) == 0 { conf.Tftp.IpxeBinaries = defIpxe } - cachedConf = *conf - cachedConf.fromFile = true return nil } @@ -174,7 +172,6 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { return errors.New("invalid ipv6 network size") } } - cachedConf = *conf return } From 832119364524b016246f5881a488eeeb27c46ba9 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 19:21:29 -0600 Subject: [PATCH 08/13] Refactor and document config/datastructure Also adjusted case for initialisms (e.g., DHCP, TFTP, NFS). Signed-off-by: Jonathon Anderson --- .gitignore | 3 +- internal/app/wwctl/configure/dhcp/main.go | 2 +- internal/app/wwctl/configure/main.go | 2 +- internal/pkg/config/buildconfig.go.in | 2 +- internal/pkg/config/datastructure.go | 61 ----------------------- internal/pkg/config/dhcp.go | 12 +++++ internal/pkg/config/mounts.go | 10 ++++ internal/pkg/config/nfs.go | 35 +++++++++++++ internal/pkg/config/root.go | 32 ++++++------ internal/pkg/config/tftp.go | 13 +++++ internal/pkg/config/warewulf.go | 14 ++++++ internal/pkg/configure/dhcp.go | 10 ++-- internal/pkg/configure/nfs.go | 6 +-- internal/pkg/configure/tftp.go | 8 +-- internal/pkg/overlay/datastructure.go | 12 ++--- 15 files changed, 122 insertions(+), 100 deletions(-) delete mode 100644 internal/pkg/config/datastructure.go create mode 100644 internal/pkg/config/dhcp.go create mode 100644 internal/pkg/config/mounts.go create mode 100644 internal/pkg/config/nfs.go create mode 100644 internal/pkg/config/tftp.go create mode 100644 internal/pkg/config/warewulf.go diff --git a/.gitignore b/.gitignore index 894fa86d..7859c0c0 100644 --- a/.gitignore +++ b/.gitignore @@ -28,8 +28,7 @@ internal/pkg/buildconfig/setconfigs.go internal/pkg/config/buildconfig.go include/systemd/warewulfd.service _dist/ -config -!internal/pkg/config +/config warewulf-*.tar.gz Defaults.mk /etc/wwapid.config diff --git a/internal/app/wwctl/configure/dhcp/main.go b/internal/app/wwctl/configure/dhcp/main.go index 38ff6f97..a98561d9 100644 --- a/internal/app/wwctl/configure/dhcp/main.go +++ b/internal/app/wwctl/configure/dhcp/main.go @@ -6,5 +6,5 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - return configure.Dhcp() + return configure.DHCP() } diff --git a/internal/app/wwctl/configure/main.go b/internal/app/wwctl/configure/main.go index 3512d2fc..798c1d03 100644 --- a/internal/app/wwctl/configure/main.go +++ b/internal/app/wwctl/configure/main.go @@ -11,7 +11,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { var err error if allFunctions { - err = configure.Dhcp() + err = configure.DHCP() if err != nil { wwlog.Error("%s", err) os.Exit(1) diff --git a/internal/pkg/config/buildconfig.go.in b/internal/pkg/config/buildconfig.go.in index 5316e518..57ef0fba 100644 --- a/internal/pkg/config/buildconfig.go.in +++ b/internal/pkg/config/buildconfig.go.in @@ -8,7 +8,7 @@ type BuildConfig struct { Datadir string `default:"@DATADIR@"` Localstatedir string `default:"@LOCALSTATEDIR@"` Srvdir string `default:"@SRVDIR@"` - Tftpdir string `default:"@TFTPDIR@"` + TFTPdir string `default:"@TFTPDIR@"` Firewallddir string `default:"@FIREWALLDDIR@"` Systemddir string `default:"@SYSTEMDDIR@"` WWOverlaydir string `default:"@WWOVERLAYDIR@"` diff --git a/internal/pkg/config/datastructure.go b/internal/pkg/config/datastructure.go deleted file mode 100644 index 4d5cefd1..00000000 --- a/internal/pkg/config/datastructure.go +++ /dev/null @@ -1,61 +0,0 @@ -package config - -import ( - "github.com/creasty/defaults" -) - -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 -} diff --git a/internal/pkg/config/dhcp.go b/internal/pkg/config/dhcp.go new file mode 100644 index 00000000..3dbcd241 --- /dev/null +++ b/internal/pkg/config/dhcp.go @@ -0,0 +1,12 @@ +package config + + +// DHCPConf represents the configuration for the DHCP service that +// Warewulf will configure. +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"` +} diff --git a/internal/pkg/config/mounts.go b/internal/pkg/config/mounts.go new file mode 100644 index 00000000..daf38c0a --- /dev/null +++ b/internal/pkg/config/mounts.go @@ -0,0 +1,10 @@ +package config + +// A MountEntry represents a bind mount that is applied to a container +// during exec and shell. +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 +} diff --git a/internal/pkg/config/nfs.go b/internal/pkg/config/nfs.go new file mode 100644 index 00000000..03726379 --- /dev/null +++ b/internal/pkg/config/nfs.go @@ -0,0 +1,35 @@ +package config + + +import ( + "github.com/creasty/defaults" +) + + +// NFSConf represents the NFS configuration that will be used by +// Warewulf to generate exports on the server and mounts on compute +// nodes. +type NFSConf struct { + Enabled bool `yaml:"enabled" default:"true"` + ExportsExtended []*NFSExportConf `yaml:"export paths" default:"[]"` + SystemdName string `yaml:"systemd name" default:"nfsd"` +} + + +// An NFSExportConf reprents a single NFS export / mount. +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"` +} + + +// Implements the Unmarshal interface for NFSConf to set default +// values. +func (conf *NFSConf) Unmarshal(unmarshal func(interface{}) error) error { + if err := defaults.Set(conf); err != nil { + return err + } + return nil +} diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index 822df5c0..9ffd667a 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -26,7 +26,7 @@ var cachedConf RootConf // RootConf is the main Warewulf configuration structure. It stores // some information about the Warewulf server locally, and has -// [WarewulfConf], [DhcpConf], [TftpConf], and [NfsConf] sub-sections. +// [WarewulfConf], [DHCPConf], [TFTPConf], and [NFSConf] sub-sections. type RootConf struct { WWInternal int `yaml:"WW_INTERNAL"` Comment string `yaml:"comment,omitempty"` @@ -37,9 +37,9 @@ type RootConf struct { 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"` + 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"` @@ -53,9 +53,9 @@ func New() (*RootConf) { cachedConf = RootConf{} cachedConf.fromFile = false cachedConf.Warewulf = new(WarewulfConf) - cachedConf.Dhcp = new(DhcpConf) - cachedConf.Tftp = new(TftpConf) - cachedConf.Nfs = new(NfsConf) + cachedConf.DHCP = new(DHCPConf) + cachedConf.TFTP = new(TFTPConf) + cachedConf.NFS = new(NFSConf) cachedConf.Paths = new(BuildConfig) if err := defaults.Set(&cachedConf); err != nil { panic(err) @@ -95,15 +95,15 @@ func (conf *RootConf) Read(confFileName string) (error) { func (conf *RootConf) Parse(data []byte) (error) { // ipxe binaries are merged not overwritten, store defaults separate defIpxe := make(map[string]string) - for k, v := range conf.Tftp.IpxeBinaries { + for k, v := range conf.TFTP.IPXEBinaries { defIpxe[k] = v - delete(conf.Tftp.IpxeBinaries, k) + delete(conf.TFTP.IPXEBinaries, k) } if err := yaml.Unmarshal(data, &conf); err != nil { return err } - if len(conf.Tftp.IpxeBinaries) == 0 { - conf.Tftp.IpxeBinaries = defIpxe + if len(conf.TFTP.IPXEBinaries) == 0 { + conf.TFTP.IPXEBinaries = defIpxe } return nil } @@ -141,14 +141,14 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { wwlog.Verbose("Network is not configured in warewulf.conf, using %s", conf.Network) } } - if conf.Dhcp.RangeStart == "" && conf.Dhcp.RangeEnd == "" { + 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) + 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 { @@ -156,8 +156,8 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { } 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) + 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 diff --git a/internal/pkg/config/tftp.go b/internal/pkg/config/tftp.go new file mode 100644 index 00000000..6681df55 --- /dev/null +++ b/internal/pkg/config/tftp.go @@ -0,0 +1,13 @@ +package config + + +// TFTPConf represents that configuration for the TFTP service that +// Warewulf will configure. +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\"}"` +} diff --git a/internal/pkg/config/warewulf.go b/internal/pkg/config/warewulf.go new file mode 100644 index 00000000..75d6b6ba --- /dev/null +++ b/internal/pkg/config/warewulf.go @@ -0,0 +1,14 @@ +package config + + +// WarewulfConf adds additional Warewulf-specific configuration to +// BaseConf. +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"` +} diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index d7ef2e69..02613f05 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -15,21 +15,21 @@ import ( Configures the dhcpd server, when show is set to false, else the dhcp configuration is checked. */ -func Dhcp() (err error) { +func DHCP() (err error) { controller := warewulfconf.Get() - if !controller.Dhcp.Enabled { + if !controller.DHCP.Enabled { wwlog.Info("This system is not configured as a Warewulf DHCP controller") os.Exit(1) } - if controller.Dhcp.RangeStart == "" { + if controller.DHCP.RangeStart == "" { wwlog.Error("Configuration is not defined: `dhcpd range start`") os.Exit(1) } - if controller.Dhcp.RangeEnd == "" { + if controller.DHCP.RangeEnd == "" { wwlog.Error("Configuration is not defined: `dhcpd range end`") os.Exit(1) } @@ -42,7 +42,7 @@ func Dhcp() (err error) { wwlog.Info("host overlays are disabled, did not modify/create dhcpd configuration") } fmt.Printf("Enabling and restarting the DHCP services\n") - err = util.SystemdStart(controller.Dhcp.SystemdName) + err = util.SystemdStart(controller.DHCP.SystemdName) if err != nil { return errors.Wrap(err, "failed to start") } diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index 343d59c8..82f70e84 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -18,7 +18,7 @@ func NFS() error { controller := warewulfconf.Get() - if controller.Nfs.Enabled { + if controller.NFS.Enabled { if controller.Warewulf.EnableHostOverlay { err := overlay.BuildHostOverlay() if err != nil { @@ -28,13 +28,13 @@ func NFS() error { wwlog.Info("host overlays are disabled, did not modify exports") } fmt.Printf("Enabling and restarting the NFS services\n") - if controller.Nfs.SystemdName == "" { + if controller.NFS.SystemdName == "" { err := util.SystemdStart("nfs-server") if err != nil { return errors.Wrap(err, "failed to start nfs-server") } } else { - err := util.SystemdStart(controller.Nfs.SystemdName) + err := util.SystemdStart(controller.NFS.SystemdName) if err != nil { return errors.Wrap(err, "failed to start") } diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 16a28bbb..333a7961 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -12,7 +12,7 @@ import ( func TFTP() error { controller := warewulfconf.Get() - var tftpdir string = path.Join(controller.Paths.Tftpdir, "warewulf") + var tftpdir string = path.Join(controller.Paths.TFTPdir, "warewulf") err := os.MkdirAll(tftpdir, 0755) if err != nil { @@ -22,7 +22,7 @@ func TFTP() error { fmt.Printf("Writing PXE files to: %s\n", tftpdir) copyCheck := make(map[string]bool) - for _, f := range controller.Tftp.IpxeBinaries { + for _, f := range controller.TFTP.IPXEBinaries { if copyCheck[f] { continue } @@ -33,13 +33,13 @@ func TFTP() error { } } - if !controller.Tftp.Enabled { + if !controller.TFTP.Enabled { wwlog.Info("Warewulf does not auto start TFTP services due to disable by warewulf.conf") os.Exit(0) } fmt.Printf("Enabling and restarting the TFTP services\n") - err = util.SystemdStart(controller.Tftp.SystemdName) + err = util.SystemdStart(controller.TFTP.SystemdName) if err != nil { wwlog.Error("%s", err) return err diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index 760d3f42..6af7adb2 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -28,10 +28,10 @@ type TemplateStruct struct { Network string NetworkCIDR string Ipv6 bool - Dhcp warewulfconf.DhcpConf - Nfs warewulfconf.NfsConf + Dhcp warewulfconf.DHCPConf + Nfs warewulfconf.NFSConf Warewulf warewulfconf.WarewulfConf - Tftp warewulfconf.TftpConf + Tftp warewulfconf.TFTPConf Paths warewulfconf.BuildConfig AllNodes []node.NodeInfo node.NodeConf @@ -60,9 +60,9 @@ func InitStruct(nodeInfo node.NodeInfo) TemplateStruct { tstruct.Hostname = nodeInfo.Id.Get() // Backwards compatibility for templates using "Keys" tstruct.AllNodes = allNodes - tstruct.Nfs = *controller.Nfs - tstruct.Dhcp = *controller.Dhcp - tstruct.Tftp = *controller.Tftp + tstruct.Nfs = *controller.NFS + tstruct.Dhcp = *controller.DHCP + tstruct.Tftp = *controller.TFTP tstruct.Warewulf = *controller.Warewulf tstruct.Ipaddr = controller.Ipaddr tstruct.Ipaddr6 = controller.Ipaddr6 From 82520c0de07b5f5f9c8e2e3b884cac0e2c3b9839 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 19:34:04 -0600 Subject: [PATCH 09/13] Fix linter errors in some tests Signed-off-by: Jonathon Anderson --- internal/app/wwctl/node/add/main_test.go | 2 +- internal/app/wwctl/node/list/main_test.go | 2 +- internal/app/wwctl/profile/list/main_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/app/wwctl/node/add/main_test.go b/internal/app/wwctl/node/add/main_test.go index f0c7e7df..71513ea1 100644 --- a/internal/app/wwctl/node/add/main_test.go +++ b/internal/app/wwctl/node/add/main_test.go @@ -243,7 +243,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.New().Read(tempWarewulfConf.Name()) + assert.NoError(t, warewulfconf.New().Read(tempWarewulfConf.Name())) nodes_yml := `WW_INTERNAL: 43` tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") diff --git a/internal/app/wwctl/node/list/main_test.go b/internal/app/wwctl/node/list/main_test.go index ebd1ac3b..2717d156 100644 --- a/internal/app/wwctl/node/list/main_test.go +++ b/internal/app/wwctl/node/list/main_test.go @@ -42,7 +42,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.New().Read(tempWarewulfConf.Name()) + assert.NoError(t, warewulfconf.New().Read(tempWarewulfConf.Name())) tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") assert.NoError(t, nodesConfErr) diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go index 00f6144f..62e2ec03 100644 --- a/internal/app/wwctl/profile/list/main_test.go +++ b/internal/app/wwctl/profile/list/main_test.go @@ -43,7 +43,7 @@ nodes: _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) assert.NoError(t, warewulfConfErr) assert.NoError(t, tempWarewulfConf.Sync()) - warewulfconf.New().Read(tempWarewulfConf.Name()) + assert.NoError(t, warewulfconf.New().Read(tempWarewulfConf.Name())) tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") assert.NoError(t, nodesConfErr) From e2e3a9062336fb1fbcf092282c761bf94077bcfd Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 19:40:54 -0600 Subject: [PATCH 10/13] Revert some caps changes Use in templates means these were an accidental interface change. Signed-off-by: Jonathon Anderson --- internal/pkg/config/buildconfig.go.in | 2 +- internal/pkg/config/root.go | 8 ++++---- internal/pkg/config/tftp.go | 4 ++-- internal/pkg/configure/tftp.go | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/pkg/config/buildconfig.go.in b/internal/pkg/config/buildconfig.go.in index 57ef0fba..5316e518 100644 --- a/internal/pkg/config/buildconfig.go.in +++ b/internal/pkg/config/buildconfig.go.in @@ -8,7 +8,7 @@ type BuildConfig struct { Datadir string `default:"@DATADIR@"` Localstatedir string `default:"@LOCALSTATEDIR@"` Srvdir string `default:"@SRVDIR@"` - TFTPdir string `default:"@TFTPDIR@"` + Tftpdir string `default:"@TFTPDIR@"` Firewallddir string `default:"@FIREWALLDDIR@"` Systemddir string `default:"@SYSTEMDDIR@"` WWOverlaydir string `default:"@WWOVERLAYDIR@"` diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index 9ffd667a..67c4447a 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -95,15 +95,15 @@ func (conf *RootConf) Read(confFileName string) (error) { func (conf *RootConf) Parse(data []byte) (error) { // ipxe binaries are merged not overwritten, store defaults separate defIpxe := make(map[string]string) - for k, v := range conf.TFTP.IPXEBinaries { + for k, v := range conf.TFTP.IpxeBinaries { defIpxe[k] = v - delete(conf.TFTP.IPXEBinaries, k) + delete(conf.TFTP.IpxeBinaries, k) } if err := yaml.Unmarshal(data, &conf); err != nil { return err } - if len(conf.TFTP.IPXEBinaries) == 0 { - conf.TFTP.IPXEBinaries = defIpxe + if len(conf.TFTP.IpxeBinaries) == 0 { + conf.TFTP.IpxeBinaries = defIpxe } return nil } diff --git a/internal/pkg/config/tftp.go b/internal/pkg/config/tftp.go index 6681df55..ca368414 100644 --- a/internal/pkg/config/tftp.go +++ b/internal/pkg/config/tftp.go @@ -5,9 +5,9 @@ package config // Warewulf will configure. type TFTPConf struct { Enabled bool `yaml:"enabled" default:"true"` - TFTPRoot string `yaml:"tftproot" default:"/var/lib/tftpboot"` + 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\"}"` + 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\"}"` } diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 333a7961..1c8da005 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -12,7 +12,7 @@ import ( func TFTP() error { controller := warewulfconf.Get() - var tftpdir string = path.Join(controller.Paths.TFTPdir, "warewulf") + var tftpdir string = path.Join(controller.Paths.Tftpdir, "warewulf") err := os.MkdirAll(tftpdir, 0755) if err != nil { @@ -22,7 +22,7 @@ func TFTP() error { fmt.Printf("Writing PXE files to: %s\n", tftpdir) copyCheck := make(map[string]bool) - for _, f := range controller.TFTP.IPXEBinaries { + for _, f := range controller.TFTP.IpxeBinaries { if copyCheck[f] { continue } From 4622153a825f95b29f7cda5bd7ae30d07a912834 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 14 Apr 2023 20:14:03 -0600 Subject: [PATCH 11/13] Support subdirectories in ipxe paths Signed-off-by: Jonathon Anderson --- internal/pkg/config/tftp.go | 2 +- internal/pkg/configure/tftp.go | 2 +- internal/pkg/overlay/overlay.go | 1 + overlays/host/etc/dhcp/dhcpd.conf.ww | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/pkg/config/tftp.go b/internal/pkg/config/tftp.go index ca368414..1c898e39 100644 --- a/internal/pkg/config/tftp.go +++ b/internal/pkg/config/tftp.go @@ -9,5 +9,5 @@ type TFTPConf struct { 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\"}"` + IpxeBinaries map[string]string `yaml:"ipxe" default:"{\"00:09\": \"ipxe/ipxe-snponly-x86_64.efi\",\"00:00\": \"ipxe/undionly.kpxe\",\"00:0B\": \"ipxe/arm64-efi/snponly.efi\",\"00:07\": \"ipxe/ipxe-snponly-x86_64.efi\"}"` } diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 1c8da005..6cfb9561 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -27,7 +27,7 @@ func TFTP() error { continue } copyCheck[f] = true - err = util.SafeCopyFile(path.Join(controller.Paths.Datadir, f), path.Join(tftpdir, f)) + err = util.SafeCopyFile(path.Join(controller.Paths.Datadir, f), path.Join(tftpdir, path.Base(f))) if err != nil { wwlog.Warn("ipxe binary could not be copied, booting may not work: %s", err) } diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 3754fa9c..0d441cc7 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -371,6 +371,7 @@ func RenderTemplateFile(fileName string, data TemplateStruct) ( "Include": templateFileInclude, "IncludeFrom": templateContainerFileInclude, "IncludeBlock": templateFileBlock, + "basename": path.Base, "inc": func(i int) int { return i + 1 }, "dec": func(i int) int { return i - 1 }, "file": func(str string) string { return fmt.Sprintf("{{ /* file \"%s\" */ }}", str) }, diff --git a/overlays/host/etc/dhcp/dhcpd.conf.ww b/overlays/host/etc/dhcp/dhcpd.conf.ww index 66b9d22e..4a9637f7 100644 --- a/overlays/host/etc/dhcp/dhcpd.conf.ww +++ b/overlays/host/etc/dhcp/dhcpd.conf.ww @@ -22,7 +22,7 @@ if exists user-class and option user-class = "iPXE" { } else { {{range $type,$name := $.Tftp.IpxeBinaries }} if option architecture-type = {{ $type }} { - filename "/warewulf/{{ $name }}"; + filename "/warewulf/{{ basename $name }}"; } {{ end }} } From 59231306766085d96eae223741bd1227855a5433 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Sat, 15 Apr 2023 16:47:12 -0600 Subject: [PATCH 12/13] Add tests for config Signed-off-by: Jonathon Anderson --- internal/pkg/config/root_test.go | 174 +++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 internal/pkg/config/root_test.go diff --git a/internal/pkg/config/root_test.go b/internal/pkg/config/root_test.go new file mode 100644 index 00000000..d1d0f3d1 --- /dev/null +++ b/internal/pkg/config/root_test.go @@ -0,0 +1,174 @@ +package config + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + + +func TestDefaultRootConf(t *testing.T) { + conf := New() + + assert.Equal(t, 9983, conf.Warewulf.Port) + assert.True(t, conf.Warewulf.Secure) + assert.Equal(t, 60, conf.Warewulf.UpdateInterval) + assert.True(t, conf.Warewulf.AutobuildOverlays) + assert.True(t, conf.Warewulf.EnableHostOverlay) + assert.False(t, conf.Warewulf.Syslog) + assert.NotEmpty(t, conf.Warewulf.DataStore) + + assert.True(t, conf.DHCP.Enabled) + assert.Equal(t, "default", conf.DHCP.Template) + assert.Empty(t, conf.DHCP.RangeStart) + assert.Empty(t, conf.DHCP.RangeEnd) + assert.Equal(t, "dhcpd", conf.DHCP.SystemdName) + + assert.True(t, conf.TFTP.Enabled) + assert.NotEmpty(t, conf.TFTP.TftpRoot) + assert.Equal(t, "tftp", conf.TFTP.SystemdName) + assert.NotEmpty(t, conf.TFTP.IpxeBinaries["00:00"]) + assert.NotEmpty(t, conf.TFTP.IpxeBinaries["00:07"]) + assert.NotEmpty(t, conf.TFTP.IpxeBinaries["00:09"]) + assert.NotEmpty(t, conf.TFTP.IpxeBinaries["00:0B"]) + + assert.True(t, conf.NFS.Enabled) + assert.Empty(t, conf.NFS.ExportsExtended) + assert.Equal(t, "nfsd", conf.NFS.SystemdName) + + assert.Equal(t, "/etc/resolv.conf", conf.MountsContainer[0].Source) + assert.Equal(t, "/etc/resolv.conf", conf.MountsContainer[0].Dest) + assert.False(t, conf.MountsContainer[0].ReadOnly) + assert.Empty(t, conf.MountsContainer[0].Options) + + assert.NotEmpty(t, conf.Paths.Bindir) + assert.NotEmpty(t, conf.Paths.Sysconfdir) + assert.NotEmpty(t, conf.Paths.Datadir) + assert.NotEmpty(t, conf.Paths.Localstatedir) + assert.NotEmpty(t, conf.Paths.Srvdir) + assert.NotEmpty(t, conf.Paths.Tftpdir) + assert.NotEmpty(t, conf.Paths.Firewallddir) + assert.NotEmpty(t, conf.Paths.Systemddir) + assert.NotEmpty(t, conf.Paths.WWOverlaydir) + assert.NotEmpty(t, conf.Paths.WWChrootdir) + assert.NotEmpty(t, conf.Paths.WWProvisiondir) + assert.NotEmpty(t, conf.Paths.Version) + assert.NotEmpty(t, conf.Paths.Release) + assert.NotEmpty(t, conf.Paths.WWClientdir) +} + + +func TestInitializedFromFile(t *testing.T) { + example_warewulf_conf := "WW_INTERNAL: 43" + tempWarewulfConf, warewulfConfErr := os.CreateTemp("", "warewulf.conf-") + assert.NoError(t, warewulfConfErr) + defer os.Remove(tempWarewulfConf.Name()) + _, warewulfConfErr = tempWarewulfConf.Write([]byte(example_warewulf_conf)) + assert.NoError(t, warewulfConfErr) + assert.NoError(t, tempWarewulfConf.Sync()) + + conf := New() + assert.False(t, conf.InitializedFromFile()) + assert.NoError(t, conf.Read(tempWarewulfConf.Name())) + assert.True(t, conf.InitializedFromFile()) +} + + +func TestExampleRootConf(t *testing.T) { + example_warewulf_conf := `WW_INTERNAL: 43 +ipaddr: 192.168.200.1 +netmask: 255.255.255.0 +network: 192.168.200.0 +warewulf: + port: 9873 + secure: false + update interval: 60 + autobuild overlays: true + host overlay: true + syslog: false +dhcp: + enabled: true + range start: 192.168.200.50 + range end: 192.168.200.99 + systemd name: dhcpd +tftp: + enabled: true + systemd name: tftp +nfs: + enabled: true + export paths: + - path: /home + export options: rw,sync + mount options: defaults + mount: true + - path: /opt + export options: ro,sync,no_root_squash + mount options: defaults + mount: false + systemd name: nfs-server +container mounts: + - source: /etc/resolv.conf + dest: /etc/resolv.conf + readonly: true` + + tempWarewulfConf, warewulfConfErr := os.CreateTemp("", "warewulf.conf-") + assert.NoError(t, warewulfConfErr) + defer os.Remove(tempWarewulfConf.Name()) + _, warewulfConfErr = tempWarewulfConf.Write([]byte(example_warewulf_conf)) + assert.NoError(t, warewulfConfErr) + assert.NoError(t, tempWarewulfConf.Sync()) + + conf := New() + assert.NoError(t, conf.Read(tempWarewulfConf.Name())) + + assert.Equal(t, "192.168.200.1", conf.Ipaddr) + assert.Equal(t, "255.255.255.0", conf.Netmask) + assert.Equal(t, "192.168.200.0", conf.Network) + + assert.Equal(t, 9873, conf.Warewulf.Port) + assert.False(t, conf.Warewulf.Secure) + assert.Equal(t, 60, conf.Warewulf.UpdateInterval) + assert.True(t, conf.Warewulf.AutobuildOverlays) + assert.True(t, conf.Warewulf.EnableHostOverlay) + assert.False(t, conf.Warewulf.Syslog) + + assert.True(t, conf.DHCP.Enabled) + assert.Equal(t, "192.168.200.50", conf.DHCP.RangeStart) + assert.Equal(t, "192.168.200.99", conf.DHCP.RangeEnd) + assert.Equal(t, "dhcpd", conf.DHCP.SystemdName) + + assert.True(t, conf.TFTP.Enabled) + assert.Equal(t, "tftp", conf.TFTP.SystemdName) + + assert.True(t, conf.NFS.Enabled) + assert.Equal(t, "/home", conf.NFS.ExportsExtended[0].Path) + assert.Equal(t, "rw,sync", conf.NFS.ExportsExtended[0].ExportOptions) + assert.Equal(t, "defaults", conf.NFS.ExportsExtended[0].MountOptions) + assert.True(t, conf.NFS.ExportsExtended[0].Mount) + assert.Equal(t, "/opt", conf.NFS.ExportsExtended[1].Path) + assert.Equal(t, "ro,sync,no_root_squash", conf.NFS.ExportsExtended[1].ExportOptions) + assert.Equal(t, "defaults", conf.NFS.ExportsExtended[1].MountOptions) + assert.False(t, conf.NFS.ExportsExtended[1].Mount) + assert.Equal(t, "nfs-server", conf.NFS.SystemdName) + + assert.Equal(t, "/etc/resolv.conf", conf.MountsContainer[0].Source) + assert.Equal(t, "/etc/resolv.conf", conf.MountsContainer[0].Dest) + assert.True(t, conf.MountsContainer[0].ReadOnly) +} + + +func TestCache(t *testing.T) { + confOrig := New() + confCached := Get() + + assert.Equal(t, 9983, confOrig.Warewulf.Port) + assert.Equal(t, 9983, confCached.Warewulf.Port) + + confOrig.Warewulf.Port = 9999 + assert.Equal(t, 9999, confCached.Warewulf.Port) + assert.Equal(t, 9999, Get().Warewulf.Port) + + New() + assert.NotEqual(t, 9999, Get().Warewulf.Port) +} From 62018eedf0b59a8b2c7dc5e8624301a6299eaae7 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Wed, 19 Apr 2023 14:03:44 -0600 Subject: [PATCH 13/13] Fix a failing unit test after a rebase Signed-off-by: Jonathon Anderson --- internal/app/wwctl/profile/add/main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/wwctl/profile/add/main_test.go b/internal/app/wwctl/profile/add/main_test.go index 8da95a45..f1a084e7 100644 --- a/internal/app/wwctl/profile/add/main_test.go +++ b/internal/app/wwctl/profile/add/main_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/stretchr/testify/assert" )