Search in sources :

Example 6 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project gocd-ecs-elastic-agent by gocd.

the class ServerPingRequestExecutor method ensureClusterSizeBasedOnPlatform.

private void ensureClusterSizeBasedOnPlatform(PluginSettings pluginSettings, ElasticAgentProfileProperties elasticAgentProfileProperties, EventStream eventStream, ConsoleLogAppender consoleLogAppender) throws LimitExceededException {
    LOG.info(format("[server-ping] Checking running {0} instances in the cluster.", elasticAgentProfileProperties.platform()));
    final EC2Config ec2Config = new EC2Config.Builder().withProfile(elasticAgentProfileProperties).withSettings(pluginSettings).build();
    String instanceName = String.format("%s_%s_INSTANCE", pluginSettings.getClusterName(), elasticAgentProfileProperties.platform());
    final List<Instance> allInstances = containerInstanceHelper.getAllOnDemandInstances(pluginSettings);
    final List<Instance> instancesForPlatform = filterBy(filterByPlatform(allInstances, ec2Config.getPlatform()), hasTag("Name", instanceName));
    int currentClusterSize = instancesForPlatform.size();
    if (currentClusterSize < ec2Config.getMinInstanceCount()) {
        int instancesToCreate = ec2Config.getMinInstanceCount() - currentClusterSize;
        LOG.info(format("[server-ping] Ensuring cluster min size, cluster {0} requires {1} more ec2 instances.", pluginSettings.getClusterName(), instancesToCreate));
        containerInstanceHelper.startOrCreateInstance(pluginSettings, elasticAgentProfileProperties, instancesToCreate, consoleLogAppender);
    } else if (currentClusterSize > ec2Config.getMaxInstancesAllowed()) {
        LOG.info(format("[server-ping] Cluster has total {0} {1} instances which is beyond permissible limit({2}). Terminating idle instances.", currentClusterSize, ec2Config.getPlatform(), ec2Config.getMaxInstancesAllowed()));
        terminateIdleContainerInstance(pluginSettings, instancesForPlatform);
    }
    eventStream.remove(EventFingerprint.forEnsureClusterMinSize());
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) EventFingerprint(com.thoughtworks.gocd.elasticagent.ecs.events.EventFingerprint)

Example 7 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project gocd-ecs-elastic-agent by gocd.

the class ClusterStatusReportExecutor method execute.

