2015-09-25 01:29:46 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-02-13 23:18:17 +00:00
|
|
|
|
2017-03-03 23:00:39 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2017-07-07 02:08:51 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-25 01:29:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValidateCommand struct {
|
|
|
|
Meta
|
2016-08-16 10:49:14 +00:00
|
|
|
JobGetter
|
2015-09-25 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ValidateCommand) Help() string {
|
|
|
|
helpText := `
|
2016-12-24 20:39:33 +00:00
|
|
|
Usage: nomad validate [options] <path>
|
2015-09-25 01:29:46 +00:00
|
|
|
|
2016-07-01 18:51:53 +00:00
|
|
|
Checks if a given HCL job file has a valid specification. This can be used to
|
|
|
|
check for any syntax errors or validation problems with a job.
|
2015-09-25 01:29:46 +00:00
|
|
|
|
2016-07-22 20:48:14 +00:00
|
|
|
If the supplied path is "-", the jobfile is read from stdin. Otherwise
|
2016-08-11 13:34:31 +00:00
|
|
|
it is read from the file at the supplied path or downloaded and
|
|
|
|
read from URL specified.
|
2015-09-25 01:29:46 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ValidateCommand) Synopsis() string {
|
|
|
|
return "Checks if a given job specification is valid"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ValidateCommand) Run(args []string) int {
|
|
|
|
flags := c.Meta.FlagSet("validate", FlagSetNone)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one node
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Get Job struct from Jobfile
|
2017-02-06 19:48:28 +00:00
|
|
|
job, err := c.JobGetter.ApiJob(args[0])
|
2015-09-25 01:29:46 +00:00
|
|
|
if err != nil {
|
2016-08-10 14:30:19 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
|
2015-09-25 01:29:46 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 255
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the region to be that of the job.
|
|
|
|
if r := job.Region; r != nil {
|
|
|
|
client.SetRegion(*r)
|
|
|
|
}
|
2015-12-18 20:17:50 +00:00
|
|
|
|
2015-09-25 01:29:46 +00:00
|
|
|
// Check that the job is valid
|
2017-02-13 23:18:17 +00:00
|
|
|
jr, _, err := client.Jobs().Validate(job, nil)
|
2017-03-03 23:00:39 +00:00
|
|
|
if err != nil {
|
|
|
|
jr, err = c.validateLocal(job)
|
|
|
|
}
|
2017-02-13 23:18:17 +00:00
|
|
|
if err != nil {
|
2015-09-25 01:29:46 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error validating job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2017-03-03 23:00:39 +00:00
|
|
|
|
2017-02-13 23:18:17 +00:00
|
|
|
if jr != nil && !jr.DriverConfigValidated {
|
2017-03-03 23:00:39 +00:00
|
|
|
c.Ui.Output(
|
|
|
|
c.Colorize().Color("[bold][yellow]Driver configuration not validated since connection to Nomad agent couldn't be established.[reset]\n"))
|
2017-02-13 23:18:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 23:00:39 +00:00
|
|
|
if jr != nil && jr.Error != "" {
|
|
|
|
c.Ui.Error(
|
|
|
|
c.Colorize().Color("[bold][red]Job validation errors:[reset]"))
|
|
|
|
c.Ui.Error(jr.Error)
|
2017-02-13 23:18:17 +00:00
|
|
|
return 1
|
|
|
|
}
|
2015-09-25 01:29:46 +00:00
|
|
|
|
2017-05-10 03:52:47 +00:00
|
|
|
// Print any warnings if there are any
|
|
|
|
if jr.Warnings != "" {
|
|
|
|
c.Ui.Output(
|
|
|
|
c.Colorize().Color(fmt.Sprintf("[bold][yellow]Job Warnings:\n%s[reset]\n", jr.Warnings)))
|
|
|
|
}
|
|
|
|
|
2015-09-25 01:29:46 +00:00
|
|
|
// Done!
|
2017-03-03 23:00:39 +00:00
|
|
|
c.Ui.Output(
|
|
|
|
c.Colorize().Color("[bold][green]Job validation successful[reset]"))
|
2015-09-25 01:29:46 +00:00
|
|
|
return 0
|
|
|
|
}
|
2017-03-03 23:00:39 +00:00
|
|
|
|
|
|
|
// validateLocal validates without talking to a Nomad agent
|
|
|
|
func (c *ValidateCommand) validateLocal(aj *api.Job) (*api.JobValidateResponse, error) {
|
|
|
|
var out api.JobValidateResponse
|
|
|
|
|
|
|
|
job := agent.ApiJobToStructJob(aj)
|
2017-07-07 02:08:51 +00:00
|
|
|
canonicalizeWarnings := job.Canonicalize()
|
2017-03-03 23:00:39 +00:00
|
|
|
|
|
|
|
if vErr := job.Validate(); vErr != nil {
|
|
|
|
if merr, ok := vErr.(*multierror.Error); ok {
|
|
|
|
for _, err := range merr.Errors {
|
|
|
|
out.ValidationErrors = append(out.ValidationErrors, err.Error())
|
|
|
|
}
|
|
|
|
out.Error = merr.Error()
|
|
|
|
} else {
|
|
|
|
out.ValidationErrors = append(out.ValidationErrors, vErr.Error())
|
|
|
|
out.Error = vErr.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 02:08:51 +00:00
|
|
|
warnings := job.Warnings()
|
|
|
|
out.Warnings = structs.MergeMultierrorWarnings(warnings, canonicalizeWarnings)
|
2017-03-03 23:00:39 +00:00
|
|
|
return &out, nil
|
|
|
|
}
|