From ee35f16105f7a987bd2bfeaf35b36d1a889431cc Mon Sep 17 00:00:00 2001 From: John Russell Date: Thu, 12 Feb 2015 11:12:30 -0500 Subject: [PATCH] Check more places where passing a null instanceID can do a search of random spot instances. and clean up Some other NPEs --- .../hudson/plugins/ec2/EC2AbstractSlave.java | 21 ++++++++++--------- .../hudson/plugins/ec2/SlaveTemplate.java | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java b/src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java index 180c0517b..29cd05e51 100644 --- a/src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java +++ b/src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java @@ -70,8 +70,8 @@ public abstract class EC2AbstractSlave extends Slave { public final String initScript; public final String tmpDir; public final String remoteAdmin; // e.g. 'ubuntu' - - + + public final String jvmopts; //e.g. -Xmx1g public final boolean stopOnTerminate; public final String idleTerminationMinutes; @@ -139,14 +139,14 @@ protected Object readResolve() { if (instanceId == null) { instanceId = getNodeName(); } - + if (amiType == null) { amiType = new UnixData(rootCommandPrefix, Integer.toString(sshPort)); } return this; } - + public EC2Cloud getCloud() { return (EC2Cloud) Hudson.getInstance().getCloud(cloudName); } @@ -198,12 +198,13 @@ public Computer createComputer() { } public static Instance getInstance(String instanceId, EC2Cloud cloud) { + if (instanceId == null || instanceId == "" || cloud == null) + return null; + Instance i = null; try { DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(Collections.singletonList(instanceId)); - if (cloud == null) - return null; AmazonEC2 ec2 = cloud.connect(); List reservations = ec2.describeInstances(request).getReservations(); if (reservations.size() > 0) { @@ -302,7 +303,7 @@ public int getSshPort() { String sshPort = amiType.isUnix() ? ((UnixData)amiType).getSshPort() : "22"; if (sshPort == null || sshPort.length() == 0) return 22; - + int port = 0; try { port = Integer.parseInt(sshPort); @@ -393,7 +394,7 @@ protected void pushLiveInstancedata() throws AmazonClientException { Instance inst = getInstance(getInstanceId(), getCloud()); /* Now that we have our instance, we can set tags on it */ - if (tags != null && !tags.isEmpty()) { + if (inst != null && tags != null && !tags.isEmpty()) { HashSet inst_tags = new HashSet(); for(EC2Tag t : tags) { @@ -429,7 +430,7 @@ public long getCreatedTime() { public boolean getUsePrivateDnsName() { return usePrivateDnsName; } - + public String getAdminPassword() { return amiType.isWindows() ? ((WindowsData)amiType).getPassword() : ""; } @@ -481,7 +482,7 @@ public ListBoxModel doFillZoneItems(@QueryParameter boolean useInstanceProfileFo AWSCredentialsProvider credentialsProvider = EC2Cloud.createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey); return fillZoneItems(credentialsProvider, region); } - + public List> getAMITypeDescriptors() { return Hudson.getInstance().>getDescriptorList(AMITypeData.class); diff --git a/src/main/java/hudson/plugins/ec2/SlaveTemplate.java b/src/main/java/hudson/plugins/ec2/SlaveTemplate.java index 4ef900ac5..131cc8585 100644 --- a/src/main/java/hudson/plugins/ec2/SlaveTemplate.java +++ b/src/main/java/hudson/plugins/ec2/SlaveTemplate.java @@ -652,7 +652,8 @@ private EC2AbstractSlave provisionSpot(TaskListener listener) throws AmazonClien } } if (!hasCustomTypeTag) { - inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "spot")); + if (inst_tags != null) + inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "spot")); } if (StringUtils.isNotBlank(getIamInstanceProfile())) {