From 47232c37f48981064bc927e3bbae5321657f1f31 Mon Sep 17 00:00:00 2001 From: Svitlana Date: Wed, 29 Apr 2020 19:06:38 +0200 Subject: [PATCH] Add script to terminate instances --- bin/delete_test_instances.sh | 29 +++++++++++++++++++ .../cloud/compute/ec2/AmazonEC2Driver.java | 12 ++++++-- .../java/bubble/model/cloud/BubbleNode.java | 1 + 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100755 bin/delete_test_instances.sh diff --git a/bin/delete_test_instances.sh b/bin/delete_test_instances.sh new file mode 100755 index 00000000..07785e41 --- /dev/null +++ b/bin/delete_test_instances.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Copyright (c) 2020 Bubble, Inc. All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/ +# +# +# Delete test compute cloud instances +# +# Usage: new_bubble.sh config-file +# +# Environment variables +# +# + +SCRIPT="${0}" +SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd) +BUBBLE_ENV="/home/bubble/current/bubble-test.env" + +INSTANCES=aws ec2 describe-instances --filters "Name=tag:Test_instance,Values=${TEST_TAG_CLOUD}" --query "Reservations[].Instances[].InstanceId" + +INSTANCES_COUNT="${#INSTANCES[@]}" + +if [[ ${INSTANCES_COUNT} -gt 0 ]] ; then + + echo "Deleting ${INSTANCES_COUNT} test instances..." + aws ec2 terminate-instances --instance-ids "$INSTANCES" + +else + echo "No instances contains tag \"${TEST_TAG_CLOUD}\" to delete" +fi diff --git a/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java b/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java index 5352db46..ad40aedb 100644 --- a/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java +++ b/bubble-server/src/main/java/bubble/cloud/compute/ec2/AmazonEC2Driver.java @@ -25,8 +25,7 @@ import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import static bubble.model.cloud.BubbleNode.TAG_INSTANCE_ID; -import static bubble.model.cloud.BubbleNode.TAG_SSH_KEY_ID; +import static bubble.model.cloud.BubbleNode.*; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.http.HttpStatusCodes.OK; import static org.cobbzilla.util.security.RsaKeyPair.newRsaKeyPair; @@ -39,7 +38,7 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase { public static final String TAG_NODE_UUID = "nodeUUID"; public static final String KEY_NAME_PREFIX = "KeyName_"; public static final int MIN_COUNT = 1; - public static final int MAX_COUNT = 5; + public static final int MAX_COUNT = 1; @Getter(lazy = true) private final AWSCredentialsProvider ec2credentials = new BubbleAwsCredentialsProvider(cloud, getCredentials()); @@ -164,6 +163,13 @@ public class AmazonEC2Driver extends ComputeServiceDriverBase { .withTags(new Tag(TAG_NODE_UUID, node.getUuid()), new Tag(TAG_CLOUD_UUID, cloud.getUuid())); + // Setting up the tag for the test instance + if (configuration.getEnvironment().containsKey("TEST_TAG_CLOUD")){ + CreateTagsRequest createTestTagRequest = new CreateTagsRequest() + .withResources(instanceId) + .withTags(new Tag(TAG_TEST, configuration.getEnvironment().get("TEST_TAG_CLOUD"))); + } + return node; } diff --git a/bubble-server/src/main/java/bubble/model/cloud/BubbleNode.java b/bubble-server/src/main/java/bubble/model/cloud/BubbleNode.java index 593604b5..a2c8c409 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/BubbleNode.java +++ b/bubble-server/src/main/java/bubble/model/cloud/BubbleNode.java @@ -55,6 +55,7 @@ public class BubbleNode extends IdentifiableBase implements HasNetwork, HasBubbl public static final String TAG_INSTANCE_ID = "instance_id"; public static final String TAG_SSH_KEY_ID = "ssh_key_id"; public static final String TAG_ERROR = "X-Bubble-Error"; + public static final String TAG_TEST = "Test_instance"; private static final List TAG_NAMES = Arrays.asList(TAG_INSTANCE_ID, TAG_SSH_KEY_ID, TAG_ERROR);