Skip to content

Commit

Permalink
JENKINS-19845, EC2 plugin incorrectly reports current instance count
Browse files Browse the repository at this point in the history
- Now using the specific 'ec2slave' tag
  • Loading branch information
rolandgroen committed Sep 11, 2014
1 parent 58fbb7f commit deffa18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Expand Up @@ -229,7 +229,7 @@ public int countCurrentEC2Slaves(String ami) throws AmazonClientException {
/**
* This method checks if the slave is provisioned by this plugin.
* It does so by: 1) checking if the ami matches and 2) checking
* for a tag with the key ec2slave. The value of the tag will be ignored.
* for a tag with the key jenkins_slave_type. The value of the tag will be ignored.
*
* See also JENKINS-19845
*
Expand All @@ -242,7 +242,7 @@ private boolean isEc2ProvisionedSlave(Instance i, String ami) {
if (ami == null || StringUtils.equals(ami, i.getImageId())) {
// Check if there is a ec2slave tag...
for (Tag tag : i.getTags()) {
if (StringUtils.equals(tag.getKey(), "ec2slave")) {
if (StringUtils.equals(tag.getKey(), "jenkins_slave_type")) {
return true;
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Expand Up @@ -62,7 +62,8 @@
* @author Kohsuke Kawaguchi
*/
public class SlaveTemplate implements Describable<SlaveTemplate> {
public final String ami;
public static final String TAG_NAME_JENKINS_SLAVE_TYPE = "jenkins_slave_type";
public final String ami;
public final String description;
public final String zone;
public final SpotConfiguration spotConfig;
Expand Down Expand Up @@ -364,16 +365,21 @@ private EC2AbstractSlave provisionOndemand(TaskListener listener) throws AmazonC
riRequest.withNetworkInterfaces(net);
}

boolean hasCustomTypeTag = false;
HashSet<Tag> inst_tags = null;
if (tags != null && !tags.isEmpty()) {
inst_tags = new HashSet<Tag>();
for(EC2Tag t : tags) {
inst_tags.add(new Tag(t.getName(), t.getValue()));
diFilters.add(new Filter("tag:"+t.getName()).withValues(t.getValue()));
if (StringUtils.equals(t.getName(), TAG_NAME_JENKINS_SLAVE_TYPE)) {
hasCustomTypeTag = true;
}
}
}

inst_tags.add(new Tag("ec2slave", "demand"));
if (!hasCustomTypeTag) {
inst_tags.add(new Tag(TAG_NAME_JENKINS_SLAVE_TYPE, "demand"));
}

DescribeInstancesRequest diRequest = new DescribeInstancesRequest();
diFilters.add(new Filter("instance-state-name").withValues(InstanceStateName.Stopped.toString(),
Expand Down Expand Up @@ -584,15 +590,20 @@ private EC2AbstractSlave provisionSpot(TaskListener listener) throws AmazonClien
launchSpecification.setKeyName(keyPair.getKeyName());
launchSpecification.setInstanceType(type.toString());

boolean hasCustomTypeTag = false;
HashSet<Tag> inst_tags = null;
if (tags != null && !tags.isEmpty()) {
inst_tags = new HashSet<Tag>();
for(EC2Tag t : tags) {
inst_tags.add(new Tag(t.getName(), t.getValue()));
if (StringUtils.equals(t.getName(), TAG_NAME_JENKINS_SLAVE_TYPE)) {
hasCustomTypeTag = true;
}
}
}

inst_tags.add(new Tag("ec2slave", "spot"));
if (!hasCustomTypeTag) {
inst_tags.add(new Tag(TAG_NAME_JENKINS_SLAVE_TYPE, "spot"));
}

if (StringUtils.isNotBlank(getIamInstanceProfile())) {
launchSpecification.setIamInstanceProfile(new IamInstanceProfileSpecification().withArn(getIamInstanceProfile()));
Expand Down

0 comments on commit deffa18

Please sign in to comment.