Skip to content
Permalink
Browse files
Merge pull request #107 from EdiaEducationTechnology/master
JENKINS-19845
  • Loading branch information
francisu committed Sep 23, 2014
2 parents b99f819 + 5286a64 commit 2b28c7acb1d45e558c0ad8b306c960ed91d8a43d
Showing 3 changed files with 54 additions and 2 deletions.
@@ -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;
@@ -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;
@@ -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.
*/
@@ -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;
@@ -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(),
@@ -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()));

0 comments on commit 2b28c7a

Please sign in to comment.