From b6107c4039ec27a7e93577acd32cb82f97dc27cb Mon Sep 17 00:00:00 2001 From: tsheasha Date: Sun, 6 Mar 2016 19:35:45 +0100 Subject: [PATCH] Watch config using viper Conflicts: Gomfile aws/manage_route_spec.go config/config.go config/config_test.go daemon/daemon.go --- aws/manage_route_spec.go | 5 +++-- config/config.go | 2 +- config/config_test.go | 5 +++-- daemon/daemon.go | 11 +++++++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/aws/manage_route_spec.go b/aws/manage_route_spec.go index 1fcf81d..02f10fa 100644 --- a/aws/manage_route_spec.go +++ b/aws/manage_route_spec.go @@ -3,13 +3,14 @@ package aws import ( "errors" "fmt" + "net" + "strings" + log "github.com/Sirupsen/logrus" "github.com/aws/aws-sdk-go/service/ec2" "github.com/bobtfish/AWSnycast/healthcheck" "github.com/bobtfish/AWSnycast/instancemetadata" "github.com/hashicorp/go-multierror" - "net" - "strings" ) type ManageRoutesSpec struct { diff --git a/config/config.go b/config/config.go index 3f43906..408fb45 100644 --- a/config/config.go +++ b/config/config.go @@ -39,7 +39,7 @@ func (c *Config) Validate(im instancemetadata.InstanceMetadata, manager aws.Rout result = multierror.Append(result, errors.New("No route_tables key in config")) } else { if len(c.RouteTables) == 0 { - result = multierror.Append(result, errors.New("No route_tables defined in config")) + result = multierror.Append(result, errors.New("No routetables defined in config")) } else { for k, v := range c.RouteTables { if err := v.Validate(im, manager, k, c.Healthchecks, c.RemoteHealthcheckTemplates); err != nil { diff --git a/config/config_test.go b/config/config_test.go index 805923b..d9ee59d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -3,6 +3,8 @@ package config import ( "errors" "fmt" + "testing" + a "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/bobtfish/AWSnycast/aws" @@ -11,7 +13,6 @@ import ( "github.com/bobtfish/AWSnycast/testhelpers" "github.com/hashicorp/go-multierror" "github.com/stretchr/testify/assert" - "testing" ) var tim instancemetadata.InstanceMetadata @@ -135,7 +136,7 @@ func TestConfigValidateEmptyRouteTables(t *testing.T) { RouteTables: r, } err := c.Validate(tim, rtm) - testhelpers.CheckOneMultiError(t, err, "No route_tables defined in config") + testhelpers.CheckOneMultiError(t, err, "No routetables defined in config") } func TestConfigValidateBadRouteTables(t *testing.T) { diff --git a/daemon/daemon.go b/daemon/daemon.go index 179387e..c06880d 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -6,6 +6,8 @@ import ( "github.com/bobtfish/AWSnycast/aws" "github.com/bobtfish/AWSnycast/config" "github.com/bobtfish/AWSnycast/instancemetadata" + "github.com/spf13/viper" + "gopkg.in/fsnotify.v1" "time" ) @@ -126,6 +128,15 @@ func (d *Daemon) Run(oneShot bool, noop bool) int { if oneShot { d.quitChan <- true } else { + // Reload the daemon on config change + viper.SetConfigFile(d.ConfigFile) + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + log.WithFields(log.Fields{"file": d.ConfigFile}).Info("Config file changed, reloading daemon") + d.quitChan <- true + d.Run(oneShot, noop) + }) + d.runHealthChecks() defer d.stopHealthChecks() d.RunSleepLoop()