Search in sources :

Example 1 with EC2Config

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

the class ServerPingRequestExecutor method ensureClusterSizeBasedOnPlatform.

private void ensureClusterSizeBasedOnPlatform(PluginSettings pluginSettings, ElasticAgentProfileProperties elasticAgentProfileProperties, EventStream eventStream, ConsoleLogAppender consoleLogAppender) throws LimitExceededException {
    LOG.info(format("[server-ping] Checking running {0} instances in the cluster.", elasticAgentProfileProperties.platform()));
    final EC2Config ec2Config = new EC2Config.Builder().withProfile(elasticAgentProfileProperties).withSettings(pluginSettings).build();
    String instanceName = String.format("%s_%s_INSTANCE", pluginSettings.getClusterName(), elasticAgentProfileProperties.platform());
    final List<Instance> allInstances = containerInstanceHelper.getAllOnDemandInstances(pluginSettings);
    final List<Instance> instancesForPlatform = filterBy(filterByPlatform(allInstances, ec2Config.getPlatform()), hasTag("Name", instanceName));
    int currentClusterSize = instancesForPlatform.size();
    if (currentClusterSize < ec2Config.getMinInstanceCount()) {
        int instancesToCreate = ec2Config.getMinInstanceCount() - currentClusterSize;
        LOG.info(format("[server-ping] Ensuring cluster min size, cluster {0} requires {1} more ec2 instances.", pluginSettings.getClusterName(), instancesToCreate));
        containerInstanceHelper.startOrCreateInstance(pluginSettings, elasticAgentProfileProperties, instancesToCreate, consoleLogAppender);
    } else if (currentClusterSize > ec2Config.getMaxInstancesAllowed()) {
        LOG.info(format("[server-ping] Cluster has total {0} {1} instances which is beyond permissible limit({2}). Terminating idle instances.", currentClusterSize, ec2Config.getPlatform(), ec2Config.getMaxInstancesAllowed()));
        terminateIdleContainerInstance(pluginSettings, instancesForPlatform);
    }
    eventStream.remove(EventFingerprint.forEnsureClusterMinSize());
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) EventFingerprint(com.thoughtworks.gocd.elasticagent.ecs.events.EventFingerprint)

Example 2 with EC2Config

use of com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config 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

Instance (com.amazonaws.services.ec2.model.Instance)2 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)2 EC2Config (com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)2 ContainerDefinition (com.amazonaws.services.ecs.model.ContainerDefinition)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ContainerInstanceHelper (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper)1 ContainerInstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.ContainerInstanceMatcher)1 InstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher)1 PENDING (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.PENDING)1 RUNNING (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.RUNNING)1 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)1 Platform (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform)1 WINDOWS (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.WINDOWS)1 PluginSettings (com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings)1 EventFingerprint (com.thoughtworks.gocd.elasticagent.ecs.events.EventFingerprint)1 Util (com.thoughtworks.gocd.elasticagent.ecs.utils.Util)1 Util.toMap (com.thoughtworks.gocd.elasticagent.ecs.utils.Util.toMap)1 MessageFormat.format (java.text.MessageFormat.format)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1