Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73640] Use AWS SDK for Java 2.x #1021

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ console](https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console),
example:

```groovy
import com.amazonaws.services.ec2.model.InstanceType
import software.amazon.awssdk.services.ec2.model.InstanceType
import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.Domain
Expand Down
26 changes: 15 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ THE SOFTWARE.
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.452.x</artifactId>
<version>3761.vd922730f0fd2</version>
<version>3850.vb_c5319efa_e29</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- TODO until in BOM -->
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>aws-global-configuration</artifactId>
<version>142.vfc0728241c1b_</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -108,6 +114,14 @@ THE SOFTWARE.
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.jenkins.plugins.aws-java-sdk2</groupId>
<artifactId>aws-java-sdk2-core</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins.aws-java-sdk2</groupId>
<artifactId>aws-java-sdk2-ec2</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
Expand Down Expand Up @@ -141,16 +155,6 @@ THE SOFTWARE.
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>trilead-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.aws-java-sdk</groupId>
<artifactId>aws-java-sdk-ec2</artifactId>
<version>1.12.696-451.v0651a_da_9ca_ec</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.aws-java-sdk</groupId>
<artifactId>aws-java-sdk-minimal</artifactId>
<version>1.12.767-467.vb_e93f0c614b_6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
Expand Down
65 changes: 33 additions & 32 deletions src/main/java/hudson/plugins/ec2/CloudHelper.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package hudson.plugins.ec2;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.AvailabilityZone;
import com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult;
import com.amazonaws.services.ec2.model.DescribeImagesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.Image;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.AvailabilityZone;
import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;
import software.amazon.awssdk.services.ec2.model.DescribeImagesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.Image;
import software.amazon.awssdk.services.ec2.model.Instance;
import software.amazon.awssdk.services.ec2.model.Reservation;

final class CloudHelper {
private static final Logger LOGGER = Logger.getLogger(CloudHelper.class.getName());

static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud)
throws AmazonClientException, InterruptedException {
static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud) throws SdkException, InterruptedException {
// Sometimes even after a successful RunInstances, DescribeInstances
// returns an error for a few seconds. We do a few retries instead of
// failing instantly. See [JENKINS-15319].
for (int i = 0; i < 5; i++) {
try {
return getInstance(instanceId, cloud);
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound")
|| EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE.equals(e.getErrorCode())) {
} catch (AwsServiceException e) {
if (e.awsErrorDetails().errorCode().equals("InvalidInstanceID.NotFound")
|| EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE.equals(

Check warning on line 32 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 32 is only partially covered, one branch is missing
e.awsErrorDetails().errorCode())) {
// retry in 5 seconds.
Thread.sleep(5000);
continue;
Expand All @@ -43,69 +43,70 @@
}

@CheckForNull
static Instance getInstance(String instanceId, EC2Cloud cloud) throws AmazonClientException {
static Instance getInstance(String instanceId, EC2Cloud cloud) throws SdkException {
if (StringUtils.isEmpty(instanceId) || cloud == null) {
return null;
}

DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setInstanceIds(Collections.singletonList(instanceId));
DescribeInstancesRequest request = DescribeInstancesRequest.builder()
.instanceIds(Collections.singletonList(instanceId))
.build();

List<Reservation> reservations =
cloud.connect().describeInstances(request).getReservations();
cloud.connect().describeInstances(request).reservations();
if (reservations.size() != 1) {
String message = "Unexpected number of reservations reported by EC2 for instance id '" + instanceId
+ "', expected 1 result, found " + reservations + ".";
if (reservations.isEmpty()) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
throw SdkException.builder().message(message).build();

Check warning on line 64 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 64 is not covered by tests
}
Reservation reservation = reservations.get(0);

List<Instance> instances = reservation.getInstances();
List<Instance> instances = reservation.instances();
if (instances.size() != 1) {
String message = "Unexpected number of instances reported by EC2 for instance id '" + instanceId
+ "', expected 1 result, found " + instances + ".";
if (instances.isEmpty()) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
throw SdkException.builder().message(message).build();
}
return instances.get(0);
}

@CheckForNull
static Image getAmiImage(AmazonEC2 ec2, String ami) {
static Image getAmiImage(Ec2Client ec2, String ami) {
List<String> images = Collections.singletonList(ami);
List<String> owners = Collections.emptyList();
List<String> users = Collections.emptyList();
DescribeImagesRequest request = new DescribeImagesRequest();
request.setImageIds(images);
request.setOwners(owners);
request.setExecutableUsers(users);
List<Image> img = ec2.describeImages(request).getImages();
DescribeImagesRequest.Builder requestBuilder = DescribeImagesRequest.builder();
requestBuilder.imageIds(images);
requestBuilder.owners(owners);
requestBuilder.executableUsers(users);
List<Image> img = ec2.describeImages(requestBuilder.build()).images();
if (img == null || img.isEmpty()) {
// de-registered AMI causes an empty list to be
// returned. so be defensive
// against other possibilities
return null;
} else {
return img.get(0);
}
}

// Retrieve the availability zones for the region connected on
static ArrayList<String> getAvailabilityZones(AmazonEC2 ec2) {
static ArrayList<String> getAvailabilityZones(Ec2Client ec2) {
ArrayList<String> availabilityZones = new ArrayList<>();

DescribeAvailabilityZonesResult zones = ec2.describeAvailabilityZones();
List<AvailabilityZone> zoneList = zones.getAvailabilityZones();
DescribeAvailabilityZonesResponse zones = ec2.describeAvailabilityZones();
List<AvailabilityZone> zoneList = zones.availabilityZones();

for (AvailabilityZone z : zoneList) {
availabilityZones.add(z.getZoneName());
availabilityZones.add(z.zoneName());

Check warning on line 109 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 76-109 are not covered by tests
}

return availabilityZones;
Expand Down
Loading
Loading