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-62939: Use latest for getting console output if Nitro #707

Merged
merged 2 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ your user is not called ***jenkins***):
"ec2:CreateTags",
"ec2:DeleteTags",
"ec2:DescribeInstances",
"ec2:DescribeInstanceTypes",
"ec2:DescribeKeyPairs",
"ec2:DescribeRegions",
"ec2:DescribeImages",
Expand Down
48 changes: 42 additions & 6 deletions src/main/java/hudson/plugins/ec2/EC2Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,6 +46,8 @@ public class EC2Computer extends SlaveComputer {
*/
private volatile Instance ec2InstanceDescription;

private volatile Boolean isNitro;

public EC2Computer(EC2AbstractSlave slave) {
super(slave);
}
Expand Down Expand Up @@ -92,19 +94,53 @@ 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;
}
}

/**
* Gets the EC2 decoded console output.
* @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();
res0nance marked this conversation as resolved.
Show resolved Hide resolved
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;
}

/**
Expand Down