Skip to content

Commit

Permalink
Organize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 17, 2024
1 parent 40c4a24 commit 5f53f07
Show file tree
Hide file tree
Showing 27 changed files with 208 additions and 105 deletions.
4 changes: 1 addition & 3 deletions src/main/java/hudson/plugins/ec2/CloudHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hudson.plugins.ec2;

import static hudson.plugins.ec2.EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
Expand Down Expand Up @@ -32,7 +30,7 @@ static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud)
return getInstance(instanceId, cloud);
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound")
|| EC2_REQUEST_EXPIRED_ERROR_CODE.equals(e.getErrorCode())) {
|| EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE.equals(e.getErrorCode())) {
// retry in 5 seconds.
Thread.sleep(5000);
continue;
Expand Down
43 changes: 32 additions & 11 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
package hudson.plugins.ec2;

import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
Expand All @@ -31,7 +28,19 @@
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.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest;
import com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceStateName;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.KeyPair;
import com.amazonaws.services.ec2.model.KeyPairInfo;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.SpotInstanceRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
Expand Down Expand Up @@ -81,7 +90,17 @@
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.*;
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.Objects;
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;
Expand All @@ -90,6 +109,7 @@
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -505,25 +525,26 @@ public void doAttach(StaplerRequest req, StaplerResponse rsp, @QueryParameter St
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");
throw HttpResponses.error(HttpServletResponse.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);
throw HttpResponses.error(HttpServletResponse.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");
throw HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "Jenkins instance is quieting down");
}
if (jenkinsInstance.isTerminating()) {
throw HttpResponses.error(SC_BAD_REQUEST, "Jenkins instance is terminating");
throw HttpResponses.error(HttpServletResponse.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);
HttpServletResponse.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
Expand All @@ -536,7 +557,7 @@ public HttpResponse doProvision(@QueryParameter String template) throws ServletE
return HttpResponses.redirectViaContextPath(
"/computer/" + nodes.get(0).getNodeName());
} catch (AmazonClientException e) {
throw HttpResponses.error(SC_INTERNAL_SERVER_ERROR, e);
throw HttpResponses.error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/hudson/plugins/ec2/EC2Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.ec2.model.DescribeInstanceTypesRequest;
import com.amazonaws.services.ec2.model.DescribeInstanceTypesResult;
import com.amazonaws.services.ec2.model.GetConsoleOutputRequest;
import com.amazonaws.services.ec2.model.GetConsoleOutputResult;
import com.amazonaws.services.ec2.model.Instance;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Util;
import hudson.model.Node;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/hudson/plugins/ec2/EC2HostAddressProvider.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hudson.plugins.ec2;

import static hudson.plugins.ec2.ConnectionStrategy.*;

import com.amazonaws.services.ec2.model.Instance;
import java.util.Optional;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -38,9 +36,9 @@ public static String mac(Instance instance, ConnectionStrategy strategy) {
}

public static String windows(Instance instance, ConnectionStrategy strategy) {
if (strategy.equals(PRIVATE_DNS) || strategy.equals(PRIVATE_IP)) {
if (strategy.equals(ConnectionStrategy.PRIVATE_DNS) || strategy.equals(ConnectionStrategy.PRIVATE_IP)) {
return getPrivateIpAddress(instance);
} else if (strategy.equals(PUBLIC_DNS) || strategy.equals(PUBLIC_IP)) {
} else if (strategy.equals(ConnectionStrategy.PUBLIC_DNS) || strategy.equals(ConnectionStrategy.PUBLIC_IP)) {
return getPublicIpAddress(instance);
} else {
throw new IllegalArgumentException("Could not windows host address for strategy = " + strategy);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/EC2OndemandSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Descriptor.FormException;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/hudson/plugins/ec2/EC2PrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package hudson.plugins.ec2;

import static hudson.plugins.ec2.EC2Cloud.SSH_PRIVATE_KEY_FILEPATH;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.KeyPairInfo;
Expand Down Expand Up @@ -155,7 +153,7 @@ public String decryptWindowsPassword(String encodedPassword) throws AmazonClient
/* visible for testing */
@CheckForNull
public static EC2PrivateKey fetchFromDisk() {
return fetchFromDisk(System.getProperty(SSH_PRIVATE_KEY_FILEPATH, ""));
return fetchFromDisk(System.getProperty(EC2Cloud.SSH_PRIVATE_KEY_FILEPATH, ""));
}

@CheckForNull
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/hudson/plugins/ec2/EC2SlaveMonitor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hudson.plugins.ec2;

import static hudson.plugins.ec2.EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.ec2.model.AmazonEC2Exception;
import hudson.Extension;
Expand Down Expand Up @@ -52,7 +50,8 @@ private void removeDeadNodes() {
}
} catch (AmazonClientException e) {
if (e instanceof AmazonEC2Exception
&& EC2_REQUEST_EXPIRED_ERROR_CODE.equals(((AmazonEC2Exception) e).getErrorCode())) {
&& EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE.equals(
((AmazonEC2Exception) e).getErrorCode())) {
LOGGER.info("EC2 request expired, skipping consideration of " + ec2Slave.getInstanceId()
+ " due to unknown state.");
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2Step.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@
import hudson.model.TaskListener;
import hudson.slaves.Cloud;
import hudson.util.ListBoxModel;
import java.util.*;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.steps.*;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
*/
package hudson.plugins.ec2;

import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_JAVA_PATH;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_SUPPORTED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentialsProvider;
Expand Down Expand Up @@ -363,7 +357,7 @@ public SlaveTemplate(
if (StringUtils.isNotBlank(javaPath)) {
this.javaPath = javaPath;
} else {
this.javaPath = DEFAULT_JAVA_PATH;
this.javaPath = EC2AbstractSlave.DEFAULT_JAVA_PATH;
}

this.jvmopts = jvmopts;
Expand Down Expand Up @@ -409,12 +403,16 @@ public SlaveTemplate(
: HostKeyVerificationStrategyEnum.CHECK_NEW_SOFT;
this.tenancy = tenancy != null ? tenancy : Tenancy.Default;
this.ebsEncryptRootVolume = ebsEncryptRootVolume != null ? ebsEncryptRootVolume : EbsEncryptRootVolume.DEFAULT;
this.metadataSupported = metadataSupported != null ? metadataSupported : DEFAULT_METADATA_SUPPORTED;
this.metadataEndpointEnabled =
metadataEndpointEnabled != null ? metadataEndpointEnabled : DEFAULT_METADATA_ENDPOINT_ENABLED;
this.metadataTokensRequired =
metadataTokensRequired != null ? metadataTokensRequired : DEFAULT_METADATA_TOKENS_REQUIRED;
this.metadataHopsLimit = metadataHopsLimit != null ? metadataHopsLimit : DEFAULT_METADATA_HOPS_LIMIT;
this.metadataSupported =
metadataSupported != null ? metadataSupported : EC2AbstractSlave.DEFAULT_METADATA_SUPPORTED;
this.metadataEndpointEnabled = metadataEndpointEnabled != null
? metadataEndpointEnabled
: EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED;
this.metadataTokensRequired = metadataTokensRequired != null
? metadataTokensRequired
: EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED;
this.metadataHopsLimit =
metadataHopsLimit != null ? metadataHopsLimit : EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;
readResolve(); // initialize
}

Expand Down Expand Up @@ -481,7 +479,7 @@ public SlaveTemplate(
numExecutors,
remoteAdmin,
amiType,
DEFAULT_JAVA_PATH,
EC2AbstractSlave.DEFAULT_JAVA_PATH,
jvmopts,
stopOnTerminate,
subnetId,
Expand All @@ -508,7 +506,7 @@ public SlaveTemplate(
metadataEndpointEnabled,
metadataTokensRequired,
metadataHopsLimit,
DEFAULT_METADATA_SUPPORTED);
EC2AbstractSlave.DEFAULT_METADATA_SUPPORTED);
}

@Deprecated
Expand Down Expand Up @@ -569,7 +567,7 @@ public SlaveTemplate(
numExecutors,
remoteAdmin,
amiType,
DEFAULT_JAVA_PATH,
EC2AbstractSlave.DEFAULT_JAVA_PATH,
jvmopts,
stopOnTerminate,
subnetId,
Expand All @@ -593,10 +591,10 @@ public SlaveTemplate(
hostKeyVerificationStrategy,
tenancy,
null,
DEFAULT_METADATA_ENDPOINT_ENABLED,
DEFAULT_METADATA_TOKENS_REQUIRED,
DEFAULT_METADATA_HOPS_LIMIT,
DEFAULT_METADATA_SUPPORTED);
EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED,
EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED,
EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT,
EC2AbstractSlave.DEFAULT_METADATA_SUPPORTED);
}

@Deprecated
Expand Down Expand Up @@ -2687,7 +2685,7 @@ protected Object readResolve() {
metadataHopsLimit = EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;
}
if (StringUtils.isBlank(javaPath)) {
javaPath = DEFAULT_JAVA_PATH;
javaPath = EC2AbstractSlave.DEFAULT_JAVA_PATH;
}

return this;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/hudson/plugins/ec2/SpotConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package hudson.plugins.ec2;

import static hudson.Functions.checkPermission;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
Expand All @@ -11,6 +9,7 @@
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.SpotPrice;
import hudson.Extension;
import hudson.Functions;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.plugins.ec2.util.AmazonEC2Factory;
Expand Down Expand Up @@ -150,7 +149,7 @@ public FormValidation doCurrentSpotPrice(
@QueryParameter String ami)
throws IOException, ServletException {

checkPermission(EC2Cloud.PROVISION);
Functions.checkPermission(EC2Cloud.PROVISION);

String cp = "";
String zoneStr = "";
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,37 @@
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.KeyPair;
import com.trilead.ssh2.*;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.HTTPProxyData;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.ServerHostKeyVerifier;
import com.trilead.ssh2.Session;
import hudson.FilePath;
import hudson.ProxyConfiguration;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.plugins.ec2.*;
import hudson.plugins.ec2.ConnectionStrategy;
import hudson.plugins.ec2.EC2AbstractSlave;
import hudson.plugins.ec2.EC2Cloud;
import hudson.plugins.ec2.EC2Computer;
import hudson.plugins.ec2.EC2ComputerLauncher;
import hudson.plugins.ec2.EC2HostAddressProvider;
import hudson.plugins.ec2.EC2PrivateKey;
import hudson.plugins.ec2.EC2Readiness;
import hudson.plugins.ec2.EC2SpotSlave;
import hudson.plugins.ec2.SlaveTemplate;
import hudson.plugins.ec2.ssh.verifiers.HostKey;
import hudson.plugins.ec2.ssh.verifiers.Messages;
import hudson.remoting.Channel;
import hudson.remoting.Channel.Listener;
import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerLauncher;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.charset.StandardCharsets;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.plugins.ec2.*;
import hudson.plugins.ec2.ConnectionStrategy;
import hudson.plugins.ec2.EC2AbstractSlave;
import hudson.plugins.ec2.EC2Cloud;
import hudson.plugins.ec2.EC2Computer;
import hudson.plugins.ec2.EC2ComputerLauncher;
import hudson.plugins.ec2.EC2HostAddressProvider;
import hudson.plugins.ec2.EC2PrivateKey;
import hudson.plugins.ec2.EC2Readiness;
import hudson.plugins.ec2.EC2SpotSlave;
import hudson.plugins.ec2.SlaveTemplate;
import hudson.plugins.ec2.ssh.verifiers.HostKey;
import hudson.plugins.ec2.ssh.verifiers.HostKeyHelper;
import hudson.plugins.ec2.ssh.verifiers.Messages;
Expand Down
Loading

0 comments on commit 5f53f07

Please sign in to comment.