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"
|
2016-03-21 19:46:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InspectCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InspectCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad inspect [options] <job>
|
|
|
|
|
|
|
|
Inspect is used to see the specification of a submitted job.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2016-08-09 13:50:18 +00:00
|
|
|
` + generalOptionsUsage() + `
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InspectCommand) Synopsis() string {
|
|
|
|
return "Inspect a submitted job"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InspectCommand) 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
|
|
|
|
2016-03-21 19:46:35 +00:00
|
|
|
flags := c.Meta.FlagSet("inspect", FlagSetClient)
|
|
|
|
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
|
2017-07-01 01:10:19 +00:00
|
|
|
if len(args) == 0 && json || len(tmpl) > 0 {
|
|
|
|
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 {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
jobID := args[0]
|
|
|
|
|
2016-03-21 19:46:35 +00:00
|
|
|
// Check if the job exists
|
2016-03-21 20:00:14 +00:00
|
|
|
jobs, _, err := client.Jobs().PrefixList(jobID)
|
2016-03-21 19:46:35 +00:00
|
|
|
if err != nil {
|
2016-03-21 20:00:14 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(jobs) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-13 23:05:58 +00:00
|
|
|
if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
|
2017-07-21 00:31:07 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
|
|
|
|
return 1
|
2016-03-21 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-07-19 20:44:30 +00:00
|
|
|
job, err := getJob(client, jobs[0].ID, 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
|
2016-06-08 23:48:02 +00:00
|
|
|
req := api.RegisterJobRequest{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, jobID string, version *uint64) (*api.Job, error) {
|
|
|
|
if version == nil {
|
|
|
|
job, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
return job, err
|
|
|
|
}
|
|
|
|
|
|
|
|
versions, _, _, err := client.Jobs().Versions(jobID, false, nil)
|
|
|
|
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)
|
|
|
|
}
|