Skip to content

Commit

Permalink
[JENKINS-19058] Fix cannot add tags to Spot Instances
Browse files Browse the repository at this point in the history
Add a ComputerListener so that we can take action when a
slave is connected. This allows us to be notified when a
Spot instance is connected and add the Spot Request's tags
to its instance.
  • Loading branch information
runnirr committed Aug 4, 2013
1 parent 98a36f1 commit f6d54a7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java
Expand Up @@ -261,6 +261,13 @@ public boolean getStopOnTerminate() {
return stopOnTerminate;
}

/**
* Called when the slave is connected to Jenkins
*/
public void onConnected() {
// Do nothing by default.
}

protected boolean isAlive(boolean force) {
fetchLiveInstanceData(force);
if (lastFetchInstance == null)
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/hudson/plugins/ec2/EC2Computer.java
Expand Up @@ -59,7 +59,7 @@ public String getInstanceId() {
EC2AbstractSlave node = (EC2AbstractSlave) super.getNode();
return node.getInstanceId();
}

public String getEc2Type() {
return getNode().getEc2Type();
}
Expand All @@ -70,7 +70,7 @@ public String getSpotInstanceRequestId(){
}
return "";
}

public EC2Cloud getCloud() {
EC2AbstractSlave node = (EC2AbstractSlave) super.getNode();
return node.cloud;
Expand Down Expand Up @@ -157,7 +157,7 @@ private Instance _describeInstanceOnce() throws AmazonClientException {
request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
return getCloud().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
}

/**
* When the slave is deleted, terminate the instance.
*/
Expand All @@ -183,5 +183,12 @@ public int getSshPort() {
public String getRootCommandPrefix() {
return getNode().getRootCommandPrefix();
}


public void onConnected(){
EC2AbstractSlave node = getNode();
if (node != null) {
node.onConnected();
}
}

}
17 changes: 17 additions & 0 deletions src/main/java/hudson/plugins/ec2/EC2ComputerListener.java
@@ -0,0 +1,17 @@
package hudson.plugins.ec2;

import hudson.Extension;
import hudson.model.TaskListener;
import hudson.model.Computer;
import hudson.slaves.ComputerListener;

@Extension
public class EC2ComputerListener extends ComputerListener {

@Override
public void onOnline(Computer c, TaskListener listener) {
if(c instanceof EC2Computer){
((EC2Computer) c).onConnected();
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/hudson/plugins/ec2/EC2SpotSlave.java
Expand Up @@ -115,6 +115,13 @@ public String getInstanceId() {
return instanceId;
}

@Override
public void onConnected() {
// The spot request has been fulfilled and is connected. If the Spot
// request had tags, we want those on the instance.
pushLiveInstancedata();
}

@Extension
public static final class DescriptorImpl extends EC2AbstractSlave.DescriptorImpl {

Expand Down

0 comments on commit f6d54a7

Please sign in to comment.