/
provisioning_agent_monitor_deploy.go
380 lines (334 loc) · 10.4 KB
/
provisioning_agent_monitor_deploy.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
package units
import (
"context"
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/utility"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/job"
"github.com/mongodb/amboy/registry"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/mongodb/jasper"
"github.com/mongodb/jasper/options"
"github.com/pkg/errors"
)
const (
agentMonitorDeployJobName = "agent-monitor-deploy"
agentMonitorPutRetries = 25
)
func init() {
registry.AddJobType(agentMonitorDeployJobName, func() amboy.Job {
return makeAgentMonitorDeployJob()
})
}
type agentMonitorDeployJob struct {
HostID string `bson:"host_id" json:"host_id" yaml:"host_id"`
job.Base `bson:"metadata" json:"metadata" yaml:"metadata"`
host *host.Host
env evergreen.Environment
}
func makeAgentMonitorDeployJob() *agentMonitorDeployJob {
j := &agentMonitorDeployJob{
Base: job.Base{
JobType: amboy.JobType{
Name: agentMonitorDeployJobName,
Version: 0,
},
},
}
return j
}
// NewAgentMonitorDeployJob creates a job that deploys the agent monitor to the
// host.
func NewAgentMonitorDeployJob(env evergreen.Environment, h host.Host, id string) amboy.Job {
j := makeAgentMonitorDeployJob()
j.host = &h
j.HostID = h.Id
j.env = env
j.SetPriority(1)
j.SetScopes([]string{fmt.Sprintf("%s.%s", agentMonitorDeployJobName, j.HostID)})
j.SetEnqueueAllScopes(true)
j.UpdateRetryInfo(amboy.JobRetryOptions{
Retryable: utility.TruePtr(),
MaxAttempts: utility.ToIntPtr(agentMonitorPutRetries),
WaitUntil: utility.ToTimeDurationPtr(15 * time.Second),
})
j.SetID(fmt.Sprintf("%s.%s.%s", agentMonitorDeployJobName, j.HostID, id))
return j
}
func (j *agentMonitorDeployJob) Run(ctx context.Context) {
defer j.MarkComplete()
flags, err := evergreen.GetServiceFlags(ctx)
if err != nil {
j.AddRetryableError(errors.Wrap(err, "getting service flags"))
return
}
if flags.AgentStartDisabled {
grip.Debug(message.Fields{
"mode": "degraded",
"host_id": j.HostID,
"job": j.ID(),
"job_type": j.Type().Name,
})
return
}
if err = j.populateIfUnset(ctx); err != nil {
j.AddRetryableError(err)
return
}
if j.hostDown() {
grip.Debug(message.Fields{
"host_id": j.host.Id,
"status": j.host.Status,
"message": "host already down, not attempting to deploy agent monitor",
})
return
}
if !j.host.NeedsNewAgentMonitor {
return
}
if err = j.host.UpdateLastCommunicated(ctx); err != nil {
grip.Error(message.WrapError(err, message.Fields{
"message": "could not update host communication time",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"job": j.ID(),
}))
}
defer func() {
if !j.HasErrors() {
return
}
event.LogHostAgentMonitorDeployFailed(j.host.Id, j.Error())
grip.Error(message.WrapError(j.Error(), message.Fields{
"message": "agent monitor deploy failed",
"host_id": j.host.Id,
"host_tag": j.host.Tag,
"distro": j.host.Distro.Id,
"provider": j.host.Provider,
}))
if j.host.Status != evergreen.HostRunning {
return
}
if j.RetryInfo().GetRemainingAttempts() > 0 {
return
}
var externallyTerminated bool
externallyTerminated, err = handleExternallyTerminatedHost(ctx, j.ID(), j.env, j.host)
j.AddError(errors.Wrapf(err, "checking if host '%s' was externally terminated", j.HostID))
if externallyTerminated {
return
}
if disableErr := HandlePoisonedHost(ctx, j.env, j.host, fmt.Sprintf("failed %d times to put agent monitor on host", agentMonitorPutRetries)); disableErr != nil {
j.AddError(errors.Wrapf(disableErr, "terminating poisoned host '%s'", j.host.Id))
}
}()
var alive bool
alive, err = j.checkAgentMonitor(ctx)
if err != nil {
j.AddRetryableError(err)
return
}
if alive {
grip.Info(message.Fields{
"message": "not deploying a new agent monitor because it is still alive",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"job_id": j.ID(),
})
j.AddRetryableError(j.host.SetNeedsNewAgentMonitor(ctx, false))
return
}
settings := *j.env.Settings()
settings.ServiceFlags = *flags
if err = j.fetchClient(ctx); err != nil {
j.AddRetryableError(err)
return
}
if err = j.runSetupScript(ctx, &settings); err != nil {
j.AddError(err)
return
}
if err := j.startAgentMonitor(ctx, &settings); err != nil {
j.AddRetryableError(err)
return
}
j.AddError(j.host.SetNeedsNewAgentMonitor(ctx, false))
}
// hostDown checks if the host is down.
func (j *agentMonitorDeployJob) hostDown() bool {
return utility.StringSliceContains(evergreen.DownHostStatus, j.host.Status)
}
// disableHost changes the host so that it is down and enqueues a job to
// terminate it.
func (j *agentMonitorDeployJob) disableHost(ctx context.Context, reason string) error {
return errors.Wrapf(HandlePoisonedHost(ctx, j.env, j.host, reason), "terminating host '%s'", j.host.Id)
}
// checkAgentMonitor returns whether or not an agent monitor is already running
// on the host.
func (j *agentMonitorDeployJob) checkAgentMonitor(ctx context.Context) (bool, error) {
var alive bool
err := j.host.WithAgentMonitor(ctx, j.env, func(procs []jasper.Process) error {
var numRunning int
for _, proc := range procs {
if proc.Running(ctx) {
alive = true
numRunning++
}
}
grip.WarningWhen(numRunning > 1, message.Fields{
"message": fmt.Sprintf("host should be running at most one agent monitor, but found %d", len(procs)),
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"job": j.ID(),
})
return nil
})
return alive, errors.Wrap(err, "checking agent monitor status")
}
// fetchClient fetches the client on the host through the host's Jasper service.
func (j *agentMonitorDeployJob) fetchClient(ctx context.Context) error {
grip.Info(message.Fields{
"message": "fetching latest Evergreen binary for agent monitor",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"communication": j.host.Distro.BootstrapSettings.Communication,
"job": j.ID(),
})
cmd, err := j.host.CurlCommand(j.env)
if err != nil {
return errors.Wrap(err, "creating command to curl agent monitor binary")
}
opts := &options.Create{
Args: []string{j.host.Distro.ShellBinary(), "-l", "-c", cmd},
}
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, evergreenCurlTimeout)
defer cancel()
output, err := j.host.RunJasperProcess(ctx, j.env, opts)
if err != nil {
grip.Error(message.WrapError(err, message.Fields{
"message": "error fetching agent monitor binary on host",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"logs": output,
"communication": j.host.Distro.BootstrapSettings.Communication,
"job": j.ID(),
}))
return errors.WithStack(err)
}
if ctx.Err() != nil {
return errors.Wrap(ctx.Err(), "curling agent monitor binary")
}
return nil
}
// runSetupScript runs the setup script on the host through the host's Jasper
// service.
func (j *agentMonitorDeployJob) runSetupScript(ctx context.Context, settings *evergreen.Settings) error {
if j.host.Distro.Setup == "" {
return nil
}
grip.Info(message.Fields{
"message": "running setup script on host",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"communication": j.host.Distro.BootstrapSettings.Communication,
"job": j.ID(),
})
opts := &options.Create{
Args: []string{j.host.Distro.ShellBinary(), "-l", "-c", j.host.SetupCommand()},
}
output, err := j.host.RunJasperProcess(ctx, j.env, opts)
if err != nil {
reason := "running setup script on host"
grip.Error(message.WrapError(err, message.Fields{
"message": reason,
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"logs": output,
"job": j.ID(),
}))
catcher := grip.NewBasicCatcher()
catcher.Wrapf(err, "%s: %s", reason, output)
catcher.Wrap(j.disableHost(ctx, reason), "disabling host after setup script failed")
return catcher.Resolve()
}
return nil
}
// startAgentMonitor starts the agent monitor on the host through the host's
// Jasper service.
func (j *agentMonitorDeployJob) startAgentMonitor(ctx context.Context, settings *evergreen.Settings) error {
// Generate the host secret if none exists.
if j.host.Secret == "" {
if err := j.host.CreateSecret(ctx, false); err != nil {
return errors.Wrapf(err, "creating secret for host '%s'", j.host.Id)
}
}
grip.Info(j.deployMessage())
if _, err := j.host.StartJasperProcess(ctx, j.env, j.host.AgentMonitorOptions(settings)); err != nil {
grip.Error(message.WrapError(err, message.Fields{
"message": "failed to start agent monitor on host",
"host_id": j.host.Id,
"distro": j.host.Distro.Id,
"job": j.ID(),
}))
return errors.Wrap(err, "creating agent monitor command")
}
event.LogHostAgentMonitorDeployed(j.host.Id)
grip.Info(message.Fields{
"message": "agent monitor deployed",
"host_id": j.host.Id,
"host_tag": j.host.Tag,
"distro": j.host.Distro.Id,
"provider": j.host.Provider,
})
return nil
}
// deployMessage builds the message containing information preceding an agent
// monitor deploy.
func (j *agentMonitorDeployJob) deployMessage() message.Fields {
m := message.Fields{
"message": "starting agent monitor on host",
"host_id": j.host.Host,
"distro": j.host.Distro.Id,
"provider": j.host.Distro.Provider,
"job": j.ID(),
}
if j.host.InstanceType != "" {
m["instance"] = j.host.InstanceType
}
sinceLCT := time.Since(j.host.LastCommunicationTime)
if j.host.NeedsNewAgentMonitor {
m["reason"] = "flagged for new agent monitor"
} else if j.host.LastCommunicationTime.IsZero() {
m["reason"] = "new host"
} else if sinceLCT > host.MaxUncommunicativeInterval {
m["reason"] = "host has exceeded last communication threshold"
m["threshold"] = host.MaxUncommunicativeInterval
m["threshold_span"] = host.MaxUncommunicativeInterval.String()
m["last_communication_at"] = sinceLCT
m["last_communication_at_time"] = sinceLCT.String()
}
return m
}
// populateIfUnset populates the unset job fields.
func (j *agentMonitorDeployJob) populateIfUnset(ctx context.Context) error {
if j.host == nil {
h, err := host.FindOneId(ctx, j.HostID)
if err != nil {
return errors.Wrapf(err, "finding host '%s'", j.HostID)
}
if h == nil {
return errors.Errorf("host '%s' not found", j.HostID)
}
j.host = h
}
if j.env == nil {
j.env = evergreen.GetEnvironment()
}
return nil
}