Search in sources :

Example 1 with ContainerInstance

use of com.amazonaws.services.ecs.model.ContainerInstance in project gocd-ecs-elastic-agent by gocd.

the class ServerPingRequestExecutor method terminateIdleContainerInstance.

private void terminateIdleContainerInstance(PluginSettings pluginSettings, List<Instance> instancesForPlatform) {
    final Instance instance = instancesForPlatform.stream().sorted(new MostIdleInstanceComparator(Clock.DEFAULT.now())).collect(toList()).get(0);
    final Optional<ContainerInstance> containerInstance = containerInstanceHelper.onDemandContainerInstances(pluginSettings).stream().filter(ci -> ci.getEc2InstanceId().equals(instance.getInstanceId())).findFirst();
    containerInstance.ifPresent(self -> terminateOperation.execute(pluginSettings, self));
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) ContainerInstanceHelper(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper) EventStream(com.thoughtworks.gocd.elasticagent.ecs.events.EventStream) Event(com.thoughtworks.gocd.elasticagent.ecs.events.Event) EventFingerprint(com.thoughtworks.gocd.elasticagent.ecs.events.EventFingerprint) LimitExceededException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException) java.util(java.util) StopPolicy(com.thoughtworks.gocd.elasticagent.ecs.aws.StopPolicy) EligibleForTerminationPredicate(com.thoughtworks.gocd.elasticagent.ecs.aws.predicate.EligibleForTerminationPredicate) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) MessageFormat.format(java.text.MessageFormat.format) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) ServerRequestFailedException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ServerRequestFailedException) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) Instance(com.amazonaws.services.ec2.model.Instance) SpotInstanceService(com.thoughtworks.gocd.elasticagent.ecs.aws.SpotInstanceService) LINUX(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) StopOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.StopOperation) InstanceSelectionStrategyFactory(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory) Collectors(java.util.stream.Collectors) Platform.values(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.values) Collectors.toList(java.util.stream.Collectors.toList) TerminateOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation) com.thoughtworks.gocd.elasticagent.ecs.domain(com.thoughtworks.gocd.elasticagent.ecs.domain) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) com.thoughtworks.gocd.elasticagent.ecs(com.thoughtworks.gocd.elasticagent.ecs) ServerPingRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.ServerPingRequest) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance)

Example 2 with ContainerInstance

use of com.amazonaws.services.ecs.model.ContainerInstance 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 3 with ContainerInstance

use of com.amazonaws.services.ecs.model.ContainerInstance 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 4 with ContainerInstance

use of com.amazonaws.services.ecs.model.ContainerInstance in project gocd-ecs-elastic-agent by gocd.

the class StopOperationTest method shouldStopInstance.

@Test
void shouldStopInstance() {
    final ContainerInstance instanceToDeregister = containerInstance("i-abcde12", "container-instance-arn");
    ;
    final ArgumentCaptor<StopInstancesRequest> stopInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(StopInstancesRequest.class);
    when(ec2Client.stopInstances(stopInstancesRequestArgumentCaptor.capture())).thenReturn(new StopInstancesResult());
    new StopOperation().execute(pluginSettings, instanceToDeregister);
    final StopInstancesRequest stopInstancesRequest = stopInstancesRequestArgumentCaptor.getValue();
    assertThat(stopInstancesRequest.getInstanceIds()).hasSize(1).contains("i-abcde12");
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 5 with ContainerInstance

use of com.amazonaws.services.ecs.model.ContainerInstance in project gocd-ecs-elastic-agent by gocd.

the class TerminateOperationTest method shouldTerminateInstance.

@Test
void shouldTerminateInstance() {
    final ContainerInstance instanceToDeregister = containerInstance("i-abcde12", "container-instance-arn");
    final ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
    final ArgumentCaptor<DeregisterContainerInstanceRequest> deregisterContainerInstanceRequestArgumentCaptor = ArgumentCaptor.forClass(DeregisterContainerInstanceRequest.class);
    when(pluginSettings.getClusterName()).thenReturn("GoCD");
    when(ecsClient.deregisterContainerInstance(deregisterContainerInstanceRequestArgumentCaptor.capture())).thenReturn(new DeregisterContainerInstanceResult().withContainerInstance(instanceToDeregister));
    when(ec2Client.terminateInstances(terminateInstancesRequestArgumentCaptor.capture())).thenReturn(new TerminateInstancesResult());
    terminateOperation.execute(pluginSettings, instanceToDeregister);
    final DeregisterContainerInstanceRequest deregisterContainerInstanceRequest = deregisterContainerInstanceRequestArgumentCaptor.getValue();
    assertThat(deregisterContainerInstanceRequest.getCluster()).isEqualTo("GoCD");
    assertThat(deregisterContainerInstanceRequest.getContainerInstance()).isEqualTo("container-instance-arn");
    assertThat(deregisterContainerInstanceRequest.getForce()).isEqualTo(true);
    final TerminateInstancesRequest terminateInstancesRequest = terminateInstancesRequestArgumentCaptor.getValue();
    assertThat(terminateInstancesRequest.getInstanceIds()).hasSize(1).contains("i-abcde12");
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) DeregisterContainerInstanceRequest(com.amazonaws.services.ecs.model.DeregisterContainerInstanceRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) DeregisterContainerInstanceResult(com.amazonaws.services.ecs.model.DeregisterContainerInstanceResult) Test(org.junit.jupiter.api.Test)

Aggregations

ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)17 Instance (com.amazonaws.services.ec2.model.Instance)13 Test (org.junit.jupiter.api.Test)9 Cluster (com.amazonaws.services.ecs.model.Cluster)7 ContainerInstanceMother.containerInstance (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceMother.containerInstance)5 ECSContainer (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer)5 ContainerInstanceHelper (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper)4 Template (freemarker.template.Template)4 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)3 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)3 EC2Config (com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)3 MostIdleInstanceComparator (com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator)3 ECSCluster (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster)3 Platform (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform)3 LimitExceededException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException)3 Collectors (java.util.stream.Collectors)3 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)2 com.thoughtworks.gocd.elasticagent.ecs (com.thoughtworks.gocd.elasticagent.ecs)2 SpotInstanceService (com.thoughtworks.gocd.elasticagent.ecs.aws.SpotInstanceService)2 StopPolicy (com.thoughtworks.gocd.elasticagent.ecs.aws.StopPolicy)2