forked from evergreen-ci/evergreen
/
host_monitoring_container_state.go
122 lines (102 loc) · 2.92 KB
/
host_monitoring_container_state.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
package units
import (
"context"
"fmt"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/dependency"
"github.com/mongodb/amboy/job"
"github.com/mongodb/amboy/registry"
"github.com/pkg/errors"
)
const hostMonitorContainerStateJobName = "host-monitoring-container-state"
func init() {
registry.AddJobType(hostMonitorContainerStateJobName, func() amboy.Job {
return makeHostMonitorContainerStateJob()
})
}
type hostMonitorContainerStateJob struct {
HostID string `bson:"host_id" json:"host_id" yaml:"host_id"`
job.Base `bson:"base" json:"base" yaml:"base"`
// cache
host *host.Host
env evergreen.Environment
provider string
settings *evergreen.Settings
}
func makeHostMonitorContainerStateJob() *hostMonitorContainerStateJob {
j := &hostMonitorContainerStateJob{
Base: job.Base{
JobType: amboy.JobType{
Name: hostMonitorContainerStateJobName,
Version: 0,
},
},
}
j.SetDependency(dependency.NewAlways())
return j
}
func NewHostMonitorContainerStateJob(env evergreen.Environment, h *host.Host, providerName, id string) amboy.Job {
job := makeHostMonitorContainerStateJob()
job.host = h
job.provider = providerName
job.HostID = h.Id
job.SetID(fmt.Sprintf("%s.%s.%s", hostMonitorContainerStateJobName, job.HostID, id))
return job
}
func (j *hostMonitorContainerStateJob) Run(ctx context.Context) {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
defer j.MarkComplete()
var err error
if j.host == nil {
j.host, err = host.FindOneId(j.HostID)
j.AddError(err)
}
if j.env == nil {
j.env = evergreen.GetEnvironment()
}
if j.settings == nil {
j.settings = j.env.Settings()
}
if j.HasErrors() {
return
}
// get containers on parent
containersFromDB, err := j.host.GetContainers()
if err != nil {
j.AddError(errors.Wrapf(err, "error getting containers on parent %s from DB", j.HostID))
return
}
// list containers using Docker provider
mgr, err := cloud.GetManager(ctx, j.provider, j.settings)
if err != nil {
j.AddError(errors.Wrap(err, "error getting Docker manager"))
return
}
containerMgr, err := cloud.ConvertContainerManager(mgr)
if err != nil {
j.AddError(errors.Wrap(err, "error getting Docker manager"))
return
}
containerIdsFromDocker, err := containerMgr.GetContainers(ctx, j.host)
if err != nil {
j.AddError(errors.Wrapf(err, "error getting containers on parent %s from Docker", j.HostID))
return
}
// build map of running container IDs
isRunning := make(map[string]bool)
for _, id := range containerIdsFromDocker {
isRunning[id] = true
}
// for each non-terminated container in DB that is not actually running on
// Docker, mark it as terminated
for _, container := range containersFromDB {
if !isRunning[container.Id] {
j.AddError(container.SetTerminated(evergreen.User))
}
}
}