use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsUpscaleService method upscale.
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) {
LOGGER.info("Upscale AWS cluster with adjustment and threshold: {}. Resources: {}", adjustmentTypeWithThreshold, resources);
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, regionName);
List<Group> scaledGroups = cloudResourceHelper.getScaledGroups(stack);
Map<String, Group> desiredAutoscalingGroupsByName = getAutoScaleGroupsByNameFromCloudFormationTemplate(ac, cloudFormationClient, scaledGroups);
LOGGER.info("Desired autoscaling groups: {}", desiredAutoscalingGroupsByName);
awsAutoScalingService.resumeAutoScaling(amazonASClient, desiredAutoscalingGroupsByName.keySet(), UPSCALE_PROCESSES);
Map<String, Integer> originalAutoScalingGroupsBySize = getAutoScalingGroupsBySize(desiredAutoscalingGroupsByName.keySet(), amazonASClient);
LOGGER.info("Update autoscaling groups for stack: {}", ac.getCloudContext().getName());
Date timeBeforeASUpdate = new Date();
updateAutoscalingGroups(amazonASClient, desiredAutoscalingGroupsByName, originalAutoScalingGroupsBySize);
List<String> knownInstances = getKnownInstancesByCloudbreak(stack);
try {
awsAutoScalingService.scheduleStatusChecks(scaledGroups, ac, cloudFormationClient, timeBeforeASUpdate, knownInstances);
} catch (AmazonAutoscalingFailed amazonAutoscalingFailed) {
LOGGER.info("Amazon autoscaling group update failed", amazonAutoscalingFailed);
recoverOriginalState(ac, stack, amazonASClient, desiredAutoscalingGroupsByName, originalAutoScalingGroupsBySize, amazonAutoscalingFailed);
sendASGUpdateFailedMessage(amazonASClient, desiredAutoscalingGroupsByName, amazonAutoscalingFailed);
}
try {
awsAutoScalingService.suspendAutoScaling(ac, stack);
validateInstanceStatusesInScaledGroups(ac, amazonASClient, amazonEC2Client, cloudFormationClient, scaledGroups);
List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cloudFormationClient, amazonASClient, scaledGroups);
associateElasticIpWithNewInstances(stack, resources, cloudFormationClient, amazonEC2Client, scaledGroups, instances);
List<Group> groupsWithNewInstances = getGroupsWithNewInstances(scaledGroups);
List<CloudResource> newInstances = getNewInstances(scaledGroups, instances);
List<CloudResource> reattachableVolumeSets = getReattachableVolumeSets(scaledGroups, resources);
List<CloudResource> networkResources = resources.stream().filter(cloudResource -> ResourceType.AWS_SUBNET.equals(cloudResource.getType())).collect(Collectors.toList());
List<CloudResourceStatus> cloudResourceStatuses = awsComputeResourceService.buildComputeResourcesForUpscale(ac, stack, groupsWithNewInstances, newInstances, reattachableVolumeSets, networkResources, adjustmentTypeWithThreshold);
List<String> failedResources = cloudResourceStatuses.stream().map(CloudResourceStatus::getCloudResource).filter(cloudResource -> CommonStatus.FAILED == cloudResource.getStatus()).map(cloudResource -> cloudResource.getType() + " - " + cloudResource.getName()).collect(Collectors.toList());
if (!failedResources.isEmpty()) {
throw new RuntimeException("Additional resource creation failed: " + failedResources);
}
awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());
awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, regionName, credentialView);
for (CloudLoadBalancer loadBalancer : stack.getLoadBalancers()) {
cfStackUtil.addLoadBalancerTargets(ac, loadBalancer, newInstances);
}
List<CloudResourceStatus> successfulInstances = newInstances.stream().map(cloudResource -> new CloudResourceStatus(cloudResource, ResourceStatus.CREATED, cloudResource.getParameter(CloudResource.PRIVATE_ID, Long.class))).collect(Collectors.toList());
return ListUtils.union(singletonList(new CloudResourceStatus(cfStackUtil.getCloudFormationStackResource(resources), ResourceStatus.UPDATED)), successfulInstances);
} catch (RuntimeException runtimeException) {
recoverOriginalState(ac, stack, amazonASClient, desiredAutoscalingGroupsByName, originalAutoScalingGroupsBySize, runtimeException);
throw new CloudConnectorException(String.format("Failed to create some resource on AWS for upscaled nodes, please check your quotas on AWS. " + "Original autoscaling group state has been recovered. Exception: %s", runtimeException.getMessage()), runtimeException);
}
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class CloudFormationStackUtil method removeLoadBalancerTargets.
public void removeLoadBalancerTargets(AuthenticatedContext ac, CloudLoadBalancer loadBalancer, List<CloudResource> resourcesToRemove) {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonElasticLoadBalancingClient amazonElbClient = awsClient.createElasticLoadBalancingClient(new AwsCredentialView(ac.getCloudCredential()), region);
for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : loadBalancer.getPortToTargetGroupMapping().entrySet()) {
// Get a list of the instance ids to remove
Set<String> instancesToRemove = getInstanceIdsForGroups(resourcesToRemove, entry.getValue());
// Find target group ARN
AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(loadBalancer.getType());
String targetGroupArn = getResourceArnByLogicalId(ac, AwsTargetGroup.getTargetGroupName(entry.getKey().getTrafficPort(), scheme), region);
// Deregister any instances that no longer exist
if (!instancesToRemove.isEmpty()) {
try {
List<TargetDescription> targetsToRemove = instancesToRemove.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
DeregisterTargetsResult deregisterTargetsResult = amazonElbClient.deregisterTargets(new DeregisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToRemove));
} catch (InvalidTargetException ignored) {
// no-op - we tried to remove a target that wasn't in the target group, which is fine
}
}
}
}
Aggregations