2021-10-12 20:30:36 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type JobAllocsCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobAllocsCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad job allocs [options] <job>
|
|
|
|
|
|
|
|
Display allocations for a particular job.
|
|
|
|
|
2023-03-09 20:00:04 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'read-job'
|
|
|
|
capability for the job's namespace. The 'list-jobs' capability is required to
|
|
|
|
run the command with a job prefix instead of the exact job ID.
|
2021-10-12 20:30:36 +00:00
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
|
|
|
|
|
|
|
Allocs Options:
|
|
|
|
|
|
|
|
-all
|
|
|
|
Display all allocations matching the job ID, even those from an older
|
|
|
|
instance of the job.
|
|
|
|
|
|
|
|
-json
|
|
|
|
Output the allocations in a JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display allocations using a Go template.
|
|
|
|
|
|
|
|
-verbose
|
|
|
|
Display full information.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobAllocsCommand) Synopsis() string {
|
|
|
|
return "List allocations for a job"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobAllocsCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-all": complete.PredictNothing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobAllocsCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Jobs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:44:59 +00:00
|
|
|
func (c *JobAllocsCommand) Name() string { return "job allocs" }
|
2021-10-12 20:30:36 +00:00
|
|
|
|
|
|
|
func (c *JobAllocsCommand) Run(args []string) int {
|
|
|
|
var json, verbose, all bool
|
|
|
|
var tmpl string
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
flags.BoolVar(&all, "all", false, "")
|
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one job
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error("This command takes one argument: <job>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the job exists
|
2023-03-03 19:43:20 +00:00
|
|
|
jobIDPrefix := strings.TrimSpace(args[0])
|
|
|
|
jobID, namespace, err := c.JobIDByPrefix(client, jobIDPrefix, nil)
|
2021-10-12 20:30:36 +00:00
|
|
|
if err != nil {
|
2023-03-03 19:43:20 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2021-10-12 20:30:36 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-03-03 19:43:20 +00:00
|
|
|
q := &api.QueryOptions{Namespace: namespace}
|
2021-10-12 20:30:36 +00:00
|
|
|
|
|
|
|
allocs, _, err := client.Jobs().Allocations(jobID, all, q)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving allocations: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, allocs)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatAllocListStubs(allocs, verbose, length))
|
|
|
|
return 0
|
|
|
|
}
|