From 2d452440dfd63ff6b516ad9edfecc675bd3cdada Mon Sep 17 00:00:00 2001 From: Joel Cooklin Date: Thu, 11 Feb 2016 19:46:34 -0800 Subject: [PATCH] moves flags out of control.config entirely --- control/config.go | 26 ++++++++- control/config_test.go | 21 ------- examples/configs/snap-config-sample.json | 9 +-- pkg/flags/flags.go | 40 +++++++++++++- snapd.go | 70 +++++++++--------------- 5 files changed, 90 insertions(+), 76 deletions(-) diff --git a/control/config.go b/control/config.go index 8702fe8e3..e4834c9fc 100644 --- a/control/config.go +++ b/control/config.go @@ -23,6 +23,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/ioutil" "reflect" "strconv" @@ -31,7 +32,6 @@ import ( "github.com/intelsdi-x/snap/core" "github.com/intelsdi-x/snap/core/cdata" "github.com/intelsdi-x/snap/core/ctypes" - "github.com/intelsdi-x/snap/pkg/flags" ) type pluginConfig struct { @@ -53,8 +53,7 @@ type pluginConfigItem struct { } type config struct { - Flags flags.FlagConfig `json:"flags"` - Plugins *pluginConfig `json:"plugins"` + Plugins *pluginConfig `json:"plugins"` } // NewConfig returns a reference to a global config type for the snap daemon @@ -82,6 +81,27 @@ func newPluginConfig() *pluginConfig { } } +func (p *config) LoadConfig(path string) { + b, err := ioutil.ReadFile(path) + if err != nil { + log.WithFields(log.Fields{ + "block": "main", + "_module": "snapd", + "error": err.Error(), + "path": path, + }).Fatal("unable to read config") + } + err = json.Unmarshal(b, p) + if err != nil { + log.WithFields(log.Fields{ + "block": "main", + "_module": "snapd", + "error": err.Error(), + "path": path, + }).Fatal("invalid config") + } +} + func (p *config) GetPluginConfigDataNode(pluginType core.PluginType, name string, ver int) cdata.ConfigDataNode { return *p.Plugins.getPluginConfigDataNode(pluginType, name, ver) } diff --git a/control/config_test.go b/control/config_test.go index e82d2ecb1..bffa81148 100644 --- a/control/config_test.go +++ b/control/config_test.go @@ -30,27 +30,6 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestFlags(t *testing.T) { - Convey("Provided a config in JSON", t, func() { - cfg := NewConfig() - b, err := ioutil.ReadFile("../examples/configs/snap-config-sample.json") - So(b, ShouldNotBeEmpty) - So(b, ShouldNotBeNil) - So(err, ShouldBeNil) - Convey("We are able to unmarshal it into a valid config", func() { - err = json.Unmarshal(b, &cfg) - So(err, ShouldBeNil) - So(cfg.Flags, ShouldNotBeNil) - So(cfg.Flags.LogLevel, ShouldNotBeNil) - So(cfg.Flags.PluginTrust, ShouldNotBeNil) - So(cfg.Flags.AutodiscoverPath, ShouldNotBeNil) - So(*cfg.Flags.LogLevel, ShouldEqual, 1) - So(*cfg.Flags.PluginTrust, ShouldEqual, 0) - So(*cfg.Flags.AutodiscoverPath, ShouldEqual, "build/plugin") - }) - }) -} - func TestPluginConfig(t *testing.T) { Convey("Given a plugin config", t, func() { cfg := NewConfig() diff --git a/examples/configs/snap-config-sample.json b/examples/configs/snap-config-sample.json index 5254d03d0..c0d556fd0 100644 --- a/examples/configs/snap-config-sample.json +++ b/examples/configs/snap-config-sample.json @@ -3,14 +3,7 @@ "log-level": 1, "plugin-trust": 0, "auto-discover": "build/plugin" - }, - "control": { - "cache_ttl": "5s" - }, - "scheduler": { - "default_deadline": "5s", - "worker_pool_size": 5 - }, + }, "plugins": { "all": { "password": "p@ssw0rd" diff --git a/pkg/flags/flags.go b/pkg/flags/flags.go index fb1086fba..a568b2599 100644 --- a/pkg/flags/flags.go +++ b/pkg/flags/flags.go @@ -19,7 +19,14 @@ limitations under the License. package flags -import "github.com/codegangsta/cli" +import ( + "encoding/json" + "io/ioutil" + + log "github.com/Sirupsen/logrus" + + "github.com/codegangsta/cli" +) // FlagConfig struct has all of the snapd flags type FlagConfig struct { @@ -43,6 +50,37 @@ type FlagConfig struct { RestCert *string `json:"rest-cert"` } +func (f *FlagConfig) LoadConfig(path string) { + b, err := ioutil.ReadFile(path) + if err != nil { + log.WithFields(log.Fields{ + "block": "New", + "_module": "flags", + "error": err.Error(), + "path": path, + }).Fatal("unable to read config") + } + var cfg map[string]interface{} + err = json.Unmarshal(b, &cfg) + if err != nil { + log.WithFields(log.Fields{ + "block": "main", + "_module": "snapd", + "error": err.Error(), + }).Fatal("invalid config") + } + if _, ok := cfg["flags"]; ok { + err = json.Unmarshal(b, f) + if err != nil { + log.WithFields(log.Fields{ + "block": "main", + "_module": "snapd", + "error": err.Error(), + }).Fatal("invalid config") + } + } +} + // GetFlagInt eturns the integer value for the flag to be used by snapd func GetFlagInt(ctx *cli.Context, cfgVal *int, flag string) int { // Checks if the flag is in the config and if the command line flag is not set diff --git a/snapd.go b/snapd.go index 715c9dabd..21013cd80 100644 --- a/snapd.go +++ b/snapd.go @@ -20,7 +20,6 @@ limitations under the License. package main import ( - "encoding/json" "fmt" "io/ioutil" "os" @@ -194,36 +193,21 @@ func main() { func action(ctx *cli.Context) { log.Info("Starting snapd (version: ", gitversion, ")") - cfg := control.NewConfig() - config := ctx.String("config") - if config != "" { - b, err := ioutil.ReadFile(config) - if err != nil { - log.WithFields(log.Fields{ - "block": "main", - "_module": "snapd", - "error": err.Error(), - "path": config, - }).Fatal("unable to read config") - } - err = json.Unmarshal(b, &cfg) - if err != nil { - log.WithFields(log.Fields{ - "block": "main", - "_module": "snapd", - "error": err.Error(), - "path": config, - }).Fatal("invalid config") - } + fcfg := &flags.FlagConfig{} + ccfg := control.NewConfig() + fpath := ctx.String("config") + if fpath != "" { + fcfg.LoadConfig(fpath) + ccfg.LoadConfig(fpath) } // Get flag values - disableAPI := flags.GetFlagBool(ctx, cfg.Flags.DisableAPI, "disable-api") - apiPort := flags.GetFlagInt(ctx, cfg.Flags.APIPort, "api-port") - logLevel := flags.GetFlagInt(ctx, cfg.Flags.LogLevel, "log-level") + disableAPI := flags.GetFlagBool(ctx, fcfg.DisableAPI, "disable-api") + apiPort := flags.GetFlagInt(ctx, fcfg.APIPort, "api-port") + logLevel := flags.GetFlagInt(ctx, fcfg.LogLevel, "log-level") // If logPath is set, we verify the logPath and set it so that all logging // goes to the log file instead of stdout. - logPath := flags.GetFlagString(ctx, cfg.Flags.LogPath, "log-path") + logPath := flags.GetFlagString(ctx, fcfg.LogPath, "log-path") if logPath != "" { f, err := os.Stat(logPath) if err != nil { @@ -238,31 +222,31 @@ func action(ctx *cli.Context) { log.Fatal(err) } defer file.Close() - log.SetOutput(file) - } - maxProcs := flags.GetFlagInt(ctx, cfg.Flags.MaxProcs, "log-level") - autodiscoverPath := flags.GetFlagString(ctx, cfg.Flags.AutodiscoverPath, "auto-discover") - maxRunning := flags.GetFlagInt(ctx, cfg.Flags.MaxRunning, "max-running-plugins") - pluginTrust := flags.GetFlagInt(ctx, cfg.Flags.PluginTrust, "plugin-trust") - keyringPaths := flags.GetFlagString(ctx, cfg.Flags.KeyringPaths, "keyring-paths") - cachestr := flags.GetFlagString(ctx, cfg.Flags.Cachestr, "cache-expiration") + // log.SetOutput + } + maxProcs := flags.GetFlagInt(ctx, fcfg.MaxProcs, "log-level") + autodiscoverPath := flags.GetFlagString(ctx, fcfg.AutodiscoverPath, "auto-discover") + maxRunning := flags.GetFlagInt(ctx, fcfg.MaxRunning, "max-running-plugins") + pluginTrust := flags.GetFlagInt(ctx, fcfg.PluginTrust, "plugin-trust") + keyringPaths := flags.GetFlagString(ctx, fcfg.KeyringPaths, "keyring-paths") + cachestr := flags.GetFlagString(ctx, fcfg.Cachestr, "cache-expiration") cache, err := time.ParseDuration(cachestr) if err != nil { log.Fatal(fmt.Sprintf("invalid cache-expiration format: %s", cachestr)) } - isTribeEnabled := flags.GetFlagBool(ctx, cfg.Flags.IsTribeEnabled, "tribe") - tribeSeed := flags.GetFlagString(ctx, cfg.Flags.TribeSeed, "tribe-seed") - tribeNodeName := flags.GetFlagString(ctx, cfg.Flags.TribeNodeName, "tribe-node-name") - tribeAddr := flags.GetFlagString(ctx, cfg.Flags.TribeAddr, "tribe-addr") - tribePort := flags.GetFlagInt(ctx, cfg.Flags.TribePort, "tribe-port") - restHTTPS := flags.GetFlagBool(ctx, cfg.Flags.RestHTTPS, "rest-https") - restKey := flags.GetFlagString(ctx, cfg.Flags.RestKey, "rest-key") - restCert := flags.GetFlagString(ctx, cfg.Flags.RestCert, "rest-cert") + isTribeEnabled := flags.GetFlagBool(ctx, fcfg.IsTribeEnabled, "tribe") + tribeSeed := flags.GetFlagString(ctx, fcfg.TribeSeed, "tribe-seed") + tribeNodeName := flags.GetFlagString(ctx, fcfg.TribeNodeName, "tribe-node-name") + tribeAddr := flags.GetFlagString(ctx, fcfg.TribeAddr, "tribe-addr") + tribePort := flags.GetFlagInt(ctx, fcfg.TribePort, "tribe-port") + restHTTPS := flags.GetFlagBool(ctx, fcfg.RestHTTPS, "rest-https") + restKey := flags.GetFlagString(ctx, fcfg.RestKey, "rest-key") + restCert := flags.GetFlagString(ctx, fcfg.RestCert, "rest-cert") controlOpts := []control.PluginControlOpt{ control.MaxRunningPlugins(maxRunning), control.CacheExpiration(cache), - control.OptSetConfig(cfg), + control.OptSetConfig(ccfg), } // Set Max Processors for snapd.