/
jobs.go
653 lines (572 loc) · 17.2 KB
/
jobs.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright 2023 tsuru-client authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"text/template"
"time"
"github.com/antihax/optional"
"github.com/mattn/go-shellwords"
"github.com/tsuru/gnuflag"
"github.com/tsuru/go-tsuruclient/pkg/tsuru"
"github.com/tsuru/tablecli"
"github.com/tsuru/tsuru-client/tsuru/formatter"
tsuruHTTP "github.com/tsuru/tsuru-client/tsuru/http"
"github.com/tsuru/tsuru/cmd"
)
type JobCreate struct {
schedule string
teamOwner string
plan string
pool string
description string
manual bool
maxRunningTime int64
tags cmd.StringSliceFlag
fs *gnuflag.FlagSet
}
func (c *JobCreate) Info() *cmd.Info {
return &cmd.Info{
Name: "job-create",
Usage: "job create <jobname> <image> \"<commands>\" [--plan/-p plan name] [--schedule/-s schedule name] [--team/-t team owner] [--pool/-o pool name] [--description/-d description] [--tag/-g tag] [--max-running-time/-m seconds] [--manual bool]...",
Desc: `Creates a new job using the given name and platform
In order to create an job, you need to be member of at least one team. All
teams that you are member (see [[tsuru team-list]]) will be able to access the
job
The [[--plan]] parameter defines the plan to be used. The plan specifies how
computational resources are allocated to your job execution. Typically this
means limits for cpu and memory usage is allocated.
The list of available plans can be found running [[tsuru plan list]]
If this parameter is not informed, tsuru will choose the plan with the
[[default]] flag set to true
The [[--schedule]] parameter defines how often this job will be executed. This string follows the unix-cron format,
if you need to test the cron expressions visit the site: https://crontab.guru/
The [[--team]] parameter describes which team is responsible for the created
app, this is only needed if the current user belongs to more than one team, in
which case this parameter will be mandatory
The [[--pool]] parameter defines which pool your app will be deployed.
This is only needed if you have more than one pool associated with your teams
The [[--description]] parameter sets a description for your job.
It is an optional parameter, and if its not set the job will only not have a
description associated
The [[--manual]] parameter sets your job as a manual job.
A manual job is only run when explicitly triggered by the user i.e: tsuru job trigger <job-name>
The [[--tag]] parameter sets a tag to your job. You can set multiple [[--tag]] parameters
The [[--max-running-time]] sets maximum amount of time (in seconds) that the job
can run. If the job exceeds this limit, it will be automatically stopped. If
this parameter is not informed, default value is 3600s`,
MinArgs: 2,
}
}
func (c *JobCreate) Flags() *gnuflag.FlagSet {
if c.fs == nil {
infoMessage := "The plan used to create the job"
c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
c.fs.StringVar(&c.plan, "plan", "", infoMessage)
c.fs.StringVar(&c.plan, "p", "", infoMessage)
schedule := "Schedule string"
c.fs.StringVar(&c.schedule, "schedule", "", schedule)
c.fs.StringVar(&c.schedule, "s", "", schedule)
teamMessage := "Team owner job"
c.fs.StringVar(&c.teamOwner, "team", "", teamMessage)
c.fs.StringVar(&c.teamOwner, "t", "", teamMessage)
poolMessage := "Pool to deploy your job"
c.fs.StringVar(&c.pool, "pool", "", poolMessage)
c.fs.StringVar(&c.pool, "o", "", poolMessage)
descriptionMessage := "Job description"
c.fs.StringVar(&c.description, "description", "", descriptionMessage)
c.fs.StringVar(&c.description, "d", "", descriptionMessage)
tagMessage := "Job tag"
c.fs.Var(&c.tags, "tag", tagMessage)
c.fs.Var(&c.tags, "g", tagMessage)
manualMessage := "Manual job"
c.fs.BoolVar(&c.manual, "manual", false, manualMessage)
maxRunningTime := "Maximum running time in seconds for the job"
c.fs.Int64Var(&c.maxRunningTime, "max-running-time", 0, maxRunningTime)
c.fs.Int64Var(&c.maxRunningTime, "m", 0, maxRunningTime)
}
return c.fs
}
func parseJobCommands(commands []string) ([]string, error) {
if len(commands) != 1 {
return commands, nil
}
quotedCommands := commands[0]
jsonCommands := []string{}
if err := json.Unmarshal([]byte(quotedCommands), &jsonCommands); err == nil {
return jsonCommands, nil
}
shellCommands, err := shellwords.Parse(quotedCommands)
if err != nil {
return nil, err
}
return shellCommands, nil
}
func (c *JobCreate) Run(ctx *cmd.Context) error {
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
if !c.manual && c.schedule == "" {
return errors.New("schedule or manual option must be set")
}
if c.manual && c.schedule != "" {
return errors.New("cannot set both manual job and schedule options")
}
jobName := ctx.Args[0]
image := ctx.Args[1]
commands := ctx.Args[2:]
parsedCommands, err := parseJobCommands(commands)
if err != nil {
return err
}
var activeDeadlineSecondsResult *int64
if c.fs != nil {
c.fs.Visit(func(f *gnuflag.Flag) {
if (f.Name == "max-running-time" || f.Name == "m") && c.maxRunningTime == 0 {
activeDeadlineSecondsResult = &c.maxRunningTime
}
})
}
if c.maxRunningTime > 0 {
activeDeadlineSecondsResult = &c.maxRunningTime
}
j := tsuru.InputJob{
Name: jobName,
Tags: c.tags,
Schedule: c.schedule,
Plan: c.plan,
Pool: c.pool,
Description: c.description,
TeamOwner: c.teamOwner,
Manual: c.manual,
ActiveDeadlineSeconds: activeDeadlineSecondsResult,
Container: tsuru.InputJobContainer{
Image: image,
Command: parsedCommands,
},
}
if _, err := apiClient.JobApi.CreateJob(context.Background(), j); err != nil {
return err
}
fmt.Fprintf(ctx.Stdout, "Job created\nUse \"tsuru job info %s\" to check the status of the job\n", jobName)
return nil
}
type JobInfo struct {
fs *gnuflag.FlagSet
json bool
}
func (c *JobInfo) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("job-info", gnuflag.ContinueOnError)
c.fs.BoolVar(&c.json, "json", false, "Show JSON")
}
return c.fs
}
func (c *JobInfo) Info() *cmd.Info {
return &cmd.Info{
Name: "job-info",
Usage: "job info <job>",
Desc: "Retrieve useful information from a job",
MinArgs: 1,
MaxArgs: 1,
}
}
const jobInfoFormat = `Job: {{.Job.Name}}
{{- with .Job.Description }}
Description: {{.}}
{{- end }}
Teams: {{.Teams}}
Created by: {{.Job.Owner}}
Pool: {{.Job.Pool}}
Plan: {{.Job.Plan.Name}}
{{- if and .Job.Spec.Schedule (not .Job.Spec.Manual) }}
Schedule: {{.Job.Spec.Schedule}}
{{- end }}
Image: {{.Job.Spec.Container.Image}}
Command: {{.Job.Spec.Container.Command}}
{{- if .Job.Spec.ActiveDeadlineSeconds }}
Max Running Time: {{.Job.Spec.ActiveDeadlineSeconds}}s
{{- end }}
{{- if .Job.Spec.ConcurrencyPolicy }}
Concurrency Policy: {{.Job.Spec.ConcurrencyPolicy}}
{{- end }}`
func (c *JobInfo) Run(ctx *cmd.Context) error {
jobName := ctx.Args[0]
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
jobInfo, _, err := apiClient.JobApi.GetJob(context.Background(), jobName)
if err != nil {
return err
}
if c.json {
return formatter.JSON(ctx.Stdout, jobInfo)
}
var buf bytes.Buffer
tmpl := template.Must(template.New("job").Parse(jobInfoFormat))
teams := renderTeams(jobInfo.Job)
err = tmpl.Execute(&buf, struct {
Job tsuru.Job
Teams string
}{jobInfo.Job, teams})
if err != nil {
return err
}
renderJobUnits(&buf, jobInfo.Units)
renderServiceInstanceBinds(&buf, jobInfo.ServiceInstanceBinds)
fmt.Fprintln(ctx.Stdout, buf.String())
return nil
}
func renderTeams(job tsuru.Job) string {
teams := []string{}
if job.TeamOwner != "" {
teams = append(teams, job.TeamOwner+" (owner)")
}
for _, t := range job.Teams {
if t != job.TeamOwner {
teams = append(teams, t)
}
}
return strings.Join(teams, ", ")
}
func renderJobUnits(buf *bytes.Buffer, units []tsuru.Unit) {
titles := []string{"Name", "Status", "Restarts", "Age"}
unitsTable := tablecli.NewTable()
tablecli.TableConfig.ForceWrap = false
unitsTable.Headers = tablecli.Row(titles)
for _, unit := range units {
row := tablecli.Row{
unit.Name,
jobUnitReadyAndStatus(unit),
countValue(unit.Restarts),
jobAge(unit.CreatedAt),
}
unitsTable.AddRow(row)
}
if unitsTable.Rows() > 0 {
unitsTable.SortByColumn(2)
buf.WriteString("\n")
buf.WriteString(fmt.Sprintf("Units: %d\n", unitsTable.Rows()))
buf.WriteString(unitsTable.String())
}
}
func jobUnitReadyAndStatus(u tsuru.Unit) string {
if u.Ready != nil && *u.Ready {
return "ready"
}
return u.Status
}
func jobAge(createdAt string) string {
t, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
return ""
}
return translateTimestampSince(&t)
}
type jobFilter struct {
name string
pool string
plan string
teamOwner string
}
type JobList struct {
fs *gnuflag.FlagSet
filter jobFilter
json bool
simplified bool
}
func (c *JobList) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("job-list", gnuflag.ContinueOnError)
c.fs.StringVar(&c.filter.name, "name", "", "Filter jobs by name")
c.fs.StringVar(&c.filter.name, "n", "", "Filter jobs by name")
c.fs.StringVar(&c.filter.pool, "pool", "", "Filter jobs by pool")
c.fs.StringVar(&c.filter.pool, "o", "", "Filter jobs by pool")
c.fs.StringVar(&c.filter.plan, "plan", "", "Filter jobs by plan")
c.fs.StringVar(&c.filter.plan, "p", "", "Filter jobs by plan")
c.fs.StringVar(&c.filter.teamOwner, "team", "", "Filter jobs by team owner")
c.fs.StringVar(&c.filter.teamOwner, "t", "", "Filter jobs by team owner")
c.fs.BoolVar(&c.simplified, "q", false, "Display only jobs name")
c.fs.BoolVar(&c.json, "json", false, "Show JSON")
}
return c.fs
}
func (c *JobList) Info() *cmd.Info {
return &cmd.Info{
Name: "job-list",
Usage: "job list",
Desc: "List jobs",
MinArgs: 0,
MaxArgs: 0,
}
}
func (c *JobList) Run(ctx *cmd.Context) error {
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
jobs, resp, err := apiClient.JobApi.ListJob(context.Background())
if resp != nil && resp.StatusCode == http.StatusNoContent {
fmt.Fprint(ctx.Stdout, "No jobs found\n")
return nil
}
if err != nil {
return err
}
jobs = c.clientSideFilter(jobs)
if c.json {
return formatter.JSON(ctx.Stdout, jobs)
}
if c.simplified {
for _, j := range jobs {
fmt.Fprintln(ctx.Stdout, j.Name)
}
return nil
}
tbl := tablecli.NewTable()
tbl.Headers = tablecli.Row{"Name", "Schedule", "Image", "Command"}
tbl.LineSeparator = true
for _, j := range jobs {
schedule := j.Spec.Schedule
if j.Spec.Manual {
schedule = "manual"
}
tbl.AddRow(tablecli.Row{
j.Name,
schedule,
j.Spec.Container.Image,
strings.Join(j.Spec.Container.Command, " "),
})
}
tbl.Sort()
fmt.Fprint(ctx.Stdout, tbl.String())
return nil
}
func (c *JobList) clientSideFilter(jobs []tsuru.Job) []tsuru.Job {
result := make([]tsuru.Job, 0, len(jobs))
for _, j := range jobs {
insert := true
if c.filter.name != "" && !strings.Contains(j.Name, c.filter.name) {
insert = false
}
if c.filter.pool != "" && j.Pool != c.filter.pool {
insert = false
}
if c.filter.plan != "" && j.Plan.Name != c.filter.plan {
insert = false
}
if c.filter.teamOwner != "" && j.TeamOwner != c.filter.teamOwner {
insert = false
}
if insert {
result = append(result, j)
}
}
return result
}
type JobDelete struct{}
func (c *JobDelete) Info() *cmd.Info {
return &cmd.Info{
Name: "job-delete",
Usage: "job delete <job-name>",
Desc: "Delete an existing job",
MinArgs: 1,
MaxArgs: 1,
}
}
func (c *JobDelete) Run(ctx *cmd.Context) error {
jobName := ctx.Args[0]
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
_, err = apiClient.JobApi.DeleteJob(context.Background(), jobName)
if err != nil {
return err
}
fmt.Fprint(ctx.Stdout, "Job successfully deleted\n")
return nil
}
type JobTrigger struct{}
func (c *JobTrigger) Info() *cmd.Info {
return &cmd.Info{
Name: "job-trigger",
Usage: "job trigger <job-name>",
Desc: "Trigger an existing job",
MinArgs: 1,
MaxArgs: 1,
}
}
func (c *JobTrigger) Run(ctx *cmd.Context) error {
jobName := ctx.Args[0]
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
_, err = apiClient.JobApi.TriggerJob(context.Background(), jobName)
if err != nil {
return err
}
fmt.Fprint(ctx.Stdout, "Job successfully triggered\n")
return nil
}
type JobUpdate struct {
schedule string
teamOwner string
plan string
pool string
description string
image string
manual bool
maxRunningTime int64
tags cmd.StringSliceFlag
fs *gnuflag.FlagSet
}
func (c *JobUpdate) Info() *cmd.Info {
return &cmd.Info{
Name: "job-update",
Usage: "job update <job-name> [--image/-i <image>] [--plan/-p plan name] [--schedule/-s schedule name] [--manual] [--team/-t team owner] [--pool/-o pool name] [--description/-d description] [--max-running-time/-m seconds] [--tag/-g tag]... -- [commands]",
Desc: "Updates a job",
MinArgs: 1,
}
}
func (c *JobUpdate) Flags() *gnuflag.FlagSet {
if c.fs == nil {
infoMessage := "The plan used to create the job"
c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
c.fs.StringVar(&c.plan, "plan", "", infoMessage)
c.fs.StringVar(&c.plan, "p", "", infoMessage)
schedule := "Schedule string"
c.fs.StringVar(&c.schedule, "schedule", "", schedule)
c.fs.StringVar(&c.schedule, "s", "", schedule)
manualMessage := "Manual job"
c.fs.BoolVar(&c.manual, "manual", false, manualMessage)
teamMessage := "Team owner job"
c.fs.StringVar(&c.teamOwner, "team", "", teamMessage)
c.fs.StringVar(&c.teamOwner, "t", "", teamMessage)
poolMessage := "Pool to deploy your job"
c.fs.StringVar(&c.pool, "pool", "", poolMessage)
c.fs.StringVar(&c.pool, "o", "", poolMessage)
descriptionMessage := "Job description"
c.fs.StringVar(&c.description, "description", "", descriptionMessage)
c.fs.StringVar(&c.description, "d", "", descriptionMessage)
tagMessage := "Job tag"
c.fs.Var(&c.tags, "tag", tagMessage)
c.fs.Var(&c.tags, "g", tagMessage)
imageMessage := "New image for the job to run"
c.fs.StringVar(&c.image, "image", "", imageMessage)
c.fs.StringVar(&c.image, "i", "", imageMessage)
maxRunningTime := "Maximum running time in seconds for the job"
c.fs.Int64Var(&c.maxRunningTime, "max-running-time", 0, maxRunningTime)
c.fs.Int64Var(&c.maxRunningTime, "m", 0, maxRunningTime)
}
return c.fs
}
func (c *JobUpdate) Run(ctx *cmd.Context) error {
jobName := ctx.Args[0]
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
if c.manual && c.schedule != "" {
return errors.New("cannot set both manual job and schedule options")
}
var jobUpdateCommands []string
if len(ctx.Args) > 1 {
jobUpdateCommands, err = parseJobCommands(ctx.Args[1:])
if err != nil {
return err
}
}
var activeDeadlineSecondsResult *int64
if c.fs != nil {
c.fs.Visit(func(f *gnuflag.Flag) {
if (f.Name == "max-running-time" || f.Name == "m") && c.maxRunningTime == 0 {
activeDeadlineSecondsResult = &c.maxRunningTime
}
})
}
if c.maxRunningTime > 0 {
activeDeadlineSecondsResult = &c.maxRunningTime
}
j := tsuru.InputJob{
Name: jobName,
Tags: c.tags,
Schedule: c.schedule,
Manual: c.manual,
Plan: c.plan,
Pool: c.pool,
Description: c.description,
TeamOwner: c.teamOwner,
ActiveDeadlineSeconds: activeDeadlineSecondsResult,
Container: tsuru.InputJobContainer{
Image: c.image,
Command: jobUpdateCommands,
},
}
_, err = apiClient.JobApi.UpdateJob(context.Background(), jobName, j)
if err != nil {
return err
}
fmt.Fprintf(ctx.Stdout, "Job updated\nUse \"tsuru job info %s\" to check the status of the job\n", jobName)
return nil
}
type JobLog struct {
follow bool
fs *gnuflag.FlagSet
}
func (c *JobLog) Info() *cmd.Info {
return &cmd.Info{
Name: "job-log",
Usage: "job log <job-name>",
Desc: "Retrieve logs a job",
MinArgs: 1,
MaxArgs: 1,
}
}
func (c *JobLog) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("job-log", gnuflag.ExitOnError)
followMsg := "Follow logs"
c.fs.BoolVar(&c.follow, "f", false, followMsg)
c.fs.BoolVar(&c.follow, "follow", false, followMsg)
}
return c.fs
}
func (c *JobLog) Run(ctx *cmd.Context) error {
jobName := ctx.Args[0]
apiClient, err := tsuruHTTP.TsuruClientFromEnvironment()
if err != nil {
return err
}
resp, err := apiClient.JobApi.JobLog(context.Background(), jobName, &tsuru.JobLogOpts{
Follow: optional.NewBool(c.follow),
})
if err != nil {
return err
}
defer resp.Body.Close()
formatter := logFormatter{}
dec := json.NewDecoder(resp.Body)
ctx.RawOutput()
for {
err = formatter.Format(ctx.Stdout, dec)
if err != nil {
if err != io.EOF {
fmt.Fprintf(ctx.Stdout, "Error: %v", err)
}
break
}
}
return nil
}