2017-07-01 00:37:12 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-08-22 20:39:06 +00:00
|
|
|
|
2020-06-17 19:39:50 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-22 20:39:06 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
2017-07-01 00:37:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type JobDeploymentsCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobDeploymentsCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad job deployments [options] <job>
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Deployments is used to display the deployments for a particular job.
|
2017-07-01 00:37:12 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'read-job' and
|
|
|
|
'list-jobs' capabilities for the job's namespace.
|
|
|
|
|
2017-07-01 00:37:12 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2017-07-01 00:37:12 +00:00
|
|
|
|
|
|
|
Deployments Options:
|
|
|
|
|
2017-07-01 01:10:19 +00:00
|
|
|
-json
|
|
|
|
Output the deployments in a JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display deployments using a Go template.
|
|
|
|
|
2017-07-01 00:37:12 +00:00
|
|
|
-latest
|
|
|
|
Display the latest deployment only.
|
|
|
|
|
|
|
|
-verbose
|
|
|
|
Display full information.
|
2019-05-16 02:16:57 +00:00
|
|
|
|
2019-09-20 17:24:53 +00:00
|
|
|
-all
|
2020-11-19 16:15:23 +00:00
|
|
|
Display all deployments matching the job ID, including those
|
2019-05-16 02:16:57 +00:00
|
|
|
from an older instance of the job.
|
2017-07-01 00:37:12 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobDeploymentsCommand) Synopsis() string {
|
|
|
|
return "List deployments for a job"
|
|
|
|
}
|
|
|
|
|
2017-08-22 20:39:06 +00:00
|
|
|
func (c *JobDeploymentsCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
"-latest": complete.PredictNothing,
|
|
|
|
"-verbose": complete.PredictNothing,
|
2019-05-16 02:16:57 +00:00
|
|
|
"-all": complete.PredictNothing,
|
2017-08-23 21:56:21 +00:00
|
|
|
})
|
2017-08-22 20:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobDeploymentsCommand) 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.Jobs, nil)
|
2017-08-22 20:39:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Jobs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobDeploymentsCommand) Name() string { return "job deployments" }
|
|
|
|
|
2017-07-01 00:37:12 +00:00
|
|
|
func (c *JobDeploymentsCommand) Run(args []string) int {
|
2019-05-10 22:15:27 +00:00
|
|
|
var json, latest, verbose, all bool
|
2017-07-01 01:10:19 +00:00
|
|
|
var tmpl string
|
2017-07-01 00:37:12 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-07-01 00:37:12 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&latest, "latest", false, "")
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2019-05-10 22:15:27 +00:00
|
|
|
flags.BoolVar(&all, "all", false, "")
|
2017-07-01 01:10:19 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2017-07-01 00:37:12 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one node
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <job>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-07-01 00:37:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
jobID := args[0]
|
|
|
|
|
|
|
|
// Check if the job exists
|
|
|
|
jobs, _, err := client.Jobs().PrefixList(jobID)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error listing jobs: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(jobs) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
|
|
|
|
return 1
|
|
|
|
}
|
2020-06-17 19:55:24 +00:00
|
|
|
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
|
2020-06-17 19:39:50 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
|
2017-07-21 00:31:07 +00:00
|
|
|
return 1
|
2017-07-01 00:37:12 +00:00
|
|
|
}
|
|
|
|
jobID = jobs[0].ID
|
2020-06-17 19:39:50 +00:00
|
|
|
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}
|
2017-07-01 00:37:12 +00:00
|
|
|
|
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
|
|
|
|
if latest {
|
2020-06-17 19:39:50 +00:00
|
|
|
deploy, _, err := client.Jobs().LatestDeployment(jobID, q)
|
2017-07-01 00:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:44 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, deploy)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:05:31 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color(formatDeployment(client, deploy, length)))
|
2017-07-01 00:37:12 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:39:50 +00:00
|
|
|
deploys, _, err := client.Jobs().Deployments(jobID, all, q)
|
2017-07-01 00:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:44 +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-07-01 00:37:12 +00:00
|
|
|
c.Ui.Output(formatDeployments(deploys, length))
|
|
|
|
return 0
|
|
|
|
}
|