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));
}
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);
}
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");
}
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();
}
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);
}
Aggregations