-
Notifications
You must be signed in to change notification settings - Fork 124
/
host_create.go
485 lines (436 loc) · 15.7 KB
/
host_create.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
package data
import (
"context"
"fmt"
"io"
"net/http"
"path"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/evergreen-ci/birch"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/apimodels"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/units"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/gimlet"
"github.com/mitchellh/mapstructure"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
// ListHostsForTask lists running hosts scoped to the task or the task's build.
func ListHostsForTask(ctx context.Context, taskID string) ([]host.Host, error) {
env := evergreen.GetEnvironment()
t, err := task.FindOneId(taskID)
if err != nil {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: errors.Wrapf(err, "finding task '%s'", taskID).Error()}
}
if t == nil {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: fmt.Sprintf("task '%s' not found", taskID)}
}
catcher := grip.NewBasicCatcher()
hostsSpawnedByTask, err := host.FindHostsSpawnedByTask(ctx, t.Id, t.Execution)
catcher.Add(err)
hostsSpawnedByBuild, err := host.FindHostsSpawnedByBuild(ctx, t.BuildId)
catcher.Add(err)
if catcher.HasErrors() {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: catcher.String()}
}
hosts := []host.Host{}
hosts = append(hosts, hostsSpawnedByBuild...)
hosts = append(hosts, hostsSpawnedByTask...)
for idx, h := range hosts {
if h.IsContainer() {
p, err := h.GetParent(ctx)
if err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: fmt.Sprintf("getting parent for container '%s'", h.Id)}
}
if p != nil {
hosts[idx].Host = p.Host
hosts[idx].IP = p.IP
}
// update port binding if instance status not yet called
if h.NeedsPortBindings() {
mgrOpts, err := cloud.GetManagerOptions(h.Distro)
if err != nil {
return nil, errors.Wrapf(err, "getting cloud manager options for distro '%s'", h.Distro.Id)
}
mgr, err := cloud.GetManager(ctx, env, mgrOpts)
if err != nil {
return nil, errors.Wrap(err, "getting cloud manager")
}
if err = mgr.SetPortMappings(ctx, &hosts[idx], p); err != nil {
return nil, errors.Wrapf(err, "getting status for container '%s'", h.Id)
}
}
}
}
return hosts, nil
}
// CreateHostsFromTask creates intent hosts for those requested by the
// host.create command in a task.
func CreateHostsFromTask(ctx context.Context, env evergreen.Environment, t *task.Task, user user.DBUser, keyNameOrVal string) error {
if t == nil {
return errors.New("no task to create hosts from")
}
keyVal, err := user.GetPublicKey(keyNameOrVal)
if err != nil {
keyVal = keyNameOrVal
}
proj, expansions, err := makeProjectAndExpansionsFromTask(ctx, env.Settings(), t)
if err != nil {
return errors.WithStack(err)
}
projectTask := proj.FindProjectTask(t.DisplayName)
if projectTask == nil {
return errors.Errorf("project config for task '%s' not found", t.Id)
}
createHostCmds := []apimodels.CreateHost{}
catcher := grip.NewBasicCatcher()
for _, commandConf := range projectTask.Commands {
var cmds []model.PluginCommandConf
if commandConf.Function != "" {
cmds = proj.Functions[commandConf.Function].List()
} else {
cmds = []model.PluginCommandConf{commandConf}
}
for _, cmd := range cmds {
createHost, err := createHostFromCommand(cmd)
if err != nil {
return err
}
if createHost == nil {
continue
}
cmdExpansions := util.NewExpansions(commandConf.Vars)
cmdExpansions.Update(expansions.Map())
err = createHost.Expand(cmdExpansions)
if err != nil {
catcher.Wrap(err, "handling expansions")
continue
}
createHostCmds = append(createHostCmds, *createHost)
}
}
if catcher.HasErrors() {
return catcher.Resolve()
}
for _, createHost := range createHostCmds {
err = createHost.Validate(ctx)
if err != nil {
catcher.Add(err)
continue
}
numHosts, err := strconv.Atoi(createHost.NumHosts)
if err != nil {
catcher.Wrapf(err, "parsing host.create number of hosts '%s' as int", createHost.NumHosts)
continue
}
d, err := distro.GetHostCreateDistro(ctx, createHost)
if err != nil {
catcher.Add(err)
continue
}
for i := 0; i < numHosts; i++ {
_, err := MakeHost(ctx, env, t.Id, user.Username(), keyVal, createHost, *d)
if err != nil {
return errors.Wrap(err, "creating intent host")
}
}
}
return catcher.Resolve()
}
func makeProjectAndExpansionsFromTask(ctx context.Context, settings *evergreen.Settings, t *task.Task) (*model.Project, *util.Expansions, error) {
v, err := model.VersionFindOne(model.VersionById(t.Version))
if err != nil {
return nil, nil, errors.Wrapf(err, "finding version '%s'", t.Version)
}
if v == nil {
return nil, nil, errors.Errorf("version '%s' not found", t.Version)
}
project, _, err := model.FindAndTranslateProjectForVersion(ctx, settings, v, false)
if err != nil {
return nil, nil, errors.Wrap(err, "loading project")
}
h, err := host.FindOne(ctx, host.ById(t.HostId))
if err != nil {
return nil, nil, errors.Wrap(err, "finding host running task")
}
oauthToken, err := settings.GetGithubOauthToken()
if err != nil {
return nil, nil, errors.Wrap(err, "getting GitHub OAuth token from admin settings")
}
pRef, err := model.FindBranchProjectRef(t.Project)
if err != nil {
return nil, nil, errors.Wrapf(err, "finding project ref '%s'", t.Project)
}
if pRef == nil {
return nil, nil, errors.Errorf("project ref '%s' not found", t.Project)
}
appToken, err := settings.CreateInstallationToken(ctx, pRef.Owner, pRef.Repo, nil)
if err != nil {
return nil, nil, errors.Wrap(err, "creating GitHub app token")
}
knownHosts := settings.Expansions[evergreen.GithubKnownHosts]
expansions, err := model.PopulateExpansions(t, h, oauthToken, appToken, knownHosts)
if err != nil {
return nil, nil, errors.Wrap(err, "populating expansions")
}
// PopulateExpansions doesn't include build variant expansions, so include
// them here.
for _, bv := range project.BuildVariants {
if bv.Name == t.BuildVariant {
expansions.Update(bv.Expansions)
}
}
if project == nil {
project = &model.Project{}
}
params := append(project.GetParameters(), v.Parameters...)
if err = updateExpansions(&expansions, t.Project, params); err != nil {
return nil, nil, errors.Wrap(err, "updating expansions")
}
return project, &expansions, nil
}
// updateExpansions updates expansions with project variables and patch
// parameters.
func updateExpansions(expansions *util.Expansions, projectId string, params []patch.Parameter) error {
projVars, err := model.FindMergedProjectVars(projectId)
if err != nil {
return errors.Wrap(err, "finding project variables")
}
if projVars == nil {
return errors.New("project variables not found")
}
expansions.Update(projVars.Vars)
for _, param := range params {
expansions.Put(param.Key, param.Value)
}
return nil
}
func createHostFromCommand(cmd model.PluginCommandConf) (*apimodels.CreateHost, error) {
if cmd.Command != evergreen.HostCreateCommandName {
return nil, nil
}
createHost := &apimodels.CreateHost{}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: createHost,
})
if err != nil {
return nil, errors.Wrap(err, "constructing mapstructure decoder")
}
err = decoder.Decode(cmd.Params)
if err != nil {
return nil, errors.Wrap(err, "parsing params")
}
return createHost, nil
}
// MakeHost creates a host or container to run for host.create.
func MakeHost(ctx context.Context, env evergreen.Environment, taskID, userID, publicKey string, createHost apimodels.CreateHost, distro distro.Distro) (*host.Host, error) {
if evergreen.IsDockerProvider(createHost.CloudProvider) {
return makeDockerIntentHost(ctx, env, taskID, userID, createHost, distro)
}
return makeEC2IntentHost(ctx, env, taskID, userID, publicKey, createHost, distro)
}
func makeDockerIntentHost(ctx context.Context, env evergreen.Environment, taskID, userID string, createHost apimodels.CreateHost, d distro.Distro) (*host.Host, error) {
options, err := getHostCreationOptions(d, taskID, userID, createHost)
if err != nil {
return nil, errors.Wrap(err, "making intent host options")
}
method := distro.DockerImageBuildTypeImport
base := path.Base(createHost.Image)
hasPrefix := strings.HasPrefix(base, "http")
if !hasPrefix { // not a url
method = distro.DockerImageBuildTypePull
}
envVars := []string{}
for key, val := range createHost.EnvironmentVars {
envVars = append(envVars, fmt.Sprintf("%s=%s", key, val))
}
options.DockerOptions = host.DockerOptions{
Image: createHost.Image,
Command: createHost.Command,
PublishPorts: createHost.PublishPorts,
RegistryName: createHost.Registry.Name,
RegistryUsername: createHost.Registry.Username,
RegistryPassword: createHost.Registry.Password,
StdinData: createHost.StdinFileContents,
Method: method,
SkipImageBuild: true,
EnvironmentVars: envVars,
ExtraHosts: createHost.ExtraHosts,
}
containerPool := env.Settings().ContainerPools.GetContainerPool(d.ContainerPool)
if containerPool == nil {
return nil, errors.Errorf("distro '%s' doesn't have a container pool", d.Id)
}
containerIntents, parentIntents, err := host.MakeContainersAndParents(ctx, d, containerPool, 1, *options)
if err != nil {
return nil, errors.Wrap(err, "generating container and parent intent hosts")
}
if len(containerIntents) != 1 {
return nil, errors.Errorf("programmatic error: should have created one new container, not %d", len(containerIntents))
}
if err = host.InsertMany(ctx, containerIntents); err != nil {
return nil, errors.Wrap(err, "inserting container intents")
}
if err = host.InsertMany(ctx, parentIntents); err != nil {
return nil, errors.Wrap(err, "inserting parent intent hosts")
}
if err := units.EnqueueHostCreateJobs(ctx, env, append(containerIntents, parentIntents...)); err != nil {
return nil, errors.Wrapf(err, "enqueueing host create jobs")
}
return &containerIntents[0], nil
}
func makeEC2IntentHost(ctx context.Context, env evergreen.Environment, taskID, userID, publicKey string, createHost apimodels.CreateHost, d distro.Distro) (*host.Host, error) {
if createHost.Region == "" {
createHost.Region = evergreen.DefaultEC2Region
}
ec2Settings := cloud.EC2ProviderSettings{}
if createHost.Distro != "" {
if err := ec2Settings.FromDistroSettings(d, createHost.Region); err != nil {
return nil, errors.Wrapf(err, "getting EC2 provider settings from distro '%s' in region '%s'", createHost.Distro, createHost.Region)
}
}
if publicKey != "" {
d.Setup += fmt.Sprintf("\necho \"\n%s\" >> %s\n", publicKey, d.GetAuthorizedKeysFile())
}
// set provider settings
if createHost.AMI != "" {
ec2Settings.AMI = createHost.AMI
}
for _, mount := range createHost.EBSDevices {
ec2Settings.MountPoints = append(ec2Settings.MountPoints, cloud.MountPoint{
DeviceName: mount.DeviceName,
Size: int32(mount.SizeGiB),
Iops: int32(mount.IOPS),
Throughput: int32(mount.Throughput),
SnapshotID: mount.SnapshotID,
})
}
if createHost.InstanceType != "" {
ec2Settings.InstanceType = createHost.InstanceType
}
if userID == "" {
ec2Settings.KeyName = "" // never use the distro's key
}
if createHost.Subnet != "" {
ec2Settings.SubnetId = createHost.Subnet
}
if createHost.UserdataCommand != "" {
ec2Settings.UserData = createHost.UserdataCommand
}
// Always override distro security group with provided security group.
if len(createHost.SecurityGroups) > 0 {
ec2Settings.SecurityGroupIDs = createHost.SecurityGroups
} else {
ec2Settings.SecurityGroupIDs = append(ec2Settings.SecurityGroupIDs, evergreen.GetEnvironment().Settings().Providers.AWS.DefaultSecurityGroup)
}
if createHost.Tenancy != "" {
ec2Settings.Tenancy = createHost.Tenancy
}
ec2Settings.IPv6 = createHost.IPv6
ec2Settings.IsVpc = true // task-spawned hosts do not support ec2 classic
if err := ec2Settings.Validate(); err != nil {
return nil, errors.Wrap(err, "EC2 settings are invalid")
}
// update local distro with modified settings
doc, err := ec2Settings.ToDocument()
if err != nil {
return nil, errors.Wrap(err, "marshalling EC2 settings to BSON document")
}
d.ProviderSettingsList = []*birch.Document{doc}
options, err := getHostCreationOptions(d, taskID, userID, createHost)
if err != nil {
return nil, errors.Wrap(err, "making intent host options")
}
intent := host.NewIntent(*options)
if err = intent.Insert(ctx); err != nil {
return nil, errors.Wrap(err, "inserting intent host")
}
event.LogHostCreated(intent.Id)
grip.Info(message.Fields{
"message": "intent host created",
"host_id": intent.Id,
"host_tag": intent.Tag,
"distro": intent.Distro.Id,
})
if err := units.EnqueueHostCreateJobs(ctx, env, []host.Host{*intent}); err != nil {
return nil, errors.Wrapf(err, "enqueueing host create job for '%s'", intent.Id)
}
return intent, nil
}
func getHostCreationOptions(d distro.Distro, taskID, userID string, createHost apimodels.CreateHost) (*host.CreateOptions, error) {
options := host.CreateOptions{
Distro: d,
}
if userID != "" {
options.UserName = userID
options.UserHost = true
options.ExpirationTime = time.Now().Add(evergreen.DefaultSpawnHostExpiration)
options.ProvisionOptions = &host.ProvisionOptions{
TaskId: taskID,
OwnerId: userID,
}
} else {
options.UserName = taskID
t, err := task.FindOneId(taskID)
if err != nil {
return nil, errors.Wrapf(err, "finding task '%s'", taskID)
}
if t == nil {
return nil, errors.Errorf("task '%s' not found", taskID)
}
if createHost.Scope == "build" {
options.SpawnOptions.BuildID = t.BuildId
}
if createHost.Scope == "task" {
options.SpawnOptions.TaskID = taskID
options.SpawnOptions.TaskExecutionNumber = t.Execution
}
options.SpawnOptions.TimeoutTeardown = time.Now().Add(time.Duration(createHost.TeardownTimeoutSecs) * time.Second)
options.SpawnOptions.TimeoutSetup = time.Now().Add(time.Duration(createHost.SetupTimeoutSecs) * time.Second)
options.SpawnOptions.Retries = createHost.Retries
options.SpawnOptions.Respawns = evergreen.SpawnHostRespawns
options.SpawnOptions.SpawnedByTask = true
}
return &options, nil
}
// GetDockerLogs retrieves the logs for the given container.
func GetDockerLogs(ctx context.Context, containerId string, parent *host.Host,
settings *evergreen.Settings, options types.ContainerLogsOptions) (io.Reader, error) {
c := cloud.GetDockerClient(settings)
if err := c.Init(settings.Providers.Docker.APIVersion); err != nil {
return nil, errors.Wrap(err, "initializing Docker client")
}
logs, err := c.GetDockerLogs(ctx, containerId, parent, options)
if err != nil {
return nil, errors.Wrapf(err, "getting Docker logs for container '%s'", containerId)
}
return logs, nil
}
// GetDockerStatus returns the status of the given Docker container.
func GetDockerStatus(ctx context.Context, containerId string, parent *host.Host, settings *evergreen.Settings) (*cloud.ContainerStatus, error) {
c := cloud.GetDockerClient(settings)
if err := c.Init(settings.Providers.Docker.APIVersion); err != nil {
return nil, errors.Wrap(err, "initializing Docker client")
}
status, err := c.GetDockerStatus(ctx, containerId, parent)
if err != nil {
return nil, errors.Wrapf(err, "getting status of container '%s'", containerId)
}
return status, nil
}