Search in sources :

Example 1 with CreateAgentRequest

use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.

the class ClusterProfilePropertiesTest method shouldGenerateSameUUIDAcrossRequests.

@Test
void shouldGenerateSameUUIDAcrossRequests() {
    String createAgentRequestJSON = "{\n" + "  \"auto_register_key\": \"secret-key\",\n" + "  \"elastic_agent_profile_properties\": {\n" + "    \"Image\": \"value1\",\n" + "    \"MaxMemory\": \"2G\",\n" + "    \"ReservedMemory\": \"150M\",\n" + "    \"TerminationPolicy\": \"\"\n" + "  },\n" + "  \"cluster_profile_properties\": {\n" + "    \"GoServerUrl\": \"https://cd.server.com/go\", \n" + "    \"ClusterName\": \"deployment-cluster\"\n" + "  },\n" + "  \"environment\": \"prod\"\n" + "}";
    String jobCompletionRequestJSON = "{\n" + "  \"elastic_agent_id\": \"ea1\",\n" + "  \"job_identifier\": {\n" + "    \"pipeline_name\": \"test-pipeline\",\n" + "    \"pipeline_counter\": 1,\n" + "    \"pipeline_label\": \"Test Pipeline\",\n" + "    \"stage_name\": \"test-stage\",\n" + "    \"stage_counter\": \"1\",\n" + "    \"job_name\": \"test-job\",\n" + "    \"job_id\": 100\n" + "  },\n" + "  \"elastic_agent_profile_properties\": {\n" + "    \"Image\": \"value1\",\n" + "    \"MaxMemory\": \"2G\",\n" + "    \"ReservedMemory\": \"150M\",\n" + "    \"TerminationPolicy\": \"\"\n" + "  },\n" + "  \"cluster_profile_properties\": {\n" + "    \"GoServerUrl\": \"https://cd.server.com/go\", \n" + "    \"ClusterName\": \"deployment-cluster\"\n" + "  }\n" + "}";
    CreateAgentRequest createAgentRequest = CreateAgentRequest.fromJSON(createAgentRequestJSON);
    String createAgentRequestUUID = createAgentRequest.clusterProfileProperties().uuid();
    JobCompletionRequest jobCompletionRequest = JobCompletionRequest.fromJSON(jobCompletionRequestJSON);
    String jobCompletionRequestUUID = jobCompletionRequest.clusterProfileProperties().uuid();
    assertThat(createAgentRequestUUID).isEqualTo(jobCompletionRequestUUID);
}
Also used : CreateAgentRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest) JobCompletionRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest) Test(org.junit.jupiter.api.Test)

Example 2 with CreateAgentRequest

use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.

the class TaskHelper method create.

public Optional<ECSTask> create(CreateAgentRequest createAgentRequest, PluginSettings pluginSettings, ConsoleLogAppender consoleLogAppender) throws ContainerInstanceFailedToRegisterException, LimitExceededException, ContainerFailedToRegisterException {
    final String taskName = "GoCD" + UUID.randomUUID().toString().replaceAll("-", "");
    final ElasticAgentProfileProperties elasticAgentProfileProperties = createAgentRequest.elasticProfile();
    ContainerDefinition containerDefinition = new ContainerDefinitionBuilder().withName(taskName).pluginSettings(pluginSettings).createAgentRequest(createAgentRequest).withServerId(getServerId()).build();
    StopPolicy stopPolicy = elasticAgentProfileProperties.platform() == LINUX ? pluginSettings.getLinuxStopPolicy() : pluginSettings.getWindowsStopPolicy();
    Optional<ContainerInstance> containerInstance = instanceSelectionStrategyFactory.strategyFor(stopPolicy).instanceForScheduling(pluginSettings, elasticAgentProfileProperties, containerDefinition);
    if (!containerInstance.isPresent()) {
        consoleLogAppender.accept("No running instance(s) found to build the ECS Task to perform current job.");
        LOG.info(format("[create-agent] No running instances found to build container with profile {0}", createAgentRequest.elasticProfile().toJson()));
        if (elasticAgentProfileProperties.runAsSpotInstance()) {
            spotInstanceService.create(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
            return Optional.empty();
        } else {
            containerInstance = containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
        }
    } else {
        consoleLogAppender.accept("Found existing running container instance platform matching ECS Task instance configuration. Not starting a new EC2 instance...");
    }
    final RegisterTaskDefinitionRequest registerTaskDefinitionRequest = registerTaskDefinitionRequestBuilder.build(pluginSettings, elasticAgentProfileProperties, containerDefinition).withFamily(taskName);
    consoleLogAppender.accept("Registering ECS Task definition with cluster...");
    LOG.debug(format("[create-agent] Registering task definition: {0} ", registerTaskDefinitionRequest.toString()));
    RegisterTaskDefinitionResult taskDefinitionResult = pluginSettings.ecsClient().registerTaskDefinition(registerTaskDefinitionRequest);
    consoleLogAppender.accept("Done registering ECS Task definition with cluster.");
    LOG.debug("[create-agent] Done registering task definition");
    TaskDefinition taskDefinitionFromNewTask = taskDefinitionResult.getTaskDefinition();
    StartTaskRequest startTaskRequest = new StartTaskRequest().withTaskDefinition(taskDefinitionFromNewTask.getTaskDefinitionArn()).withContainerInstances(containerInstance.get().getContainerInstanceArn()).withCluster(pluginSettings.getClusterName());
    consoleLogAppender.accept("Starting ECS Task to perform current job...");
    LOG.debug(format("[create-agent] Starting task : {0} ", startTaskRequest.toString()));
    StartTaskResult startTaskResult = pluginSettings.ecsClient().startTask(startTaskRequest);
    LOG.debug("[create-agent] Done executing start task request.");
    if (isStarted(startTaskResult)) {
        String message = elasticAgentProfileProperties.runAsSpotInstance() ? "[WARNING] The ECS task is scheduled on a Spot Instance. A spot instance termination would re-schedule the job." : String.format("ECS Task %s scheduled on container instance %s.", taskName, containerInstance.get().getEc2InstanceId());
        consoleLogAppender.accept(message);
        LOG.info(format("[create-agent] Task {0} scheduled on container instance {1}", taskName, containerInstance.get().getEc2InstanceId()));
        return Optional.of(new ECSTask(startTaskResult.getTasks().get(0), taskDefinitionFromNewTask, elasticAgentProfileProperties, createAgentRequest.getJobIdentifier(), createAgentRequest.environment(), containerInstance.get().getEc2InstanceId()));
    } else {
        deregisterTaskDefinition(pluginSettings, taskDefinitionFromNewTask.getTaskDefinitionArn());
        String errors = startTaskResult.getFailures().stream().map(failure -> "    " + failure.getArn() + " failed with reason :" + failure.getReason()).collect(Collectors.joining("\n"));
        throw new ContainerFailedToRegisterException("Fail to start task " + taskName + ":\n" + errors);
    }
}
Also used : Optional.empty(java.util.Optional.empty) com.amazonaws.services.ecs.model(com.amazonaws.services.ecs.model) LimitExceededException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException) java.util(java.util) ContainerInstanceFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException) InstanceSelectionStrategyFactory(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory) Collectors(java.util.stream.Collectors) MessageFormat(java.text.MessageFormat) Constants(com.thoughtworks.gocd.elasticagent.ecs.Constants) ECSElasticPlugin.getServerId(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId) MessageFormat.format(java.text.MessageFormat.format) com.thoughtworks.gocd.elasticagent.ecs.domain(com.thoughtworks.gocd.elasticagent.ecs.domain) ContainerFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException) CreateAgentRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) AmazonECS(com.amazonaws.services.ecs.AmazonECS) StringUtils.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.equalsIgnoreCase) LINUX(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ContainerFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)

