-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config command and config validate subcommand to nomad CLI
- Loading branch information
Showing
4 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
type ConfigCommand struct { | ||
Meta | ||
} | ||
|
||
func (f *ConfigCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad config <subcommand> [options] [args] | ||
This command groups subcommands for interacting with configurations. | ||
Users can validate configurations for the Nomad agent. | ||
Validate configuration: | ||
$ nomad config validate <config_path> [<config_path>...] | ||
Please see the individual subcommand help for detailed usage information. | ||
` | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (f *ConfigCommand) Synopsis() string { | ||
return "Interact with configurations" | ||
} | ||
|
||
func (f *ConfigCommand) Name() string { return "config" } | ||
|
||
func (f *ConfigCommand) Run(args []string) int { | ||
return cli.RunResultHelp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
multierror "github.com/hashicorp/go-multierror" | ||
agent "github.com/hashicorp/nomad/command/agent" | ||
) | ||
|
||
type ConfigValidateCommand struct { | ||
Meta | ||
} | ||
|
||
func (c *ConfigValidateCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad config validate <config_path> [<config_path...>] | ||
Performs a thorough sanity test on Nomad configuration files. For each file | ||
or directory given, the validate command will attempt to parse the contents | ||
just as the "nomad agent" command would, and catch any errors. | ||
This is useful to do a test of the configuration only, without actually | ||
starting the agent. This performs all of the validation the agent would, so | ||
this should be given the complete set of configuration files that are going | ||
to be loaded by the agent. This command cannot operate on partial | ||
configuration fragments since those won't pass the full agent validation. | ||
Returns 0 if the configuration is valid, or 1 if there are problems. | ||
` | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *ConfigValidateCommand) Synopsis() string { | ||
return "Validate config files/directories" | ||
} | ||
|
||
func (c *ConfigValidateCommand) Name() string { return "config validate" } | ||
|
||
func (c *ConfigValidateCommand) Run(args []string) int { | ||
var mErr multierror.Error | ||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
if err := flags.Parse(args); err != nil { | ||
c.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
configPath := flags.Args() | ||
if len(configPath) < 1 { | ||
c.Ui.Error("Must specify at least one config file or directory") | ||
return 1 | ||
} | ||
|
||
var config *agent.Config | ||
|
||
for _, path := range configPath { | ||
current, err := agent.LoadConfig(path) | ||
if err != nil { | ||
_ = multierror.Append(&mErr, fmt.Errorf( | ||
"Error loading configuration from %s: %s", path, err)) | ||
continue | ||
} | ||
|
||
if config == nil { | ||
config = current | ||
} else { | ||
config = config.Merge(current) | ||
} | ||
} | ||
|
||
if err := mErr.ErrorOrNil(); err != nil { | ||
c.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
c.Ui.Output("Configuration is valid!") | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package command | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func TestConfigValidateCommand_SucceedWithEmptyDir(t *testing.T) { | ||
t.Parallel() | ||
fh, err := ioutil.TempDir("", "nomad") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
defer os.Remove(fh) | ||
|
||
ui := cli.NewMockUi() | ||
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}} | ||
args := []string{fh} | ||
|
||
code := cmd.Run(args) | ||
if code != 0 { | ||
t.Fatalf("expected exit 0, actual: %d", code) | ||
} | ||
} | ||
|
||
func TestConfigValidateCommand_SucceedWithMinimalConfigFile(t *testing.T) { | ||
t.Parallel() | ||
fh, err := ioutil.TempDir("", "nomad") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
defer os.Remove(fh) | ||
|
||
fp := filepath.Join(fh, "config.hcl") | ||
err = ioutil.WriteFile(fp, []byte(`datacenter = "abc01"`), 0644) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||