2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-26 00:06:20 +00:00
|
|
|
"sort"
|
2016-05-25 21:11:14 +00:00
|
|
|
"strings"
|
2019-08-07 16:50:35 +00:00
|
|
|
"time"
|
2016-05-25 21:11:14 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-14 14:24:35 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
2016-05-25 21:11:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EvalStatusCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EvalStatusCommand) Help() string {
|
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad eval status [options] <evaluation>
|
2016-05-25 21:11:14 +00:00
|
|
|
|
|
|
|
Display information about evaluations. This command can be used to inspect the
|
|
|
|
current status of an evaluation as well as determine the reason an evaluation
|
|
|
|
did not place all allocations.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2016-05-25 21:11:14 +00:00
|
|
|
|
|
|
|
Eval Status Options:
|
|
|
|
|
|
|
|
-monitor
|
|
|
|
Monitor an outstanding evaluation
|
|
|
|
|
|
|
|
-verbose
|
|
|
|
Show full information.
|
2016-07-30 10:20:43 +00:00
|
|
|
|
2016-08-04 10:19:31 +00:00
|
|
|
-json
|
2016-08-06 09:54:30 +00:00
|
|
|
Output the evaluation in its JSON format.
|
2016-07-30 10:20:43 +00:00
|
|
|
|
|
|
|
-t
|
2016-08-06 09:54:30 +00:00
|
|
|
Format and display evaluation using a Go template.
|
2016-05-25 21:11:14 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EvalStatusCommand) Synopsis() string {
|
2016-05-26 01:52:14 +00:00
|
|
|
return "Display evaluation status and placement failure reasons"
|
2016-05-25 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 20:41:42 +00:00
|
|
|
func (c *EvalStatusCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-monitor": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
})
|
2017-08-22 20:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EvalStatusCommand) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Evals, nil)
|
2017-08-22 20:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Evals]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *EvalStatusCommand) Name() string { return "eval status" }
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
func (c *EvalStatusCommand) Run(args []string) int {
|
2016-08-04 10:19:31 +00:00
|
|
|
var monitor, verbose, json bool
|
|
|
|
var tmpl string
|
2016-05-25 21:11:14 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2016-05-25 21:11:14 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&monitor, "monitor", false, "")
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2016-08-04 10:19:31 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
2016-07-30 10:20:43 +00:00
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2016-05-25 21:11:14 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one evaluation ID
|
|
|
|
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 len(args) != 1 {
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error("This command takes one argument")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
2016-08-06 11:30:12 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
evalID := args[0]
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the allocation info
|
|
|
|
if len(evalID) == 1 {
|
2020-12-09 19:05:18 +00:00
|
|
|
c.Ui.Error("Identifier must contain at least two characters.")
|
2016-05-25 21:11:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:56:32 +00:00
|
|
|
if json && len(tmpl) > 0 {
|
|
|
|
c.Ui.Error("Both json and template formatting are not allowed")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:52:59 +00:00
|
|
|
evalID = sanitizeUUIDPrefix(evalID)
|
2016-05-25 21:11:14 +00:00
|
|
|
evals, _, err := client.Evaluations().PrefixList(evalID)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying evaluation: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(evals) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No evaluation(s) with prefix or id %q found", evalID))
|
|
|
|
return 1
|
|
|
|
}
|
2016-08-06 11:30:12 +00:00
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
if len(evals) > 1 {
|
2022-07-07 17:13:34 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple evaluations\n\n%s", formatEvalList(evals, verbose)))
|
2017-07-21 00:31:07 +00:00
|
|
|
return 1
|
2016-05-25 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are in monitor mode, monitor and exit
|
|
|
|
if monitor {
|
|
|
|
mon := newMonitor(c.Ui, client, length)
|
2020-11-02 18:24:49 +00:00
|
|
|
return mon.monitor(evals[0].ID)
|
2016-05-25 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix lookup matched a single evaluation
|
|
|
|
eval, _, err := client.Evaluations().Info(evals[0].ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying evaluation: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-07-30 10:20:43 +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, eval)
|
2016-07-30 10:20:43 +00:00
|
|
|
if err != nil {
|
2017-07-01 01:10:19 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2016-07-30 10:20:43 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-06-10 18:02:15 +00:00
|
|
|
failureString, failures := evalFailureStatus(eval)
|
2016-05-25 21:11:14 +00:00
|
|
|
triggerNoun, triggerSubj := getTriggerDetails(eval)
|
|
|
|
statusDesc := eval.StatusDescription
|
|
|
|
if statusDesc == "" {
|
|
|
|
statusDesc = eval.Status
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:50:35 +00:00
|
|
|
// Format eval timestamps
|
|
|
|
var formattedCreateTime, formattedModifyTime string
|
|
|
|
if verbose {
|
|
|
|
formattedCreateTime = formatUnixNanoTime(eval.CreateTime)
|
|
|
|
formattedModifyTime = formatUnixNanoTime(eval.ModifyTime)
|
|
|
|
} else {
|
|
|
|
formattedCreateTime = prettyTimeDiff(time.Unix(0, eval.CreateTime), time.Now())
|
|
|
|
formattedModifyTime = prettyTimeDiff(time.Unix(0, eval.ModifyTime), time.Now())
|
|
|
|
}
|
|
|
|
|
2016-05-27 00:37:08 +00:00
|
|
|
// Format the evaluation data
|
2016-05-25 21:11:14 +00:00
|
|
|
basic := []string{
|
|
|
|
fmt.Sprintf("ID|%s", limit(eval.ID, length)),
|
2019-08-07 16:50:35 +00:00
|
|
|
fmt.Sprintf("Create Time|%s", formattedCreateTime),
|
|
|
|
fmt.Sprintf("Modify Time|%s", formattedModifyTime),
|
2016-05-25 21:11:14 +00:00
|
|
|
fmt.Sprintf("Status|%s", eval.Status),
|
|
|
|
fmt.Sprintf("Status Description|%s", statusDesc),
|
|
|
|
fmt.Sprintf("Type|%s", eval.Type),
|
|
|
|
fmt.Sprintf("TriggeredBy|%s", eval.TriggeredBy),
|
2022-07-07 17:13:34 +00:00
|
|
|
fmt.Sprintf("Job ID|%s", eval.JobID),
|
|
|
|
fmt.Sprintf("Namespace|%s", eval.Namespace),
|
2016-05-25 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 00:16:43 +00:00
|
|
|
if triggerNoun != "" && triggerSubj != "" {
|
|
|
|
basic = append(basic, fmt.Sprintf("%s|%s", triggerNoun, triggerSubj))
|
|
|
|
}
|
|
|
|
|
|
|
|
basic = append(basic,
|
|
|
|
fmt.Sprintf("Priority|%d", eval.Priority),
|
|
|
|
fmt.Sprintf("Placement Failures|%s", failureString))
|
|
|
|
|
2018-04-11 19:01:42 +00:00
|
|
|
if !eval.WaitUntil.IsZero() {
|
|
|
|
basic = append(basic,
|
|
|
|
fmt.Sprintf("Wait Until|%s", formatTime(eval.WaitUntil)))
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
if verbose {
|
|
|
|
// NextEval, PreviousEval, BlockedEval
|
|
|
|
basic = append(basic,
|
|
|
|
fmt.Sprintf("Previous Eval|%s", eval.PreviousEval),
|
|
|
|
fmt.Sprintf("Next Eval|%s", eval.NextEval),
|
|
|
|
fmt.Sprintf("Blocked Eval|%s", eval.BlockedEval))
|
|
|
|
}
|
|
|
|
c.Ui.Output(formatKV(basic))
|
|
|
|
|
|
|
|
if failures {
|
2016-06-12 20:16:07 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Failed Placements[reset]"))
|
2016-05-26 00:06:20 +00:00
|
|
|
sorted := sortedTaskGroupFromMetrics(eval.FailedTGAllocs)
|
|
|
|
for _, tg := range sorted {
|
|
|
|
metrics := eval.FailedTGAllocs[tg]
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
noun := "allocation"
|
|
|
|
if metrics.CoalescedFailures > 0 {
|
|
|
|
noun += "s"
|
|
|
|
}
|
|
|
|
c.Ui.Output(fmt.Sprintf("Task Group %q (failed to place %d %s):", tg, metrics.CoalescedFailures+1, noun))
|
2016-05-31 21:51:23 +00:00
|
|
|
c.Ui.Output(formatAllocMetrics(metrics, false, " "))
|
2016-05-26 00:06:20 +00:00
|
|
|
c.Ui.Output("")
|
2016-05-25 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if eval.BlockedEval != "" {
|
2016-05-27 00:37:08 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Evaluation %q waiting for additional capacity to place remainder",
|
2016-05-25 21:11:14 +00:00
|
|
|
limit(eval.BlockedEval, length)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
func sortedTaskGroupFromMetrics(groups map[string]*api.AllocationMetric) []string {
|
|
|
|
tgs := make([]string, 0, len(groups))
|
2017-09-26 22:26:33 +00:00
|
|
|
for tg := range groups {
|
2016-05-26 00:06:20 +00:00
|
|
|
tgs = append(tgs, tg)
|
|
|
|
}
|
|
|
|
sort.Strings(tgs)
|
|
|
|
return tgs
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
func getTriggerDetails(eval *api.Evaluation) (noun, subject string) {
|
|
|
|
switch eval.TriggeredBy {
|
|
|
|
case "node-update":
|
|
|
|
return "Node ID", eval.NodeID
|
|
|
|
case "max-plan-attempts":
|
|
|
|
return "Previous Eval", eval.PreviousEval
|
|
|
|
default:
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
}
|