Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #33 from zzzeek/master

fallback a manual or timeout-based terminate to stop if terminate fails
  • Loading branch information...
commit f0d1e98468f8c82a14554a66199be506049be8e4 2 parents a4e404c + 9101f7e
@francisu francisu authored
Showing with 47 additions and 32 deletions.
  1. +47 −32 src/main/java/hudson/plugins/ec2/EC2Slave.java
View
79 src/main/java/hudson/plugins/ec2/EC2Slave.java
@@ -57,7 +57,7 @@
/**
* Slave running on EC2.
- *
+ *
* @author Kohsuke Kawaguchi
*/
public final class EC2Slave extends Slave {
@@ -93,7 +93,7 @@
private final int sshPort;
public static final String TEST_ZONE = "testZone";
-
+
public EC2Slave(String instanceId, String description, String remoteFS, int sshPort, int numExecutors, String labelString, Mode mode, String initScript, String remoteAdmin, String rootCommandPrefix, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, String publicDNS, String privateDNS, List<EC2Tag> tags) throws FormException, IOException {
this(instanceId, description, remoteFS, sshPort, numExecutors, mode, labelString, initScript, Collections.<NodeProperty<?>>emptyList(), remoteAdmin, rootCommandPrefix, jvmopts, stopOnTerminate, idleTerminationMinutes, publicDNS, privateDNS, tags, false);
}
@@ -177,47 +177,62 @@ public static Instance getInstance(String instanceId) {
}
return i;
}
-
+
/**
* Terminates the instance in EC2.
*/
public void terminate() {
+ if (!isAlive(true)) {
+ /* The node has been killed externally, so we've nothing to do here */
+ LOGGER.info("EC2 instance already terminated: "+getInstanceId());
+ } else if (!terminateInstance()) {
+ LOGGER.info("EC2 terminate failed, attempting a stop");
+ stop();
+ return;
+ }
+
try {
- if (!isAlive(true)) {
- /* The node has been killed externally, so we've nothing to do here */
- LOGGER.info("EC2 instance already terminated: "+getInstanceId());
- } else {
- AmazonEC2 ec2 = EC2Cloud.get().connect();
- TerminateInstancesRequest request = new TerminateInstancesRequest(Collections.singletonList(getInstanceId()));
- ec2.terminateInstances(request);
- LOGGER.info("Terminated EC2 instance (terminated): "+getInstanceId());
- }
Hudson.getInstance().removeNode(this);
- } catch (AmazonClientException e) {
- LOGGER.log(Level.WARNING,"Failed to terminate EC2 instance: "+getInstanceId(),e);
} catch (IOException e) {
LOGGER.log(Level.WARNING,"Failed to terminate EC2 instance: "+getInstanceId(),e);
}
}
+ void stop() {
+ try {
+ AmazonEC2 ec2 = EC2Cloud.get().connect();
+ StopInstancesRequest request = new StopInstancesRequest(
+ Collections.singletonList(getInstanceId()));
+ ec2.stopInstances(request);
+ LOGGER.info("EC2 instance stopped: " + getInstanceId());
+ toComputer().disconnect(null);
+ } catch (AmazonClientException e) {
+ Instance i = getInstance(getNodeName());
+ LOGGER.log(Level.WARNING, "Failed to terminate EC2 instance: "+getInstanceId() + " info: "+((i != null)?i:"") , e);
+ }
+ }
+
+ boolean terminateInstance() {
+ try {
+ AmazonEC2 ec2 = EC2Cloud.get().connect();
+ TerminateInstancesRequest request = new TerminateInstancesRequest(Collections.singletonList(getInstanceId()));
+ ec2.terminateInstances(request);
+ LOGGER.info("Terminated EC2 instance (terminated): "+getInstanceId());
+ return true;
+ } catch (AmazonClientException e) {
+ LOGGER.log(Level.WARNING,"Failed to terminate EC2 instance: "+getInstanceId(),e);
+ return false;
+ }
+ }
+
void idleTimeout() {
LOGGER.info("EC2 instance idle time expired: "+getInstanceId());
if (!stopOnTerminate) {
terminate();
- return;
}
-
- try {
- AmazonEC2 ec2 = EC2Cloud.get().connect();
- StopInstancesRequest request = new StopInstancesRequest(
- Collections.singletonList(getInstanceId()));
- ec2.stopInstances(request);
- toComputer().disconnect(null);
- } catch (AmazonClientException e) {
- Instance i = getInstance(getNodeName());
- LOGGER.log(Level.WARNING, "Failed to terminate EC2 instance: "+getInstanceId() + " info: "+((i != null)?i:"") , e);
- }
- LOGGER.info("EC2 instance stopped: " + getInstanceId());
+ else {
+ stop();
+ }
}
String getRemoteAdmin() {
@@ -306,7 +321,7 @@ private void pushLiveInstancedata() throws AmazonClientException {
for(EC2Tag t : tags) {
inst_tags.add(new Tag(t.getName(), t.getValue()));
- }
+ }
CreateTagsRequest tag_request = new CreateTagsRequest();
tag_request.withResources(inst.getInstanceId()).setTags(inst_tags);
@@ -361,14 +376,14 @@ public Node reconfigure(final StaplerRequest req, JSONObject form) throws FormEx
public boolean getUsePrivateDnsName() {
return usePrivateDnsName;
}
-
+
public static ListBoxModel fillZoneItems(String accessId, String secretKey, String region) throws IOException, ServletException {
ListBoxModel model = new ListBoxModel();
if (AmazonEC2Cloud.testMode) {
model.add(TEST_ZONE);
return model;
}
-
+
if (!StringUtils.isEmpty(accessId) && !StringUtils.isEmpty(secretKey) && !StringUtils.isEmpty(region)) {
AmazonEC2 client = EC2Cloud.connect(accessId, secretKey, AmazonEC2Cloud.getEc2EndpointUrl(region));
DescribeAvailabilityZonesResult zones = client.describeAvailabilityZones();
@@ -380,8 +395,8 @@ public static ListBoxModel fillZoneItems(String accessId, String secretKey, Stri
}
return model;
}
-
-
+
+
@Extension
public static final class DescriptorImpl extends SlaveDescriptor {
@Override
Please sign in to comment.
Something went wrong with that request. Please try again.