2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-25 01:29:46 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-19 16:18:06 +00:00
|
|
|
"os"
|
2015-09-25 01:29:46 +00:00
|
|
|
"strings"
|
2017-02-13 23:18:17 +00:00
|
|
|
|
2021-04-03 07:50:23 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-03-03 23:00:39 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2023-02-13 21:14:59 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2017-07-18 20:18:17 +00:00
|
|
|
"github.com/posener/complete"
|
2015-09-25 01:29:46 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
type JobValidateCommand struct {
|
2015-09-25 01:29:46 +00:00
|
|
|
Meta
|
2016-08-16 10:49:14 +00:00
|
|
|
JobGetter
|
2015-09-25 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) Help() string {
|
2015-09-25 01:29:46 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad job validate [options] <path>
|
2018-03-21 01:28:14 +00:00
|
|
|
Alias: nomad validate
|
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.
|
2020-10-21 14:22:08 +00:00
|
|
|
|
2022-05-19 16:18:06 +00:00
|
|
|
The run command will set the vault_token of the job based on the following
|
|
|
|
precedence, going from highest to lowest: the -vault-token flag, the
|
|
|
|
$VAULT_TOKEN environment variable and finally the value in the job file.
|
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'read-job'
|
|
|
|
capability for the job's namespace.
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
|
|
|
|
2020-10-21 14:22:08 +00:00
|
|
|
Validate Options:
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
-json
|
|
|
|
Parses the job file as JSON. If the outer object has a Job field, such as
|
|
|
|
from "nomad job inspect" or "nomad run -output", the value of the field is
|
|
|
|
used as the job.
|
|
|
|
|
2020-10-21 14:22:08 +00:00
|
|
|
-hcl1
|
2022-09-01 14:42:08 +00:00
|
|
|
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
|
2020-11-09 20:02:58 +00:00
|
|
|
|
2021-11-04 15:33:09 +00:00
|
|
|
-hcl2-strict
|
|
|
|
Whether an error should be produced from the HCL2 parser where a variable
|
|
|
|
has been supplied which is not defined within the root variables. Defaults
|
2022-09-01 14:42:08 +00:00
|
|
|
to true, but ignored if "-hcl1" is also defined.
|
2021-11-04 15:33:09 +00:00
|
|
|
|
2022-05-19 16:18:06 +00:00
|
|
|
-vault-token
|
|
|
|
Used to validate if the user submitting the job has permission to run the job
|
|
|
|
according to its Vault policies. A Vault token must be supplied if the vault
|
2023-01-30 14:48:43 +00:00
|
|
|
block allow_unauthenticated is disabled in the Nomad server configuration.
|
2022-05-19 16:18:06 +00:00
|
|
|
If the -vault-token flag is set, the passed Vault token is added to the jobspec
|
|
|
|
before sending to the Nomad servers. This allows passing the Vault token
|
|
|
|
without storing it in the job file. This overrides the token found in the
|
|
|
|
$VAULT_TOKEN environment variable and the vault_token field in the job file.
|
|
|
|
This token is cleared from the job after validating and cannot be used within
|
2023-01-30 14:48:43 +00:00
|
|
|
the job executing environment. Use the vault block when templating in a job
|
2022-05-19 16:18:06 +00:00
|
|
|
with a Vault token.
|
|
|
|
|
|
|
|
-vault-namespace
|
|
|
|
If set, the passed Vault namespace is stored in the job before sending to the
|
|
|
|
Nomad servers.
|
|
|
|
|
2020-11-09 20:02:58 +00:00
|
|
|
-var 'key=value'
|
|
|
|
Variable for template, can be used multiple times.
|
|
|
|
|
|
|
|
-var-file=path
|
|
|
|
Path to HCL2 file containing user variables.
|
2015-09-25 01:29:46 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) Synopsis() string {
|
2015-09-25 01:29:46 +00:00
|
|
|
return "Checks if a given job specification is valid"
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) AutocompleteFlags() complete.Flags {
|
2020-10-21 14:22:08 +00:00
|
|
|
return complete.Flags{
|
2022-05-19 16:18:06 +00:00
|
|
|
"-hcl1": complete.PredictNothing,
|
|
|
|
"-hcl2-strict": complete.PredictNothing,
|
|
|
|
"-vault-token": complete.PredictAnything,
|
|
|
|
"-vault-namespace": complete.PredictAnything,
|
|
|
|
"-var": complete.PredictAnything,
|
|
|
|
"-var-file": complete.PredictFiles("*.var"),
|
2020-10-21 14:22:08 +00:00
|
|
|
}
|
2017-07-18 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) AutocompleteArgs() complete.Predictor {
|
2022-04-21 20:20:36 +00:00
|
|
|
return complete.PredictOr(
|
|
|
|
complete.PredictFiles("*.nomad"),
|
|
|
|
complete.PredictFiles("*.hcl"),
|
|
|
|
complete.PredictFiles("*.json"),
|
|
|
|
)
|
2017-07-18 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobValidateCommand) Name() string { return "job validate" }
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) Run(args []string) int {
|
2022-05-19 16:18:06 +00:00
|
|
|
var vaultToken, vaultNamespace string
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
flagSet := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2020-12-11 03:26:10 +00:00
|
|
|
flagSet.Usage = func() { c.Ui.Output(c.Help()) }
|
2022-04-21 20:20:36 +00:00
|
|
|
flagSet.BoolVar(&c.JobGetter.JSON, "json", false, "")
|
|
|
|
flagSet.BoolVar(&c.JobGetter.HCL1, "hcl1", false, "")
|
|
|
|
flagSet.BoolVar(&c.JobGetter.Strict, "hcl2-strict", true, "")
|
2022-05-19 16:18:06 +00:00
|
|
|
flagSet.StringVar(&vaultToken, "vault-token", "", "")
|
|
|
|
flagSet.StringVar(&vaultNamespace, "vault-namespace", "", "")
|
2022-04-21 20:20:36 +00:00
|
|
|
flagSet.Var(&c.JobGetter.Vars, "var", "")
|
|
|
|
flagSet.Var(&c.JobGetter.VarFiles, "var-file", "")
|
2020-10-21 14:22:08 +00:00
|
|
|
|
2020-12-11 03:26:10 +00:00
|
|
|
if err := flagSet.Parse(args); err != nil {
|
2015-09-25 01:29:46 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one node
|
2020-12-11 03:26:10 +00:00
|
|
|
args = flagSet.Args()
|
2015-09-25 01:29:46 +00:00
|
|
|
if len(args) != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <path>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2015-09-25 01:29:46 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:42:08 +00:00
|
|
|
if c.JobGetter.HCL1 {
|
|
|
|
c.JobGetter.Strict = false
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
if err := c.JobGetter.Validate(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Get Job struct from Jobfile
|
2022-04-21 20:20:36 +00:00
|
|
|
job, err := c.JobGetter.Get(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
|
|
|
|
2022-05-19 16:18:06 +00:00
|
|
|
// Parse the Vault token
|
|
|
|
if vaultToken == "" {
|
|
|
|
// Check the environment variable
|
|
|
|
vaultToken = os.Getenv("VAULT_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
if vaultToken != "" {
|
2022-08-17 16:26:34 +00:00
|
|
|
job.VaultToken = pointer.Of(vaultToken)
|
2022-05-19 16:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if vaultNamespace != "" {
|
2022-08-17 16:26:34 +00:00
|
|
|
job.VaultNamespace = pointer.Of(vaultNamespace)
|
2022-05-19 16:18:06 +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 != "" {
|
2023-02-13 21:14:59 +00:00
|
|
|
c.Ui.Output(c.FormatWarnings("Job", jr.Warnings))
|
2017-05-10 03:52:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobValidateCommand) validateLocal(aj *api.Job) (*api.JobValidateResponse, error) {
|
2017-03-03 23:00:39 +00:00
|
|
|
var out api.JobValidateResponse
|
|
|
|
|
|
|
|
job := agent.ApiJobToStructJob(aj)
|
2021-01-14 20:46:35 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:14:59 +00:00
|
|
|
out.Warnings = helper.MergeMultierrorWarnings(job.Warnings())
|
2017-03-03 23:00:39 +00:00
|
|
|
return &out, nil
|
|
|
|
}
|