-
Notifications
You must be signed in to change notification settings - Fork 690
/
EC2Cloud.java
1275 lines (1108 loc) · 55.8 KB
/
EC2Cloud.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License
*
* Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number of other of contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package hudson.plugins.ec2;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.InstanceProfileCredentialsProvider;
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl;
import com.cloudbees.jenkins.plugins.awscredentials.AmazonWebServicesCredentials;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ProxyConfiguration;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.PeriodicWork;
import hudson.model.TaskListener;
import hudson.plugins.ec2.util.AmazonEC2Factory;
import hudson.security.ACL;
import hudson.slaves.Cloud;
import hudson.slaves.NodeProvisioner.PlannedNode;
import hudson.util.FormValidation;
import hudson.util.HttpResponses;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import hudson.util.StreamTaskListener;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import javax.servlet.ServletException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.verb.POST;
/**
* Hudson's view of EC2.
*
* @author Kohsuke Kawaguchi
*/
public abstract class EC2Cloud extends Cloud {
private static final Logger LOGGER = Logger.getLogger(EC2Cloud.class.getName());
public static final String DEFAULT_EC2_HOST = "us-east-1";
public static final String DEFAULT_EC2_ENDPOINT = "https://ec2.amazonaws.com";
public static final String AWS_URL_HOST = "amazonaws.com";
public static final String AWS_CN_URL_HOST = "amazonaws.com.cn";
public static final String EC2_SLAVE_TYPE_SPOT = "spot";
public static final String EC2_SLAVE_TYPE_DEMAND = "demand";
public static final String EC2_REQUEST_EXPIRED_ERROR_CODE = "RequestExpired";
private static final SimpleFormatter sf = new SimpleFormatter();
private transient ReentrantLock slaveCountingLock = new ReentrantLock();
private final boolean useInstanceProfileForCredentials;
private final String roleArn;
private final String roleSessionName;
/**
* Id of the {@link AmazonWebServicesCredentials} used to connect to Amazon ECS
*/
@CheckForNull
private String credentialsId;
@CheckForNull
@Deprecated
private transient String accessId;
@CheckForNull
@Deprecated
private transient Secret secretKey;
@CheckForNull
@Deprecated
private transient EC2PrivateKey privateKey;
@CheckForNull
private String sshKeysCredentialsId;
/**
* Upper bound on how many instances we may provision.
*/
private final int instanceCap;
private List<? extends SlaveTemplate> templates;
private transient KeyPair usableKeyPair;
private transient volatile AmazonEC2 connection;
protected EC2Cloud(String name, boolean useInstanceProfileForCredentials, String credentialsId, String privateKey, String sshKeysCredentialsId,
String instanceCapStr, List<? extends SlaveTemplate> templates, String roleArn, String roleSessionName) {
super(name);
this.useInstanceProfileForCredentials = useInstanceProfileForCredentials;
this.roleArn = roleArn;
this.roleSessionName = roleSessionName;
this.credentialsId = Util.fixEmpty(credentialsId);
this.sshKeysCredentialsId = Util.fixEmpty(sshKeysCredentialsId);
if (templates == null) {
this.templates = Collections.emptyList();
} else {
this.templates = templates;
}
if (instanceCapStr == null || instanceCapStr.isEmpty()) {
this.instanceCap = Integer.MAX_VALUE;
} else {
this.instanceCap = Integer.parseInt(instanceCapStr);
}
readResolve(); // set parents
}
@Deprecated
protected EC2Cloud(String id, boolean useInstanceProfileForCredentials, String credentialsId, String privateKey,
String instanceCapStr, List<? extends SlaveTemplate> templates, String roleArn, String roleSessionName) {
this(id, useInstanceProfileForCredentials, credentialsId, privateKey, null, instanceCapStr, templates, roleArn, roleSessionName);
}
@CheckForNull
public EC2PrivateKey resolvePrivateKey(){
if (sshKeysCredentialsId != null) {
SSHUserPrivateKey privateKeyCredential = getSshCredential(sshKeysCredentialsId, Jenkins.get());
if (privateKeyCredential != null) {
return new EC2PrivateKey(privateKeyCredential.getPrivateKey());
}
}
return null;
}
public abstract URL getEc2EndpointUrl() throws IOException;
public abstract URL getS3EndpointUrl() throws IOException;
public void addTemplate(SlaveTemplate newTemplate) throws Exception {
String newTemplateDescription = newTemplate.description;
if (getTemplate(newTemplateDescription) != null) throw new Exception(
String.format("A SlaveTemplate with description %s already exists", newTemplateDescription));
List<SlaveTemplate> templatesHolder = new ArrayList<>(templates);
templatesHolder.add(newTemplate);
templates = templatesHolder;
}
private void migratePrivateSshKeyToCredential(String privateKey) {
// GET matching private key credential from Credential API if exists
Optional<SSHUserPrivateKey> keyCredential = SystemCredentialsProvider.getInstance().getCredentials()
.stream()
.filter((cred) -> cred instanceof SSHUserPrivateKey)
.filter((cred) -> ((SSHUserPrivateKey)cred).getPrivateKey().trim().equals(privateKey.trim()))
.map(cred -> (SSHUserPrivateKey)cred)
.findFirst();
if (keyCredential.isPresent()){
// SET this.sshKeysCredentialsId with the found credential
sshKeysCredentialsId = keyCredential.get().getId();
} else {
// CREATE new credential
String credsId = UUID.randomUUID().toString();
SSHUserPrivateKey sshKeyCredentials = new BasicSSHUserPrivateKey(CredentialsScope.SYSTEM, credsId, "key",
new BasicSSHUserPrivateKey.PrivateKeySource() {
@NonNull
@Override
public List<String> getPrivateKeys() {
return Collections.singletonList(privateKey.trim());
}
}, "", "EC2 Cloud Private Key - " + getDisplayName());
addNewGlobalCredential(sshKeyCredentials);
sshKeysCredentialsId = credsId;
}
}
protected Object readResolve() {
this.slaveCountingLock = new ReentrantLock();
for (SlaveTemplate t : templates)
t.parent = this;
if (this.sshKeysCredentialsId == null && this.privateKey != null ){
migratePrivateSshKeyToCredential(this.privateKey.getPrivateKey());
}
this.privateKey = null; // This enforces it not to be persisted and that CasC will never output privateKey on export
if (this.accessId != null && this.secretKey != null && credentialsId == null) {
String secretKeyEncryptedValue = this.secretKey.getEncryptedValue();
// REPLACE this.accessId and this.secretId by a credential
SystemCredentialsProvider systemCredentialsProvider = SystemCredentialsProvider.getInstance();
// ITERATE ON EXISTING CREDS AND DON'T CREATE IF EXIST
for (Credentials credentials: systemCredentialsProvider.getCredentials()) {
if (credentials instanceof AmazonWebServicesCredentials) {
AmazonWebServicesCredentials awsCreds = (AmazonWebServicesCredentials) credentials;
AWSCredentials awsCredentials = awsCreds.getCredentials();
if (accessId.equals(awsCredentials.getAWSAccessKeyId()) &&
Secret.toString(this.secretKey).equals(awsCredentials.getAWSSecretKey())) {
this.credentialsId = awsCreds.getId();
this.accessId = null;
this.secretKey = null;
return this;
}
}
}
// CREATE
String credsId = UUID.randomUUID().toString();
addNewGlobalCredential(new AWSCredentialsImpl(
CredentialsScope.SYSTEM, credsId, this.accessId, secretKeyEncryptedValue,
"EC2 Cloud - " + getDisplayName()));
this.credentialsId = credsId;
this.accessId = null;
this.secretKey = null;
// PROBLEM, GLOBAL STORE NOT FOUND
LOGGER.log(Level.WARNING, "EC2 Plugin could not migrate credentials to the Jenkins Global Credentials Store, EC2 Plugin for cloud {0} must be manually reconfigured", getDisplayName());
}
return this;
}
private void addNewGlobalCredential(Credentials credentials){
for (CredentialsStore credentialsStore: CredentialsProvider.lookupStores(Jenkins.get())) {
if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
try {
credentialsStore.addCredentials(Domain.global(), credentials);
} catch (IOException e) {
this.credentialsId = null;
LOGGER.log(Level.WARNING, "Exception converting legacy configuration to the new credentials API", e);
}
}
}
}
public boolean isUseInstanceProfileForCredentials() {
return useInstanceProfileForCredentials;
}
public String getRoleArn() {
return roleArn;
}
public String getRoleSessionName() {
return roleSessionName;
}
public String getCredentialsId() {
return credentialsId;
}
@CheckForNull
public String getSshKeysCredentialsId() {
return sshKeysCredentialsId;
}
@Deprecated
public EC2PrivateKey getPrivateKey() {
return privateKey;
}
public String getInstanceCapStr() {
if (instanceCap == Integer.MAX_VALUE)
return "";
else
return String.valueOf(instanceCap);
}
public int getInstanceCap() {
return instanceCap;
}
public List<SlaveTemplate> getTemplates() {
return Collections.unmodifiableList(templates);
}
@CheckForNull
public SlaveTemplate getTemplate(String template) {
for (SlaveTemplate t : templates) {
if (t.description.equals(template)) {
return t;
}
}
return null;
}
/**
* Gets {@link SlaveTemplate} that has the matching {@link Label}.
*/
@Deprecated
public SlaveTemplate getTemplate(Label label) {
for (SlaveTemplate t : templates) {
if (t.getMode() == Node.Mode.NORMAL) {
if (label == null || label.matches(t.getLabelSet())) {
return t;
}
} else if (t.getMode() == Node.Mode.EXCLUSIVE) {
if (label != null && label.matches(t.getLabelSet())) {
return t;
}
}
}
return null;
}
/**
* Gets list of {@link SlaveTemplate} that matches {@link Label}.
*/
public Collection<SlaveTemplate> getTemplates(Label label) {
List<SlaveTemplate> matchingTemplates = new ArrayList<>();
for (SlaveTemplate t : templates) {
if (t.getMode() == Node.Mode.NORMAL) {
if (label == null || label.matches(t.getLabelSet())) {
matchingTemplates.add(t);
}
} else if (t.getMode() == Node.Mode.EXCLUSIVE) {
if (label != null && label.matches(t.getLabelSet())) {
matchingTemplates.add(t);
}
}
}
return matchingTemplates;
}
/**
* Gets the {@link KeyPairInfo} used for the launch.
*/
@CheckForNull
public synchronized KeyPair getKeyPair() throws AmazonClientException, IOException {
if (usableKeyPair == null) {
EC2PrivateKey ec2PrivateKey = this.resolvePrivateKey();
if (ec2PrivateKey != null) {
usableKeyPair = ec2PrivateKey.find(connect());
}
}
return usableKeyPair;
}
/**
* Debug command to attach to a running instance.
*/
@RequirePOST
public void doAttach(StaplerRequest req, StaplerResponse rsp, @QueryParameter String id)
throws ServletException, IOException, AmazonClientException {
checkPermission(PROVISION);
SlaveTemplate t = getTemplates().get(0);
StringWriter sw = new StringWriter();
StreamTaskListener listener = new StreamTaskListener(sw);
EC2AbstractSlave node = t.attach(id, listener);
Jenkins.get().addNode(node);
rsp.sendRedirect2(req.getContextPath() + "/computer/" + node.getNodeName());
}
@RequirePOST
public HttpResponse doProvision(@QueryParameter String template) throws ServletException, IOException {
checkPermission(PROVISION);
if (template == null) {
throw HttpResponses.error(SC_BAD_REQUEST, "The 'template' query parameter is missing");
}
SlaveTemplate t = getTemplate(template);
if (t == null) {
throw HttpResponses.error(SC_BAD_REQUEST, "No such template: " + template);
}
final Jenkins jenkinsInstance = Jenkins.get();
if (jenkinsInstance.isQuietingDown()) {
throw HttpResponses.error(SC_BAD_REQUEST, "Jenkins instance is quieting down");
}
if (jenkinsInstance.isTerminating()) {
throw HttpResponses.error(SC_BAD_REQUEST, "Jenkins instance is terminating");
}
try {
List<EC2AbstractSlave> nodes = getNewOrExistingAvailableSlave(t, 1, true);
if (nodes == null || nodes.isEmpty())
throw HttpResponses.error(SC_BAD_REQUEST, "Cloud or AMI instance cap would be exceeded for: " + template);
//Reconnect a stopped instance, the ADD is invoking the connect only for the node creation
Computer c = nodes.get(0).toComputer();
if (nodes.get(0).getStopOnTerminate() && c != null) {
c.connect(false);
}
jenkinsInstance.addNode(nodes.get(0));
return HttpResponses.redirectViaContextPath("/computer/" + nodes.get(0).getNodeName());
} catch (AmazonClientException e) {
throw HttpResponses.error(SC_INTERNAL_SERVER_ERROR, e);
}
}
/**
* Counts the number of instances in EC2 that can be used with the specified image and a template. Also removes any
* nodes associated with canceled requests.
*
* @param template If left null, then all instances are counted.
*/
private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientException {
String jenkinsServerUrl = JenkinsLocationConfiguration.get().getUrl();
if (jenkinsServerUrl == null) {
LOGGER.log(Level.WARNING, "No Jenkins server URL specified, it is strongly recommended to open /configure and set the server URL. " +
"Not having has disabled the per-controller instance cap counting (cf. https://github.com/jenkinsci/ec2-plugin/pull/310)");
}
LOGGER.log(Level.FINE, "Counting current agents: "
+ (template != null ? (" AMI: " + template.getAmi() + " TemplateDesc: " + template.description) : " All AMIS")
+ " Jenkins Server: " + jenkinsServerUrl);
int n = 0;
Set<String> instanceIds = new HashSet<>();
String description = template != null ? template.description : null;
List<Filter> filters = getGenericFilters(jenkinsServerUrl, template);
filters.add(new Filter("instance-state-name").withValues("running", "pending", "stopping"));
DescribeInstancesRequest dir = new DescribeInstancesRequest().withFilters(filters);
DescribeInstancesResult result = null;
do {
result = connect().describeInstances(dir);
dir.setNextToken(result.getNextToken());
for (Reservation r : result.getReservations()) {
for (Instance i : r.getInstances()) {
if (isEc2ProvisionedAmiSlave(i.getTags(), description)) {
LOGGER.log(Level.FINE, "Existing instance found: " + i.getInstanceId() + " AMI: " + i.getImageId()
+ (template != null ? (" Template: " + description) : "") + " Jenkins Server: " + jenkinsServerUrl);
n++;
instanceIds.add(i.getInstanceId());
}
}
}
} while(result.getNextToken() != null);
n += countCurrentEC2SpotSlaves(template, jenkinsServerUrl, instanceIds);
return n;
}
/**
* Counts the number of EC2 Spot instances that can be used with the specified image and a template. Also removes any
* nodes associated with canceled requests.
*
* @param template If left null, then all spot instances are counted.
*/
private int countCurrentEC2SpotSlaves(SlaveTemplate template, String jenkinsServerUrl, Set<String> instanceIds) throws AmazonClientException {
int n = 0;
String description = template != null ? template.description : null;
List<SpotInstanceRequest> sirs = null;
List<Filter> filters = getGenericFilters(jenkinsServerUrl, template);
if (template != null) {
filters.add(new Filter("launch.image-id").withValues(template.getAmi()));
}
DescribeSpotInstanceRequestsRequest dsir = new DescribeSpotInstanceRequestsRequest().withFilters(filters).withMaxResults(100);
Set<SpotInstanceRequest> sirSet = new HashSet<>();
DescribeSpotInstanceRequestsResult sirResp = null;
do {
try {
sirResp = connect().describeSpotInstanceRequests(dsir);
sirs = sirResp.getSpotInstanceRequests();
dsir.setNextToken(sirResp.getNextToken());
} catch (Exception ex) {
// Some ec2 implementations don't implement spot requests (Eucalyptus)
LOGGER.log(Level.FINEST, "Describe spot instance requests failed", ex);
break;
}
if (sirs != null) {
for (SpotInstanceRequest sir : sirs) {
sirSet.add(sir);
if (sir.getState().equals("open") || sir.getState().equals("active")) {
if (sir.getInstanceId() != null && instanceIds.contains(sir.getInstanceId()))
continue;
if (isEc2ProvisionedAmiSlave(sir.getTags(), description)) {
LOGGER.log(Level.FINE, "Spot instance request found: " + sir.getSpotInstanceRequestId() + " AMI: "
+ sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus());
n++;
if (sir.getInstanceId() != null)
instanceIds.add(sir.getInstanceId());
}
} else {
// Cancelled or otherwise dead
for (Node node : Jenkins.get().getNodes()) {
try {
if (!(node instanceof EC2SpotSlave))
continue;
EC2SpotSlave ec2Slave = (EC2SpotSlave) node;
if (ec2Slave.getSpotInstanceRequestId().equals(sir.getSpotInstanceRequestId())) {
LOGGER.log(Level.INFO, "Removing dead request: " + sir.getSpotInstanceRequestId() + " AMI: "
+ sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus());
Jenkins.get().removeNode(node);
break;
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to remove node for dead request: " + sir.getSpotInstanceRequestId()
+ " AMI: " + sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus(),
e);
}
}
}
}
}
} while(sirResp.getNextToken() != null);
n += countJenkinsNodeSpotInstancesWithoutRequests(template, sirSet, instanceIds);
return n;
}
// Count nodes where the spot request does not yet exist (sometimes it takes time for the request to appear
// in the EC2 API)
private int countJenkinsNodeSpotInstancesWithoutRequests(SlaveTemplate template, Set<SpotInstanceRequest> sirSet, Set<String> instanceIds) throws AmazonClientException {
int n = 0;
for (Node node : Jenkins.get().getNodes()) {
if (!(node instanceof EC2SpotSlave))
continue;
EC2SpotSlave ec2Slave = (EC2SpotSlave) node;
SpotInstanceRequest sir = ec2Slave.getSpotRequest();
if (sir == null) {
LOGGER.log(Level.FINE, "Found spot node without request: " + ec2Slave.getSpotInstanceRequestId());
n++;
continue;
}
if (sirSet.contains(sir))
continue;
sirSet.add(sir);
if (sir.getState().equals("open") || sir.getState().equals("active")) {
if (template != null) {
List<Tag> instanceTags = sir.getTags();
for (Tag tag : instanceTags) {
if (StringUtils.equals(tag.getKey(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE) && StringUtils.equals(tag.getValue(), getSlaveTypeTagValue(EC2_SLAVE_TYPE_SPOT, template.description)) && sir.getLaunchSpecification().getImageId().equals(template.getAmi())) {
if (sir.getInstanceId() != null && instanceIds.contains(sir.getInstanceId()))
continue;
LOGGER.log(Level.FINE, "Spot instance request found (from node): " + sir.getSpotInstanceRequestId() + " AMI: "
+ sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus());
n++;
if (sir.getInstanceId() != null)
instanceIds.add(sir.getInstanceId());
}
}
}
}
}
return n;
}
private List<Filter> getGenericFilters(String jenkinsServerUrl, SlaveTemplate template) {
List<Filter> filters = new ArrayList<>();
filters.add(new Filter("tag-key").withValues(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE));
if (jenkinsServerUrl != null) {
// The instances must match the jenkins server url
filters.add(new Filter("tag:" + EC2Tag.TAG_NAME_JENKINS_SERVER_URL).withValues(jenkinsServerUrl));
} else {
filters.add(new Filter("tag-key").withValues(EC2Tag.TAG_NAME_JENKINS_SERVER_URL));
}
if (template != null) {
List<EC2Tag> tags = template.getTags();
if (tags != null) {
for (EC2Tag tag : tags) {
if (tag.getName() != null && tag.getValue() != null) {
filters.add(new Filter("tag:" + tag.getName()).withValues(tag.getValue()));
}
}
}
}
return filters;
}
private boolean isEc2ProvisionedAmiSlave(List<Tag> tags, String description) {
for (Tag tag : tags) {
if (StringUtils.equals(tag.getKey(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
if (description == null) {
return true;
} else if (StringUtils.equals(tag.getValue(), EC2Cloud.EC2_SLAVE_TYPE_DEMAND)
|| StringUtils.equals(tag.getValue(), EC2Cloud.EC2_SLAVE_TYPE_SPOT)) {
// To handle cases where description is null and also upgrade cases for existing agent nodes.
return true;
} else if (StringUtils.equals(tag.getValue(), getSlaveTypeTagValue(EC2Cloud.EC2_SLAVE_TYPE_DEMAND, description))
|| StringUtils.equals(tag.getValue(), getSlaveTypeTagValue(EC2Cloud.EC2_SLAVE_TYPE_SPOT, description))) {
return true;
} else {
return false;
}
}
}
return false;
}
/**
* Returns the maximum number of possible agents that can be created.
*/
private int getPossibleNewSlavesCount(SlaveTemplate template) throws AmazonClientException {
int estimatedTotalSlaves = countCurrentEC2Slaves(null);
int estimatedAmiSlaves = countCurrentEC2Slaves(template);
int availableTotalSlaves = instanceCap - estimatedTotalSlaves;
int availableAmiSlaves = template.getInstanceCap() - estimatedAmiSlaves;
LOGGER.log(Level.FINE, "Available Total Agents: " + availableTotalSlaves + " Available AMI agents: " + availableAmiSlaves
+ " AMI: " + template.getAmi() + " TemplateDesc: " + template.description);
return Math.min(availableAmiSlaves, availableTotalSlaves);
}
/**
* Obtains a agent whose AMI matches the AMI of the given template, and that also has requiredLabel (if requiredLabel is non-null)
* forceCreateNew specifies that the creation of a new agent is required. Otherwise, an existing matching agent may be re-used
*/
private List<EC2AbstractSlave> getNewOrExistingAvailableSlave(SlaveTemplate t, int number, boolean forceCreateNew) throws IOException {
try {
slaveCountingLock.lock();
int possibleSlavesCount = getPossibleNewSlavesCount(t);
if (possibleSlavesCount <= 0) {
LOGGER.log(Level.INFO, "{0}. Cannot provision - no capacity for instances: " + possibleSlavesCount, t);
return null;
}
EnumSet<SlaveTemplate.ProvisionOptions> provisionOptions;
if (forceCreateNew)
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.FORCE_CREATE);
else
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.ALLOW_CREATE);
if (number > possibleSlavesCount) {
LOGGER.log(Level.INFO, String.format("%d nodes were requested for the template %s, " +
"but because of instance cap only %d can be provisioned", number, t, possibleSlavesCount));
number = possibleSlavesCount;
}
return t.provision(number, provisionOptions);
} finally { slaveCountingLock.unlock(); }
}
@Override
public Collection<PlannedNode> provision(final Label label, int excessWorkload) {
final Collection<SlaveTemplate> matchingTemplates = getTemplates(label);
List<PlannedNode> plannedNodes = new ArrayList<>();
Jenkins jenkinsInstance = Jenkins.get();
if (jenkinsInstance.isQuietingDown()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is quieting down");
return Collections.emptyList();
} else if (jenkinsInstance.isTerminating()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is terminating");
return Collections.emptyList();
}
for (SlaveTemplate t : matchingTemplates) {
try {
LOGGER.log(Level.INFO, "{0}. Attempting to provision agent needed by excess workload of " + excessWorkload + " units", t);
int number = Math.max(excessWorkload / t.getNumExecutors(), 1);
final List<EC2AbstractSlave> slaves = getNewOrExistingAvailableSlave(t, number, false);
if (slaves == null || slaves.isEmpty()) {
LOGGER.warning("Can't raise nodes for " + t);
continue;
}
for (final EC2AbstractSlave slave : slaves) {
if (slave == null) {
LOGGER.warning("Can't raise node for " + t);
continue;
}
plannedNodes.add(createPlannedNode(t, slave));
excessWorkload -= t.getNumExecutors();
}
LOGGER.log(Level.INFO, "{0}. Attempting provision finished, excess workload: " + excessWorkload, t);
if (excessWorkload == 0) break;
} catch (AmazonServiceException e) {
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
if (e.getErrorCode().equals("RequestExpired")) {
// JENKINS-71554: A RequestExpired error can indicate that credentials have expired so reconnect
LOGGER.log(Level.INFO, "[JENKINS-71554] Reconnecting to EC2 due to RequestExpired error");
try {
reconnectToEc2();
} catch (IOException e2) {
LOGGER.log(Level.WARNING, "Failed to reconnect ec2", e2);
}
}
} catch (AmazonClientException | IOException e) {
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
}
}
LOGGER.log(Level.INFO, "We have now {0} computers, waiting for {1} more",
new Object[]{jenkinsInstance.getComputers().length, plannedNodes.size()});
return plannedNodes;
}
private static void attachSlavesToJenkins(Jenkins jenkins, List<EC2AbstractSlave> slaves, SlaveTemplate t) throws IOException {
for (final EC2AbstractSlave slave : slaves) {
if (slave == null) {
LOGGER.warning("Can't raise node for " + t);
continue;
}
Computer c = slave.toComputer();
if (slave.getStopOnTerminate() && c != null) {
c.connect(false);
}
jenkins.addNode(slave);
}
}
public void provision(SlaveTemplate t, int number) {
Jenkins jenkinsInstance = Jenkins.get();
if (jenkinsInstance.isQuietingDown()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is quieting down");
return;
} else if (jenkinsInstance.isTerminating()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is terminating");
return;
}
try {
LOGGER.log(Level.INFO, "{0}. Attempting to provision {1} agent(s)", new Object[]{t, number});
final List<EC2AbstractSlave> slaves = getNewOrExistingAvailableSlave(t, number, false);
if (slaves == null || slaves.isEmpty()) {
LOGGER.warning("Can't raise nodes for " + t);
return;
}
attachSlavesToJenkins(jenkinsInstance, slaves, t);
LOGGER.log(Level.INFO, "{0}. Attempting provision finished", t);
LOGGER.log(Level.INFO, "We have now {0} computers, waiting for {1} more",
new Object[]{Jenkins.get().getComputers().length, number});
} catch (AmazonClientException | IOException e) {
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
}
}
/**
* Helper method to reattach lost EC2 node agents @Issue("JENKINS-57795")
*
* @param jenkinsInstance Jenkins object that the nodes are to be re-attached to.
* @param template The corresponding SlaveTemplate of the nodes that are to be re-attached
* @param requestedNum The requested number of nodes to re-attach. We don't go above this in the case its value corresponds to an instance cap.
*/
void attemptReattachOrphanOrStoppedNodes(Jenkins jenkinsInstance, SlaveTemplate template, int requestedNum) throws IOException {
LOGGER.info("Attempting to wake & re-attach orphan/stopped nodes");
AmazonEC2 ec2 = this.connect();
DescribeInstancesResult diResult = template.getDescribeInstanceResult(ec2,true);
List<Instance> orphansOrStopped = template.findOrphansOrStopped(diResult, requestedNum);
template.wakeOrphansOrStoppedUp(ec2, orphansOrStopped);
/* If the number of possible nodes to re-attach is greater than the number of nodes requested, will only attempt to re-attach up to the number requested */
while (orphansOrStopped.size() > requestedNum) {
orphansOrStopped.remove(0);
}
attachSlavesToJenkins(jenkinsInstance, template.toSlaves(orphansOrStopped), template);
if (orphansOrStopped.size() > 0) {
LOGGER.info("Found and re-attached " + orphansOrStopped.size() + " orphan/stopped nodes");
}
}
private PlannedNode createPlannedNode(final SlaveTemplate t, final EC2AbstractSlave slave) {
return new PlannedNode(t.getDisplayName(),
Computer.threadPoolForRemoting.submit(new Callable<Node>() {
int retryCount = 0;
private static final int DESCRIBE_LIMIT = 2;
public Node call() throws Exception {
while (true) {
String instanceId = slave.getInstanceId();
if (slave instanceof EC2SpotSlave) {
if (((EC2SpotSlave) slave).isSpotRequestDead()) {
LOGGER.log(Level.WARNING, "{0} Spot request died, can't do anything. Terminate provisioning", t);
return null;
}
// Spot Instance does not have instance id yet.
if (StringUtils.isEmpty(instanceId)) {
Thread.sleep(5000);
continue;
}
}
Instance instance = CloudHelper.getInstanceWithRetry(instanceId, slave.getCloud());
if (instance == null) {
LOGGER.log(Level.WARNING, "{0} Can't find instance with instance id `{1}` in cloud {2}. Terminate provisioning ",
new Object[]{t, instanceId, slave.cloudName});
return null;
}
InstanceStateName state = InstanceStateName.fromValue(instance.getState().getName());
if (state.equals(InstanceStateName.Running)) {
//Spot instance are not reconnected automatically,
// but could be new orphans that has the option enable
Computer c = slave.toComputer();
if (slave.getStopOnTerminate() && (c != null )) {
c.connect(false);
}
long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - instance.getLaunchTime().getTime());
LOGGER.log(Level.INFO, "{0} Node {1} moved to RUNNING state in {2} seconds and is ready to be connected by Jenkins",
new Object[]{t, slave.getNodeName(), startTime});
return slave;
}
if (!state.equals(InstanceStateName.Pending)) {
if (retryCount >= DESCRIBE_LIMIT){
LOGGER.log(Level.WARNING,"Instance {0} did not move to running after {1} attempts, terminating provisioning",
new Object[]{instanceId, retryCount});
return null;
}
LOGGER.log(Level.INFO, "Attempt {0}: {1}. Node {2} is neither pending, neither running, it''s {3}. Will try again after 5s",
new Object[]{retryCount, t, slave.getNodeName(), state});
retryCount++;
}
Thread.sleep(5000);
}
}
})
, t.getNumExecutors());
}
@Override
public boolean canProvision(Label label) {
return !getTemplates(label).isEmpty();
}
protected AWSCredentialsProvider createCredentialsProvider() {
return createCredentialsProvider(useInstanceProfileForCredentials, credentialsId);
}
public static String getSlaveTypeTagValue(String slaveType, String templateDescription) {
return templateDescription != null ? slaveType + "_" + templateDescription : slaveType;
}
public static AWSCredentialsProvider createCredentialsProvider(final boolean useInstanceProfileForCredentials, final String credentialsId) {
if (useInstanceProfileForCredentials) {
return new InstanceProfileCredentialsProvider(false);
} else if (StringUtils.isBlank(credentialsId)) {
return new DefaultAWSCredentialsProviderChain();
} else {
AmazonWebServicesCredentials credentials = getCredentials(credentialsId);
if (credentials != null)
return new AWSStaticCredentialsProvider(credentials.getCredentials());
}
return new DefaultAWSCredentialsProviderChain();
}
public static AWSCredentialsProvider createCredentialsProvider(
final boolean useInstanceProfileForCredentials,
final String credentialsId,
final String roleArn,
final String roleSessionName,
final String region) {
AWSCredentialsProvider provider = createCredentialsProvider(useInstanceProfileForCredentials, credentialsId);
if (StringUtils.isNotEmpty(roleArn) && StringUtils.isNotEmpty(roleSessionName)) {
return new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, roleSessionName)
.withStsClient(AWSSecurityTokenServiceClientBuilder.standard()
.withCredentials(provider)
.withRegion(region)
.withClientConfiguration(createClientConfiguration(convertHostName(region)))
.build())
.build();
}
return provider;
}
@CheckForNull
private static AmazonWebServicesCredentials getCredentials(@CheckForNull String credentialsId) {
if (StringUtils.isBlank(credentialsId)) {
return null;
}
return (AmazonWebServicesCredentials) CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(AmazonWebServicesCredentials.class, Jenkins.get(),
ACL.SYSTEM, Collections.emptyList()),
CredentialsMatchers.withId(credentialsId));
}
private AmazonEC2 reconnectToEc2() throws IOException {
synchronized(this) {
connection = AmazonEC2Factory.getInstance().connect(createCredentialsProvider(), getEc2EndpointUrl());
return connection;
}
}
/**
* Connects to EC2 and returns {@link AmazonEC2}, which can then be used to communicate with EC2.
*/
public AmazonEC2 connect() throws AmazonClientException {
try {
if (connection != null) {
return connection;
}
else {
return reconnectToEc2();
}
} catch (IOException e) {
throw new AmazonClientException("Failed to retrieve the endpoint", e);
}