github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/config_validate.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  
     8  	multierror "github.com/hashicorp/go-multierror"
     9  	agent "github.com/hashicorp/nomad/command/agent"
    10  )
    11  
    12  type ConfigValidateCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *ConfigValidateCommand) Help() string {
    17  	helpText := `
    18  Usage: nomad config validate <config_path> [<config_path...>]
    19  
    20    Perform validation on a set of Nomad configuration files. This is useful
    21    to test the Nomad configuration without starting the agent.
    22  
    23    Accepts the path to either a single config file or a directory of
    24    config files to use for configuring the Nomad agent. This option may
    25    be specified multiple times. If multiple config files are used, the
    26    values from each will be merged together. During merging, values from
    27    files found later in the list are merged over values from previously
    28    parsed files.
    29  
    30    This command cannot operate on partial configuration fragments since
    31    those won't pass the full agent validation. This command does not
    32    require an ACL token.
    33  
    34    Returns 0 if the configuration is valid, or 1 if there are problems.
    35  `
    36  
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (c *ConfigValidateCommand) Synopsis() string {
    41  	return "Validate config files/directories"
    42  }
    43  
    44  func (c *ConfigValidateCommand) Name() string { return "config validate" }
    45  
    46  func (c *ConfigValidateCommand) Run(args []string) int {
    47  	var mErr multierror.Error
    48  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    49  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    50  	if err := flags.Parse(args); err != nil {
    51  		c.Ui.Error(err.Error())
    52  		return 1
    53  	}
    54  
    55  	configPath := flags.Args()
    56  	if len(configPath) < 1 {
    57  		c.Ui.Error("Must specify at least one config file or directory")
    58  		return 1
    59  	}
    60  
    61  	config := agent.DefaultConfig()
    62  
    63  	for _, path := range configPath {
    64  		fc, err := agent.LoadConfig(path)
    65  		if err != nil {
    66  			multierror.Append(&mErr, fmt.Errorf(
    67  				"Error loading configuration from %s: %s", path, err))
    68  			continue
    69  		}
    70  		if fc == nil || reflect.DeepEqual(fc, &agent.Config{}) {
    71  			c.Ui.Warn(fmt.Sprintf("No configuration loaded from %s", path))
    72  		}
    73  
    74  		config = config.Merge(fc)
    75  	}
    76  	if err := mErr.ErrorOrNil(); err != nil {
    77  		c.Ui.Error(err.Error())
    78  		return 1
    79  	}
    80  	cmd := agent.Command{Ui: c.Ui}
    81  	valid := cmd.IsValidConfig(config, agent.DefaultConfig())
    82  	if !valid {
    83  		c.Ui.Error("Configuration is invalid")
    84  		return 1
    85  	}
    86  
    87  	c.Ui.Output("Configuration is valid!")
    88  	return 0
    89  }