2016-03-21 19:46:35 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-04-13 23:53:39 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-22 20:19:29 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
2016-03-21 19:46:35 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
type JobInspectCommand struct {
|
2016-03-21 19:46:35 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInspectCommand) Help() string {
|
2016-03-21 19:46:35 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad job inspect [options] <job>
|
2018-03-21 01:28:14 +00:00
|
|
|
Alias: nomad inspect
|
2016-03-21 19:46:35 +00:00
|
|
|
|
|
|
|
Inspect is used to see the specification of a submitted job.
|
|
|
|
|
2023-03-09 20:00:04 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'read-job'
|
|
|
|
capability for the job's namespace. The 'list-jobs' capability is required to
|
|
|
|
run the command with a job prefix instead of the exact job ID.
|
2020-11-19 21:38:08 +00:00
|
|
|
|
2016-03-21 19:46:35 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2016-03-21 19:46:35 +00:00
|
|
|
|
2016-08-09 13:50:18 +00:00
|
|
|
Inspect Options:
|
|
|
|
|
2017-07-19 20:44:30 +00:00
|
|
|
-version <job version>
|
2017-07-25 16:23:44 +00:00
|
|
|
Display the job at the given job version.
|
2017-07-19 20:44:30 +00:00
|
|
|
|
2016-08-09 13:50:18 +00:00
|
|
|
-json
|
2017-07-01 01:10:19 +00:00
|
|
|
Output the job in its JSON format.
|
2016-08-09 13:50:18 +00:00
|
|
|
|
|
|
|
-t
|
2017-07-01 01:10:19 +00:00
|
|
|
Format and display job using a Go template.
|
2016-08-09 13:50:18 +00:00
|
|
|
`
|
2016-03-21 19:46:35 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInspectCommand) Synopsis() string {
|
2016-03-21 19:46:35 +00:00
|
|
|
return "Inspect a submitted job"
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInspectCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-version": complete.PredictAnything,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
2017-08-22 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInspectCommand) AutocompleteArgs() complete.Predictor {
|
2017-08-22 20:19:29 +00:00
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
|
2017-08-22 20:19:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Jobs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobInspectCommand) Name() string { return "job inspect" }
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *JobInspectCommand) Run(args []string) int {
|
2017-07-01 01:10:19 +00:00
|
|
|
var json bool
|
2017-07-19 20:44:30 +00:00
|
|
|
var tmpl, versionStr string
|
2016-08-06 11:30:12 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2016-03-21 19:46:35 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2017-07-01 01:10:19 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
2016-08-06 11:30:12 +00:00
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2017-07-19 20:44:30 +00:00
|
|
|
flags.StringVar(&versionStr, "version", "", "")
|
2016-03-21 19:46:35 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-08-06 11:30:12 +00:00
|
|
|
// If args not specified but output format is specified, format and output the jobs data list
|
2021-02-03 21:29:05 +00:00
|
|
|
if len(args) == 0 && (json || len(tmpl) > 0) {
|
2017-07-01 01:10:19 +00:00
|
|
|
jobs, _, err := client.Jobs().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err))
|
2016-08-06 12:38:41 +00:00
|
|
|
return 1
|
2016-08-06 11:30:12 +00:00
|
|
|
}
|
2017-07-01 01:10:19 +00:00
|
|
|
|
|
|
|
out, err := Format(json, tmpl, jobs)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
2016-08-06 11:30:12 +00:00
|
|
|
}
|
2017-07-01 01:10:19 +00:00
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
2016-08-06 11:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one job
|
|
|
|
if len(args) != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <job>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2016-08-06 11:30:12 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:46:35 +00:00
|
|
|
// Check if the job exists
|
2023-03-03 19:43:20 +00:00
|
|
|
jobIDPrefix := strings.TrimSpace(args[0])
|
|
|
|
jobID, namespace, err := c.JobIDByPrefix(client, jobIDPrefix, nil)
|
2016-03-21 19:46:35 +00:00
|
|
|
if err != nil {
|
2023-03-03 19:43:20 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2016-03-21 20:00:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:44:30 +00:00
|
|
|
var version *uint64
|
|
|
|
if versionStr != "" {
|
|
|
|
v, _, err := parseVersion(versionStr)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing version value %q: %v", versionStr, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
version = &v
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:00:14 +00:00
|
|
|
// Prefix lookup matched a single job
|
2023-03-03 19:43:20 +00:00
|
|
|
job, err := getJob(client, namespace, jobID, version)
|
2016-03-21 20:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
|
|
|
|
return 1
|
2016-03-21 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 13:50:18 +00:00
|
|
|
// If output format is specified, format and output the data
|
2017-07-01 01:10:19 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, job)
|
2016-08-09 13:50:18 +00:00
|
|
|
if err != nil {
|
2017-07-01 01:10:19 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2016-08-09 13:50:18 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:46:35 +00:00
|
|
|
// Print the contents of the job
|
2020-06-16 03:39:44 +00:00
|
|
|
req := struct {
|
|
|
|
Job *api.Job
|
|
|
|
}{
|
|
|
|
Job: job,
|
|
|
|
}
|
2017-02-15 23:14:47 +00:00
|
|
|
f, err := DataFormat("json", "")
|
2016-04-13 23:53:39 +00:00
|
|
|
if err != nil {
|
2017-02-15 23:14:47 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
|
2016-04-13 23:53:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-15 23:14:47 +00:00
|
|
|
out, err := f.TransformData(req)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.Ui.Output(out)
|
2016-03-21 19:46:35 +00:00
|
|
|
return 0
|
|
|
|
}
|
2017-07-19 20:44:30 +00:00
|
|
|
|
|
|
|
// getJob retrieves the job optionally at a particular version.
|
2020-06-17 19:39:50 +00:00
|
|
|
func getJob(client *api.Client, namespace, jobID string, version *uint64) (*api.Job, error) {
|
|
|
|
var q *api.QueryOptions
|
|
|
|
if namespace != "" {
|
|
|
|
q = &api.QueryOptions{Namespace: namespace}
|
|
|
|
}
|
2017-07-19 20:44:30 +00:00
|
|
|
if version == nil {
|
2020-06-17 19:39:50 +00:00
|
|
|
job, _, err := client.Jobs().Info(jobID, q)
|
2017-07-19 20:44:30 +00:00
|
|
|
return job, err
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:39:50 +00:00
|
|
|
versions, _, _, err := client.Jobs().Versions(jobID, false, q)
|
2017-07-19 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, j := range versions {
|
|
|
|
if *j.Version != *version {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return j, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("job %q with version %d couldn't be found", jobID, *version)
|
|
|
|
}
|