2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-30 01:42:37 +00:00
|
|
|
"strconv"
|
2017-06-30 00:16:20 +00:00
|
|
|
"strings"
|
2017-06-30 02:08:25 +00:00
|
|
|
"time"
|
2017-06-30 01:42:37 +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-07 02:29:47 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
2017-06-30 00:16:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type JobHistoryCommand struct {
|
|
|
|
Meta
|
2017-06-30 01:42:37 +00:00
|
|
|
formatter DataFormatter
|
2017-06-30 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobHistoryCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad job history [options] <job>
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
History is used to display the known versions of a particular job. The command
|
|
|
|
can display the diff between job versions and can be useful for understanding
|
|
|
|
the changes that occurred to the job as well as deciding job versions to revert
|
|
|
|
to.
|
2017-06-30 00:16:20 +00:00
|
|
|
|
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.
|
2020-11-19 21:38:08 +00:00
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2017-06-30 00:16:20 +00:00
|
|
|
|
|
|
|
History Options:
|
|
|
|
|
|
|
|
-p
|
|
|
|
Display the difference between each job and its predecessor.
|
2017-08-18 15:09:52 +00:00
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
-full
|
|
|
|
Display the full job definition for each version.
|
|
|
|
|
2017-07-17 18:04:07 +00:00
|
|
|
-version <job version>
|
2017-06-30 00:16:20 +00:00
|
|
|
Display only the history for the given job version.
|
2017-07-01 01:10:19 +00:00
|
|
|
|
|
|
|
-json
|
|
|
|
Output the job versions in a JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display the job versions using a Go template.
|
2017-06-30 00:16:20 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobHistoryCommand) Synopsis() string {
|
|
|
|
return "Display all tracked versions of a job"
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:19:16 +00:00
|
|
|
func (c *JobHistoryCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-p": complete.PredictNothing,
|
|
|
|
"-full": complete.PredictNothing,
|
|
|
|
"-version": complete.PredictAnything,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
2017-08-22 20:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobHistoryCommand) 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 *JobHistoryCommand) Name() string { return "job history" }
|
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
func (c *JobHistoryCommand) Run(args []string) int {
|
2017-07-01 01:10:19 +00:00
|
|
|
var json, diff, full bool
|
|
|
|
var tmpl, versionStr string
|
2017-06-30 00:16:20 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-06-30 00:16:20 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&diff, "p", false, "")
|
|
|
|
flags.BoolVar(&full, "full", false, "")
|
2017-07-01 01:10:19 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
2017-07-17 18:04:07 +00:00
|
|
|
flags.StringVar(&versionStr, "version", "", "")
|
2017-07-01 01:10:19 +00:00
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2017-06-30 00:16:20 +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 || l > 2 {
|
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-06-30 00:16:20 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-01 01:10:19 +00:00
|
|
|
if (json || len(tmpl) != 0) && (diff || full) {
|
|
|
|
c.Ui.Error("-json and -t are exclusive with -p and -full")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
// 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)
|
2017-06-30 01:42:37 +00:00
|
|
|
if err != nil {
|
2023-03-03 19:43:20 +00:00
|
|
|
c.Ui.Error(err.Error())
|
2017-06-30 01:42:37 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-03-03 19:43:20 +00:00
|
|
|
q := &api.QueryOptions{Namespace: namespace}
|
2020-06-17 19:39:50 +00:00
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
// Prefix lookup matched a single job
|
2023-03-03 19:43:20 +00:00
|
|
|
versions, diffs, _, err := client.Jobs().Versions(jobID, diff, q)
|
2017-06-30 00:16:20 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
f, err := DataFormat("json", "")
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.formatter = f
|
|
|
|
|
|
|
|
if versionStr != "" {
|
|
|
|
version, _, err := parseVersion(versionStr)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing version value %q: %v", versionStr, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var job *api.Job
|
|
|
|
var diff *api.JobDiff
|
|
|
|
var nextVersion uint64
|
|
|
|
for i, v := range versions {
|
|
|
|
if *v.Version != version {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
job = v
|
|
|
|
if i+1 <= len(diffs) {
|
|
|
|
diff = diffs[i]
|
|
|
|
nextVersion = *versions[i+1].Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 01:10:19 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, job)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
if err := c.formatJobVersion(job, diff, nextVersion, full); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-07-01 01:10:19 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, versions)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
if err := c.formatJobVersions(versions, diffs, full); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
return 0
|
|
|
|
}
|
2017-06-30 01:42:37 +00:00
|
|
|
|
|
|
|
// parseVersion parses the version flag and returns the index, whether it
|
|
|
|
// was set and potentially an error during parsing.
|
|
|
|
func parseVersion(input string) (uint64, bool, error) {
|
|
|
|
if input == "" {
|
|
|
|
return 0, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := strconv.ParseUint(input, 10, 64)
|
|
|
|
return u, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobHistoryCommand) formatJobVersions(versions []*api.Job, diffs []*api.JobDiff, full bool) error {
|
|
|
|
vLen := len(versions)
|
|
|
|
dLen := len(diffs)
|
|
|
|
if dLen != 0 && vLen != dLen+1 {
|
|
|
|
return fmt.Errorf("Number of job versions %d doesn't match number of diffs %d", vLen, dLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, version := range versions {
|
|
|
|
var diff *api.JobDiff
|
|
|
|
var nextVersion uint64
|
|
|
|
if i+1 <= dLen {
|
|
|
|
diff = diffs[i]
|
|
|
|
nextVersion = *versions[i+1].Version
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.formatJobVersion(version, diff, nextVersion, full); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a blank
|
|
|
|
if i != vLen-1 {
|
|
|
|
c.Ui.Output("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobHistoryCommand) formatJobVersion(job *api.Job, diff *api.JobDiff, nextVersion uint64, full bool) error {
|
2018-08-10 18:20:57 +00:00
|
|
|
if job == nil {
|
2018-08-14 18:06:04 +00:00
|
|
|
return fmt.Errorf("Error printing job history for non-existing job or job version")
|
2018-08-10 18:20:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 01:42:37 +00:00
|
|
|
basic := []string{
|
|
|
|
fmt.Sprintf("Version|%d", *job.Version),
|
|
|
|
fmt.Sprintf("Stable|%v", *job.Stable),
|
2017-06-30 02:08:25 +00:00
|
|
|
fmt.Sprintf("Submit Date|%v", formatTime(time.Unix(0, *job.SubmitTime))),
|
2017-06-30 01:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff != nil {
|
|
|
|
//diffStr := fmt.Sprintf("Difference between version %d and %d:", *job.Version, nextVersion)
|
|
|
|
basic = append(basic, fmt.Sprintf("Diff|\n%s", strings.TrimSpace(formatJobDiff(diff, false))))
|
|
|
|
}
|
|
|
|
|
|
|
|
if full {
|
|
|
|
out, err := c.formatter.TransformData(job)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error formatting the data: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
basic = append(basic, fmt.Sprintf("Full|JSON Job:\n%s", out))
|
|
|
|
}
|
|
|
|
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
columnConf.Glue = " = "
|
|
|
|
columnConf.NoTrim = true
|
|
|
|
output := columnize.Format(basic, columnConf)
|
|
|
|
|
|
|
|
c.Ui.Output(c.Colorize().Color(output))
|
|
|
|
return nil
|
|
|
|
}
|