2017-06-30 19:35:59 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-30 17:41:34 +00:00
|
|
|
"sort"
|
2017-06-30 19:35:59 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-22 20:05:24 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
2017-06-30 19:35:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DeploymentStatusCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeploymentStatusCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad deployment status [options] <deployment id>
|
|
|
|
|
|
|
|
Status is used to display the status of a deployment. The status will display
|
|
|
|
the number of desired changes as well as the currently applied changes.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Status Options:
|
|
|
|
|
|
|
|
-verbose
|
|
|
|
Display full information.
|
|
|
|
|
|
|
|
-json
|
2017-07-01 01:10:19 +00:00
|
|
|
Output the deployment in its JSON format.
|
2017-06-30 19:35:59 +00:00
|
|
|
|
|
|
|
-t
|
2017-07-01 01:10:19 +00:00
|
|
|
Format and display deployment using a Go template.
|
2017-06-30 19:35:59 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeploymentStatusCommand) Synopsis() string {
|
|
|
|
return "Display the status of a deployment"
|
|
|
|
}
|
|
|
|
|
2017-08-22 20:05:24 +00:00
|
|
|
func (c *DeploymentStatusCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
2017-08-22 20:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeploymentStatusCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
|
2017-08-22 20:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Deployments]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:35:59 +00:00
|
|
|
func (c *DeploymentStatusCommand) Run(args []string) int {
|
|
|
|
var json, verbose bool
|
|
|
|
var tmpl string
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet("deployment status", FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got no arguments
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 1 {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
dID := args[0]
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a prefix lookup
|
|
|
|
deploy, possible, err := getDeployment(client.Deployments(), dID)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(possible) != 0 {
|
2017-07-21 00:31:07 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
|
|
|
|
return 1
|
2017-06-30 19:35:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 01:10:19 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, deploy)
|
2017-06-30 19:35:59 +00:00
|
|
|
if err != nil {
|
2017-07-01 01:10:19 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2017-06-30 19:35:59 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(c.Colorize().Color(formatDeployment(deploy, length)))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeployment(client *api.Deployments, dID string) (match *api.Deployment, possible []*api.Deployment, err error) {
|
|
|
|
// First attempt an immediate lookup if we have a proper length
|
|
|
|
if len(dID) == 36 {
|
|
|
|
d, _, err := client.Info(dID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dID = strings.Replace(dID, "-", "", -1)
|
|
|
|
if len(dID) == 1 {
|
|
|
|
return nil, nil, fmt.Errorf("Identifier must contain at least two characters.")
|
|
|
|
}
|
|
|
|
if len(dID)%2 == 1 {
|
|
|
|
// Identifiers must be of even length, so we strip off the last byte
|
|
|
|
// to provide a consistent user experience.
|
|
|
|
dID = dID[:len(dID)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have to do a prefix lookup
|
|
|
|
deploys, _, err := client.PrefixList(dID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(deploys)
|
|
|
|
switch {
|
|
|
|
case l == 0:
|
|
|
|
return nil, nil, fmt.Errorf("Deployment ID %q matched no deployments", dID)
|
|
|
|
case l == 1:
|
|
|
|
return deploys[0], nil, nil
|
|
|
|
default:
|
|
|
|
return nil, deploys, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatDeployment(d *api.Deployment, uuidLength int) string {
|
|
|
|
// Format the high-level elements
|
|
|
|
high := []string{
|
|
|
|
fmt.Sprintf("ID|%s", limit(d.ID, uuidLength)),
|
2017-07-08 00:34:50 +00:00
|
|
|
fmt.Sprintf("Job ID|%s", d.JobID),
|
2017-06-30 19:35:59 +00:00
|
|
|
fmt.Sprintf("Job Version|%d", d.JobVersion),
|
|
|
|
fmt.Sprintf("Status|%s", d.Status),
|
|
|
|
fmt.Sprintf("Description|%s", d.StatusDescription),
|
|
|
|
}
|
|
|
|
|
|
|
|
base := formatKV(high)
|
|
|
|
if len(d.TaskGroups) == 0 {
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
base += "\n\n[bold]Deployed[reset]\n"
|
2017-07-07 05:44:05 +00:00
|
|
|
base += formatDeploymentGroups(d, uuidLength)
|
|
|
|
return base
|
|
|
|
}
|
2017-06-30 19:35:59 +00:00
|
|
|
|
2017-07-07 05:44:05 +00:00
|
|
|
func formatDeploymentGroups(d *api.Deployment, uuidLength int) string {
|
2017-06-30 19:35:59 +00:00
|
|
|
// Detect if we need to add these columns
|
|
|
|
canaries, autorevert := false, false
|
2017-08-30 17:41:34 +00:00
|
|
|
tgNames := make([]string, 0, len(d.TaskGroups))
|
|
|
|
for name, state := range d.TaskGroups {
|
|
|
|
tgNames = append(tgNames, name)
|
2017-06-30 19:35:59 +00:00
|
|
|
if state.AutoRevert {
|
|
|
|
autorevert = true
|
|
|
|
}
|
|
|
|
if state.DesiredCanaries > 0 {
|
|
|
|
canaries = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 17:41:34 +00:00
|
|
|
// Sort the task group names to get a reliable ordering
|
|
|
|
sort.Strings(tgNames)
|
|
|
|
|
2017-06-30 19:35:59 +00:00
|
|
|
// Build the row string
|
|
|
|
rowString := "Task Group|"
|
|
|
|
if autorevert {
|
|
|
|
rowString += "Auto Revert|"
|
|
|
|
}
|
2017-07-19 20:34:24 +00:00
|
|
|
if canaries {
|
|
|
|
rowString += "Promoted|"
|
|
|
|
}
|
2017-06-30 19:35:59 +00:00
|
|
|
rowString += "Desired|"
|
|
|
|
if canaries {
|
2017-07-19 20:34:24 +00:00
|
|
|
rowString += "Canaries|"
|
2017-06-30 19:35:59 +00:00
|
|
|
}
|
|
|
|
rowString += "Placed|Healthy|Unhealthy"
|
|
|
|
|
|
|
|
rows := make([]string, len(d.TaskGroups)+1)
|
|
|
|
rows[0] = rowString
|
|
|
|
i := 1
|
2017-08-30 17:41:34 +00:00
|
|
|
for _, tg := range tgNames {
|
|
|
|
state := d.TaskGroups[tg]
|
2017-06-30 19:35:59 +00:00
|
|
|
row := fmt.Sprintf("%s|", tg)
|
|
|
|
if autorevert {
|
|
|
|
row += fmt.Sprintf("%v|", state.AutoRevert)
|
|
|
|
}
|
2017-07-19 20:34:24 +00:00
|
|
|
if canaries {
|
2017-07-25 18:18:21 +00:00
|
|
|
if state.DesiredCanaries > 0 {
|
|
|
|
row += fmt.Sprintf("%v|", state.Promoted)
|
|
|
|
} else {
|
|
|
|
row += fmt.Sprintf("%v|", "N/A")
|
|
|
|
}
|
2017-07-19 20:34:24 +00:00
|
|
|
}
|
2017-06-30 19:35:59 +00:00
|
|
|
row += fmt.Sprintf("%d|", state.DesiredTotal)
|
|
|
|
if canaries {
|
|
|
|
row += fmt.Sprintf("%d|", state.DesiredCanaries)
|
|
|
|
}
|
|
|
|
row += fmt.Sprintf("%d|%d|%d", state.PlacedAllocs, state.HealthyAllocs, state.UnhealthyAllocs)
|
|
|
|
rows[i] = row
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:44:05 +00:00
|
|
|
return formatList(rows)
|
2017-06-30 19:35:59 +00:00
|
|
|
}
|