use of com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings 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));
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings in project gocd-ecs-elastic-agent by gocd.
the class JobCompletionRequestExecutor method execute.
@Override
public GoPluginApiResponse execute() throws Exception {
PluginSettings clusterProfileProperties = jobCompletionRequest.clusterProfileProperties();
String elasticAgentId = jobCompletionRequest.getElasticAgentId();
Agents agents = pluginRequest.listAgents();
if (!agents.agentIds().contains(elasticAgentId)) {
LOG.debug("[Job Completion] Skipping request to delete agent with id '{}' as the agent does not exist on the server.", elasticAgentId);
return DefaultGoPluginApiResponse.success("");
}
Agent agent = new Agent(elasticAgentId);
LOG.debug("[Job Completion] Disabling elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
pluginRequest.disableAgents(Collections.singletonList(agent));
LOG.debug("[Job Completion] Terminating elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
agentInstances.terminate(agent.elasticAgentId(), clusterProfileProperties);
LOG.debug("[Job Completion] Deleting elastic agent with id {} on job completion {}.", agent.elasticAgentId(), jobCompletionRequest.jobIdentifier());
pluginRequest.deleteAgents(Collections.singletonList(agent));
return DefaultGoPluginApiResponse.success("");
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings in project gocd-ecs-elastic-agent by gocd.
the class MigrateConfigurationRequestExecutor method execute.
@Override
public GoPluginApiResponse execute() throws Exception {
LOG.info("[Migrate Config] Request for Config Migration Started...");
PluginSettings pluginSettings = migrateConfigurationRequest.getPluginSettings();
List<ClusterProfile> existingClusterProfiles = migrateConfigurationRequest.getClusterProfiles();
List<ElasticAgentProfile> existingElasticAgentProfiles = migrateConfigurationRequest.getElasticAgentProfiles();
if (!arePluginSettingsConfigured(pluginSettings)) {
LOG.info("[Migrate Config] No Plugin Settings are configured. Skipping Config Migration...");
return new DefaultGoPluginApiResponse(200, migrateConfigurationRequest.toJSON());
}
if (existingClusterProfiles.size() == 0) {
LOG.info("[Migrate Config] Did not find any Cluster Profile. Possibly, user just have configured plugin settings and haven't define any elastic agent profiles.");
String newClusterId = UUID.randomUUID().toString();
LOG.info("[Migrate Config] Migrating existing plugin settings to new cluster profile '{}'", newClusterId);
ClusterProfile clusterProfile = new ClusterProfile(newClusterId, Constants.PLUGIN_ID, pluginSettings);
return getGoPluginApiResponse(pluginSettings, Collections.singletonList(clusterProfile), existingElasticAgentProfiles);
}
LOG.info("[Migrate Config] Checking to perform migrations on Cluster Profiles '{}'.", existingClusterProfiles.stream().map(ClusterProfile::getId).collect(Collectors.toList()));
for (ClusterProfile clusterProfile : existingClusterProfiles) {
List<ElasticAgentProfile> associatedElasticAgentProfiles = findAssociatedElasticAgentProfiles(clusterProfile, existingElasticAgentProfiles);
if (associatedElasticAgentProfiles.size() == 0) {
LOG.info("[Migrate Config] Skipping migration for the cluster '{}' as no Elastic Agent Profiles are associated with it.", clusterProfile.getId());
continue;
}
if (!arePluginSettingsConfigured(clusterProfile.getClusterProfileProperties())) {
List<String> associatedProfileIds = associatedElasticAgentProfiles.stream().map(ElasticAgentProfile::getId).collect(Collectors.toList());
LOG.info("[Migrate Config] Found an empty cluster profile '{}' associated with '{}' elastic agent profiles.", clusterProfile.getId(), associatedProfileIds);
migrateConfigForCluster(pluginSettings, associatedElasticAgentProfiles, clusterProfile);
} else {
LOG.info("[Migrate Config] Skipping migration for the cluster '{}' as cluster has already been configured.", clusterProfile.getId());
}
}
return new DefaultGoPluginApiResponse(200, migrateConfigurationRequest.toJSON());
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings 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();
}
use of com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings 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());
}
Aggregations