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());
}
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();
}
Aggregations