Skip to content

Commit

Permalink
Merge pull request #2 from perfectsense/feature/sample-down-command
Browse files Browse the repository at this point in the history
Example command, "gyro down", that will stop instances in a Gyro config
  • Loading branch information
Jeremy Collins authored Jan 22, 2020
2 parents 47b8672 + 9777864 commit 4558f4b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (System.getenv('TRAVIS_BRANCH') && System.getenv('TRAVIS_PULL_REQUEST') == 'f
}

group = 'gyro'
version = '0.15-SNAPSHOT'
version = '0.99.1-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -48,20 +48,22 @@ repositories {
}

dependencies {
api 'gyro:gyro-core:0.15-SNAPSHOT'
api 'gyro:gyro-core:0.99.1-SNAPSHOT'

implementation 'com.psddev:dari-util:3.3.607-xe0f27a'
implementation 'org.yaml:snakeyaml:1.24'
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.0.201906121030-r'
implementation 'us.monoid.web:resty:0.3.2'
implementation 'gyro:gyro-aws-provider:0.15-SNAPSHOT'
implementation 'gyro:gyro-aws-provider:0.99.1-SNAPSHOT'
implementation enforcedPlatform('software.amazon.awssdk:bom:2.5.70')
implementation 'software.amazon.awssdk:s3'
implementation 'software.amazon.awssdk:apache-client'
implementation 'com.spotify:docker-client:8.6.2:shaded'
implementation 'org.apache.httpcomponents:httpclient:4.5'
implementation 'org.apache.httpcomponents:httpcore:4.4'
implementation 'javax.activation:activation:1.1.1'
implementation 'software.amazon.awssdk:autoscaling'
implementation 'software.amazon.awssdk:ec2'
}

publishing {
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/gyro/sample/command/DownCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package gyro.sample.command;

import java.util.List;
import java.util.stream.Collectors;

import gyro.aws.AwsCredentials;
import gyro.aws.autoscaling.AutoScalingGroupResource;
import gyro.aws.ec2.InstanceResource;
import gyro.core.GyroCore;
import gyro.core.command.AbstractConfigCommand;
import gyro.core.resource.Resource;
import gyro.core.scope.RootScope;
import gyro.core.scope.State;
import io.airlift.airline.Command;
import software.amazon.awssdk.services.autoscaling.AutoScalingClient;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.InstanceStateName;

@Command(name = "down", description = "Stop AWS instances and clear instances in autoscaling groups.")
public class DownCommand extends AbstractConfigCommand {

@Override
public void doExecute(RootScope current, RootScope pending, State state) {
List<Resource> resources = current.findResourcesIn(current.getLoadFiles());
List<InstanceResource> instances = resources
.stream()
.filter(InstanceResource.class::isInstance)
.map(InstanceResource.class::cast)
.filter(this::isInstanceRunning)
.collect(Collectors.toList());

List<AutoScalingGroupResource> autoscalingGroups = resources
.stream()
.filter(AutoScalingGroupResource.class::isInstance)
.map(AutoScalingGroupResource.class::cast)
.filter(this::isAutoScalingGroupActive)
.collect(Collectors.toList());

if (instances.isEmpty() && autoscalingGroups.isEmpty()) {
GyroCore.ui().write("\nNo running instances or auto scaling groups found, beam down terminate.\n");
return;
}

if (!instances.isEmpty()) {
GyroCore.ui().write("\nInstances:\n");
instances.forEach(this::displayInstance);
}

if (!autoscalingGroups.isEmpty()) {
GyroCore.ui().write("\nAuto scaling groups:\n");
autoscalingGroups.forEach(this::displayAutoScalingGroup);
}

if (GyroCore.ui()
.readBoolean(
Boolean.FALSE,
"\nAre you sure you want to stop instances and clear the auto scaling groups listed above?")) {
instances.forEach(this::stopInstance);
autoscalingGroups.forEach(this::clearAutoscalingGroup);
}
}

private boolean isInstanceRunning(InstanceResource instance) {
return InstanceStateName.RUNNING.toString().equals(instance.getGyroInstanceState());
}

private void displayInstance(InstanceResource instance) {
GyroCore.ui().write("- [%s] %s\n", instance.getGyroInstanceLocation(), instance.getGyroInstanceId());
}

private void stopInstance(InstanceResource instance) {
try (Ec2Client client = InstanceResource.createClient(
Ec2Client.class,
instance.credentials(AwsCredentials.class))) {
client.stopInstances(r -> r.instanceIds(instance.getGyroInstanceId()));
}
}

private boolean isAutoScalingGroupActive(AutoScalingGroupResource group) {
return group.getMinSize() > 0 || group.getMaxSize() > 0;
}

private void displayAutoScalingGroup(AutoScalingGroupResource group) {
GyroCore.ui().write("- %s\n", group.getName());
}

private void clearAutoscalingGroup(AutoScalingGroupResource group) {
try (AutoScalingClient client = AutoScalingGroupResource.createClient(
AutoScalingClient.class,
group.credentials(AwsCredentials.class))) {
client.updateAutoScalingGroup(r -> r
.autoScalingGroupName(group.getName())
.maxSize(0)
.minSize(0));
}
}

}

0 comments on commit 4558f4b

Please sign in to comment.