Search in sources :

Example 1 with ClusterProfileProperties

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

the class ClusterStatusReportRequestTest method shouldDeserializeFromJSON.

@Test
void shouldDeserializeFromJSON() {
    String json = "{\n" + "  \"cluster_profile_properties\": {\n" + "    \"GoServerUrl\": \"https://cd.server.com/go\", \n" + "    \"ClusterName\": \"deployment-cluster\"\n" + "  }\n" + "}";
    ClusterStatusReportRequest request = ClusterStatusReportRequest.fromJSON(json);
    Map<String, String> clusterProfileConfigurations = new HashMap<>();
    clusterProfileConfigurations.put("GoServerUrl", "https://cd.server.com/go");
    clusterProfileConfigurations.put("ClusterName", "deployment-cluster");
    ClusterProfileProperties expectedClusterProfileProperties = ClusterProfileProperties.fromConfiguration(clusterProfileConfigurations);
    assertThat(request.clusterProfileProperties()).isEqualTo(expectedClusterProfileProperties);
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 2 with ClusterProfileProperties

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

the class ShouldAssignWorkRequestTest method shouldDeserializeFromJSON.

@Test
void shouldDeserializeFromJSON() {
    String json = "{\n" + "  \"environment\": \"prod\",\n" + "  \"agent\": {\n" + "    \"agent_id\": \"42\",\n" + "    \"agent_state\": \"Idle\",\n" + "    \"build_state\": \"Idle\",\n" + "    \"config_state\": \"Enabled\"\n" + "  },\n" + "  \"elastic_agent_profile_properties\": {\n" + "    \"Image\": \"go-agent\"\n" + "  },\n" + "  \"cluster_profile_properties\": {\n" + "    \"GoServerUrl\": \"https://cd.server.com/go\", \n" + "    \"ClusterName\": \"deployment-cluster\"\n" + "  }\n" + "}";
    ShouldAssignWorkRequest request = ShouldAssignWorkRequest.fromJSON(json);
    assertThat(request.environment()).isEqualTo("prod");
    assertThat(request.agent()).isEqualTo(new Agent("42", Agent.AgentState.Idle, Agent.BuildState.Idle, Agent.ConfigState.Enabled));
    assertThat(request.elasticProfile().getImage()).isEqualTo("go-agent");
    Map<String, String> clusterProfileConfigurations = new HashMap<>();
    clusterProfileConfigurations.put("GoServerUrl", "https://cd.server.com/go");
    clusterProfileConfigurations.put("ClusterName", "deployment-cluster");
    ClusterProfileProperties expectedClusterProfileProperties = ClusterProfileProperties.fromConfiguration(clusterProfileConfigurations);
    assertThat(request.clusterProfileProperties()).isEqualTo(expectedClusterProfileProperties);
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) Agent(com.thoughtworks.gocd.elasticagent.ecs.domain.Agent) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with ClusterProfileProperties

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

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

the class JobCompletionRequestExecutorTest method shouldTerminateElasticAgentOnJobCompletion.

@Test
public void shouldTerminateElasticAgentOnJobCompletion() throws Exception {
    JobIdentifier jobIdentifier = new JobIdentifier("test", 1L, "test", "test_stage", "1", "test_job", 100L);
    String elasticAgentId = "agent-1";
    ClusterProfileProperties clusterProfileProperties = new ClusterProfileProperties();
    JobCompletionRequest request = new JobCompletionRequest(elasticAgentId, jobIdentifier, new ElasticAgentProfileProperties(), clusterProfileProperties);
    JobCompletionRequestExecutor executor = new JobCompletionRequestExecutor(request, mockAgentInstances, mockPluginRequest);
    Agents agents = new Agents();
    agents.add(new Agent(elasticAgentId));
    when(mockPluginRequest.listAgents()).thenReturn(agents);
    GoPluginApiResponse response = executor.execute();
    InOrder inOrder = inOrder(mockPluginRequest, mockAgentInstances);
    inOrder.verify(mockPluginRequest).disableAgents(agentsArgumentCaptor.capture());
    inOrder.verify(mockAgentInstances).terminate(elasticAgentId, clusterProfileProperties);
    inOrder.verify(mockPluginRequest).deleteAgents(agentsArgumentCaptor.capture());
    List<Agent> agentsToDisabled = agentsArgumentCaptor.getValue();
    assertThat(1).isEqualTo(agentsToDisabled.size());
    assertThat(elasticAgentId).isEqualTo(agentsToDisabled.get(0).elasticAgentId());
    List<Agent> agentsToDelete = agentsArgumentCaptor.getValue();
    assertThat(agentsToDisabled).isEqualTo(agentsToDelete);
    assertThat(200).isEqualTo(response.responseCode());
    assertThat(response.responseBody().isEmpty()).isTrue();
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) Agent(com.thoughtworks.gocd.elasticagent.ecs.domain.Agent) InOrder(org.mockito.InOrder) Agents(com.thoughtworks.gocd.elasticagent.ecs.Agents) JobCompletionRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) Test(org.junit.jupiter.api.Test)

