diff --git a/command/agent/command.go b/command/agent/command.go index ecae14d81a70..1af7ff41a8b0 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -333,6 +333,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)) diff --git a/command/agent/config.go b/command/agent/config.go index 5f0bfdf78500..760030108ab1 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -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 @@ -366,6 +369,9 @@ func (a *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 } @@ -565,7 +571,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 diff --git a/command/agent/config_test.go b/command/agent/config_test.go index c13ff91f367d..d3544781e954 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -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) @@ -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) { diff --git a/command/agent/test-resources/etcnomad/common.hcl b/command/agent/test-resources/etcnomad/common.hcl new file mode 100644 index 000000000000..f7f24a2db8d1 --- /dev/null +++ b/command/agent/test-resources/etcnomad/common.hcl @@ -0,0 +1 @@ +bind_addr = "0.0.0.0" \ No newline at end of file diff --git a/command/agent/test-resources/etcnomad/dummy b/command/agent/test-resources/etcnomad/dummy new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/command/agent/test-resources/etcnomad/server.json b/command/agent/test-resources/etcnomad/server.json new file mode 100644 index 000000000000..7f09d32d5f04 --- /dev/null +++ b/command/agent/test-resources/etcnomad/server.json @@ -0,0 +1,5 @@ +{ + "advertise": { + "rpc": "127.0.0.1:4647" + } +} \ No newline at end of file diff --git a/command/agent/test-resources/myconf b/command/agent/test-resources/myconf new file mode 100644 index 000000000000..36cc3a661580 --- /dev/null +++ b/command/agent/test-resources/myconf @@ -0,0 +1 @@ +data_dir = "/var/lib/nomad" \ No newline at end of file