forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_history.go
275 lines (225 loc) · 6.34 KB
/
job_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package command
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
"github.com/ryanuber/columnize"
)
type JobHistoryCommand struct {
Meta
formatter DataFormatter
}
func (c *JobHistoryCommand) Help() string {
helpText := `
Usage: nomad job history [options] <job>
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.
General Options:
` + generalOptionsUsage() + `
History Options:
-p
Display the difference between each job and its predecessor.
-full
Display the full job definition for each version.
-version <job version>
Display only the history for the given job version.
-json
Output the job versions in a JSON format.
-t
Format and display the job versions using a Go template.
`
return strings.TrimSpace(helpText)
}
func (c *JobHistoryCommand) Synopsis() string {
return "Display all tracked versions of a job"
}
func (c *JobHistoryCommand) Autocompleteflags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-p": complete.PredictNothing,
"-full": complete.PredictNothing,
"-version": complete.PredictAnything,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}
func (c *JobHistoryCommand) 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]
})
}
func (c *JobHistoryCommand) Run(args []string) int {
var json, diff, full bool
var tmpl, versionStr string
flags := c.Meta.FlagSet("job history", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&diff, "p", false, "")
flags.BoolVar(&full, "full", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&versionStr, "version", "", "")
flags.StringVar(&tmpl, "t", "", "")
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 {
c.Ui.Error(c.Help())
return 1
}
if (json || len(tmpl) != 0) && (diff || full) {
c.Ui.Error("-json and -t are exclusive with -p and -full")
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
}
if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
return 1
}
// Prefix lookup matched a single job
versions, diffs, _, err := client.Jobs().Versions(jobs[0].ID, diff, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
return 1
}
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
}
}
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
}
if err := c.formatJobVersion(job, diff, nextVersion, full); err != nil {
c.Ui.Error(err.Error())
return 1
}
} else {
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
}
if err := c.formatJobVersions(versions, diffs, full); err != nil {
c.Ui.Error(err.Error())
return 1
}
}
return 0
}
// 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 {
basic := []string{
fmt.Sprintf("Version|%d", *job.Version),
fmt.Sprintf("Stable|%v", *job.Stable),
fmt.Sprintf("Submit Date|%v", formatTime(time.Unix(0, *job.SubmitTime))),
}
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
}