-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_validate.go
138 lines (114 loc) · 3.54 KB
/
job_validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package command
import (
"fmt"
"strings"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/posener/complete"
)
type JobValidateCommand struct {
Meta
JobGetter
}
func (c *JobValidateCommand) Help() string {
helpText := `
Usage: nomad job validate [options] <path>
Alias: nomad validate
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.
If the supplied path is "-", the jobfile is read from stdin. Otherwise
it is read from the file at the supplied path or downloaded and
read from URL specified.
`
return strings.TrimSpace(helpText)
}
func (c *JobValidateCommand) Synopsis() string {
return "Checks if a given job specification is valid"
}
func (c *JobValidateCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *JobValidateCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictOr(complete.PredictFiles("*.nomad"), complete.PredictFiles("*.hcl"))
}
func (c *JobValidateCommand) Name() string { return "job validate" }
func (c *JobValidateCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), 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("This command takes one argument: <path>")
c.Ui.Error(commandErrorText(c))
return 1
}
// Get Job struct from Jobfile
job, err := c.JobGetter.ApiJob(args[0])
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
return 1
}
// 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)
}
// Check that the job is valid
jr, _, err := client.Jobs().Validate(job, nil)
if err != nil {
jr, err = c.validateLocal(job)
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error validating job: %s", err))
return 1
}
if jr != nil && !jr.DriverConfigValidated {
c.Ui.Output(
c.Colorize().Color("[bold][yellow]Driver configuration not validated since connection to Nomad agent couldn't be established.[reset]\n"))
}
if jr != nil && jr.Error != "" {
c.Ui.Error(
c.Colorize().Color("[bold][red]Job validation errors:[reset]"))
c.Ui.Error(jr.Error)
return 1
}
// 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)))
}
// Done!
c.Ui.Output(
c.Colorize().Color("[bold][green]Job validation successful[reset]"))
return 0
}
// validateLocal validates without talking to a Nomad agent
func (c *JobValidateCommand) validateLocal(aj *api.Job) (*api.JobValidateResponse, error) {
var out api.JobValidateResponse
job := agent.ApiJobToStructJob(aj)
canonicalizeWarnings := job.Canonicalize()
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()
}
}
warnings := job.Warnings()
out.Warnings = structs.MergeMultierrorWarnings(warnings, canonicalizeWarnings)
return &out, nil
}