Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
moves flags out of control.config entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
jcooklin authored and tiffanyfay committed Feb 12, 2016
1 parent e63c844 commit 2d45244
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 76 deletions.
26 changes: 23 additions & 3 deletions control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strconv"

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 0 additions & 21 deletions control/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 1 addition & 8 deletions examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 39 additions & 1 deletion pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
70 changes: 27 additions & 43 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down

0 comments on commit 2d45244

Please sign in to comment.