Skip to content
Permalink
Browse files
find and destroy clusters that were started more than 7 days ago (#74)
Co-authored-by: Helena Bales <hbales@pivotal.io>
Co-authored-by: Sean Goller <sgoller@pivotal.io>
  • Loading branch information
3 people committed Apr 9, 2019
1 parent 9639b6d commit 483da1706af53dff54a81580280b1ce747e4bd8e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
@@ -57,3 +57,9 @@ task(destroyCluster, dependsOn: 'classes', type: JavaExec) {

systemProperty 'TEST_CI', project.findProperty('ci')
}

task(expireClusters, dependsOn: 'classes', type: JavaExec) {
main = 'org.apache.geode.infrastructure.aws.ExpireClusters'
workingDir = rootDir
classpath = sourceSets.main.runtimeClasspath
}
@@ -0,0 +1,101 @@
package org.apache.geode.infrastructure.aws;

import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.Instance;
import software.amazon.awssdk.services.ec2.model.Tag;

public class ExpireClusters {
static final int days_old = 7;

static Ec2Client ec2 = Ec2Client.create();

public static void main(String[] args) throws IOException, InterruptedException {
if (args.length != 0) {
usage("Usage: ExpireClusters");
return;

}

List<String> expiredClusters = findExpiredClusters();
for (String expiredCluster : expiredClusters) {
String[] arguments = new String[1];
arguments[0] = expiredCluster;

System.out.println("Destroying cluster with tag " + expiredCluster + ".");
DestroyCluster.main(arguments);
}
}

private static List<String> findExpiredClusters() {
Instant expirationTime = getExpirationTime().toInstant();

DescribeInstancesResponse describeInstancesResponse = ec2.describeInstances(
DescribeInstancesRequest.builder()
.filters(Filter.builder()
.name("instance-state-name")
.values("running")
.build())
.build());
Stream<Instance> instances = describeInstancesResponse.reservations().stream().flatMap(reservation -> reservation.instances().stream());

Stream<Instance> expiredInstances = instances.filter(instance -> isBefore(instance.getValueForField("LaunchTime", Instant.class), expirationTime));
Stream<String> tags = expiredInstances.map(ExpireClusters::getTagForInstance);
List<String> distinctTags = tags.distinct().filter(tag -> !tag.isEmpty()).collect(Collectors.toList());
return distinctTags;
}

private static boolean isBefore(Optional<Instant> launchTime, Instant expirationTime) {
if(launchTime.isPresent()) {
if(launchTime.get().isBefore(expirationTime)) {
return true;
}
}
return false;
}

private static String getTagForInstance(Instance expiredInstance) {
Stream<Tag> expiredInstanceTagStream = expiredInstance.tags().stream();
Stream<Tag> geodeBenchmarksTagStream = expiredInstanceTagStream.filter(tag -> tag.key().equals("geode-benchmarks"));
List<String> expiredTags = geodeBenchmarksTagStream.map(Tag::value).collect(Collectors.toList());
if (expiredTags.size() > 0) {
return expiredTags.get(0);
}
else {
return "";
}
}

// private static Date convertToDate(Optional<Instant> optionalDate) {
// try {
// // "2018-03-20T21:38:47.000Z"
// if(optionalDate.isPresent()) {
// String date = optionalDate.get();
// return new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSSZ").parse(date);
// }
// } catch (ParseException e) {
// e.printStackTrace();
// }
//
// return null;
// }

private static void usage(String s) {
throw new IllegalStateException(s);
}

private static Date getExpirationTime() {
long DAY_IN_MS = 1000 * 60 * 60 * 24;
return new Date(System.currentTimeMillis() - (days_old * DAY_IN_MS));
}
}

0 comments on commit 483da17

Please sign in to comment.