-
Notifications
You must be signed in to change notification settings - Fork 124
/
registry.go
275 lines (234 loc) · 9.2 KB
/
registry.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"
"sync"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
var evgRegistry *commandRegistry
func init() {
evgRegistry = newCommandRegistry()
cmds := map[string]CommandFactory{
"archive.auto_pack": autoArchiveCreateFactory,
"archive.auto_extract": autoExtractFactory,
"archive.targz_pack": tarballCreateFactory,
"archive.targz_extract": tarballExtractFactory,
"archive.zip_pack": zipArchiveCreateFactory,
"archive.zip_extract": zipExtractFactory,
evergreen.AttachResultsCommandName: attachResultsFactory,
evergreen.AttachXUnitResultsCommandName: xunitResultsFactory,
evergreen.AttachArtifactsCommandName: attachArtifactsFactory,
evergreen.HostCreateCommandName: createHostFactory,
"ec2.assume_role": ec2AssumeRoleFactory,
"host.list": listHostFactory,
"expansions.update": updateExpansionsFactory,
"expansions.write": writeExpansionsFactory,
"generate.tasks": generateTaskFactory,
"git.apply_patch": gitApplyPatchFactory,
"git.get_project": gitFetchProjectFactory,
"git.merge_pr": gitMergePRFactory,
"git.push": gitPushFactory,
"gotest.parse_files": goTestFactory,
"keyval.inc": keyValIncFactory,
"manifest.load": manifestLoadFactory,
"papertrail.trace": papertrailTraceFactory,
"perf.send": perfSendFactory,
"downstream_expansions.set": setExpansionsFactory,
"s3.get": s3GetFactory,
"s3.put": s3PutFactory,
"s3Copy.copy": s3CopyFactory,
evergreen.S3PushCommandName: s3PushFactory,
evergreen.S3PullCommandName: s3PullFactory,
evergreen.ShellExecCommandName: shellExecFactory,
"subprocess.exec": subprocessExecFactory,
"setup.initial": initialSetupFactory,
"timeout.update": timeoutUpdateFactory,
}
for name, factory := range cmds {
grip.EmergencyPanic(RegisterCommand(name, factory))
}
}
func RegisterCommand(name string, factory CommandFactory) error {
return errors.Wrapf(evgRegistry.registerCommand(name, factory), "registering command '%s'", name)
}
func GetCommandFactory(name string) (CommandFactory, bool) {
return evgRegistry.getCommandFactory(name)
}
// Render takes a command specification and returns the commands to actually
// run. It resolves the command specification into either a single command (in
// the case of standalone command) or a list of commands (in the case of a
// function).
func Render(c model.PluginCommandConf, project *model.Project, blockInfo BlockInfo) ([]Command, error) {
return evgRegistry.renderCommands(c, project, blockInfo)
}
func RegisteredCommandNames() []string { return evgRegistry.registeredCommandNames() }
type CommandFactory func() Command
type commandRegistry struct {
mu *sync.RWMutex
cmds map[string]CommandFactory
}
func newCommandRegistry() *commandRegistry {
return &commandRegistry{
cmds: map[string]CommandFactory{},
mu: &sync.RWMutex{},
}
}
func (r *commandRegistry) registeredCommandNames() []string {
out := []string{}
r.mu.RLock()
defer r.mu.RUnlock()
for name := range r.cmds {
out = append(out, name)
}
return out
}
func (r *commandRegistry) registerCommand(name string, factory CommandFactory) error {
r.mu.Lock()
defer r.mu.Unlock()
if name == "" {
return errors.New("cannot register a command without a name")
}
if _, ok := r.cmds[name]; ok {
return errors.Errorf("command '%s' is already registered", name)
}
if factory == nil {
return errors.Errorf("cannot register a nil factory for command '%s'", name)
}
r.cmds[name] = factory
return nil
}
func (r *commandRegistry) getCommandFactory(name string) (CommandFactory, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
factory, ok := r.cmds[name]
return factory, ok
}
func (r *commandRegistry) renderCommands(commandInfo model.PluginCommandConf,
project *model.Project, blockInfo BlockInfo) ([]Command, error) {
var parsed []model.PluginCommandConf
catcher := grip.NewBasicCatcher()
if funcName := commandInfo.Function; funcName != "" {
cmds, ok := project.Functions[funcName]
if !ok {
catcher.Errorf("function '%s' not found in project functions", funcName)
} else if cmds != nil {
cmdsInFunc := cmds.List()
for i, c := range cmdsInFunc {
if c.Function != "" {
catcher.Errorf("cannot reference a function ('%s') within another function ('%s')", c.Function, funcName)
continue
}
// If there's no command-specific type/timeout, use the
// function's command type/timeout
if c.Type == "" {
c.Type = commandInfo.Type
}
if c.TimeoutSecs == 0 {
c.TimeoutSecs = commandInfo.TimeoutSecs
}
funcInfo := FunctionInfo{
Function: funcName,
SubCmdNum: i + 1,
TotalSubCmds: len(cmdsInFunc),
}
c.DisplayName = GetFullDisplayName(c.Command, c.DisplayName, blockInfo, funcInfo)
c.FailureMetadataTags = utility.UniqueStrings(append(c.FailureMetadataTags, commandInfo.FailureMetadataTags...))
parsed = append(parsed, c)
}
}
} else {
commandInfo.DisplayName = GetFullDisplayName(commandInfo.Command, commandInfo.DisplayName, blockInfo, FunctionInfo{})
parsed = append(parsed, commandInfo)
}
var out []Command
for _, c := range parsed {
factory, ok := r.getCommandFactory(c.Command)
if !ok {
catcher.Errorf("command '%s' is not registered", c.Command)
continue
}
cmd := factory()
// Note: this parses the parameters before expansions are applied.
// Expansions are only available when the command is executed.
if err := cmd.ParseParams(c.Params); err != nil {
catcher.Wrapf(err, "parsing parameters for command %s", c.DisplayName)
continue
}
cmd.SetType(c.GetType(project))
cmd.SetFullDisplayName(c.DisplayName)
cmd.SetIdleTimeout(time.Duration(c.TimeoutSecs) * time.Second)
cmd.SetRetryOnFailure(c.RetryOnFailure)
cmd.SetFailureMetadataTags(c.FailureMetadataTags)
out = append(out, cmd)
}
if catcher.HasErrors() {
return nil, catcher.Resolve()
}
return out, nil
}
// BlockType is the name of the block that a command runs in.
type BlockType string
const (
MainTaskBlock BlockType = ""
TaskTimeoutBlock BlockType = "timeout"
PreBlock BlockType = "pre"
SetupTaskBlock BlockType = "setup_task"
TeardownTaskBlock BlockType = "teardown_task"
SetupGroupBlock BlockType = "setup_group"
TeardownGroupBlock BlockType = "teardown_group"
PostBlock BlockType = "post"
TaskSyncBlock BlockType = "task_sync"
)
// BlockInfo contains information about the enclosing block in which a function
// or standalone command runs. For example, this would contain information about
// the pre block that contains a particular shell.exec command.
type BlockInfo struct {
// Block is the name of the block that the command is part of.
Block BlockType
// CmdNum is the ordinal of a command in the block.
CmdNum int
// TotalCmds is the total number of commands in the block.
TotalCmds int
}
// FunctionInfo contains information about the enclosing function in which a
// command runs. For example, this would contain information about the second
// shell.exec that runs in a function.
type FunctionInfo struct {
// Function is the name of the function that the command is part of.
Function string
// SubCmdNum is the ordinal of the command within the function.
SubCmdNum int
// TotalSubCmds is the total number of sub-commands within the function.
TotalSubCmds int
}
// GetFullDisplayName returns the full, unambiguous display name for a command.
// cmdName is the type of command (e.g. shell.exec), displayName is the
// human-readable display name (if specified), or the command name (if no
// display name is given). blockInfo and funcInfo include contextual information
// about the block/func that the command is running in.
func GetFullDisplayName(cmdName, displayName string, blockInfo BlockInfo, funcInfo FunctionInfo) string {
fullName := fmt.Sprintf("'%s'", cmdName)
if displayName != "" {
fullName = fmt.Sprintf("%s ('%s')", fullName, displayName)
}
if funcInfo.Function != "" {
fullName = fmt.Sprintf("%s in function '%s'", fullName, funcInfo.Function)
}
if blockInfo.CmdNum > 0 && blockInfo.TotalCmds > 0 {
if funcInfo.SubCmdNum > 0 && funcInfo.TotalSubCmds > 1 {
// Include the function sub-command number only if the function runs
// more than one command.
fullName = fmt.Sprintf("%s (step %d.%d of %d)", fullName, blockInfo.CmdNum, funcInfo.SubCmdNum, blockInfo.TotalCmds)
} else {
fullName = fmt.Sprintf("%s (step %d of %d)", fullName, blockInfo.CmdNum, blockInfo.TotalCmds)
}
}
if blockInfo.Block != "" {
fullName = fmt.Sprintf("%s in block '%s'", fullName, blockInfo.Block)
}
return fullName
}