From 7228f20307bdfc539fea10b2ae9ac97870825c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Bostr=C3=B6m?= Date: Mon, 14 Feb 2022 19:11:10 +0100 Subject: [PATCH 1/2] JENKINS-62939: Use latest for getting console output if Nitro --- README.md | 1 + .../java/hudson/plugins/ec2/EC2Computer.java | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e2c95bab3..f6eda53e1 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ your user is not called ***jenkins***): "ec2:CreateTags", "ec2:DeleteTags", "ec2:DescribeInstances", + "ec2:DescribeInstanceTypes", "ec2:DescribeKeyPairs", "ec2:DescribeRegions", "ec2:DescribeImages", diff --git a/src/main/java/hudson/plugins/ec2/EC2Computer.java b/src/main/java/hudson/plugins/ec2/EC2Computer.java index 1e0dd546a..491a6c6d2 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Computer.java +++ b/src/main/java/hudson/plugins/ec2/EC2Computer.java @@ -23,19 +23,19 @@ */ package hudson.plugins.ec2; +import com.amazonaws.services.ec2.model.*; import edu.umd.cs.findbugs.annotations.CheckForNull; import hudson.Util; import hudson.model.Node; import hudson.slaves.SlaveComputer; import java.io.IOException; +import java.util.Collections; import org.kohsuke.stapler.HttpRedirect; import org.kohsuke.stapler.HttpResponse; import com.amazonaws.AmazonClientException; import com.amazonaws.services.ec2.AmazonEC2; -import com.amazonaws.services.ec2.model.GetConsoleOutputRequest; -import com.amazonaws.services.ec2.model.Instance; /** * @author Kohsuke Kawaguchi @@ -46,6 +46,8 @@ public class EC2Computer extends SlaveComputer { */ private volatile Instance ec2InstanceDescription; + private volatile Boolean isNitro; + public EC2Computer(EC2AbstractSlave slave) { super(slave); } @@ -92,9 +94,11 @@ public SlaveTemplate getSlaveTemplate() { * Gets the EC2 console output. */ public String getConsoleOutput() throws AmazonClientException { - AmazonEC2 ec2 = getCloud().connect(); - GetConsoleOutputRequest request = new GetConsoleOutputRequest(getInstanceId()); - return ec2.getConsoleOutput(request).getOutput(); + try { + return getDecodedConsoleOutputResponse().getOutput(); + } catch (InterruptedException e) { + return null; + } } /** @@ -102,9 +106,41 @@ public String getConsoleOutput() throws AmazonClientException { * @since TODO */ public String getDecodedConsoleOutput() throws AmazonClientException { + try { + return getDecodedConsoleOutputResponse().getDecodedOutput(); + } catch (InterruptedException e) { + return null; + } + } + + private GetConsoleOutputResult getDecodedConsoleOutputResponse() throws AmazonClientException, InterruptedException { AmazonEC2 ec2 = getCloud().connect(); GetConsoleOutputRequest request = new GetConsoleOutputRequest(getInstanceId()); - return ec2.getConsoleOutput(request).getDecodedOutput(); + if (checkIfNitro()) { + //Can only be used if instance has hypervisor Nitro + request.setLatest(true); + } + return ec2.getConsoleOutput(request); + } + + /** + * Check if instance has hypervisor Nitro + */ + private boolean checkIfNitro() throws AmazonClientException, InterruptedException { + if (isNitro == null) { + DescribeInstanceTypesRequest request = new DescribeInstanceTypesRequest(); + request.setInstanceTypes(Collections.singletonList(describeInstance().getInstanceType())); + AmazonEC2 ec2 = getCloud().connect(); + DescribeInstanceTypesResult result = ec2.describeInstanceTypes(request); + if (result.getInstanceTypes().size() == 1) { + String hypervisor = result.getInstanceTypes().get(0).getHypervisor(); + isNitro = hypervisor.equals("nitro"); + } else { + isNitro = false; + } + + } + return isNitro; } /** From 785228d8d43dfd875a9d5bd3982c7a740336a529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Bostr=C3=B6m?= Date: Tue, 22 Feb 2022 11:33:34 +0100 Subject: [PATCH 2/2] Add try catch AmazonClientException if IAM permissions are lacking --- .../java/hudson/plugins/ec2/EC2Computer.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2Computer.java b/src/main/java/hudson/plugins/ec2/EC2Computer.java index 491a6c6d2..4332cd026 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Computer.java +++ b/src/main/java/hudson/plugins/ec2/EC2Computer.java @@ -30,6 +30,8 @@ import hudson.slaves.SlaveComputer; import java.io.IOException; import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; import org.kohsuke.stapler.HttpRedirect; @@ -41,6 +43,9 @@ * @author Kohsuke Kawaguchi */ public class EC2Computer extends SlaveComputer { + + private static final Logger LOGGER = Logger.getLogger(EC2Computer.class.getName()); + /** * Cached description of this EC2 instance. Lazily fetched. */ @@ -127,20 +132,26 @@ private GetConsoleOutputResult getDecodedConsoleOutputResponse() throws AmazonCl * Check if instance has hypervisor Nitro */ private boolean checkIfNitro() throws AmazonClientException, InterruptedException { - if (isNitro == null) { - DescribeInstanceTypesRequest request = new DescribeInstanceTypesRequest(); - request.setInstanceTypes(Collections.singletonList(describeInstance().getInstanceType())); - AmazonEC2 ec2 = getCloud().connect(); - DescribeInstanceTypesResult result = ec2.describeInstanceTypes(request); - if (result.getInstanceTypes().size() == 1) { - String hypervisor = result.getInstanceTypes().get(0).getHypervisor(); - isNitro = hypervisor.equals("nitro"); - } else { - isNitro = false; - } + try { + if (isNitro == null) { + DescribeInstanceTypesRequest request = new DescribeInstanceTypesRequest(); + request.setInstanceTypes(Collections.singletonList(describeInstance().getInstanceType())); + AmazonEC2 ec2 = getCloud().connect(); + DescribeInstanceTypesResult result = ec2.describeInstanceTypes(request); + if (result.getInstanceTypes().size() == 1) { + String hypervisor = result.getInstanceTypes().get(0).getHypervisor(); + isNitro = hypervisor.equals("nitro"); + } else { + isNitro = false; + } + } + return isNitro; + } catch (AmazonClientException e) { + LOGGER.log(Level.WARNING, "Could not describe-instance-types to check if instance is nitro based", e); + isNitro = false; + return isNitro; } - return isNitro; } /**