Search in sources :

Example 1 with TerminateOperation

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

the class ContainerInstanceHelper method terminateMostIdleStoppedInstance.

private void terminateMostIdleStoppedInstance(PluginSettings pluginSettings, List<Instance> stoppedInstances) {
    stoppedInstances.sort(new MostIdleInstanceComparator(Clock.DEFAULT.now()));
    final String instanceId = stoppedInstances.get(0).getInstanceId();
    LOG.info(format("Terminating stopped instance as max cluster limit is reached {0}.", instanceId));
    final Optional<ContainerInstance> containerInstance = getContainerInstances(pluginSettings).stream().filter(ci -> ci.getEc2InstanceId().equals(instanceId)).findFirst();
    containerInstance.ifPresent(self -> new TerminateOperation().execute(pluginSettings, self));
}
Also used : ListUtils.union(org.apache.commons.collections4.ListUtils.union) com.amazonaws.services.ecs.model(com.amazonaws.services.ecs.model) LimitExceededException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException) java.util(java.util) ConsoleLogAppender(com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender) ECSElasticPlugin(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin) com.amazonaws.services.ec2.model(com.amazonaws.services.ec2.model) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) PeriodFormatter(org.joda.time.format.PeriodFormatter) Poller(com.thoughtworks.gocd.elasticagent.ecs.aws.wait.Poller) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Constants(com.thoughtworks.gocd.elasticagent.ecs.Constants) LABEL_SERVER_ID(com.thoughtworks.gocd.elasticagent.ecs.Constants.LABEL_SERVER_ID) EC2InstanceState(com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState) MessageFormat.format(java.text.MessageFormat.format) Result(com.thoughtworks.gocd.elasticagent.ecs.aws.wait.Result) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) Platform(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform) InstanceMatcher(com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher) SPOT_INSTANCE_NAME_FORMAT(com.thoughtworks.gocd.elasticagent.ecs.aws.SpotInstanceHelper.SPOT_INSTANCE_NAME_FORMAT) Clock(com.thoughtworks.gocd.elasticagent.ecs.Clock) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) PluginSettings(com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings) ContainerInstanceFailedToRegisterException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ContainerInstanceFailedToRegisterException) Collectors(java.util.stream.Collectors) LAST_SEEN_IDLE(com.thoughtworks.gocd.elasticagent.ecs.Constants.LAST_SEEN_IDLE) ECSElasticPlugin.getServerId(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId) TerminateOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation) Stream(java.util.stream.Stream) String.valueOf(java.lang.String.valueOf) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) Tag(com.amazonaws.services.ec2.model.Tag) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) TerminateOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation)

Aggregations

com.amazonaws.services.ec2.model (com.amazonaws.services.ec2.model)1 Tag (com.amazonaws.services.ec2.model.Tag)1 com.amazonaws.services.ecs.model (com.amazonaws.services.ecs.model)1 Clock (com.thoughtworks.gocd.elasticagent.ecs.Clock)1 Constants (com.thoughtworks.gocd.elasticagent.ecs.Constants)1 LABEL_SERVER_ID (com.thoughtworks.gocd.elasticagent.ecs.Constants.LABEL_SERVER_ID)1 LAST_SEEN_IDLE (com.thoughtworks.gocd.elasticagent.ecs.Constants.LAST_SEEN_IDLE)1 ECSElasticPlugin (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ECSElasticPlugin.getServerId (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.getServerId)1 SPOT_INSTANCE_NAME_FORMAT (com.thoughtworks.gocd.elasticagent.ecs.aws.SpotInstanceHelper.SPOT_INSTANCE_NAME_FORMAT)1 MostIdleInstanceComparator (com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator)1 InstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher)1 TerminateOperation (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation)1 Poller (com.thoughtworks.gocd.elasticagent.ecs.aws.wait.Poller)1 Result (com.thoughtworks.gocd.elasticagent.ecs.aws.wait.Result)1 ConsoleLogAppender (com.thoughtworks.gocd.elasticagent.ecs.domain.ConsoleLogAppender)1 EC2InstanceState (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState)1 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)1 Platform (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform)1