Search in sources :

Example 1 with ECSContainer

use of com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer 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 2 with ECSContainer

use of com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer in project gocd-ecs-elastic-agent by gocd.

the class AgentStatusReportViewTest method shouldCreateAgentStatusReport.

@Test
void shouldCreateAgentStatusReport() throws ParseException, IOException, TemplateException {
    final JobIdentifier jobIdentifier = new JobIdentifier("up42", 1L, "foo", "up42_stage", "2", "up42_job", 2L);
    final ECSContainer container = containerWith("arn/container-instance-1", "alpine-container", "alpine", 100, 200, "13/05/2017 12:55:00", "13/05/2017 12:56:30", jobIdentifier);
    final PluginStatusReportViewBuilder statusReportViewBuilder = PluginStatusReportViewBuilder.instance();
    final Template template = statusReportViewBuilder.getTemplate("agent-status-report.template.ftlh");
    final String view = statusReportViewBuilder.build(template, container);
    assertView(view, container, jobIdentifier);
}
Also used : ECSContainer(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 3 with ECSContainer

use of com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer in project gocd-ecs-elastic-agent by gocd.

the class PluginStatusReportViewTest method assertContainersView.

private void assertContainersView(Document document, List<ECSContainer> containers) {
    final Elements headers = document.select(".cluster .ea-c-collapse_body .containers thead tr th");
    final String[] expectedHeaders = { "Name", "Job Identifier", "Image", "Status", "Hard/Soft Memory Limits (MB)", "CPU Units", "Created At", "Started At" };
    final List<String> actualHeaders = headers.stream().map(Element::text).collect(Collectors.toList());
    assertThat(actualHeaders).hasSize(expectedHeaders.length);
    assertThat(actualHeaders).contains(expectedHeaders);
    final Elements containerElements = document.select(".cluster .ea-c-collapse_body .containers tbody tr");
    IntStream.range(0, containers.size()).forEach(index -> {
        ECSContainer container = containers.get(index);
        final Element containerElement = containerElements.get(index);
        assertThat(elementAt(containerElement, "td", 1).text()).isEqualTo(container.getName());
        assertThat(elementAt(containerElement, "td", 2).text()).isEqualTo(container.getJobIdentifier().getRepresentation());
        assertThat(elementAt(containerElement, "td", 3).text()).isEqualTo(container.getImage());
        assertThat(elementAt(containerElement, "td", 4).text()).isEqualTo(container.getLastStatus());
        assertThat(elementAt(containerElement, "td", 5).text()).isEqualTo(format("%s / %s", container.getMemory(), container.getMemoryReservation()));
        assertThat(elementAt(containerElement, "td", 6).text()).isEqualTo(Integer.toString(container.getCpu()));
        assertThat(elementAt(containerElement, "td", 7).text()).startsWith(toDateTimeString(container.getCreatedAt()));
        assertThat(elementAt(containerElement, "td", 8).text()).startsWith(toDateTimeString(container.getStartedAt()));
    });
}
Also used : ECSContainer(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Example 4 with ECSContainer

use of com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer 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 5 with ECSContainer

use of com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer in project gocd-ecs-elastic-agent by gocd.

the class ClusterStatusReportExecutorTest method shouldGenerateHealthView.

@Test
void shouldGenerateHealthView() throws Exception {
    final List<ContainerInstance> containerInstances = Collections.emptyList();
    final List<Instance> ec2Instances = Collections.emptyList();
    final List<ECSContainer> ecsContainers = Collections.emptyList();
    final Cluster cluster = mock(Cluster.class);
    when(request.clusterProfileProperties()).thenReturn(clusterProfileProperties);
    when(containerInstanceHelper.getCluster(clusterProfileProperties)).thenReturn(cluster);
    when(containerInstanceHelper.getContainerInstances(clusterProfileProperties)).thenReturn(containerInstances);
    when(containerInstanceHelper.ec2InstancesFromContainerInstances(clusterProfileProperties, containerInstances)).thenReturn(ec2Instances);
    when(taskHelper.allRunningContainers(clusterProfileProperties)).thenReturn(ecsContainers);
    when(pluginStatusReportViewBuilder.build(any(), anyMap())).thenReturn("plugin_health_html");
    final GoPluginApiResponse response = clusterStatusReportExecutor.execute();
    final String expectedJSON = "{\n" + "\"view\" : \"plugin_health_html\"\n" + "}";
    assertThat(response.responseCode()).isEqualTo(200);
    JSONAssert.assertEquals(expectedJSON, response.responseBody(), true);
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ECSContainer(com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) Cluster(com.amazonaws.services.ecs.model.Cluster) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ECSContainer (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainer)9 Template (freemarker.template.Template)7 Test (org.junit.jupiter.api.Test)6 Instance (com.amazonaws.services.ec2.model.Instance)5 Cluster (com.amazonaws.services.ecs.model.Cluster)5 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)5 JobIdentifier (com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier)4 ECSCluster (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster)3 JsonObject (com.google.gson.JsonObject)2 ContainerInstanceMother.containerInstance (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceMother.containerInstance)2 ECSContainerInstance (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSContainerInstance)2 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)1 AWSModelMother.containerWith (com.thoughtworks.gocd.elasticagent.ecs.domain.AWSModelMother.containerWith)1 ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)1 ServerRequestFailedException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ServerRequestFailedException)1 StatusReportGenerationException (com.thoughtworks.gocd.elasticagent.ecs.reports.StatusReportGenerationException)1