Search in sources :

Example 1 with MostIdleInstanceComparator

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

the class ServerPingRequestExecutor method terminateIdleContainerInstance.

private void terminateIdleContainerInstance(PluginSettings pluginSettings, List<Instance> instancesForPlatform) {
    final Instance instance = instancesForPlatform.stream().sorted(new MostIdleInstanceComparator(Clock.DEFAULT.now())).collect(toList()).get(0);
    final Optional<ContainerInstance> containerInstance = containerInstanceHelper.onDemandContainerInstances(pluginSettings).stream().filter(ci -> ci.getEc2InstanceId().equals(instance.getInstanceId())).findFirst();
    containerInstance.ifPresent(self -> terminateOperation.execute(pluginSettings, self));
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) ContainerInstanceHelper(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper) EventStream(com.thoughtworks.gocd.elasticagent.ecs.events.EventStream) Event(com.thoughtworks.gocd.elasticagent.ecs.events.Event) EventFingerprint(com.thoughtworks.gocd.elasticagent.ecs.events.EventFingerprint) LimitExceededException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException) java.util(java.util) StopPolicy(com.thoughtworks.gocd.elasticagent.ecs.aws.StopPolicy) EligibleForTerminationPredicate(com.thoughtworks.gocd.elasticagent.ecs.aws.predicate.EligibleForTerminationPredicate) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) MessageFormat.format(java.text.MessageFormat.format) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) ServerRequestFailedException(com.thoughtworks.gocd.elasticagent.ecs.exceptions.ServerRequestFailedException) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) Instance(com.amazonaws.services.ec2.model.Instance) SpotInstanceService(com.thoughtworks.gocd.elasticagent.ecs.aws.SpotInstanceService) LINUX(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.LINUX) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) StopOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.StopOperation) InstanceSelectionStrategyFactory(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.InstanceSelectionStrategyFactory) Collectors(java.util.stream.Collectors) Platform.values(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.values) Collectors.toList(java.util.stream.Collectors.toList) TerminateOperation(com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation) com.thoughtworks.gocd.elasticagent.ecs.domain(com.thoughtworks.gocd.elasticagent.ecs.domain) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) com.thoughtworks.gocd.elasticagent.ecs(com.thoughtworks.gocd.elasticagent.ecs) ServerPingRequest(com.thoughtworks.gocd.elasticagent.ecs.requests.ServerPingRequest) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance)

Example 2 with MostIdleInstanceComparator

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

Example 3 with MostIdleInstanceComparator

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

the class StopIdleInstanceSelectionStrategy method findInstancesToStop.

@Override
protected List<ContainerInstance> findInstancesToStop(PluginSettings pluginSettings, Platform platform, Map<String, ContainerInstance> instanceIdToContainerInstance, List<Instance> idleInstances) {
    final Period timeInstanceCanStayIdle = platform == Platform.LINUX ? pluginSettings.stopLinuxInstanceAfter() : pluginSettings.stopWindowsInstanceAfter();
    idleInstances.sort(new MostIdleInstanceComparator(clock.now()));
    return idleInstances.stream().filter(isIdlePeriodIsMoreThan(timeInstanceCanStayIdle)).map(instance -> instanceIdToContainerInstance.get(instance.getInstanceId())).collect(Collectors.toList());
}
Also used : ContainerInstanceHelper(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper) Period(org.joda.time.Period) Clock(com.thoughtworks.gocd.elasticagent.ecs.Clock) Predicate(java.util.function.Predicate) DateTime(org.joda.time.DateTime) PluginSettings(com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings) Collectors(java.util.stream.Collectors) LAST_SEEN_IDLE(com.thoughtworks.gocd.elasticagent.ecs.Constants.LAST_SEEN_IDLE) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) List(java.util.List) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) Tag(com.amazonaws.services.ec2.model.Tag) Map(java.util.Map) Optional(java.util.Optional) Platform(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform) Instance(com.amazonaws.services.ec2.model.Instance) MostIdleInstanceComparator(com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator) Period(org.joda.time.Period)

Aggregations

MostIdleInstanceComparator (com.thoughtworks.gocd.elasticagent.ecs.aws.comparator.MostIdleInstanceComparator)3 Collectors (java.util.stream.Collectors)3 Instance (com.amazonaws.services.ec2.model.Instance)2 Tag (com.amazonaws.services.ec2.model.Tag)2 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)2 Clock (com.thoughtworks.gocd.elasticagent.ecs.Clock)2 LAST_SEEN_IDLE (com.thoughtworks.gocd.elasticagent.ecs.Constants.LAST_SEEN_IDLE)2 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)2 ContainerInstanceHelper (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper)2 TerminateOperation (com.thoughtworks.gocd.elasticagent.ecs.aws.strategy.TerminateOperation)2 Platform (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform)2 PluginSettings (com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings)2 LimitExceededException (com.thoughtworks.gocd.elasticagent.ecs.exceptions.LimitExceededException)2 MessageFormat.format (java.text.MessageFormat.format)2 java.util (java.util)2 Predicate (java.util.function.Predicate)2 com.amazonaws.services.ec2.model (com.amazonaws.services.ec2.model)1 com.amazonaws.services.ecs.model (com.amazonaws.services.ecs.model)1 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)1 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)1