Search in sources :

Example 1 with ECSTask

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

the class TaskHelper method fromTaskInfo.

public Optional<ECSTask> fromTaskInfo(Task task, TaskDefinition taskDefinition, Map<String, String> arnToInstanceId, String serverId) {
    List<ContainerDefinition> containerDefinitions = taskDefinition.getContainerDefinitions();
    Map<String, String> labels = containerDefinitions.get(0).getDockerLabels();
    if (!equalsIgnoreCase(labels.getOrDefault(LABEL_SERVER_ID, serverId), serverId)) {
        LOG.debug(MessageFormat.format("Ignoring task {0} as server id({1}) doest not match with {2}", task.getTaskArn(), labels.get(LABEL_SERVER_ID), serverId));
        return empty();
    }
    final String instanceId = arnToInstanceId.get(task.getContainerInstanceArn());
    ElasticAgentProfileProperties elasticAgentProfileProperties = ElasticAgentProfileProperties.fromJson(labels.get(CONFIGURATION_LABEL_KEY));
    JobIdentifier jobIdentifier = JobIdentifier.fromJson(labels.get(LABEL_JOB_IDENTIFIER));
    String env = labels.get(ENVIRONMENT_LABEL_KEY);
    return Optional.of(new ECSTask(task, taskDefinition, elasticAgentProfileProperties, jobIdentifier, env, instanceId));
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask)

Example 2 with ECSTask

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

the class TaskHelperTest method shouldCreateTaskAndScaleUpIfNoMatchingContainerInstanceFound.

@Test
void shouldCreateTaskAndScaleUpIfNoMatchingContainerInstanceFound() throws Exception {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    final TaskDefinition taskDefinition = new TaskDefinition().withTaskDefinitionArn("task-definition-arn");
    final Task task = new Task().withTaskArn("task-arn").withTaskDefinitionArn(taskDefinition.getTaskDefinitionArn());
    final ContainerInstance containerInstance = new ContainerInstance().withContainerInstanceArn("container-instance-arn");
    RegisterTaskDefinitionRequest registerTaskDefinitionRequest = new RegisterTaskDefinitionRequest();
    when(pluginSettings.efsDnsOrIP()).thenReturn("efs-dns-name");
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(ecsClient.registerTaskDefinition(any(RegisterTaskDefinitionRequest.class))).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(taskDefinition));
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(registerTaskDefinitionRequest);
    when(containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender)).thenReturn(Optional.of(containerInstance));
    when(ecsClient.startTask(any(StartTaskRequest.class))).thenReturn(new StartTaskResult().withTasks(task));
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.empty());
    final Optional<ECSTask> ecsTask = taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
    assertThat(ecsTask.isPresent()).isTrue();
    assertThat(ecsTask.get().taskArn()).isEqualTo(task.getTaskArn());
    assertThat(ecsTask.get().taskDefinition()).isEqualTo(taskDefinition);
    verify(containerInstanceHelper).startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
    verifyNoMoreInteractions(containerInstanceHelper);
    InOrder inOrder = inOrder(consoleLogAppender);
    inOrder.verify(consoleLogAppender, times(1)).accept("No running instance(s) found to build the ECS Task to perform current job.");
    inOrder.verify(consoleLogAppender, times(1)).accept("Registering ECS Task definition with cluster...");
    inOrder.verify(consoleLogAppender, times(1)).accept("Done registering ECS Task definition with cluster.");
    inOrder.verify(consoleLogAppender, times(1)).accept("Starting ECS Task to perform current job...");
    inOrder.verify(consoleLogAppender, times(1)).accept(String.format("ECS Task %s scheduled on container instance %s.", registerTaskDefinitionRequest.getFamily(), containerInstance.getEc2InstanceId()));
    verifyNoMoreInteractions(consoleLogAppender);
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) InOrder(org.mockito.InOrder) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Example 3 with ECSTask

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

the class TaskHelperTest method shouldCreateECSTaskFromTaskAndTaskDefinitionOnlyWhenServerIdMatches.

@Test
void shouldCreateECSTaskFromTaskAndTaskDefinitionOnlyWhenServerIdMatches() {
    final Task task = mock(Task.class);
    final TaskDefinition taskDefinition = mock(TaskDefinition.class);
    final ContainerDefinition containerDefinition = mock(ContainerDefinition.class);
    final Map<String, String> dockerLabels = new HashMap<String, String>() {

        {
            put(CONFIGURATION_LABEL_KEY, "{\"Image\":\"alpine\"}");
            put(CREATED_BY_LABEL_KEY, PLUGIN_ID);
            put(ENVIRONMENT_LABEL_KEY, "environment");
            put(LABEL_SERVER_ID, "gocd-server-id");
        }
    };
    when(task.getTaskArn()).thenReturn("task-arn");
    when(task.getContainerInstanceArn()).thenReturn("container-instance-arn");
    when(taskDefinition.getContainerDefinitions()).thenReturn(Collections.singletonList(containerDefinition));
    when(containerDefinition.getDockerLabels()).thenReturn(dockerLabels);
    final Optional<ECSTask> ecsTask = taskHelper.fromTaskInfo(task, taskDefinition, Collections.singletonMap("container-instance-arn", "i-12345ab"), "gocd-server-id");
    assertThat(ecsTask.isPresent()).isTrue();
    assertThat(ecsTask.get().elasticProfile().getImage()).isEqualTo("alpine");
    assertThat(ecsTask.get().environment()).isEqualTo("environment");
    assertThat(ecsTask.get().getEC2InstanceId()).isEqualTo("i-12345ab");
    assertThat(ecsTask.get().taskDefinition()).isEqualTo(taskDefinition);
    assertThat(ecsTask.get().taskArn()).isEqualTo("task-arn");
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) Test(org.junit.jupiter.api.Test)