Example 3 with CreateAgentRequest

use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.

the class CreateAgentRequestExecutorTest method shouldAskECSTaskToCreateAnAgent.

@Test
void shouldAskECSTaskToCreateAnAgent() throws Exception {
    ClusterProfileProperties settings = mock(ClusterProfileProperties.class);
    CreateAgentRequest request = mock(CreateAgentRequest.class);
    when(request.clusterProfileProperties()).thenReturn(settings);
    when(request.getJobIdentifier()).thenReturn(new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", 100L));
    when(request.elasticProfile()).thenReturn(new ElasticAgentProfileProperties());
    ECSTasks agentInstances = mock(ECSTasks.class);
    PluginRequest pluginRequest = mock(PluginRequest.class);
    final EventStream eventStream = mock(EventStream.class);
    new CreateAgentRequestExecutor(request, agentInstances, pluginRequest, eventStream).execute();
    verify(agentInstances).create(eq(request), eq(settings), any(ConsoleLogAppender.class));
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) CreateAgentRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest) EventStream(com.thoughtworks.gocd.elasticagent.ecs.events.EventStream) JobIdentifier(com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier) ECSTasks(com.thoughtworks.gocd.elasticagent.ecs.ECSTasks) PluginRequest(com.thoughtworks.gocd.elasticagent.ecs.PluginRequest) ConsoleLogAppender(com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) Test(org.junit.jupiter.api.Test)

Example 4 with CreateAgentRequest

use of com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest in project gocd-ecs-elastic-agent by gocd.

the class ServerPingRequestExecutorTest method testShouldTerminateAgentsThatNeverAutoRegistered.

@Test
void testShouldTerminateAgentsThatNeverAutoRegistered() throws Exception {
    final ECSTask task = mock(ECSTask.class);
    when(task.name()).thenReturn("task-name");
    when(pluginRequest.listAgents()).thenReturn(new Agents(new ArrayList<>()));
    verifyNoMoreInteractions(pluginRequest);
    when(taskHelper.create(any(CreateAgentRequest.class), eq(clusterProfileProperties), any(ConsoleLogAppender.class))).thenReturn(Optional.of(task));
    agentInstances.clock = new Clock.TestClock().forward(Period.minutes(11));
    Optional<ECSTask> container = agentInstances.create(new CreateAgentRequest(null, elasticAgentProfileProperties, null, null), clusterProfileProperties, consoleLogAppender);
    executor.execute();
    assertThat(agentInstances.hasInstance(container.get().name())).isFalse();
}
Also used : CreateAgentRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CreateAgentRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest)4 Test (org.junit.jupiter.api.Test)3 AmazonECS (com.amazonaws.services.ecs.AmazonECS)1 com.amazonaws.services.ecs.model (com.amazonaws.services.ecs.model)1 Constants (com.thoughtworks.gocd.elasticagent.ecs.Constants)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ECSElasticPlugin.getServerId (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId)1 ECSTask (com.thoughtworks.gocd.elasticagent.ecs.ECSTask)1 ECSTasks (com.thoughtworks.gocd.elasticagent.ecs.ECSTasks)1 PluginRequest (com.thoughtworks.gocd.elasticagent.ecs.PluginRequest)1 InstanceSelectionStrategyFactory (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory)1 com.thoughtworks.gocd.elasticagent.ecs.domain (com.thoughtworks.gocd.elasticagent.ecs.domain)1 ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)1 ConsoleLogAppender (com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender)1 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)1 JobIdentifier (com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier)1 LINUX (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX)1 EventStream (com.thoughtworks.gocd.elasticagent.ecs.events.EventStream)1 ContainerFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)1 ContainerInstanceFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException)1