Search in sources :

Example 6 with ElasticAgentProfileProperties

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

the class InstanceSelectionStrategy method instanceForScheduling.

public Optional<ContainerInstance> instanceForScheduling(PluginSettings pluginSettings, ElasticAgentProfileProperties elasticAgentProfileProperties, ContainerDefinition containerDefinition) {
    List<ContainerInstance> containerInstanceList = containerInstanceHelper.getContainerInstances(pluginSettings);
    final EC2Config ec2Config = new EC2Config.Builder().withProfile(elasticAgentProfileProperties).withSettings(pluginSettings).build();
    if (containerInstanceList.isEmpty()) {
        return Optional.empty();
    }
    final List<Instance> ec2Instances = containerInstanceHelper.ec2InstancesFromContainerInstances(pluginSettings, containerInstanceList).stream().filter(instance -> ACCEPTABLE_STATES.contains(instance.getState().getName().toLowerCase())).collect(toList());
    final Map<String, ContainerInstance> instanceMap = toMap(containerInstanceList, ContainerInstance::getEc2InstanceId, containerInstance -> containerInstance);
    sortInstancesForScheduling(ec2Instances);
    for (Instance instance : ec2Instances) {
        final ContainerInstance containerInstance = instanceMap.get(instance.getInstanceId());
        if (instanceMatcher.matches(ec2Config, instance) && containerInstanceMatcher.matches(containerInstance, containerDefinition)) {
            if (isSpotInstance(instance)) {
                containerInstanceHelper.removeLastSeenIdleTag(pluginSettings, asList(instance.getInstanceId()));
            }
            return Optional.of(containerInstance);
        } else {
            LOG.info(format("Skipped container creation on container instance {0}: required resources are not available.", instance.getInstanceId()));
        }
    }
    return Optional.empty();
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ContainerInstanceHelper(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper) PENDING(com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.PENDING) WINDOWS(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.WINDOWS) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) MessageFormat.format(java.text.MessageFormat.format) Util(com.thoughtworks.gocd.elasticagent.ecs.utils.Util) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) RUNNING(com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.RUNNING) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) Platform(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform) Instance(com.amazonaws.services.ec2.model.Instance) ContainerInstanceMatcher(com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.ContainerInstanceMatcher) InstanceMatcher(com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher) PluginSettings(com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) Util.toMap(com.thoughtworks.gocd.elasticagent.ecs.utils.Util.toMap) ContainerDefinition(com.amazonaws.services.ecs.model.ContainerDefinition) Optional(java.util.Optional) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)

Aggregations

ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)6 ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)3 JobIdentifier (com.thoughtworks.gocd.elasticagent.ecs.domain.JobIdentifier)3 Test (org.junit.jupiter.api.Test)3 ContainerDefinition (com.amazonaws.services.ecs.model.ContainerDefinition)2 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 Agents (com.thoughtworks.gocd.elasticagent.ecs.Agents)2 JobCompletionRequest (com.thoughtworks.gocd.elasticagent.ecs.requests.JobCompletionRequest)2 Instance (com.amazonaws.services.ec2.model.Instance)1 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ECSTasks (com.thoughtworks.gocd.elasticagent.ecs.ECSTasks)1 PluginRequest (com.thoughtworks.gocd.elasticagent.ecs.PluginRequest)1 ContainerInstanceHelper (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper)1 EC2Config (com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)1 ContainerInstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.ContainerInstanceMatcher)1 InstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher)1 Agent (com.thoughtworks.gocd.elasticagent.ecs.domain.Agent)1 ConsoleLogAppender (com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender)1 PENDING (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.PENDING)1