Search in sources :

Example 76 with AwsCredentialView

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);
}
Also used : LoadBalancerService(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerService) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) INSTANCE_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Service(org.springframework.stereotype.Service) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Waiter(com.amazonaws.waiters.Waiter) Logger(org.slf4j.Logger) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Collection(java.util.Collection) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) Instance(com.amazonaws.services.autoscaling.model.Instance) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) ArrayList(java.util.ArrayList) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) ArrayList(java.util.ArrayList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 77 with AwsCredentialView

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);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) ImportKeyPairRequest(com.amazonaws.services.ec2.model.ImportKeyPairRequest) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 78 with AwsCredentialView

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;
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 79 with AwsCredentialView

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);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) AmazonRdsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonRdsClient) ModifyDBInstanceRequest(com.amazonaws.services.rds.model.ModifyDBInstanceRequest) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Example 80 with AwsCredentialView

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;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DBInstanceNotFoundException(com.amazonaws.services.rds.model.DBInstanceNotFoundException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonRdsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonRdsClient) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Aggregations

AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)94 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)32 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)32 List (java.util.List)25 AmazonServiceException (com.amazonaws.AmazonServiceException)22 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)21 Logger (org.slf4j.Logger)21 Inject (javax.inject.Inject)20 ArrayList (java.util.ArrayList)19 Collectors (java.util.stream.Collectors)19 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)18 Group (com.sequenceiq.cloudbreak.cloud.model.Group)18 Set (java.util.Set)18 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)17 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)17 Map (java.util.Map)16 LoggerFactory (org.slf4j.LoggerFactory)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)15 Service (org.springframework.stereotype.Service)15 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)14