forked from evergreen-ci/evergreen
/
host_termination.go
261 lines (228 loc) · 6.99 KB
/
host_termination.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
package units
import (
"context"
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/util"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/dependency"
"github.com/mongodb/amboy/job"
"github.com/mongodb/amboy/registry"
"github.com/mongodb/grip"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
const hostTerminationJobName = "host-termination-job"
const teardownFailurePreface = "[TEARDOWN-FAILURE]"
func init() {
registry.AddJobType(hostTerminationJobName, func() amboy.Job {
return makeHostTerminationJob()
})
}
type hostTerminationJob 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 makeHostTerminationJob() *hostTerminationJob {
j := &hostTerminationJob{
Base: job.Base{
JobType: amboy.JobType{
Name: hostTerminationJobName,
Version: 0,
},
},
}
j.SetDependency(dependency.NewAlways())
return j
}
func NewHostTerminationJob(env evergreen.Environment, h host.Host) amboy.Job {
j := makeHostTerminationJob()
j.host = &h
j.HostID = h.Id
j.env = env
j.SetPriority(2)
ts := util.RoundPartOfHour(2).Format(tsFormat)
j.SetID(fmt.Sprintf("%s.%s.%s", hostTerminationJobName, j.HostID, ts))
return j
}
func (j *hostTerminationJob) Run(ctx context.Context) {
var err error
defer j.MarkComplete()
if j.host == nil {
j.host, err = host.FindOneId(j.HostID)
if err != nil {
j.AddError(err)
return
}
if j.host == nil {
j.AddError(fmt.Errorf("could not find host %s for job %s", j.HostID, j.TaskID))
return
}
}
if !j.host.IsEphemeral() {
grip.Notice(message.Fields{
"job": j.ID(),
"host": j.HostID,
"job_type": j.Type().Name,
"status": j.host.Status,
"provider": j.host.Distro.Provider,
"message": "host termination for a non-spawnable distro",
"cause": "programmer error",
})
return
}
if j.env == nil {
j.env = evergreen.GetEnvironment()
}
settings := j.env.Settings()
idleTimeStartsAt := j.host.LastTaskCompletedTime
if idleTimeStartsAt.IsZero() || idleTimeStartsAt == util.ZeroTime {
idleTimeStartsAt = j.host.StartTime
}
// clear the running task of the host in case one has been assigned.
if j.host.RunningTask != "" {
grip.Warning(message.Fields{
"message": "Host has running task; clearing before terminating",
"job": j.ID(),
"job_type": j.Type().Name,
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"task": j.host.RunningTask,
})
j.AddError(model.ClearAndResetStrandedTask(j.host))
}
// convert the host to a cloud host
cloudHost, err := cloud.GetCloudHost(ctx, j.host, settings)
if err != nil {
err = errors.Wrapf(err, "error getting cloud host for %s", j.HostID)
j.AddError(err)
grip.Error(message.WrapError(err, message.Fields{
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"job_type": j.Type().Name,
"job": j.ID(),
"message": "problem getting cloud host instance, aborting termination",
}))
return
}
cloudStatus, err := cloudHost.GetInstanceStatus(ctx)
if err != nil {
// host may still be an intent host
if j.host.Status == evergreen.HostUninitialized {
if err = j.host.Terminate(evergreen.User); err != nil {
j.AddError(errors.Wrap(err, "problem terminating intent host in db"))
grip.Error(message.WrapError(err, message.Fields{
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"job_type": j.Type().Name,
"job": j.ID(),
"message": "problem terminating intent host in db",
}))
}
return
}
j.AddError(err)
grip.Error(message.WrapError(err, message.Fields{
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"job_type": j.Type().Name,
"job": j.ID(),
"message": "problem getting cloud host instance status",
}))
}
if cloudStatus == cloud.StatusTerminated {
j.AddError(errors.New("host is already terminated"))
grip.Error(message.Fields{
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"job_type": j.Type().Name,
"job": j.ID(),
"message": "attempted to terminated an already terminated host",
})
if err := j.host.Terminate(evergreen.User); err != nil {
j.AddError(errors.Wrap(err, "problem terminating host in db"))
grip.Error(message.WrapError(err, message.Fields{
"host": j.host.Id,
"provider": j.host.Distro.Provider,
"job_type": j.Type().Name,
"job": j.ID(),
"message": "problem terminating host in db",
}))
}
return
}
if j.host.Status != evergreen.HostProvisionFailed {
// only run teardown if provisioning was successful
if err := runHostTeardown(ctx, j.host, cloudHost); err != nil {
grip.Error(message.WrapError(err, message.Fields{
"job_type": j.Type().Name,
"message": "Error running teardown script",
"host": j.host.Id,
}))
subj := fmt.Sprintf("%v Error running teardown for host %v",
teardownFailurePreface, j.host.Id)
mailer, serr := j.env.GetSender(evergreen.SenderEmail)
if serr != nil {
grip.Alert(message.Fields{
"message": "problem getting sender",
"operation": "host termination issue",
"sender_err": serr,
"error": err,
"host": j.host.Id,
"subject": subj,
})
} else {
mailer.Send(message.NewEmailMessage(level.Error, message.Email{
From: settings.Notify.SMTP.From,
Recipients: settings.Notify.SMTP.AdminEmail,
Subject: subj,
Body: err.Error(),
}))
}
}
}
if err := cloudHost.TerminateInstance(ctx, evergreen.User); err != nil {
j.AddError(err)
grip.Error(message.WrapError(err, message.Fields{
"message": "problem terminating host",
"host": j.host.Id,
"job": j.ID(),
"job_type": j.Type().Name,
}))
return
}
hostBillingEnds := j.host.TerminationTime
pad := cloudHost.CloudMgr.TimeTilNextPayment(j.host)
if pad > time.Second {
hostBillingEnds = hostBillingEnds.Add(pad)
}
idleJob := newHostIdleJobForTermination(j.env, settings, cloudHost.CloudMgr, j.host, idleTimeStartsAt, hostBillingEnds)
idleJob.Run(ctx)
j.AddError(idleJob.Error())
}
func runHostTeardown(ctx context.Context, h *host.Host, cloudHost *cloud.CloudHost) error {
if h.Distro.Teardown == "" {
return nil
}
sshOptions, err := cloudHost.GetSSHOptions()
if err != nil {
return errors.Wrapf(err, "error getting ssh options for host %s", h.Id)
}
startTime := time.Now()
// run the teardown script with the agent
logs, err := h.RunSSHCommand(ctx, h.TearDownCommand(), sshOptions)
if err != nil {
event.LogHostTeardown(h.Id, logs, false, time.Since(startTime))
return errors.Wrapf(err, "error running teardown script on remote host: %s", logs)
}
event.LogHostTeardown(h.Id, logs, true, time.Since(startTime))
return nil
}