-
Notifications
You must be signed in to change notification settings - Fork 162
/
run_errand.go
115 lines (93 loc) · 2.51 KB
/
run_errand.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"fmt"
boshdir "github.com/cloudfoundry/bosh-cli/director"
biui "github.com/cloudfoundry/bosh-cli/ui"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"
)
type RunErrandCmd struct {
deployment boshdir.Deployment
downloader Downloader
ui biui.UI
}
func NewRunErrandCmd(
deployment boshdir.Deployment,
downloader Downloader,
ui biui.UI,
) RunErrandCmd {
return RunErrandCmd{deployment: deployment, downloader: downloader, ui: ui}
}
func (c RunErrandCmd) Run(opts RunErrandOpts) error {
results, err := c.deployment.RunErrand(
opts.Args.Name,
opts.KeepAlive,
opts.WhenChanged,
opts.InstanceGroupOrInstanceSlugFlags.Slugs,
)
if err != nil {
return err
}
errandErr := c.summarize(opts.Args.Name, results)
for _, result := range results {
if opts.DownloadLogs && len(result.LogsBlobstoreID) > 0 {
err := c.downloader.Download(
result.LogsBlobstoreID,
result.LogsSHA1,
opts.Args.Name,
opts.LogsDirectory.Path,
)
if err != nil {
return bosherr.WrapError(err, "Downloading errand logs")
}
}
}
return errandErr
}
func (c RunErrandCmd) summarize(errandName string, results []boshdir.ErrandResult) error {
table := boshtbl.Table{
Content: "errand(s)",
Header: []boshtbl.Header{
boshtbl.NewHeader("Instance"),
boshtbl.NewHeader("Exit Code"),
boshtbl.NewHeader("Stdout"),
boshtbl.NewHeader("Stderr"),
},
SortBy: []boshtbl.ColumnSort{
{Column: 0, Asc: true},
},
Notes: []string{},
FillFirstColumn: true,
Transpose: true,
}
var errandErr error
for _, result := range results {
instance := ""
if result.InstanceGroup != "" {
instance = boshdir.NewInstanceGroupOrInstanceSlug(result.InstanceGroup, result.InstanceID).String()
}
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString(instance),
boshtbl.NewValueInt(result.ExitCode),
boshtbl.NewValueString(result.Stdout),
boshtbl.NewValueString(result.Stderr),
})
prefix := fmt.Sprintf("Errand '%s'", errandName)
suffix := fmt.Sprintf("(exit code %d)", result.ExitCode)
switch {
case result.ExitCode == 0:
case result.ExitCode > 128:
errandErr = bosherr.Errorf("%s was canceled %s", prefix, suffix)
default:
errandErr = bosherr.Errorf("%s completed with error %s", prefix, suffix)
}
}
c.ui.PrintTable(table)
return errandErr
}
func (c RunErrandCmd) printOutput(title, output string) {
if len(output) > 0 {
c.ui.PrintLinef("%s", title)
c.ui.PrintLinef("%s", output)
}
}