@Override
public GoPluginApiResponse execute() throws IOException, TemplateException {
    final Map<String, Object> dataModel = new HashMap<>();
    try {
        final ClusterProfileProperties clusterProfileProperties = request.clusterProfileProperties();
        agentInstances.refreshAll(clusterProfileProperties);
        if (clusterProfileProperties == null || !clusterProfileProperties.isConfigured()) {
            throw new RuntimeException("Please configure the cluster profile properly before using the plugin.");
        }
        final Cluster cluster = helper.getCluster(clusterProfileProperties);
        final List<ContainerInstance> containerInstances = new ArrayList<>();
        final List<Instance> instances = new ArrayList<>();
        final List<ECSContainer> ecsContainers = new ArrayList<>();
        if (cluster.getRegisteredContainerInstancesCount() != 0) {
            containerInstances.addAll(helper.getContainerInstances(clusterProfileProperties));
            instances.addAll(helper.ec2InstancesFromContainerInstances(clusterProfileProperties, containerInstances));
            ecsContainers.addAll(taskHelper.allRunningContainers(clusterProfileProperties));
        }
        dataModel.put("region", clusterProfileProperties.getRegion());
        final ECSCluster ecsCluster = new ECSCluster(cluster, containerInstances, instances, ecsContainers, clusterProfileProperties.getMaxLinuxInstancesAllowed(), clusterProfileProperties.getMaxWindowsInstancesAllowed(), clusterProfileProperties.getMaxLinuxSpotInstanceAllowed(), clusterProfileProperties.getMaxWindowsSpotInstanceAllowed());
        dataModel.put("cluster", ecsCluster);
        eventStream.remove(EventFingerprint.forStatusReport());
    } catch (Exception e) {
        LOG.error("[status report] Error accessing ECS cluster details", e);
        final String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        eventStream.update(Event.errorEvent(EventFingerprint.forStatusReport(), "Error accessing ECS cluster details", errorMessage));
    }
    addPluginVersionInformation(dataModel);
    dataModel.put("errors", eventStream.allErrors());
    dataModel.put("warnings", eventStream.allWarnings());
    final Template template = pluginStatusReportViewBuilder.getTemplate("status-report.template.ftlh");
    final String view = pluginStatusReportViewBuilder.build(template, dataModel);
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("view", view);
    return DefaultGoPluginApiResponse.success(jsonObject.toString());
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) HashMap(java.util.HashMap) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) ECSCluster(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster) Cluster(com.amazonaws.services.ecs.model.Cluster) JsonObject(com.google.gson.JsonObject) ECSCluster(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ECSContainer(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer) JsonObject(com.google.gson.JsonObject)

Example 8 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project gocd-ecs-elastic-agent by gocd.

the class PluginStatusReportViewTest method shouldBuildStatusReportView.

@Test
void shouldBuildStatusReportView() throws Exception {
    final Cluster cluster = clusterWith("GoCD", 5, 10, 0);
    final ContainerInstance containerInstance = containerInstance("instance-id-1", "arn/container-instance-1", "ACTIVE", 8, 4096, 4, 1024);
    final Instance instance = instance("instance-id-1", C3Large, "ami-2dad3da", toDate("13/05/2017 12:50:20"));
    final ECSContainer alpineContainer = containerWith("arn/container-instance-1", "alpine-container", "alpine", 100, 200, "13/05/2017 12:55:00", "13/05/2017 12:56:30", new JobIdentifier("up42", 1L, "foo", "up42_stage", "2", "up42_job", 25632868237L));
    final ECSCluster ecsCluster = new ECSCluster(cluster, singletonList(containerInstance), singletonList(instance), singletonList(alpineContainer), 2, 3, 0, 0);
    Map<String, Object> dataModel = new HashMap<>();
    dataModel.put("cluster", ecsCluster);
    dataModel.put("errors", Collections.emptyList());
    dataModel.put("region", "us-east-2");
    final PluginStatusReportViewBuilder statusReportViewBuilder = PluginStatusReportViewBuilder.instance();
    final Template template = statusReportViewBuilder.getTemplate("status-report.template.ftlh");
    final String view = statusReportViewBuilder.build(template, dataModel);
    assertThat(view).contains("/go/admin/status_reports/com.thoughtworks.gocd.elastic-agent.ecs/agent/alpine-container?job_id=25632868237");
    assertView(view, ecsCluster);
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ECSContainerInstance(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainerInstance) ECSContainer(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ContainerInstanceMother.containerInstance(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceMother.containerInstance) Instance(com.amazonaws.services.ec2.model.Instance) ECSContainerInstance(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainerInstance) ECSCluster(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster) Cluster(com.amazonaws.services.ecs.model.Cluster) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) ECSCluster(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 9 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project gocd-ecs-elastic-agent by gocd.

the class EligibleForTerminationPredicateTest method shouldReturnFalseForInstanceWithState.

@ParameterizedTest
@ValueSource(strings = { PENDING, RUNNING, STOPPING, SHUTTING_DOWN, TERMINATED })
void shouldReturnFalseForInstanceWithState(String state) {
    final Instance instance = instance("i-abcd123", state, Platform.WINDOWS.name());
    final boolean testResult = new EligibleForTerminationPredicate(null).test(instance);
    assertThat(testResult).isFalse();
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project gocd-ecs-elastic-agent by gocd.

the class EligibleForTerminationPredicateTest method shouldReturnFalseWhenTerminationTimeIsNotReached.

@ParameterizedTest
@EnumSource(Platform.class)
void shouldReturnFalseWhenTerminationTimeIsNotReached(Platform platform) {
    final Clock.TestClock testClock = new Clock.TestClock();
    final Instance instance = instance("i-abcd123", STOPPED, platform.name()).withTags(new Tag(STOPPED_AT, String.valueOf(testClock.now().getMillis())));
    when(pluginSettings.terminateStoppedLinuxInstanceAfter()).thenReturn(Period.seconds(20));
    when(pluginSettings.terminateStoppedWindowsInstanceAfter()).thenReturn(Period.seconds(20));
    testClock.forward(Period.seconds(19));
    final boolean testResult = new EligibleForTerminationPredicate(pluginSettings, testClock).test(instance);
    assertThat(testResult).isFalse();
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) InstanceMother.linuxInstanceWithTag(com.thoughtworks.gocd.elasticagent.ecs.aws.InstanceMother.linuxInstanceWithTag) Tag(com.amazonaws.services.ec2.model.Tag) Clock(com.thoughtworks.gocd.elasticagent.ecs.Clock) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18