geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smgol...@apache.org
Subject [geode-benchmarks] branch develop updated: find and destroy clusters that were started more than 7 days ago (#74)
Date Tue, 09 Apr 2019 23:45:22 GMT
This is an automated email from the ASF dual-hosted git repository.

smgoller pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-benchmarks.git


The following commit(s) were added to refs/heads/develop by this push:
     new 483da17  find and destroy clusters that were started more than 7 days ago (#74)
483da17 is described below

commit 483da1706af53dff54a81580280b1ce747e4bd8e
Author: Sean Goller <sean@goller.net>
AuthorDate: Tue Apr 9 16:45:17 2019 -0700

    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>
---
 infrastructure/build.gradle                        |   6 ++
 .../geode/infrastructure/aws/ExpireClusters.java   | 101 +++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/infrastructure/build.gradle b/infrastructure/build.gradle
index 990300d..16eef21 100644
--- a/infrastructure/build.gradle
+++ b/infrastructure/build.gradle
@@ -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
+}
diff --git a/infrastructure/src/main/java/org/apache/geode/infrastructure/aws/ExpireClusters.java
b/infrastructure/src/main/java/org/apache/geode/infrastructure/aws/ExpireClusters.java
new file mode 100644
index 0000000..320d452
--- /dev/null
+++ b/infrastructure/src/main/java/org/apache/geode/infrastructure/aws/ExpireClusters.java
@@ -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));
+  }
+}


Mime
View raw message