Skip to content

Commit

Permalink
Merge pull request #536 from hashicorp/f-log-config-files
Browse files Browse the repository at this point in the history
Log the list of config files loaded when starting the nomad agent
  • Loading branch information
cbednarski committed Dec 9, 2015
2 parents e864e5d + 015a156 commit dc23448
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ func (c *Command) Run(args []string) int {
return 1
}

// Log config files
c.Ui.Info(fmt.Sprintf("Loaded configuration from %s", strings.Join(config.Files, ", ")))

// Initialize the telemetry
if err := c.setupTelementry(config); err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing telemetry: %s", err))
Expand Down
14 changes: 13 additions & 1 deletion command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type Config struct {
Revision string
Version string
VersionPrerelease string

// List of config files that have been loaded (in order)
Files []string
}

// AtlasConfig is used to enable an parameterize the Atlas integration
Expand Down Expand Up @@ -383,6 +386,9 @@ func (c *Config) Merge(b *Config) *Config {
result.Atlas = result.Atlas.Merge(b.Atlas)
}

// Merge config files lists
result.Files = append(result.Files, b.Files...)

return &result
}

Expand Down Expand Up @@ -581,7 +587,13 @@ func LoadConfigFile(path string) (*Config, error) {
if err != nil {
return nil, err
}
return LoadConfigString(string(d))

config, err := LoadConfigString(string(d))
if err == nil {
config.Files = append(config.Files, path)
}

return config, err
}

// LoadConfigDir loads all the configurations in the given directory
Expand Down
37 changes: 37 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ func TestConfig_LoadConfig(t *testing.T) {
t.Fatalf("bad: %#v", config)
}

expectedConfigFiles := []string{fh.Name()}
if !reflect.DeepEqual(config.Files, expectedConfigFiles) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
expectedConfigFiles, config.Files)
}

dir, err := ioutil.TempDir("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
Expand All @@ -286,6 +292,37 @@ func TestConfig_LoadConfig(t *testing.T) {
if config.Datacenter != "sfo" {
t.Fatalf("bad: %#v", config)
}

expectedConfigFiles = []string{file1}
if !reflect.DeepEqual(config.Files, expectedConfigFiles) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
expectedConfigFiles, config.Files)
}
}

func TestConfig_LoadConfigsFileOrder(t *testing.T) {
config1, err := LoadConfigDir("test-resources/etcnomad")
if err != nil {
t.Fatalf("Failed to load config: %s", err)
}

config2, err := LoadConfig("test-resources/myconf")
if err != nil {
t.Fatalf("Failed to load config: %s", err)
}

expected := []string{
"test-resources/etcnomad/common.hcl",
"test-resources/etcnomad/server.json",
"test-resources/myconf",
}

config := config1.Merge(config2)

if !reflect.DeepEqual(config.Files, expected) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
expected, config.Files)
}
}

func TestConfig_Listener(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions command/agent/test-resources/etcnomad/common.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bind_addr = "0.0.0.0"
Empty file.
5 changes: 5 additions & 0 deletions command/agent/test-resources/etcnomad/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"advertise": {
"rpc": "127.0.0.1:4647"
}
}
1 change: 1 addition & 0 deletions command/agent/test-resources/myconf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data_dir = "/var/lib/nomad"

0 comments on commit dc23448

Please sign in to comment.