Search in sources :

Example 1 with AmazonECS

use of com.amazonaws.services.ecs.AmazonECS in project gocd-ecs-elastic-agent by gocd.

the class TaskHelper method listAllTasks.

public Map<Task, TaskDefinition> listAllTasks(PluginSettings settings) {
    String clusterName = settings.getClusterName();
    AmazonECS ecsClient = settings.ecsClient();
    List<String> taskArns = ecsClient.listTasks(new ListTasksRequest().withCluster(clusterName)).getTaskArns();
    if (taskArns.isEmpty()) {
        return Collections.emptyMap();
    }
    List<Task> tasks = ecsClient.describeTasks(new DescribeTasksRequest().withTasks(taskArns).withCluster(clusterName)).getTasks();
    return tasks.stream().collect(Collectors.toMap(task -> task, task -> ecsClient.describeTaskDefinition(new DescribeTaskDefinitionRequest().withTaskDefinition(task.getTaskDefinitionArn())).getTaskDefinition()));
}
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) AmazonECS(com.amazonaws.services.ecs.AmazonECS)

Example 2 with AmazonECS

use of com.amazonaws.services.ecs.AmazonECS in project gocd-ecs-elastic-agent by gocd.

the class TaskHelper method allRunningContainers.

public List<ECSContainer> allRunningContainers(PluginSettings settings) {
    String clusterName = settings.getClusterName();
    AmazonECS ecsClient = settings.ecsClient();
    List<String> taskArns = ecsClient.listTasks(new ListTasksRequest().withCluster(clusterName).withDesiredStatus(DesiredStatus.RUNNING)).getTaskArns();
    if (taskArns.isEmpty()) {
        return Collections.emptyList();
    }
    List<Task> tasks = ecsClient.describeTasks(new DescribeTasksRequest().withTasks(taskArns).withCluster(clusterName)).getTasks();
    return tasks.stream().map(t -> {
        final TaskDefinition taskDefinition = ecsClient.describeTaskDefinition(new DescribeTaskDefinitionRequest().withTaskDefinition(t.getTaskDefinitionArn())).getTaskDefinition();
        return new ECSContainer(t, taskDefinition);
    }).collect(Collectors.toList());
}
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) AmazonECS(com.amazonaws.services.ecs.AmazonECS)

Example 3 with AmazonECS

use of com.amazonaws.services.ecs.AmazonECS in project gocd-ecs-elastic-agent by gocd.

the class TaskHelper method refreshTask.

public Optional<Task> refreshTask(PluginSettings settings, String taskArn) {
    String clusterName = settings.getClusterName();
    AmazonECS ecsClient = settings.ecsClient();
    List<Task> tasks = ecsClient.describeTasks(new DescribeTasksRequest().withTasks(taskArn).withCluster(clusterName)).getTasks();
    if (tasks.isEmpty()) {
        return empty();
    }
    return Optional.of(tasks.get(0));
}
Also used : ECSTask(com.thoughtworks.gocd.elasticagent.ecs.ECSTask) AmazonECS(com.amazonaws.services.ecs.AmazonECS)

Aggregations

AmazonECS (com.amazonaws.services.ecs.AmazonECS)3 ECSTask (com.thoughtworks.gocd.elasticagent.ecs.ECSTask)3 com.amazonaws.services.ecs.model (com.amazonaws.services.ecs.model)2 Constants (com.thoughtworks.gocd.elasticagent.ecs.Constants)2 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)2 ECSElasticPlugin.getServerId (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId)2 InstanceSelectionStrategyFactory (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory)2 com.thoughtworks.gocd.elasticagent.ecs.domain (com.thoughtworks.gocd.elasticagent.ecs.domain)2 LINUX (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX)2 ContainerFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerFailedToRegisterException)2 ContainerInstanceFailedToRegisterException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException)2 LimitExceededException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException)2 CreateAgentRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.CreateAgentRequest)2 MessageFormat (java.text.MessageFormat)2 MessageFormat.format (java.text.MessageFormat.format)2 java.util (java.util)2 Optional.empty (java.util.Optional.empty)2 Collectors (java.util.stream.Collectors)2 StringUtils.equalsIgnoreCase (org.apache.commons.lang3.StringUtils.equalsIgnoreCase)2