open-nomad/command/status.go

112 lines
2.4 KiB
Go
Raw Normal View History

2015-09-11 07:38:15 +00:00
package command
import (
"flag"
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/ryanuber/columnize"
)
type StatusCommand struct {
Ui cli.Ui
}
func (c *StatusCommand) Help() string {
helpText := `
2015-09-11 17:00:55 +00:00
Usage: nomad status [options] [job]
2015-09-11 07:38:15 +00:00
2015-09-13 18:39:49 +00:00
Display status information about jobs. If no job ID is given,
2015-09-11 17:00:55 +00:00
a list of all known jobs will be dumped.
2015-09-11 07:38:15 +00:00
Options:
-help
Display this message
-http-addr
2015-09-11 17:00:55 +00:00
Address of the Nomad API to connect. Can also be specified
using the environment variable NOMAD_HTTP_ADDR.
2015-09-11 07:38:15 +00:00
Default = http://127.0.0.1:4646
`
return strings.TrimSpace(helpText)
}
func (c *StatusCommand) Synopsis() string {
2015-09-13 18:39:49 +00:00
return "Display status information about jobs"
2015-09-11 07:38:15 +00:00
}
func (c *StatusCommand) Run(args []string) int {
var httpAddr *string
flags := flag.NewFlagSet("status", flag.ContinueOnError)
flags.Usage = func() { c.Ui.Output(c.Help()) }
2015-09-11 17:16:36 +00:00
httpAddr = httpAddrFlag(flags)
2015-09-11 07:38:15 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we either got no jobs or exactly one.
if len(flags.Args()) > 1 {
c.Ui.Error(c.Help())
return 1
}
// Get the HTTP client
2015-09-11 17:16:36 +00:00
client, err := httpClient(*httpAddr)
2015-09-11 07:38:15 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed initializing Nomad client: %s", err))
return 1
}
// Invoke list mode if no job ID.
if len(flags.Args()) == 0 {
jobs, _, err := client.Jobs().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed querying jobs: %s", err))
return 1
}
2015-09-11 17:00:55 +00:00
// No output if we have no jobs
if len(jobs) == 0 {
return 0
}
2015-09-13 18:39:49 +00:00
out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
2015-09-13 18:39:49 +00:00
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
2015-09-11 07:38:15 +00:00
job.ID,
job.Type,
job.Priority,
2015-09-13 18:39:49 +00:00
job.Status)
2015-09-11 07:38:15 +00:00
}
c.Ui.Output(columnize.SimpleFormat(out))
return 0
}
// Try querying the job
jobID := flags.Args()[0]
job, _, err := client.Jobs().Info(jobID, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed querying job: %s", err))
return 1
}
// Format the job info
basic := []string{
fmt.Sprintf("ID | %s", job.ID),
fmt.Sprintf("Name | %s", job.Name),
fmt.Sprintf("Type | %s", job.Type),
fmt.Sprintf("Priority | %d", job.Priority),
fmt.Sprintf("Datacenters | %s", strings.Join(job.Datacenters, ",")),
fmt.Sprintf("Status | %s", job.Status),
fmt.Sprintf("StatusDescription | %s", job.StatusDescription),
}
c.Ui.Output(columnize.SimpleFormat(basic))
return 0
}