open-nomad/command/job_inspect.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

209 lines
4.6 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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.
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.
2016-03-21 19:46:35 +00:00
General Options:
` + 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 {
client, err := c.Meta.Client()
if err != nil {
return nil
}
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]
})
}
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
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, "")
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
}
// If args not specified but output format is specified, format and output the jobs data list
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))
return 1
}
2017-07-01 01:10:19 +00:00
out, err := Format(json, tmpl, jobs)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
2017-07-01 01:10:19 +00:00
c.Ui.Output(out)
return 0
}
// Check that we got exactly one job
if len(args) != 1 {
c.Ui.Error("This command takes one argument: <job>")
c.Ui.Error(commandErrorText(c))
return 1
}
2016-03-21 19:46:35 +00:00
// Check if the job exists
jobIDPrefix := strings.TrimSpace(args[0])
jobID, namespace, err := c.JobIDByPrefix(client, jobIDPrefix, nil)
2016-03-21 19:46:35 +00:00
if err != nil {
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
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
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.
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 {
job, _, err := client.Jobs().Info(jobID, q)
2017-07-19 20:44:30 +00:00
return job, err
}
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)
}