/
cloud.go
229 lines (193 loc) · 8.3 KB
/
cloud.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
package cloud
import (
"context"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/rest/model"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
// ProviderSettings exposes provider-specific configuration settings for a Manager.
type ProviderSettings interface {
Validate() error
// If zone is specified, returns the provider settings for that region.
// This is currently only being implemented for EC2 hosts.
FromDistroSettings(distro.Distro, string) error
}
// Manager is an interface which handles creating new hosts or modifying
// them via some third-party API.
type Manager interface {
// Load credentials or other settings from the config file
Configure(context.Context, *evergreen.Settings) error
// SpawnHost attempts to create a new host by requesting one from the
// provider's API.
SpawnHost(context.Context, *host.Host) (*host.Host, error)
// ModifyHost modifies an existing host
ModifyHost(context.Context, *host.Host, host.HostModifyOptions) error
// Get the status of an instance
GetInstanceStatus(context.Context, *host.Host) (CloudStatus, error)
// SetPortMappings sets the port mappings for the container
SetPortMappings(context.Context, *host.Host, *host.Host) error
// TerminateInstance destroys the host in the underlying provider
TerminateInstance(context.Context, *host.Host, string, string) error
// StopInstance stops an instance.
StopInstance(ctx context.Context, h *host.Host, shouldKeepOff bool, user string) error
// StartInstance starts a stopped instance.
StartInstance(context.Context, *host.Host, string) error
// GetDNSName returns the DNS name of a host.
GetDNSName(context.Context, *host.Host) (string, error)
// AttachVolume attaches a volume to a host.
AttachVolume(context.Context, *host.Host, *host.VolumeAttachment) error
// DetachVolume detaches a volume from a host.
DetachVolume(context.Context, *host.Host, string) error
// CreateVolume creates a new volume for attaching to a host.
CreateVolume(context.Context, *host.Volume) (*host.Volume, error)
// DeleteVolume deletes a volume.
DeleteVolume(context.Context, *host.Volume) error
// ModifyVolume modifies an existing volume.
ModifyVolume(context.Context, *host.Volume, *model.VolumeModifyOptions) error
// GetVolumeAttachment gets a volume's attachment
GetVolumeAttachment(context.Context, string) (*VolumeAttachment, error)
// CheckInstanceType determines if the given instance type is available in the current region.
CheckInstanceType(context.Context, string) error
// TimeTilNextPayment returns how long there is until the next payment
// is due for a particular host
TimeTilNextPayment(*host.Host) time.Duration
// Cleanup triggers the manager to clean up resources left behind by day-to-day operations.
Cleanup(context.Context) error
// AddSSHKey adds an SSH key for this manager's hosts. Adding an existing
// key is a no-op.
AddSSHKey(context.Context, evergreen.SSHKeyPair) error
}
type ContainerManager interface {
Manager
// GetContainers returns the IDs of all running containers on a specified host
GetContainers(context.Context, *host.Host) ([]string, error)
// RemoveOldestImage removes the earliest created image on a specified host
RemoveOldestImage(ctx context.Context, h *host.Host) error
// CalculateImageSpaceUsage returns the total space taken up by docker images on a specified host
CalculateImageSpaceUsage(ctx context.Context, h *host.Host) (int64, error)
// GetContainerImage downloads a container image onto parent specified by URL, and builds evergreen agent unless otherwise specified
GetContainerImage(ctx context.Context, parent *host.Host, options host.DockerOptions) error
}
// BatchManager is an interface for cloud providers that support batch operations.
type BatchManager interface {
// GetInstanceStatuses gets the statuses of a slice of instances. It returns
// a map of the instance IDs to their current status. If some of the
// instance statuses cannot be retrieved, implementations are allowed to
// either return an error or return StatusNonExistent for those hosts.
// If there is no error, implementations should return the same number of
// results in the map as there are hosts.
GetInstanceStatuses(context.Context, []host.Host) (map[string]CloudStatus, error)
}
// ManagerOpts is a struct containing the fields needed to get a new cloud manager
// of the proper type.
type ManagerOpts struct {
Provider string
Region string
ProviderKey string
ProviderSecret string
}
// GetSettings returns an uninitialized ProviderSettings based on the given
// provider.
func GetSettings(provider string) (ProviderSettings, error) {
switch provider {
case evergreen.ProviderNameEc2OnDemand, evergreen.ProviderNameEc2Fleet:
return &EC2ProviderSettings{}, nil
case evergreen.ProviderNameStatic:
return &StaticSettings{}, nil
case evergreen.ProviderNameMock:
return &MockProviderSettings{}, nil
case evergreen.ProviderNameDocker, evergreen.ProviderNameDockerMock:
return &dockerSettings{}, nil
}
return nil, errors.Errorf("invalid provider name '%s'", provider)
}
// GetManager returns an implementation of Manager for the given manager options.
// It returns an error if the provider name doesn't have a known implementation.
func GetManager(ctx context.Context, env evergreen.Environment, mgrOpts ManagerOpts) (Manager, error) {
var provider Manager
switch mgrOpts.Provider {
case evergreen.ProviderNameEc2OnDemand:
provider = &ec2Manager{
env: env,
EC2ManagerOptions: &EC2ManagerOptions{
client: &awsClientImpl{},
region: mgrOpts.Region,
providerKey: mgrOpts.ProviderKey,
providerSecret: mgrOpts.ProviderSecret,
},
}
case evergreen.ProviderNameEc2Fleet:
provider = &ec2FleetManager{
env: env,
EC2FleetManagerOptions: &EC2FleetManagerOptions{
client: &awsClientImpl{},
region: mgrOpts.Region,
providerKey: mgrOpts.ProviderKey,
providerSecret: mgrOpts.ProviderSecret,
},
}
case evergreen.ProviderNameStatic:
provider = &staticManager{}
case evergreen.ProviderNameMock:
provider = makeMockManager()
case evergreen.ProviderNameDocker:
provider = &dockerManager{env: env}
case evergreen.ProviderNameDockerMock:
provider = &dockerManager{env: env, client: &dockerClientMock{}}
default:
return nil, errors.Errorf("no known provider '%s'", mgrOpts.Provider)
}
if err := provider.Configure(ctx, env.Settings()); err != nil {
return nil, errors.Wrap(err, "configuring cloud provider")
}
return provider, nil
}
// GetManagerOptions gets the manager options from the provider settings object for a given
// provider name.
func GetManagerOptions(d distro.Distro) (ManagerOpts, error) {
region := ""
if len(d.ProviderSettingsList) > 1 {
if evergreen.IsEc2Provider(d.Provider) {
// this shouldn't ever happen, but if it does we want to continue so we don't inadvertently block task queues
grip.Error(message.Fields{
"message": "distro should be modified to have only one provider settings",
"provider_settings": d.ProviderSettingsList,
"distro": d.Id,
})
region = evergreen.DefaultEC2Region
} else {
return ManagerOpts{}, errors.Errorf(
"distro '%s' has multiple provider settings, but is provider '%s'", d.Id, d.Provider)
}
}
if evergreen.IsEc2Provider(d.Provider) {
s := &EC2ProviderSettings{}
if err := s.FromDistroSettings(d, region); err != nil {
return ManagerOpts{}, errors.Wrapf(err, "getting EC2 provider settings from distro")
}
return getEC2ManagerOptionsFromSettings(d.Provider, s), nil
}
if d.Provider == evergreen.ProviderNameMock {
return getMockManagerOptions(d.Provider, d.ProviderSettingsList)
}
return ManagerOpts{Provider: d.Provider}, nil
}
// ConvertContainerManager converts a regular manager into a container manager,
// errors if type conversion not possible.
func ConvertContainerManager(m Manager) (ContainerManager, error) {
if cm, ok := m.(ContainerManager); ok {
return cm, nil
}
return nil, errors.Errorf("programmer error: cannot convert manager %T to container manager", m)
}
// VolumeAttachment contains information about a volume attached to a host.
type VolumeAttachment struct {
VolumeID string
HostID string
DeviceName string
}