Search in sources :

Example 16 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsLaunchService method suspendAutoscalingGoupsWhenNewInstancesAreReady.

private void suspendAutoscalingGoupsWhenNewInstancesAreReady(AuthenticatedContext ac, CloudStack stack) {
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    try {
        awsAutoScalingService.scheduleStatusChecks(stack.getGroups(), ac, cloudFormationClient);
    } catch (AmazonAutoscalingFailed amazonAutoscalingFailed) {
        LOGGER.info("Amazon autoscaling failed", amazonAutoscalingFailed);
        throw new CloudConnectorException(amazonAutoscalingFailed);
    }
    awsAutoScalingService.suspendAutoScaling(ac, stack);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 17 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method updateCloudFormationStack.

private ListStackResourcesResult updateCloudFormationStack(AuthenticatedContext ac, CloudStack stack, ModelContext modelContext) {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
    LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
    cfClient.updateStack(awsStackRequestHelper.createUpdateStackRequest(ac, stack, cFStackName, cfTemplate));
    Waiter<DescribeStacksRequest> updateWaiter = cfClient.waiters().stackUpdateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(updateWaiter, describeStacksRequest, stackCancellationCheck, String.format("CloudFormation stack %s update failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.UPDATE_FAILED, ResourceStatus.CREATE_FAILED));
    return cfClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 18 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsRdsLaunchService method launch.

public List<CloudResourceStatus> launch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier) {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    DatabaseServer databaseServer = stack.getDatabaseServer();
    boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
    try {
        cfClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException exception) {
        // all subnets desired for DB subnet group are in the stack
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        LOGGER.warn("API call failed with this error:", exception);
        if (!existingSubnet) {
            throw new CloudConnectorException("Can only create RDS instance with existing subnets", exception);
        }
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        RDSModelContext rdsModelContext = new RDSModelContext().withTemplate(stack.getTemplate()).withNetworkCidrs(awsNetworkView.getExistingVpcCidrs()).withHasPort(databaseServer.getPort() != null).withUseSslEnforcement(useSslEnforcement).withSslCertificateIdentifierDefined(new AwsRdsInstanceView(databaseServer).isSslCertificateIdentifierDefined()).withHasSecurityGroup(!databaseServer.getSecurity().getCloudSecurityIds().isEmpty()).withIsKmsCustom(stack.getDatabaseServer().getParameters().containsKey("key")).withGetKmsKey(getKmsKey(stack));
        String cfTemplate = cloudFormationTemplateBuilder.build(rdsModelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("RDS CloudFormation stack %s creation failed", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, CREATE_FAILED));
    List<CloudResource> databaseResources = getCreatedOutputs(ac, stack, cFStackName, cfClient, resourceNotifier, useSslEnforcement);
    databaseResources.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
    // FIXME: For now, just return everything wrapped in a status object
    return databaseResources.stream().map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED)).collect(Collectors.toList());
// FIXME check does nothing?!
// return awsResourceConnector.check(ac, databaseResources);
}
Also used : AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) LoggerFactory(org.slf4j.LoggerFactory) CREATE_FAILED(com.amazonaws.services.cloudformation.model.ResourceStatus.CREATE_FAILED) ArrayList(java.util.ArrayList) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) Map(java.util.Map) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) Waiter(com.amazonaws.waiters.Waiter) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) AwsStackRequestHelper(com.sequenceiq.cloudbreak.cloud.aws.AwsStackRequestHelper) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) List(java.util.List) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AmazonServiceException(com.amazonaws.AmazonServiceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView)

Example 19 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsRdsStartService method start.

public void start(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();
    StartDBInstanceRequest startDBInstanceRequest = new StartDBInstanceRequest();
    startDBInstanceRequest.setDBInstanceIdentifier(dbInstanceIdentifier);
    try {
        rdsClient.startDBInstance(startDBInstanceRequest);
    } catch (RuntimeException ex) {
        throw new CloudConnectorException(ex.getMessage(), ex);
    }
    Waiter<DescribeDBInstancesRequest> rdsWaiter = rdsClient.waiters().dBInstanceAvailable();
    DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier);
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(rdsWaiter, describeDBInstancesRequest, stackCancellationCheck, "RDS start failed");
}
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) StartDBInstanceRequest(com.amazonaws.services.rds.model.StartDBInstanceRequest) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Example 20 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class CloudFormationStackUtil method addLoadBalancerTargets.

public void addLoadBalancerTargets(AuthenticatedContext ac, CloudLoadBalancer loadBalancer, List<CloudResource> resourcesToAdd) {
    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 new instances in the target groups
        Set<String> updatedInstanceIds = getInstanceIdsForGroups(resourcesToAdd, entry.getValue());
        // Find target group ARN
        AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(loadBalancer.getType());
        String targetGroupArn = getResourceArnByLogicalId(ac, AwsTargetGroup.getTargetGroupName(entry.getKey().getTrafficPort(), scheme), region);
        // Use ARN to fetch a list of current targets
        DescribeTargetHealthResult targetHealthResult = amazonElbClient.describeTargetHealth(new DescribeTargetHealthRequest().withTargetGroupArn(targetGroupArn));
        List<TargetDescription> targetDescriptions = targetHealthResult.getTargetHealthDescriptions().stream().map(TargetHealthDescription::getTarget).collect(Collectors.toList());
        Set<String> alreadyRegisteredInstanceIds = targetDescriptions.stream().map(TargetDescription::getId).collect(Collectors.toSet());
        // Remove any targets that have already been registered from the list being processed
        updatedInstanceIds.removeAll(alreadyRegisteredInstanceIds);
        // Register any new instances
        if (!updatedInstanceIds.isEmpty()) {
            List<TargetDescription> targetsToAdd = updatedInstanceIds.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
            RegisterTargetsResult registerTargetsResult = amazonElbClient.registerTargets(new RegisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToAdd));
        }
    }
}
Also used : RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TargetHealthDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) 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) Splitter(com.google.common.base.Splitter) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) Collection(java.util.Collection) Output(com.amazonaws.services.cloudformation.model.Output) Set(java.util.Set) Retry(com.sequenceiq.cloudbreak.service.Retry) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) Objects(java.util.Objects) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) PRIVATE_ID(com.sequenceiq.cloudbreak.cloud.model.CloudResource.PRIVATE_ID) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) INSTANCE_TYPE(com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) Supplier(java.util.function.Supplier) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Service(org.springframework.stereotype.Service) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Retryable(org.springframework.retry.annotation.Retryable) DescribeStackResourceRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourceRequest) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeFileSystemsRequest(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsRequest) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) SdkClientException(com.amazonaws.SdkClientException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Instance(com.amazonaws.services.autoscaling.model.Instance) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult) Collections(java.util.Collections) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Set(java.util.Set) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)

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