use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsDownscaleService method downscale.
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vmsToDownscale) {
if (!vmsToDownscale.isEmpty()) {
List<String> instanceIdsToDownscale = new ArrayList<>();
for (CloudInstance vm : vmsToDownscale) {
instanceIdsToDownscale.add(vm.getInstanceId());
}
AwsCredentialView credentialView = new AwsCredentialView(auth.getCloudCredential());
AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(auth);
String regionName = authenticatedContextView.getRegion();
LOGGER.debug("Calling deleteCloudWatchAlarmsForSystemFailures from AwsDownscaleService");
awsCloudWatchService.deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIdsToDownscale);
List<CloudResource> resourcesToDownscale = resources.stream().filter(resource -> instanceIdsToDownscale.contains(resource.getInstanceId())).collect(Collectors.toList());
awsComputeResourceService.deleteComputeResources(auth, stack, resourcesToDownscale);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, auth.getCloudContext().getLocation().getRegion().value());
AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, auth.getCloudContext().getLocation().getRegion().value());
Map<String, List<CloudInstance>> downscaledGroupsWithCloudInstances = vmsToDownscale.stream().collect(Collectors.groupingBy(cloudInstance -> cloudInstance.getTemplate().getGroupName()));
Long stackId = auth.getCloudContext().getId();
List<String> terminatedInstances = terminateInstances(auth, amazonASClient, amazonEC2Client, downscaledGroupsWithCloudInstances, stackId);
if (!terminatedInstances.isEmpty()) {
waitForTerminateInstances(stackId, terminatedInstances, amazonEC2Client);
}
updateAutoscalingGroups(auth, amazonASClient, downscaledGroupsWithCloudInstances);
List<String> targetGroupArns = getTargetGroupArns(stack.getLoadBalancers(), auth);
loadBalancerService.removeLoadBalancerTargets(auth, targetGroupArns, resourcesToDownscale);
}
return awsResourceConnector.check(auth, resources);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsLaunchService method createKeyPair.
private void createKeyPair(AuthenticatedContext ac, CloudStack stack) {
if (!awsClient.existingKeyPairNameSpecified(stack.getInstanceAuthentication())) {
AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
LOGGER.debug("Importing public key to {} region on AWS", region);
AmazonEc2Client client = awsClient.createEc2Client(awsCredential, region);
String keyPairName = awsClient.getKeyPairName(ac);
ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, stack.getInstanceAuthentication().getPublicKey());
try {
client.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyPairName));
LOGGER.debug("Key-pair already exists: {}", keyPairName);
} catch (AmazonServiceException e) {
client.importKeyPair(importKeyPairRequest);
}
} catch (Exception e) {
String errorMessage = String.format("Failed to import public key [roleArn:'%s'], detailed message: %s", awsCredential.getRoleArn(), e.getMessage());
LOGGER.info(errorMessage, e);
throw new CloudConnectorException(e.getMessage(), e);
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsLoadBalancerLaunchService method updateCloudformationWithLoadBalancers.
public List<CloudResourceStatus> updateCloudformationWithLoadBalancers(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, ModelContext modelContext) {
List<CloudResourceStatus> statuses = new ArrayList<>();
List<CloudLoadBalancer> cloudLoadBalancers = stack.getLoadBalancers();
String cFStackName = cfStackUtil.getCfStackName(ac);
if (!cloudLoadBalancers.isEmpty()) {
LOGGER.debug("Creating load balancers of types " + cloudLoadBalancers.stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::name).collect(Collectors.joining(",")));
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfRetryClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());
Network network = stack.getNetwork();
AwsNetworkView awsNetworkView = new AwsNetworkView(network);
if (modelContext == null) {
LOGGER.debug("Creating default model context");
modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
}
Map<String, List<String>> instanceIdsByGroupName = instances.stream().collect(Collectors.groupingBy(CloudResource::getGroup, mapping(CloudResource::getInstanceId, toList())));
List<AwsLoadBalancer> awsLoadBalancers = loadBalancerCommonService.getAwsLoadBalancers(cloudLoadBalancers, instanceIdsByGroupName, awsNetworkView);
modelContext.withLoadBalancers(awsLoadBalancers);
LOGGER.debug("Starting CloudFormation update to create load balancer and target groups.");
ListStackResourcesResult result;
if (checkForLoadBalancerAndTargetGroupResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
LOGGER.debug("Load balancer and target group resources already exist, skipping creation");
result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
} else {
result = updateCloudFormationStack(ac, stack, modelContext);
}
setLoadBalancerMetadata(awsLoadBalancers, result);
LOGGER.debug("Starting CloudFormation update to create listeners.");
if (checkForListenerResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
LOGGER.debug("Listener resources already exist, skipping creation");
result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
} else {
result = updateCloudFormationStack(ac, stack, modelContext);
}
ListStackResourcesResult finalResult = result;
awsLoadBalancers.forEach(lb -> statuses.add(createLoadBalancerStatus(ac, lb, finalResult)));
} else {
LOGGER.debug("No load balancers in stack");
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsRdsModifyService method disableDeleteProtection.
public void disableDeleteProtection(AuthenticatedContext ac, DatabaseStack dbStack) throws ExecutionException, TimeoutException, InterruptedException {
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonRdsClient rdsClient = awsClient.createRdsClient(credentialView, regionName);
String dbInstanceIdentifier = dbStack.getDatabaseServer().getServerId();
ModifyDBInstanceRequest modifyDBInstanceRequest = new ModifyDBInstanceRequest();
modifyDBInstanceRequest.setDBInstanceIdentifier(dbInstanceIdentifier);
modifyDBInstanceRequest.setDeletionProtection(false);
LOGGER.debug("RDS modify request to disable delete protection for DB: {}", dbInstanceIdentifier);
try {
rdsClient.modifyDBInstance(modifyDBInstanceRequest);
} catch (RuntimeException ex) {
throw new CloudConnectorException(ex.getMessage(), ex);
}
Waiter<DescribeDBInstancesRequest> rdsWaiter = customAmazonWaiterProvider.getDbInstanceModifyWaiter(rdsClient);
DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier);
StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
run(rdsWaiter, describeDBInstancesRequest, stackCancellationCheck);
LOGGER.debug("RDS delete protection is disabled for DB Instance ID: {}", dbInstanceIdentifier);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsRdsStatusLookupService method getDescribeDBInstancesResultInternal.
private DescribeDBInstancesResult getDescribeDBInstancesResultInternal(AuthenticatedContext ac, DatabaseStack dbStack, String preDescribeMessage, String notFoundMessage) {
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonRdsClient rdsClient = awsClient.createRdsClient(credentialView, regionName);
String dbInstanceIdentifier = dbStack.getDatabaseServer().getServerId();
DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier);
DescribeDBInstancesResult describeDBInstancesResult;
try {
if (preDescribeMessage != null) {
LOGGER.debug(preDescribeMessage);
}
describeDBInstancesResult = rdsClient.describeDBInstances(describeDBInstancesRequest);
} catch (DBInstanceNotFoundException ex) {
LOGGER.debug(notFoundMessage, ex.getMessage());
describeDBInstancesResult = null;
} catch (RuntimeException ex) {
throw new CloudConnectorException(ex.getMessage(), ex);
}
return describeDBInstancesResult;
}
Aggregations