open-nomad/command/deployment_list.go

142 lines
3.1 KiB
Go
Raw Normal View History

2017-06-30 17:59:19 +00:00
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
2017-08-18 15:09:52 +00:00
"github.com/posener/complete"
2017-06-30 17:59:19 +00:00
)
type DeploymentListCommand struct {
Meta
}
func (c *DeploymentListCommand) Help() string {
helpText := `
Usage: nomad deployment list [options]
2017-10-13 21:36:02 +00:00
List is used to list the set of deployments tracked by Nomad.
2017-06-30 17:59:19 +00:00
When ACLs are enabled, this command requires a token with the 'read-job'
capability for the deployment's namespace.
2017-06-30 17:59:19 +00:00
General Options:
` + generalOptionsUsage(usageOptsDefault) + `
2017-06-30 17:59:19 +00:00
List Options:
2017-07-01 01:10:19 +00:00
-json
Output the deployments in a JSON format.
-filter
Specifies an expression used to filter query results.
2017-07-01 01:10:19 +00:00
-t
Format and display the deployments using a Go template.
2017-06-30 17:59:19 +00:00
-verbose
Display full information.
`
return strings.TrimSpace(helpText)
}
2017-08-18 15:09:52 +00:00
func (c *DeploymentListCommand) AutocompleteFlags() complete.Flags {
2017-08-23 21:56:21 +00:00
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-filter": complete.PredictAnything,
2017-08-23 21:56:21 +00:00
"-t": complete.PredictAnything,
"-verbose": complete.PredictNothing,
})
2017-08-18 15:09:52 +00:00
}
func (c *DeploymentListCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
2017-06-30 17:59:19 +00:00
func (c *DeploymentListCommand) Synopsis() string {
return "List all deployments"
}
func (c *DeploymentListCommand) Name() string { return "deployment list" }
2017-06-30 17:59:19 +00:00
func (c *DeploymentListCommand) Run(args []string) int {
2017-07-01 01:10:19 +00:00
var json, verbose bool
var filter, tmpl string
2017-06-30 17:59:19 +00:00
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
2017-06-30 17:59:19 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
2017-07-01 01:10:19 +00:00
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&filter, "filter", "", "")
2017-07-01 01:10:19 +00:00
flags.StringVar(&tmpl, "t", "", "")
2017-06-30 17:59:19 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
2017-06-30 17:59:19 +00:00
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
opts := &api.QueryOptions{
Filter: filter,
}
deploys, _, err := client.Deployments().List(opts)
2017-06-30 17:59:19 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
return 1
}
2017-07-01 01:10:19 +00:00
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, deploys)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
2017-06-30 17:59:19 +00:00
c.Ui.Output(formatDeployments(deploys, length))
return 0
}
func formatDeployments(deploys []*api.Deployment, uuidLength int) string {
if len(deploys) == 0 {
return "No deployments found"
}
rows := make([]string, len(deploys)+1)
rows[0] = "ID|Job ID|Job Version|Status|Description"
for i, d := range deploys {
rows[i+1] = fmt.Sprintf("%s|%s|%d|%s|%s",
limit(d.ID, uuidLength),
d.JobID,
d.JobVersion,
d.Status,
d.StatusDescription)
}
return formatList(rows)
}