Example 5 with ClusterProfileProperties

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

the class ECSElasticPlugin method handle.

@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) {
    ClusterProfileProperties clusterProfileProperties;
    try {
        fetchServerIdFromServer();
        LOG.debug("Request from server: " + request.requestName());
        switch(Request.fromString(request.requestName())) {
            case PLUGIN_SETTINGS_GET_ICON:
                return new GetPluginSettingsIconExecutor().execute();
            case REQUEST_GET_ELASTIC_AGENT_PROFILE_METADATA:
                return new GetProfileMetadataExecutor().execute();
            case REQUEST_GET_ELASTIC_AGENT_PROFILE_VIEW:
                return new GetProfileViewExecutor().execute();
            case REQUEST_VALIDATE_ELASTIC_AGENT_PROFILE:
                return ProfileValidateRequest.fromJSON(request.requestBody()).executor().execute();
            case REQUEST_GET_CLUSTER_PROFILE_METADATA:
                return new GetClusterProfileMetadataExecutor().execute();
            case REQUEST_GET_CLUSTER_PROFILE_VIEW:
                return new GetClusterProfileViewRequestExecutor().execute();
            case REQUEST_VALIDATE_CLUSTER_PROFILE_CONFIGURATION:
                return ValidateClusterProfileRequest.fromJSON(request.requestBody()).executor().execute();
            case REQUEST_CREATE_AGENT:
                CreateAgentRequest createAgentRequest = CreateAgentRequest.fromJSON(request.requestBody());
                clusterProfileProperties = createAgentRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return createAgentRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest, getEventStreamFor(clusterProfileProperties)).execute();
            case REQUEST_SHOULD_ASSIGN_WORK:
                ShouldAssignWorkRequest shouldAssignWorkRequest = ShouldAssignWorkRequest.fromJSON(request.requestBody());
                clusterProfileProperties = shouldAssignWorkRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return shouldAssignWorkRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
            case REQUEST_JOB_COMPLETION:
                JobCompletionRequest jobCompletionRequest = JobCompletionRequest.fromJSON(request.requestBody());
                clusterProfileProperties = jobCompletionRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return jobCompletionRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest).execute();
            case REQUEST_SERVER_PING:
                ServerPingRequest serverPingRequest = ServerPingRequest.fromJSON(request.requestBody());
                List<ClusterProfileProperties> listOfClusterProfileProperties = serverPingRequest.allClusterProfileProperties();
                refreshInstancesForAllClusters(listOfClusterProfileProperties);
                return serverPingRequest.executor(clusterSpecificAgentInstances, pluginRequest).execute();
            case REQUEST_CLUSTER_STATUS_REPORT:
                ClusterStatusReportRequest clusterStatusReportRequest = ClusterStatusReportRequest.fromJSON(request.requestBody());
                clusterProfileProperties = clusterStatusReportRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return clusterStatusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties), getEventStreamFor(clusterProfileProperties)).execute();
            case REQUEST_AGENT_STATUS_REPORT:
                AgentStatusReportRequest statusReportRequest = AgentStatusReportRequest.fromJSON(request.requestBody());
                clusterProfileProperties = statusReportRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return statusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
            case REQUEST_GET_CAPABILITIES:
                return new GetCapabilitiesExecutor().execute();
            case REQUEST_CLUSTER_PROFILE_CHANGED:
                return new DefaultGoPluginApiResponse(200);
            case REQUEST_MIGRATE_CONFIGURATION:
                return MigrateConfigurationRequest.fromJSON(request.requestBody()).executor().execute();
            default:
                throw new UnhandledRequestTypeException(request.requestName());
        }
    } catch (AmazonEC2Exception e) {
        LOG.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
        return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
    } catch (Exception e) {
        LOG.error("Failed to handle request " + request.requestName() + " due to:", e);
        return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
    }
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Aggregations

ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)10 Test (org.junit.jupiter.api.Test)8 HashMap (java.util.HashMap)6 JobIdentifier (com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier)5 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)3 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 Agents (com.thoughtworks.gocd.elasticagent.ecs.Agents)2 Agent (com.thoughtworks.gocd.elasticagent.ecs.domain.Agent)2 JobCompletionRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 Instance (com.amazonaws.services.ec2.model.Instance)1 Cluster (com.amazonaws.services.ecs.model.Cluster)1 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)1 JsonObject (com.google.gson.JsonObject)1 UnhandledRequestTypeException (com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException)1 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)1 ECSTasks (com.thoughtworks.gocd.elasticagent.ecs.ECSTasks)1 PluginRequest (com.thoughtworks.gocd.elasticagent.ecs.PluginRequest)1 ConsoleLogAppender (com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender)1 ECSCluster (com.thoughtworks.gocd.elasticagent.ecs.domain.ECSCluster)1