Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #107 from EdiaEducationTechnology/master
Browse files Browse the repository at this point in the history
JENKINS-19845
  • Loading branch information
francisu committed Sep 23, 2014
2 parents b99f819 + 5286a64 commit 2b28c7a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Expand Up @@ -58,6 +58,8 @@

import jenkins.model.Jenkins;
import jenkins.slaves.iterators.api.NodeIterator;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand All @@ -76,6 +78,7 @@
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.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
Expand Down Expand Up @@ -211,16 +214,45 @@ public int countCurrentEC2Slaves(String ami) throws AmazonClientException {
int n=0;
for (Reservation r : connect().describeInstances().getReservations()) {
for (Instance i : r.getInstances()) {
if (ami == null || ami.equals(i.getImageId())) {
if (isEc2ProvisionedSlave(i, ami)) {
InstanceStateName stateName = InstanceStateName.fromValue(i.getState().getName());
if (stateName == InstanceStateName.Pending || stateName == InstanceStateName.Running)
if (stateName == InstanceStateName.Pending || stateName == InstanceStateName.Running) {
n++;
}
}
}
}
return n;
}

/**
* This method checks if the slave is provisioned by this plugin.
* An instance is a provisioned slave if:
* <ol>
* <li>The AMI id matches.</li>
* <li>There is a tag with the key name {@link EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE}</li>.
* </ol>
*
* @see JENKINS-19845 (https://issues.jenkins-ci.org/browse/JENKINS-19845)
*
* @param i the instance to be checked.
* @param ami the ami id.
* @return true if the instance is a provisioned slave
*/
protected boolean isEc2ProvisionedSlave(Instance i, String ami) {
// Check if the ami matches
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(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
return true;
}
}
return false;
}
return false;
}

/**
* Debug command to attach to a running instance.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/hudson/plugins/ec2/EC2Tag.java
Expand Up @@ -39,6 +39,12 @@ public class EC2Tag extends AbstractDescribableImpl<EC2Tag>
private String name;
private String value;

/**
* Tag name for the specific jenkings slave type tag, used to identify the
* EC2 instances provisioned by this plugin.
*/
public static final String TAG_NAME_JENKINS_SLAVE_TYPE = "jenkins_slave_type";

@DataBoundConstructor
public EC2Tag(String name, String value) {
this.name = name;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Expand Up @@ -364,14 +364,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(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
hasCustomTypeTag = true;
}
}
}
if (!hasCustomTypeTag) {
inst_tags.add(new Tag(EC2Tag.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 @@ -582,13 +589,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(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
hasCustomTypeTag = true;
}
}
}
if (!hasCustomTypeTag) {
inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "spot"));
}

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

0 comments on commit 2b28c7a

Please sign in to comment.