-
Notifications
You must be signed in to change notification settings - Fork 11
/
reporter.go
154 lines (123 loc) · 3.97 KB
/
reporter.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
package shell
import (
"context"
"fmt"
"time"
s2hv1beta1 "github.com/agoda-com/samsahai/api/v1beta1"
"github.com/agoda-com/samsahai/internal"
s2herrors "github.com/agoda-com/samsahai/internal/errors"
s2hlog "github.com/agoda-com/samsahai/internal/log"
"github.com/agoda-com/samsahai/internal/util/cmd"
"github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)
var logger = s2hlog.Log.WithName(ReporterName)
const (
ReporterName = "shell"
ExecutionTimeout = 60 * time.Second
)
type execCommand func(ctx context.Context, configPath string, cmdObj *s2hv1beta1.CommandAndArgs) ([]byte, error)
type reporter struct {
timeout time.Duration
execCommand execCommand
}
// NewOption allows specifying various configuration
type NewOption func(*reporter)
func WithExecCommand(execCommand execCommand) NewOption {
return func(r *reporter) {
r.execCommand = execCommand
}
}
// WithTimeout specifies timeout to override when executing shell command
func WithTimeout(timeout time.Duration) NewOption {
return func(r *reporter) {
r.timeout = timeout
}
}
// New creates a new shell reporter
func New(opts ...NewOption) internal.Reporter {
r := &reporter{
timeout: ExecutionTimeout,
execCommand: cmd.ExecuteCommand,
}
// apply the new options
for _, opt := range opts {
opt(r)
}
return r
}
// GetName returns shell type
func (r *reporter) GetName() string {
return ReporterName
}
// SendComponentUpgrade implements the reporter SendComponentUpgrade function
func (r *reporter) SendComponentUpgrade(configCtrl internal.ConfigController, comp *internal.ComponentUpgradeReporter) error {
config, err := configCtrl.Get(comp.TeamName)
if err != nil {
return err
}
if config.Spec.Reporter == nil ||
config.Spec.Reporter.Shell == nil ||
config.Spec.Reporter.Shell.ComponentUpgrade == nil {
return nil
}
cmdObj := cmd.RenderTemplate(config.Spec.Reporter.Shell.ComponentUpgrade.Command,
config.Spec.Reporter.Shell.ComponentUpgrade.Args, comp)
if err := r.execute(cmdObj, internal.ComponentUpgradeType); err != nil {
return err
}
return nil
}
// SendActivePromotionStatus implements the reporter SendActivePromotionStatus function
func (r *reporter) SendActivePromotionStatus(configCtrl internal.ConfigController, atpRpt *internal.ActivePromotionReporter) error {
config, err := configCtrl.Get(atpRpt.TeamName)
if err != nil {
return err
}
if config.Spec.Reporter == nil ||
config.Spec.Reporter.Shell == nil ||
config.Spec.Reporter.Shell.ActivePromotion == nil {
return nil
}
cmdObj := cmd.RenderTemplate(config.Spec.Reporter.Shell.ActivePromotion.Command,
config.Spec.Reporter.Shell.ActivePromotion.Args, atpRpt)
if err := r.execute(cmdObj, internal.ActivePromotionType); err != nil {
return err
}
return nil
}
// SendImageMissing implements the reporter SendImageMissing function
func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, images *rpc.Image) error {
config, err := configCtrl.Get(teamName)
if err != nil {
return err
}
if config.Spec.Reporter == nil ||
config.Spec.Reporter.Shell == nil ||
config.Spec.Reporter.Shell.ImageMissing == nil {
return nil
}
cmdObj := cmd.RenderTemplate(config.Spec.Reporter.Shell.ImageMissing.Command,
config.Spec.Reporter.Shell.ImageMissing.Args, images)
if err := r.execute(cmdObj, internal.ImageMissingType); err != nil {
return err
}
return nil
}
func (r *reporter) execute(cmdObj *s2hv1beta1.CommandAndArgs, event internal.EventType) error {
logger.Debug("start executing command", "event", event)
ctx, cancelFunc := context.WithTimeout(context.Background(), r.timeout)
defer cancelFunc()
errCh := make(chan error)
go func() {
out, err := r.execCommand(context.TODO(), ".", cmdObj)
logger.Debug(fmt.Sprintf("output: %s", out), "event", event)
errCh <- err
}()
select {
case <-ctx.Done():
logger.Error(s2herrors.ErrExecutionTimeout, fmt.Sprintf("executing took more than %v", r.timeout))
return s2herrors.ErrExecutionTimeout
case err := <-errCh:
return err
}
}