open-nomad/command/inspect.go

170 lines
3.6 KiB
Go
Raw Normal View History

2016-03-21 19:46:35 +00:00
package command
import (
2016-04-13 23:53:39 +00:00
"encoding/json"
2016-03-21 19:46:35 +00:00
"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:
-json
Output the evaluation in its JSON format.
-t
Format and display evaluation using a Go template.
`
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 {
var ojson bool
var tmpl string
2016-03-21 19:46:35 +00:00
flags := c.Meta.FlagSet("inspect", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&ojson, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
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 {
var format string
if ojson && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if ojson {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
jobs, _, err := client.Jobs().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err))
return 1
}
f, err := DataFormat(format, tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
// Return nothing if no jobs found
if len(jobs) == 0 {
return 0
}
out, err := f.TransformData(jobs)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}
}
// 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
}
if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
2016-03-21 20:00:14 +00:00
out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
job.ID,
job.Type,
job.Priority,
job.Status)
2016-03-21 19:46:35 +00:00
}
2016-03-21 20:00:14 +00:00
c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out)))
return 0
}
// Prefix lookup matched a single job
2016-04-13 23:53:39 +00:00
job, _, err := client.Jobs().Info(jobs[0].ID, nil)
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
var format string
if ojson {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
out, err := f.TransformData(job)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
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}
2016-04-13 23:53:39 +00:00
buf, err := json.MarshalIndent(req, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
return 1
}
c.Ui.Output(string(buf))
2016-03-21 19:46:35 +00:00
return 0
}