Skip to content

Commit

Permalink
Merge pull request #175 from mapuri/signal
Browse files Browse the repository at this point in the history
make manager.Config have it's own Read and Merge methods
  • Loading branch information
mapuri authored Aug 25, 2016
2 parents 9a0edd3 + 2171c12 commit cd624f9
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 190 deletions.
61 changes: 20 additions & 41 deletions management/src/clusterm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package main

import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/contiv/cluster/management/src/clusterm/manager"
"github.com/contiv/errored"
"github.com/imdario/mergo"
)

// version is provided by build
Expand Down Expand Up @@ -61,48 +58,32 @@ func main() {
app.Run(os.Args)
}

func mergeConfig(dst *manager.Config, srcJSON []byte) (*manager.Config, error) {
src := &manager.Config{}
if err := json.Unmarshal(srcJSON, src); err != nil {
return nil, errored.Errorf("failed to parse configuration. Error: %s", err)
}

if err := mergo.MergeWithOverwrite(dst, src); err != nil {
return nil, errored.Errorf("failed to merge configuration. Error: %s", err)
}

return dst, nil
}

func readConfig(c *cli.Context) (*manager.Config, error) {
mgrConfig := manager.DefaultConfig()
func getConfig(c *cli.Context) (*manager.Config, error) {
var reader io.Reader
config := manager.DefaultConfig()
if !c.GlobalIsSet("config") {
log.Debugf("no configuration was specified, starting with default.")
return mgrConfig, nil
}

var (
reader io.Reader
err error
config []byte
)
if c.GlobalString("config") == "-" {
} else if c.GlobalString("config") == "-" {
log.Debugf("reading configuration from stdin")
reader = bufio.NewReader(os.Stdin)
} else {
var f *os.File
if f, err = os.Open(c.GlobalString("config")); err != nil {
return nil, err
f, err := os.Open(c.GlobalString("config"))
if err != nil {
return nil, errored.Errorf("failed to open config file. Error: %v", err)
}
log.Debugf("reading configuration from file: %q", c.GlobalString("config"))
reader = bufio.NewReader(f)
}

if config, err = ioutil.ReadAll(reader); err != nil {
return nil, err
if reader != nil {
uConfig := &manager.Config{}
if err := uConfig.Read(reader); err != nil {
return nil, errored.Errorf("failed to read configuration. Error: %v", err)
}
if err := config.Merge(uConfig); err != nil {
return nil, errored.Errorf("failed to merge configuration. Error: %v", err)
}
}

return mergeConfig(mgrConfig, config)
return config, nil
}

func startDaemon(c *cli.Context) {
Expand All @@ -111,13 +92,11 @@ func startDaemon(c *cli.Context) {
log.SetLevel(level.value)
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})

var (
err error
config *manager.Config
)
if config, err = readConfig(c); err != nil {
log.Fatalf("failed to read configuration. Error: %s", err)
config, err := getConfig(c)
if err != nil {
log.Fatalf("failed to get configuration. Error: %v", err)
}

mgr, err := manager.NewManager(config)
if err != nil {
log.Fatalf("failed to initialize the manager. Error: %s", err)
Expand Down
104 changes: 0 additions & 104 deletions management/src/clusterm/main_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions management/src/clusterm/manager/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package manager

import (
"encoding/json"
"io"
"io/ioutil"

"github.com/contiv/cluster/management/src/boltdb"
"github.com/contiv/cluster/management/src/collins"
"github.com/contiv/cluster/management/src/configuration"
"github.com/contiv/errored"
"github.com/imdario/mergo"
"github.com/mapuri/serf/client"
)

type clustermConfig struct {
Addr string `json:"addr"`
}

type inventorySubsysConfig struct {
Collins *collins.Config `json:"collins,omitempty"`
BoltDB *boltdb.Config `json:"boltdb,omitempty"`
}

// Config is the configuration to cluster manager daemon
type Config struct {
Serf client.Config `json:"serf"`
Inventory inventorySubsysConfig `json:"inventory"`
Ansible configuration.AnsibleSubsysConfig `json:"ansible"`
Manager clustermConfig `json:"manager"`
}

// DefaultConfig returns the default configuration values for the cluster manager
// and it's sub-systems
func DefaultConfig() *Config {
return &Config{
Serf: client.Config{
Addr: "127.0.0.1:7373",
},
Inventory: inventorySubsysConfig{
BoltDB: nil,
Collins: nil,
},
Ansible: configuration.AnsibleSubsysConfig{
ConfigurePlaybook: "site.yml",
CleanupPlaybook: "cleanup.yml",
UpgradePlaybook: "rolling-upgrade.yml",
PlaybookLocation: "/vagrant/vendor/ansible",
User: "vagrant",
PrivKeyFile: "/vagrant/management/src/demo/files/insecure_private_key",
},
Manager: clustermConfig{
Addr: "0.0.0.0:9007",
},
}
}

// Read parses the configuration from the specified reader
func (c *Config) Read(r io.Reader) error {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return err
}

if err := json.Unmarshal(bytes, c); err != nil {
return err
}

return nil
}

// Merge merges the passed configuration into the receiving configuration
func (c *Config) Merge(src *Config) error {
if err := mergo.MergeWithOverwrite(c, src); err != nil {
return errored.Errorf("failed to merge configuration. Error: %s", err)
}
return nil
}
95 changes: 95 additions & 0 deletions management/src/clusterm/manager/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// +build unittest

package manager

import (
"strings"

"github.com/contiv/cluster/management/src/boltdb"
"github.com/contiv/cluster/management/src/collins"
"github.com/contiv/cluster/management/src/configuration"
. "gopkg.in/check.v1"
)

type configSuite struct {
}

var _ = Suite(&configSuite{})

func (s *configSuite) TestReadConfigSuccess(c *C) {
config := &Config{}
confStr := `{
"ansible" : {
"playbook_location" : "foo"
}
}`
err := config.Read(strings.NewReader(confStr))
c.Assert(err, IsNil)
c.Assert(config.Ansible.PlaybookLocation, Equals, "foo")
}

func (s *configSuite) TestReadConfigInvalidJSON(c *C) {
config := &Config{}
confStr := `{
"ansible" : {
"playbook_location" : "extra-comma-error",
}
}`
err := config.Read(strings.NewReader(confStr))
c.Assert(err, NotNil)
}

func (s *configSuite) TestMergeConfigSuccess(c *C) {
dst := DefaultConfig()
src := &Config{
Ansible: configuration.AnsibleSubsysConfig{PlaybookLocation: "override-location"},
}
exptdDst := DefaultConfig()
exptdDst.Ansible.PlaybookLocation = "override-location"

err := dst.Merge(src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, exptdDst)
}

func (s *configSuite) TestMergeConfigSuccessNoInventory(c *C) {
dst := DefaultConfig()
src := &Config{}
exptdDst := DefaultConfig()

err := dst.Merge(src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, exptdDst)
c.Assert(dst.Inventory.Collins, Equals, (*collins.Config)(nil))
c.Assert(dst.Inventory.BoltDB, Equals, (*boltdb.Config)(nil))
}

func (s *configSuite) TestMergeConfigSuccessCollinsInventory(c *C) {
dst := DefaultConfig()
src := &Config{
Inventory: inventorySubsysConfig{Collins: &collins.Config{}},
}
exptdDst := DefaultConfig()
exptdDst.Inventory.Collins = &collins.Config{}

err := dst.Merge(src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, exptdDst)
c.Assert(dst.Inventory.BoltDB, Equals, (*boltdb.Config)(nil))
c.Assert(dst.Inventory.Collins, DeepEquals, exptdDst.Inventory.Collins)
}

func (s *configSuite) TestMergeConfigSuccessBoltdbInventory(c *C) {
dst := DefaultConfig()
src := &Config{
Inventory: inventorySubsysConfig{BoltDB: &boltdb.Config{}},
}
exptdDst := DefaultConfig()
exptdDst.Inventory.BoltDB = &boltdb.Config{}

err := dst.Merge(src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, exptdDst)
c.Assert(dst.Inventory.BoltDB, DeepEquals, exptdDst.Inventory.BoltDB)
c.Assert(dst.Inventory.Collins, Equals, (*collins.Config)(nil))
}
Loading

0 comments on commit cd624f9

Please sign in to comment.