Example 4 with ECSTask

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

the class TaskHelperTest method shouldIgnoreTaskOnlyWhenContainerDefinitionIsTaggedWithDifferentServerId.

@Test
void shouldIgnoreTaskOnlyWhenContainerDefinitionIsTaggedWithDifferentServerId() {
    final Task task = mock(Task.class);
    final TaskDefinition taskDefinition = mock(TaskDefinition.class);
    final ContainerDefinition containerDefinition = mock(ContainerDefinition.class);
    final Map<String, String> dockerLabels = new HashMap<String, String>() {

        {
            put(CONFIGURATION_LABEL_KEY, "{\"Image\":\"alpine\"}");
            put(CREATED_BY_LABEL_KEY, PLUGIN_ID);
            put(ENVIRONMENT_LABEL_KEY, "environment");
            put(LABEL_SERVER_ID, "unknown-server-id");
        }
    };
    when(task.getTaskArn()).thenReturn("task-arn");
    when(task.getContainerInstanceArn()).thenReturn("container-instance-arn");
    when(taskDefinition.getContainerDefinitions()).thenReturn(Collections.singletonList(containerDefinition));
    when(containerDefinition.getDockerLabels()).thenReturn(dockerLabels);
    final Optional<ECSTask> ecsTask = taskHelper.fromTaskInfo(task, taskDefinition, Collections.singletonMap("container-instance-arn", "i-12345ab"), "gocd-server-id");
    assertThat(ecsTask.isPresent()).isFalse();
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) Test(org.junit.jupiter.api.Test)

Example 5 with ECSTask

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

the class TaskHelperTest method shouldScaleUpASpotInstance_IfNotMatchingContainerInstanceFound_And_IfProfileRequiresASpotInstance.

@Test
void shouldScaleUpASpotInstance_IfNotMatchingContainerInstanceFound_And_IfProfileRequiresASpotInstance() throws LimitExceededException, ContainerFailedToRegisterException {
    final InstanceSelectionStrategy instanceSelectionStrategy = mock(InstanceSelectionStrategy.class);
    final TaskDefinition taskDefinition = new TaskDefinition().withTaskDefinitionArn("task-definition-arn");
    final Task task = new Task().withTaskArn("task-arn").withTaskDefinitionArn(taskDefinition.getTaskDefinitionArn());
    final ContainerInstance containerInstance = new ContainerInstance().withContainerInstanceArn("container-instance-arn");
    RegisterTaskDefinitionRequest registerTaskDefinitionRequest = new RegisterTaskDefinitionRequest();
    when(pluginSettings.efsDnsOrIP()).thenReturn("efs-dns-name");
    when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
    when(elasticAgentProfileProperties.runAsSpotInstance()).thenReturn(true);
    when(ecsClient.registerTaskDefinition(any(RegisterTaskDefinitionRequest.class))).thenReturn(new RegisterTaskDefinitionResult().withTaskDefinition(taskDefinition));
    when(registerTaskDefinitionRequestBuilder.build(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(registerTaskDefinitionRequest);
    when(containerInstanceHelper.startOrCreateOneInstance(pluginSettings, elasticAgentProfileProperties, consoleLogAppender)).thenReturn(Optional.of(containerInstance));
    when(ecsClient.startTask(any(StartTaskRequest.class))).thenReturn(new StartTaskResult().withTasks(task));
    when(instanceSelectionStrategyFactory.strategyFor(any())).thenReturn(instanceSelectionStrategy);
    when(instanceSelectionStrategy.instanceForScheduling(eq(pluginSettings), eq(elasticAgentProfileProperties), any(ContainerDefinition.class))).thenReturn(Optional.empty());
    final Optional<ECSTask> ecsTask = taskHelper.create(createAgentRequest, pluginSettings, consoleLogAppender);
    assertThat(ecsTask.isPresent()).isFalse();
    verify(spotInstanceService).create(pluginSettings, elasticAgentProfileProperties, consoleLogAppender);
    verifyNoMoreInteractions(containerInstanceHelper);
    InOrder inOrder = inOrder(consoleLogAppender);
    inOrder.verify(consoleLogAppender, times(1)).accept("No running instance(s) found to build the ECS Task to perform current job.");
    verifyNoMoreInteractions(consoleLogAppender);
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) InOrder(org.mockito.InOrder) InstanceSelectionStrategy(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

ECSTask (com.thoughtworks.gocd.elasticagent.ecs.ECSTask)10 Test (org.junit.jupiter.api.Test)7 InstanceSelectionStrategy (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategy)3 InOrder (org.mockito.InOrder)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 InstanceSelectionStrategyFactory (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory)1 com.thoughtworks.gocd.elasticagent.ecs.domain (com.thoughtworks.gocd.elasticagent.ecs.domain)1 LINUX (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX)1 ContainerFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)1 ContainerInstanceFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException)1 LimitExceededException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException)1 CreateAgentRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest)1 MessageFormat (java.text.MessageFormat)1 MessageFormat.format (java.text.MessageFormat.format)1 java.util (java.util)1 Optional.empty (java.util.Optional.empty)1