Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filter query when describing instances #356

Merged
merged 8 commits into from
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 38 additions & 34 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.CreateKeyPairRequest;
import com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceStateName;
Expand Down Expand Up @@ -371,8 +373,7 @@ public HttpResponse doProvision(@QueryParameter String template) throws ServletE
* @param template If left null, then all instances are counted.
*/
private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientException {
String jenkinsServerUrl = null;
jenkinsServerUrl = JenkinsLocationConfiguration.get().getUrl();
String jenkinsServerUrl = JenkinsLocationConfiguration.get().getUrl();

if (jenkinsServerUrl == null) {
LOGGER.log(Level.WARNING, "No Jenkins server URL specified, it is strongly recommended to open /configure and set the server URL. " +
Expand All @@ -383,53 +384,42 @@ private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientExc
+ (template != null ? (" AMI: " + template.getAmi() + " TemplateDesc: " + template.description) : " All AMIS")
+ " Jenkins Server: " + jenkinsServerUrl);
int n = 0;
Set<String> instanceIds = new HashSet<String>();
Set<String> instanceIds = new HashSet<>();
String description = template != null ? template.description : null;

//FIXME convert to a filter query
for (Reservation r : connect().describeInstances().getReservations()) {
for (Instance i : r.getInstances()) {
if (isEc2ProvisionedAmiSlave(i.getTags(), description)
&& isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)
&& (template == null || template.getAmi().equals(i.getImageId()))) {
InstanceStateName stateName = InstanceStateName.fromValue(i.getState().getName());
if (stateName != InstanceStateName.Terminated &&
stateName != InstanceStateName.ShuttingDown &&
stateName != InstanceStateName.Stopped ) {
List<Filter> filters = getGenericFilters(jenkinsServerUrl, template);
filters.add(new Filter("instance-state-name").withValues("running", "pending", "stopping"));
DescribeInstancesRequest dir = new DescribeInstancesRequest().withFilters(filters);
DescribeInstancesResult result = null;
do {
result = connect().describeInstances(dir);
dir.setNextToken(result.getNextToken());
for (Reservation r : result.getReservations()) {
for (Instance i : r.getInstances()) {
if (isEc2ProvisionedAmiSlave(i.getTags(), description)) {
LOGGER.log(Level.FINE, "Existing instance found: " + i.getInstanceId() + " AMI: " + i.getImageId()
+ (template != null ? (" Template: " + description) : "") + " Jenkins Server: " + jenkinsServerUrl);
n++;
instanceIds.add(i.getInstanceId());
}
}
}
}
} while(result.getNextToken() != null);

List<SpotInstanceRequest> sirs = null;
List<Filter> filters = new ArrayList<Filter>();
List<String> values;
filters = getGenericFilters(jenkinsServerUrl, template);
if (template != null) {
values = new ArrayList<String>();
values.add(template.getAmi());
filters.add(new Filter("launch.image-id", values));
}

if(jenkinsServerUrl!=null) {
// The instances must match the jenkins server url
filters.add(new Filter("tag:" + EC2Tag.TAG_NAME_JENKINS_SERVER_URL + "=" + jenkinsServerUrl));
filters.add(new Filter("launch.image-id").withValues(template.getAmi()));
}

values = new ArrayList<String>();
values.add(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE);
filters.add(new Filter("tag-key", values));

DescribeSpotInstanceRequestsRequest dsir = new DescribeSpotInstanceRequestsRequest().withFilters(filters);
Set<SpotInstanceRequest> sirSet = new HashSet<>();
try {
sirs = connect().describeSpotInstanceRequests(dsir).getSpotInstanceRequests();
} catch (Exception ex) {
// Some ec2 implementations don't implement spot requests (Eucalyptus)
LOGGER.log(Level.FINEST, "Describe spot instance requests failed", ex);
}
Set<SpotInstanceRequest> sirSet = new HashSet<>();

if (sirs != null) {
for (SpotInstanceRequest sir : sirs) {
Expand Down Expand Up @@ -512,13 +502,27 @@ && isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)
return n;
}

private boolean isEc2ProvisionedJenkinsSlave(List<Tag> tags, String serverUrl) {
for (Tag tag : tags) {
if (StringUtils.equals(tag.getKey(), EC2Tag.TAG_NAME_JENKINS_SERVER_URL)) {
return StringUtils.equals(tag.getValue(), serverUrl);
private List<Filter> getGenericFilters(String jenkinsServerUrl, SlaveTemplate template) {
List<Filter> filters = new ArrayList<>();
filters.add(new Filter("tag-key").withValues(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE));
if (jenkinsServerUrl != null) {
// The instances must match the jenkins server url
filters.add(new Filter("tag:" + EC2Tag.TAG_NAME_JENKINS_SERVER_URL).withValues(jenkinsServerUrl));
} else {
filters.add(new Filter("tag-key").withValues(EC2Tag.TAG_NAME_JENKINS_SERVER_URL));
}

if (template != null) {
List<EC2Tag> tags = template.getTags();
if (tags != null) {
for (EC2Tag tag : tags) {
if (tag.getName() != null && tag.getValue() != null) {
filters.add(new Filter("tag:" + tag.getName()).withValues(tag.getValue()));
}
}
}
}
return (serverUrl == null);
return filters;
}

private boolean isEc2ProvisionedAmiSlave(List<Tag> tags, String description) {
Expand Down