-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAwsSpotinstCloud.java
404 lines (320 loc) · 15.6 KB
/
AwsSpotinstCloud.java
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
package hudson.plugins.spotinst.cloud;
import hudson.Extension;
import hudson.model.Node;
import hudson.plugins.spotinst.api.infra.ApiResponse;
import hudson.plugins.spotinst.api.infra.JsonMapper;
import hudson.plugins.spotinst.common.ConnectionMethodEnum;
import hudson.plugins.spotinst.common.SpotAwsInstanceTypesHelper;
import hudson.plugins.spotinst.model.aws.*;
import hudson.plugins.spotinst.repos.IAwsGroupRepo;
import hudson.plugins.spotinst.repos.RepoManager;
import hudson.plugins.spotinst.slave.*;
import hudson.slaves.ComputerConnector;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.tools.ToolLocationNodeProperty;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
/**
* Created by ohadmuchnik on 20/03/2017.
*/
public class AwsSpotinstCloud extends BaseSpotinstCloud {
//region Members
private static final Logger LOGGER =
LoggerFactory.getLogger(AwsSpotinstCloud.class);
private static final String CLOUD_URL = "aws/ec2";
protected Map<String, Integer> executorsByInstanceType;
private List<? extends SpotinstInstanceWeight> executorsForTypes;
//endregion
//region Constructor
@DataBoundConstructor
public AwsSpotinstCloud(String groupId, String labelString, String idleTerminationMinutes, String workspaceDir,
List<? extends SpotinstInstanceWeight> executorsForTypes, SlaveUsageEnum usage,
String tunnel, Boolean shouldUseWebsocket, Boolean shouldRetriggerBuilds, String vmargs,
EnvironmentVariablesNodeProperty environmentVariables,
ToolLocationNodeProperty toolLocations, String accountId,
ConnectionMethodEnum connectionMethod, ComputerConnector computerConnector,
Boolean shouldUsePrivateIp, SpotGlobalExecutorOverride globalExecutorOverride) {
super(groupId, labelString, idleTerminationMinutes, workspaceDir, usage, tunnel, shouldUseWebsocket,
shouldRetriggerBuilds, vmargs, environmentVariables, toolLocations, accountId, connectionMethod,
computerConnector, shouldUsePrivateIp, globalExecutorOverride);
this.executorsForTypes = new LinkedList<>();
if (executorsForTypes != null) {
this.executorsForTypes = executorsForTypes;
}
initExecutorsByInstanceType();
}
//endregion
//region Overrides
@Override
List<SpotinstSlave> scaleUp(ProvisionRequest request) {
List<SpotinstSlave> retVal = new LinkedList<>();
IAwsGroupRepo awsGroupRepo = RepoManager.getInstance().getAwsGroupRepo();
ApiResponse<AwsScaleUpResult> scaleUpResponse =
awsGroupRepo.scaleUp(groupId, request.getExecutors(), this.accountId);
if (scaleUpResponse.isRequestSucceed()) {
AwsScaleUpResult scaleUpResult = scaleUpResponse.getValue();
if (scaleUpResult != null) {
LOGGER.info(String.format("Scale up group %s succeeded", groupId));
if (scaleUpResult.getNewInstances() != null) {
List<SpotinstSlave> newInstanceSlaves = handleNewAwsInstances(scaleUpResult, request.getLabel());
retVal.addAll(newInstanceSlaves);
}
if (scaleUpResult.getNewSpotRequests() != null) {
List<SpotinstSlave> newSpotSlaves = handleNewAwsSpots(scaleUpResult, request.getLabel());
retVal.addAll(newSpotSlaves);
}
}
else {
LOGGER.error(String.format("Failed to scale up group: %s", groupId));
}
}
else {
LOGGER.error(
String.format("Failed to scale up group: %s. Errors: %s", groupId, scaleUpResponse.getErrors()));
}
return retVal;
}
@Override
public Boolean detachInstance(String instanceId) {
Boolean retVal = false;
IAwsGroupRepo awsGroupRepo = RepoManager.getInstance().getAwsGroupRepo();
ApiResponse<Boolean> detachInstanceResponse = awsGroupRepo.detachInstance(instanceId, this.accountId);
if (detachInstanceResponse.isRequestSucceed()) {
LOGGER.info(String.format("Instance %s detached", instanceId));
retVal = true;
}
else {
LOGGER.error(String.format("Failed to detach instance %s. Errors: %s", instanceId,
detachInstanceResponse.getErrors()));
}
return retVal;
}
@Override
public void syncGroupInstances() {
IAwsGroupRepo awsGroupRepo = RepoManager.getInstance().getAwsGroupRepo();
ApiResponse<List<AwsGroupInstance>> instancesResponse = awsGroupRepo.getGroupInstances(groupId, this.accountId);
if (instancesResponse.isRequestSucceed()) {
List<AwsGroupInstance> instances = instancesResponse.getValue();
LOGGER.info(String.format("There are %s instances in group %s", instances.size(), groupId));
Map<String, SlaveInstanceDetails> slaveInstancesDetailsByInstanceId = new HashMap<>();
for (AwsGroupInstance instance : instances) {
SlaveInstanceDetails instanceDetails = SlaveInstanceDetails.build(instance);
slaveInstancesDetailsByInstanceId.put(instanceDetails.getInstanceId(), instanceDetails);
}
this.slaveInstancesDetailsByInstanceId = new HashMap<>(slaveInstancesDetailsByInstanceId);
addNewSlaveInstances(instances);
removeOldSlaveInstances(instances);
}
else {
LOGGER.error(String.format("Failed to get group %s instances. Errors: %s", groupId,
instancesResponse.getErrors()));
}
}
@Override
public Map<String, String> getInstanceIpsById() {
Map<String, String> retVal = new HashMap<>();
IAwsGroupRepo awsGroupRepo = RepoManager.getInstance().getAwsGroupRepo();
ApiResponse<List<AwsGroupInstance>> instancesResponse = awsGroupRepo.getGroupInstances(groupId, this.accountId);
if (instancesResponse.isRequestSucceed()) {
List<AwsGroupInstance> instances = instancesResponse.getValue();
for (AwsGroupInstance instance : instances) {
if (this.getShouldUsePrivateIp()) {
retVal.put(instance.getInstanceId(), instance.getPrivateIp());
}
else {
retVal.put(instance.getInstanceId(), instance.getPublicIp());
}
}
}
else {
LOGGER.error(String.format("Failed to get group %s instances. Errors: %s", groupId,
instancesResponse.getErrors()));
}
return retVal;
}
@Override
public String getCloudUrl() {
return CLOUD_URL;
}
@Override
protected Integer getDefaultExecutorsNumber(String instanceType) {
Integer retVal = null;
LOGGER.info(String.format("Getting the # of default executors for instance type: %s", instanceType));
Optional<AwsInstanceType> awsInstanceType = SpotAwsInstanceTypesHelper.getAllInstanceTypes().stream()
.filter(i -> i.getInstanceType()
.equals(instanceType))
.findFirst();
if (awsInstanceType.isPresent()) {
retVal = awsInstanceType.get().getVCPU();
}
return retVal;
}
//endregion
//region Private Methods
@Override
protected Integer getNumOfExecutors(String instanceType) {
Integer retVal;
if (executorsByInstanceType == null) {
initExecutorsByInstanceType();
}
if (executorsByInstanceType.containsKey(instanceType)) {
retVal = executorsByInstanceType.get(instanceType);
LOGGER.info(String.format("We have a weight definition for this type of %s", retVal));
}
else {
retVal = super.getNumOfExecutors(instanceType);
}
return retVal;
}
private List<SpotinstSlave> handleNewAwsSpots(AwsScaleUpResult scaleUpResult, String label) {
List<SpotinstSlave> retVal = new LinkedList<>();
LOGGER.info(String.format("%s new spot requests created", scaleUpResult.getNewSpotRequests().size()));
for (AwsScaleResultNewSpot spot : scaleUpResult.getNewSpotRequests()) {
SpotinstSlave slave = handleNewAwsInstance(spot.getInstanceId(), spot.getInstanceType(), label);
retVal.add(slave);
}
return retVal;
}
private List<SpotinstSlave> handleNewAwsInstances(AwsScaleUpResult scaleUpResult, String label) {
List<SpotinstSlave> retVal = new LinkedList<>();
LOGGER.info(String.format("%s new instances launched", scaleUpResult.getNewInstances().size()));
for (AwsScaleResultNewInstance instance : scaleUpResult.getNewInstances()) {
SpotinstSlave slave = handleNewAwsInstance(instance.getInstanceId(), instance.getInstanceType(), label);
retVal.add(slave);
}
return retVal;
}
private SpotinstSlave handleNewAwsInstance(String instanceId, String instanceType, String label) {
Integer executors = getNumOfExecutors(instanceType);
addToPending(instanceId, executors, PendingInstance.StatusEnum.INSTANCE_INITIATING, label);
SpotinstSlave retVal = buildSpotinstSlave(instanceId, instanceType, String.valueOf(executors));
return retVal;
}
private void addNewSlaveInstances(List<AwsGroupInstance> elastigroupInstances) {
if (elastigroupInstances.size() > 0) {
for (AwsGroupInstance instance : elastigroupInstances) {
Boolean isSlaveExist = isSlaveExistForInstance(instance);
if (isSlaveExist == false) {
LOGGER.info(String.format("Instance: %s of group: %s doesn't have slave , adding new one",
JsonMapper.toJson(instance), groupId));
addSpotinstSlave(instance);
}
}
}
else {
LOGGER.info(String.format("There are no new instances to add for group: %s", groupId));
}
}
private void removeOldSlaveInstances(List<AwsGroupInstance> elastigroupInstances) {
List<SpotinstSlave> allGroupsSlaves = getAllSpotinstSlaves();
if (allGroupsSlaves.size() > 0) {
LOGGER.info(String.format("Found %s existing nodes for group %s", allGroupsSlaves.size(), groupId));
List<String> groupInstanceAndSpotRequestIds = getGroupInstanceAndSpotIds(elastigroupInstances);
for (SpotinstSlave slave : allGroupsSlaves) {
String slaveInstanceId = slave.getInstanceId();
if (slaveInstanceId != null) {
if (groupInstanceAndSpotRequestIds.contains(slaveInstanceId) == false) {
LOGGER.info(
String.format("Slave for instance: %s is no longer running in group: %s, removing it",
slaveInstanceId, groupId));
try {
Jenkins.getInstance().removeNode(slave);
LOGGER.info(String.format("Slave: %s removed successfully", slaveInstanceId));
}
catch (IOException e) {
LOGGER.error(String.format("Failed to remove slave from group: %s", groupId), e);
}
}
else {
// looking for zombie slave instances which meet the following conditions and clearing them from the EG when 'SLAVE_OFFLINE_THRESHOLD_IN_MINUTES' is meet
// 1.is a group slave for this slave id; - trivial here
// 2.slave is offline
// 3.slave is not connecting
// 4.zombie threshold passed
terminateOfflineSlaves(slave, slaveInstanceId);
}
}
}
}
else {
LOGGER.info(String.format("There are no slaves for group: %s", groupId));
}
}
private List<String> getGroupInstanceAndSpotIds(List<AwsGroupInstance> elastigroupInstances) {
List<String> retVal = new LinkedList<>();
for (AwsGroupInstance instance : elastigroupInstances) {
if (instance.getInstanceId() != null) {
retVal.add(instance.getInstanceId());
}
if (instance.getSpotInstanceRequestId() != null) {
retVal.add(instance.getSpotInstanceRequestId());
}
}
return retVal;
}
private Boolean isSlaveExistForInstance(AwsGroupInstance instance) {
Boolean retVal = false;
Node node;
String instanceId = instance.getInstanceId();
if (instanceId != null) {
LOGGER.info(String.format("Checking if slave exist for instance id: %s", instanceId));
node = Jenkins.getInstance().getNode(instanceId);
if (node != null) {
LOGGER.info(String.format("Found slave for instance id: %s", instanceId));
retVal = true;
}
}
String spotRequestId = instance.getSpotInstanceRequestId();
if (retVal == false && spotRequestId != null) {
LOGGER.info(String.format("Checking if slave exist for spot request id: %s", spotRequestId));
node = Jenkins.getInstance().getNode(spotRequestId);
if (node != null) {
LOGGER.info(String.format("Found slave for spot request id: %s", spotRequestId));
retVal = true;
}
}
return retVal;
}
private void addSpotinstSlave(AwsGroupInstance instance) {
SpotinstSlave slave = handleNewAwsInstance(instance.getInstanceId(), instance.getInstanceType(), null);
if (slave != null) {
try {
Jenkins.getInstance().addNode(slave);
}
catch (IOException e) {
LOGGER.error(String.format("Failed to create node for slave: %s", slave.getInstanceId()), e);
}
}
}
private void initExecutorsByInstanceType() {
this.executorsByInstanceType = new HashMap<>();
if (this.executorsForTypes != null) {
for (SpotinstInstanceWeight instance : this.executorsForTypes) {
if (instance.getExecutors() != null) {
Integer executors = instance.getExecutors();
String type = instance.getAwsInstanceTypeFromAPI();
this.executorsByInstanceType.put(type, executors);
}
}
}
}
//endregion
//region Getters
public List<? extends SpotinstInstanceWeight> getExecutorsForTypes() {
return executorsForTypes;
}
//endregion
//region Classes
@Extension
public static class DescriptorImpl extends BaseSpotinstCloud.DescriptorImpl {
@Override
public String getDisplayName() {
return "Spot AWS Elastigroup";
}
}
//endregion
}