From e79be7deff3f971fbbdcf28de5c01b562d79ab30 Mon Sep 17 00:00:00 2001 From: Gregor Noczinski Date: Sun, 10 Feb 2019 16:30:00 +0100 Subject: [PATCH] Fixed problem where the config was not correctly picked up --- app/appFactory.go | 4 +--- app/configWrapper.go | 18 ++++++++++++------ app/daemon.go | 10 +++++++--- caretakerd.go | 4 ++-- client/client.go | 4 ++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/appFactory.go b/app/appFactory.go index a6f7f81..fce88f6 100644 --- a/app/appFactory.go +++ b/app/appFactory.go @@ -93,9 +93,7 @@ func newAppFor(config *ConfigWrapper, platform string, executableType Executable PlaceHolder(config.ListenAddress().String()). SetValue(config.ListenAddress()) - if executableType == Daemon { - config.forDaemon = true - } else { + if executableType != Daemon { app.Flag("pem", "Location of PEM file which contains the private public key pair for access to the daemon."). Short('p'). PlaceHolder(config.PemFile().String()). diff --git a/app/configWrapper.go b/app/configWrapper.go index 9c48b04..39e31d9 100644 --- a/app/configWrapper.go +++ b/app/configWrapper.go @@ -5,6 +5,7 @@ import ( "github.com/echocat/caretakerd/defaults" "github.com/echocat/caretakerd/errors" "github.com/echocat/caretakerd/values" + "runtime" "sync" ) @@ -13,7 +14,6 @@ import ( type ConfigWrapper struct { config *caretakerd.Config explicitSet bool - forDaemon bool listenAddress *FlagWrapper pemFile *FlagWrapper platform string @@ -33,6 +33,7 @@ func NewConfigWrapperFor(platform string) *ConfigWrapper { listenAddress: NewFlagWrapper(&defaultListenAddress), pemFile: NewFlagWrapper(&defaultPemFile), platform: platform, + mutex: new(sync.Mutex), } } @@ -46,6 +47,7 @@ func (instance *ConfigWrapper) Set(value string) error { if config, err := instance.loadConfigFrom(values.String(value)); err != nil { return err } else { + instance.explicitSet = true instance.config = config return nil } @@ -63,14 +65,14 @@ func (instance ConfigWrapper) loadConfigFrom(fileName values.String) (*caretaker return &conf, nil } -func (instance *ConfigWrapper) populateAndValidate(conf *caretakerd.Config) error { +func (instance *ConfigWrapper) populateAndValidate(forDaemon bool, conf *caretakerd.Config) error { instance.listenAddress.AssignIfExplicitSet(&conf.RPC.Listen) instance.pemFile.AssignIfExplicitSet(&conf.Control.Access.PemFile) err := conf.Validate() if err != nil { return err } - if instance.forDaemon { + if forDaemon { return conf.ValidateMaster() } return nil @@ -92,7 +94,7 @@ func (instance ConfigWrapper) PemFile() *FlagWrapper { } // ProvideConfig will either return the already loaded configuration or will load it -func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) { +func (instance *ConfigWrapper) ProvideConfig(forDaemon bool) (*caretakerd.Config, error) { instance.mutex.Lock() defer instance.mutex.Unlock() if instance.loaded { @@ -105,9 +107,13 @@ func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) { } else { filename := defaults.ConfigFilenameFor(instance.platform) if lConfig, err := instance.loadConfigFrom(filename); caretakerd.IsConfigNotExists(err) { - if instance.forDaemon { + if forDaemon { return nil, errors.New("There is neither the --config flag set nor does a configuration file under default position (%v) exist.", filename) } + if lConfig == nil { + nConfig := caretakerd.NewConfigFor(runtime.GOOS) + lConfig = &nConfig + } config = lConfig } else if err != nil { return nil, err @@ -116,7 +122,7 @@ func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) { } } - if err := instance.populateAndValidate(config); err != nil { + if err := instance.populateAndValidate(forDaemon, config); err != nil { return nil, err } instance.config = config diff --git a/app/daemon.go b/app/daemon.go index 5096614..0548171 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -22,8 +22,8 @@ func attachArgsToMasterIfPossible(args []string, to *caretakerd.Config) { } } -func runDaemon(conf caretakerd.Config, args []string) { - attachArgsToMasterIfPossible(args, &conf) +func runDaemon(conf *caretakerd.Config, args []string) { + attachArgsToMasterIfPossible(args, conf) instance, err := caretakerd.NewCaretakerd(conf, sync.NewGroup()) if err != nil { stack.Print(err, os.Stderr, 0) @@ -58,7 +58,11 @@ func registerDaemonCommandsAt(config *ConfigWrapper, executableType ExecutableTy Strings() cmd.Action(func(*kingpin.ParseContext) error { - runDaemon(*config.config, *arguments) + config, err := config.ProvideConfig(true) + if err != nil { + return err + } + runDaemon(config, *arguments) return nil }) } diff --git a/caretakerd.go b/caretakerd.go index 7ee8b45..a69a119 100644 --- a/caretakerd.go +++ b/caretakerd.go @@ -19,7 +19,7 @@ import ( // Caretakerd instance structure type Caretakerd struct { - config Config + config *Config logger *logger.Logger control *control.Control services *service.Services @@ -36,7 +36,7 @@ func finalize(what *Caretakerd) { } // NewCaretakerd creates a new Caretakerd instance from the given config -func NewCaretakerd(conf Config, syncGroup *usync.Group) (*Caretakerd, error) { +func NewCaretakerd(conf *Config, syncGroup *usync.Group) (*Caretakerd, error) { err := conf.Validate() if err != nil { return nil, err diff --git a/client/client.go b/client/client.go index 3148245..2dead3f 100644 --- a/client/client.go +++ b/client/client.go @@ -18,7 +18,7 @@ import ( ) type ConfigProvider interface { - ProvideConfig() (*caretakerd.Config, error) + ProvideConfig(forDaemon bool) (*caretakerd.Config, error) } // AccessDeniedError represents an error that occurs if someone tries to access a @@ -63,7 +63,7 @@ func NewFactory(configProvider ConfigProvider) *Factory { // NewClient creates a new Client. func (instance *Factory) NewClient() (*Client, error) { - config, err := instance.configProvider.ProvideConfig() + config, err := instance.configProvider.ProvideConfig(false) if err != nil { return nil, err }