forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_start.go
422 lines (391 loc) · 13.9 KB
/
agent_start.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
package clicommand
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/buildkite/agent/agent"
"github.com/buildkite/agent/cliconfig"
"github.com/buildkite/agent/logger"
"github.com/buildkite/shellwords"
"github.com/urfave/cli"
)
var StartDescription = `Usage:
buildkite-agent start [arguments...]
Description:
When a job is ready to run it will call the "bootstrap-script"
and pass it all the environment variables required for the job to run.
This script is responsible for checking out the code, and running the
actual build script defined in the pipeline.
The agent will run any jobs within a PTY (pseudo terminal) if available.
Example:
$ buildkite-agent start --token xxx`
// Adding config requires changes in a few different spots
// - The AgentStartConfig struct with a cli parameter
// - As a flag in the AgentStartCommand (with matching env)
// - Into an env to be passed to the bootstrap in agent/job_runner.go, createEnvironment()
// - Into clicommand/bootstrap.go to read it from the env into the bootstrap config
type AgentStartConfig struct {
Config string `cli:"config"`
Token string `cli:"token" validate:"required"`
Name string `cli:"name"`
Priority string `cli:"priority"`
DisconnectAfterJob bool `cli:"disconnect-after-job"`
DisconnectAfterJobTimeout int `cli:"disconnect-after-job-timeout"`
BootstrapScript string `cli:"bootstrap-script" normalize:"commandpath"`
BuildPath string `cli:"build-path" normalize:"filepath" validate:"required"`
HooksPath string `cli:"hooks-path" normalize:"filepath"`
PluginsPath string `cli:"plugins-path" normalize:"filepath"`
Shell string `cli:"shell"`
Tags []string `cli:"tags" normalize:"list"`
TagsFromEC2 bool `cli:"tags-from-ec2"`
TagsFromEC2Tags bool `cli:"tags-from-ec2-tags"`
TagsFromGCP bool `cli:"tags-from-gcp"`
TagsFromHost bool `cli:"tags-from-host"`
WaitForEC2TagsTimeout string `cli:"wait-for-ec2-tags-timeout"`
GitCloneFlags string `cli:"git-clone-flags"`
GitCleanFlags string `cli:"git-clean-flags"`
NoGitSubmodules bool `cli:"no-git-submodules"`
NoColor bool `cli:"no-color"`
NoSSHKeyscan bool `cli:"no-ssh-keyscan"`
NoCommandEval bool `cli:"no-command-eval"`
NoLocalHooks bool `cli:"no-local-hooks"`
NoPlugins bool `cli:"no-plugins"`
NoPluginValidation bool `cli:"no-plugin-validation"`
NoPTY bool `cli:"no-pty"`
TimestampLines bool `cli:"timestamp-lines"`
Endpoint string `cli:"endpoint" validate:"required"`
Debug bool `cli:"debug"`
DebugHTTP bool `cli:"debug-http"`
Experiments []string `cli:"experiment" normalize:"list"`
/* Deprecated */
NoSSHFingerprintVerification bool `cli:"no-automatic-ssh-fingerprint-verification" deprecated-and-renamed-to:"NoSSHKeyscan"`
MetaData []string `cli:"meta-data" deprecated-and-renamed-to:"Tags"`
MetaDataEC2 bool `cli:"meta-data-ec2" deprecated-and-renamed-to:"TagsFromEC2"`
MetaDataEC2Tags bool `cli:"meta-data-ec2-tags" deprecated-and-renamed-to:"TagsFromEC2Tags"`
MetaDataGCP bool `cli:"meta-data-gcp" deprecated-and-renamed-to:"TagsFromGCP"`
}
func DefaultShell() string {
// https://github.com/golang/go/blob/master/src/go/build/syslist.go#L7
switch runtime.GOOS {
case "windows":
return `C:\Windows\System32\CMD.exe /S /C`
case "freebsd", "openbsd", "netbsd":
return `/usr/local/bin/bash -e -c`
default:
return `/bin/bash -e -c`
}
}
func DefaultConfigFilePaths() (paths []string) {
// Toggle beetwen windows an *nix paths
if runtime.GOOS == "windows" {
paths = []string{
"C:\\buildkite-agent\\buildkite-agent.cfg",
"$USERPROFILE\\AppData\\Local\\BuildkiteAgent\\buildkite-agent.cfg",
}
} else {
paths = []string{
"$HOME/.buildkite-agent/buildkite-agent.cfg",
"/usr/local/etc/buildkite-agent/buildkite-agent.cfg",
"/etc/buildkite-agent/buildkite-agent.cfg",
}
}
// Also check to see if there's a buildkite-agent.cfg in the folder
// that the binary is running in.
pathToBinary, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err == nil {
pathToRelativeConfig := filepath.Join(pathToBinary, "buildkite-agent.cfg")
paths = append([]string{pathToRelativeConfig}, paths...)
}
return
}
var AgentStartCommand = cli.Command{
Name: "start",
Usage: "Starts a Buildkite agent",
Description: StartDescription,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "",
Usage: "Path to a configuration file",
EnvVar: "BUILDKITE_AGENT_CONFIG",
},
cli.StringFlag{
Name: "token",
Value: "",
Usage: "Your account agent token",
EnvVar: "BUILDKITE_AGENT_TOKEN",
},
cli.StringFlag{
Name: "name",
Value: "",
Usage: "The name of the agent",
EnvVar: "BUILDKITE_AGENT_NAME",
},
cli.StringFlag{
Name: "priority",
Value: "",
Usage: "The priority of the agent (higher priorities are assigned work first)",
EnvVar: "BUILDKITE_AGENT_PRIORITY",
},
cli.BoolFlag{
Name: "disconnect-after-job",
Usage: "Disconnect the agent after running a job",
EnvVar: "BUILDKITE_AGENT_DISCONNECT_AFTER_JOB",
},
cli.IntFlag{
Name: "disconnect-after-job-timeout",
Value: 120,
Usage: "When --disconnect-after-job is specified, the number of seconds to wait for a job before shutting down",
EnvVar: "BUILDKITE_AGENT_DISCONNECT_AFTER_JOB_TIMEOUT",
},
cli.StringFlag{
Name: "shell",
Value: DefaultShell(),
Usage: "The shell commamnd used to interpret build commands, e.g /bin/bash -e -c",
EnvVar: "BUILDKITE_SHELL",
},
cli.StringSliceFlag{
Name: "tags",
Value: &cli.StringSlice{},
Usage: "A comma-separated list of tags for the agent (e.g. \"linux\" or \"mac,xcode=8\")",
EnvVar: "BUILDKITE_AGENT_TAGS",
},
cli.BoolFlag{
Name: "tags-from-host",
Usage: "Include tags from the host (hostname, machine-id, os)",
EnvVar: "BUILDKITE_AGENT_TAGS_FROM_HOST",
},
cli.BoolFlag{
Name: "tags-from-ec2",
Usage: "Include the host's EC2 meta-data as tags (instance-id, instance-type, and ami-id)",
EnvVar: "BUILDKITE_AGENT_TAGS_FROM_EC2",
},
cli.BoolFlag{
Name: "tags-from-ec2-tags",
Usage: "Include the host's EC2 tags as tags",
EnvVar: "BUILDKITE_AGENT_TAGS_FROM_EC2_TAGS",
},
cli.BoolFlag{
Name: "tags-from-gcp",
Usage: "Include the host's Google Cloud meta-data as tags (instance-id, machine-type, preemptible, project-id, region, and zone)",
EnvVar: "BUILDKITE_AGENT_TAGS_FROM_GCP",
},
cli.DurationFlag{
Name: "wait-for-ec2-tags-timeout",
Usage: "The amount of time to wait for tags from EC2 before proceeding",
EnvVar: "BUILDKITE_AGENT_WAIT_FOR_EC2_TAGS_TIMEOUT",
Value: time.Second * 10,
},
cli.StringFlag{
Name: "git-clone-flags",
Value: "-v",
Usage: "Flags to pass to the \"git clone\" command",
EnvVar: "BUILDKITE_GIT_CLONE_FLAGS",
},
cli.StringFlag{
Name: "git-clean-flags",
Value: "-fxdq",
Usage: "Flags to pass to \"git clean\" command",
EnvVar: "BUILDKITE_GIT_CLEAN_FLAGS",
},
cli.StringFlag{
Name: "bootstrap-script",
Value: "",
Usage: "The command that is executed for bootstrapping a job, defaults to the bootstrap sub-command of this binary",
EnvVar: "BUILDKITE_BOOTSTRAP_SCRIPT_PATH",
},
cli.StringFlag{
Name: "build-path",
Value: "",
Usage: "Path to where the builds will run from",
EnvVar: "BUILDKITE_BUILD_PATH",
},
cli.StringFlag{
Name: "hooks-path",
Value: "",
Usage: "Directory where the hook scripts are found",
EnvVar: "BUILDKITE_HOOKS_PATH",
},
cli.StringFlag{
Name: "plugins-path",
Value: "",
Usage: "Directory where the plugins are saved to",
EnvVar: "BUILDKITE_PLUGINS_PATH",
},
cli.BoolFlag{
Name: "timestamp-lines",
Usage: "Prepend timestamps on each line of output.",
EnvVar: "BUILDKITE_TIMESTAMP_LINES",
},
cli.BoolFlag{
Name: "no-pty",
Usage: "Do not run jobs within a pseudo terminal",
EnvVar: "BUILDKITE_NO_PTY",
},
cli.BoolFlag{
Name: "no-ssh-keyscan",
Usage: "Don't automatically run ssh-keyscan before checkout",
EnvVar: "BUILDKITE_NO_SSH_KEYSCAN",
},
cli.BoolFlag{
Name: "no-command-eval",
Usage: "Don't allow this agent to run arbitrary console commands, including plugins",
EnvVar: "BUILDKITE_NO_COMMAND_EVAL",
},
cli.BoolFlag{
Name: "no-plugins",
Usage: "Don't allow this agent to load plugins",
EnvVar: "BUILDKITE_NO_PLUGINS",
},
cli.BoolTFlag{
Name: "no-plugin-validation",
Usage: "Don't validate plugin configuration and requirements",
EnvVar: "BUILDKITE_NO_PLUGIN_VALIDATION",
},
cli.BoolFlag{
Name: "no-local-hooks",
Usage: "Don't allow local hooks to be run from checked out repositories",
EnvVar: "BUILDKITE_NO_LOCAL_HOOKS",
},
cli.BoolFlag{
Name: "no-git-submodules",
Usage: "Don't automatically checkout git submodules",
EnvVar: "BUILDKITE_NO_GIT_SUBMODULES,BUILDKITE_DISABLE_GIT_SUBMODULES",
},
ExperimentsFlag,
EndpointFlag,
NoColorFlag,
DebugFlag,
DebugHTTPFlag,
/* Deprecated flags which will be removed in v4 */
cli.StringSliceFlag{
Name: "meta-data",
Value: &cli.StringSlice{},
Hidden: true,
EnvVar: "BUILDKITE_AGENT_META_DATA",
},
cli.BoolFlag{
Name: "meta-data-ec2",
Hidden: true,
EnvVar: "BUILDKITE_AGENT_META_DATA_EC2",
},
cli.BoolFlag{
Name: "meta-data-ec2-tags",
Hidden: true,
EnvVar: "BUILDKITE_AGENT_TAGS_FROM_EC2_TAGS",
},
cli.BoolFlag{
Name: "meta-data-gcp",
Hidden: true,
EnvVar: "BUILDKITE_AGENT_META_DATA_GCP",
},
cli.BoolFlag{
Name: "no-automatic-ssh-fingerprint-verification",
Hidden: true,
EnvVar: "BUILDKITE_NO_AUTOMATIC_SSH_FINGERPRINT_VERIFICATION",
},
},
Action: func(c *cli.Context) {
// The configuration will be loaded into this struct
cfg := AgentStartConfig{}
// Setup the config loader. You'll see that we also path paths to
// potential config files. The loader will use the first one it finds.
loader := cliconfig.Loader{
CLI: c,
Config: &cfg,
DefaultConfigFilePaths: DefaultConfigFilePaths(),
}
// Load the configuration
if err := loader.Load(); err != nil {
logger.Fatal("%s", err)
}
// Setup the any global configuration options
HandleGlobalFlags(cfg)
// Force some settings if on Windows (these aren't supported yet)
if runtime.GOOS == "windows" {
cfg.NoPTY = true
}
// Set a useful default for the bootstrap script
if cfg.BootstrapScript == "" {
cfg.BootstrapScript = fmt.Sprintf("%s bootstrap", shellwords.Quote(os.Args[0]))
}
// Show a warning if plugins are enabled by no-command-eval or no-local-hooks is set
if c.IsSet("no-plugins") && cfg.NoPlugins == false {
msg := `Plugins have been specifically enabled, despite %s being enabled. ` +
`Plugins can execute arbitrary hooks and commands, make sure you are ` +
`whitelisting your plugins in ` +
`your environment hook.`
switch {
case cfg.NoCommandEval:
logger.Warn(msg, `no-command-eval`)
case cfg.NoLocalHooks:
logger.Warn(msg, `no-local-hooks`)
}
}
// Turning off command eval or local hooks will also turn off plugins unless
// `--no-plugins=false` is provided specifically
if (cfg.NoCommandEval || cfg.NoLocalHooks) && !c.IsSet("no-plugins") {
cfg.NoPlugins = true
}
// Guess the shell if none is provided
if cfg.Shell == "" {
cfg.Shell = DefaultShell()
}
// Make sure the DisconnectAfterJobTimeout value is correct
if cfg.DisconnectAfterJob && cfg.DisconnectAfterJobTimeout < 120 {
logger.Fatal("The timeout for `disconnect-after-job` must be at least 120 seconds")
}
var ec2TagTimeout time.Duration
if t := cfg.WaitForEC2TagsTimeout; t != "" {
var err error
ec2TagTimeout, err = time.ParseDuration(t)
if err != nil {
logger.Fatal("Failed to parse ec2 tag timeout: %v", err)
}
}
// Setup the agent
pool := agent.AgentPool{
Token: cfg.Token,
Name: cfg.Name,
Priority: cfg.Priority,
Tags: cfg.Tags,
TagsFromEC2: cfg.TagsFromEC2,
TagsFromEC2Tags: cfg.TagsFromEC2Tags,
TagsFromGCP: cfg.TagsFromGCP,
TagsFromHost: cfg.TagsFromHost,
WaitForEC2TagsTimeout: ec2TagTimeout,
Endpoint: cfg.Endpoint,
AgentConfiguration: &agent.AgentConfiguration{
BootstrapScript: cfg.BootstrapScript,
BuildPath: cfg.BuildPath,
HooksPath: cfg.HooksPath,
PluginsPath: cfg.PluginsPath,
GitCloneFlags: cfg.GitCloneFlags,
GitCleanFlags: cfg.GitCleanFlags,
GitSubmodules: !cfg.NoGitSubmodules,
SSHKeyscan: !cfg.NoSSHKeyscan,
CommandEval: !cfg.NoCommandEval,
PluginsEnabled: !cfg.NoPlugins,
PluginValidation: !cfg.NoPluginValidation,
LocalHooksEnabled: !cfg.NoLocalHooks,
RunInPty: !cfg.NoPTY,
TimestampLines: cfg.TimestampLines,
DisconnectAfterJob: cfg.DisconnectAfterJob,
DisconnectAfterJobTimeout: cfg.DisconnectAfterJobTimeout,
Shell: cfg.Shell,
},
}
// Store the loaded config file path on the pool and agent config so we can
// show it when the agent starts and set an env
if loader.File != nil {
pool.ConfigFilePath = loader.File.Path
pool.AgentConfiguration.ConfigPath = loader.File.Path
}
// Start the agent pool
if err := pool.Start(); err != nil {
logger.Fatal("%s", err)
}
},
}