Search in sources :

Example 1 with StackCancellationCheck

use of com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck in project cloudbreak by hortonworks.

the class AwsLaunchService method launch.

public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws Exception {
    LOGGER.info("Launch AWS cluster with adjustment and threshold: {}", adjustmentTypeWithThreshold);
    createKeyPair(ac, stack);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, regionName);
    Network network = stack.getNetwork();
    AwsNetworkView awsNetworkView = new AwsNetworkView(network);
    boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    ModelContext modelContext = null;
    try {
        cfClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        boolean existingVPC = awsNetworkView.isExistingVPC();
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        String cidr = network.getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
        modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, subnet, 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("CloudFormation stack %s creation failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.CREATE_FAILED));
    List<CloudResource> networkResources = saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
    suspendAutoscalingGoupsWhenNewInstancesAreReady(ac, stack);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
    List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfClient, amazonASClient, stack.getGroups());
    if (mapPublicIpOnLaunch) {
        associatePublicIpsToGatewayInstances(stack, cFStackName, cfClient, amazonEC2Client, instances);
    }
    awsComputeResourceService.buildComputeResourcesForLaunch(ac, stack, adjustmentTypeWithThreshold, instances, networkResources);
    awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());
    awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, regionName, credentialView);
    return awsResourceConnector.check(ac, instances);
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) 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) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 2 with StackCancellationCheck

use of com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck 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 3 with StackCancellationCheck

use of com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck 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 4 with StackCancellationCheck

use of com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck 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 5 with StackCancellationCheck

use of com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck in project cloudbreak by hortonworks.

the class AwsAutoScalingService method waitForGroup.

private void waitForGroup(AmazonEc2Client amClient, AmazonAutoScalingClient asClient, String autoScalingGroupName, Integer requiredInstanceCount, Long stackId, List<String> knownInstances) throws AmazonAutoscalingFailed {
    Waiter<DescribeAutoScalingGroupsRequest> groupInServiceWaiter = asClient.waiters().groupInService();
    PollingStrategy backoff = getBackoffCancellablePollingStrategy(new StackCancellationCheck(stackId));
    try {
        groupInServiceWaiter.run(new WaiterParameters<>(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(autoScalingGroupName)).withPollingStrategy(backoff));
    } catch (Exception e) {
        throw new AmazonAutoscalingFailed(e.getMessage(), e);
    }
    Waiter<DescribeAutoScalingGroupsRequest> instancesInServiceWaiter = customAmazonWaiterProvider.getAutoscalingInstancesInServiceWaiter(asClient, requiredInstanceCount);
    try {
        instancesInServiceWaiter.run(new WaiterParameters<>(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(autoScalingGroupName)).withPollingStrategy(backoff));
    } catch (WaiterTimedOutException e) {
        String message = String.format("Polling timed out. Not all the %s instance(s) reached InService state.", requiredInstanceCount);
        throw new AmazonAutoscalingFailed(message, e);
    } catch (Exception e) {
        throw new AmazonAutoscalingFailed(e.getMessage(), e);
    }
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(asClient, autoScalingGroupName);
    List<String> instanceIdsForWait = getInstanceIdsForWait(instanceIds, knownInstances);
    if (requiredInstanceCount != 0) {
        List<List<String>> partitionedInstanceIdsList = Lists.partition(instanceIdsForWait, MAX_INSTANCE_ID_SIZE);
        Waiter<DescribeInstancesRequest> instanceRunningStateWaiter = amClient.waiters().instanceRunning();
        for (List<String> partitionedInstanceIds : partitionedInstanceIdsList) {
            try {
                instanceRunningStateWaiter.run(new WaiterParameters<>(new DescribeInstancesRequest().withInstanceIds(partitionedInstanceIds)).withPollingStrategy(backoff));
            } catch (AmazonServiceException e) {
                LOGGER.error("Cannot describeInstances", e);
                e.setErrorMessage("Cannot describeInstances. " + e.getErrorMessage());
                throw e;
            } catch (Exception e) {
                throw new AmazonAutoscalingFailed("Error occurred in describeInstances: " + e.getMessage(), e);
            }
        }
    }
}
Also used : WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) WaiterParameters(com.amazonaws.waiters.WaiterParameters) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) BackoffCancellablePollingStrategy.getBackoffCancellablePollingStrategy(com.sequenceiq.cloudbreak.cloud.aws.scheduler.BackoffCancellablePollingStrategy.getBackoffCancellablePollingStrategy) PollingStrategy(com.amazonaws.waiters.PollingStrategy) AmazonServiceException(com.amazonaws.AmazonServiceException) List(java.util.List)

Aggregations

StackCancellationCheck (com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck)9 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)4 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)4 DescribeDBInstancesRequest (com.amazonaws.services.rds.model.DescribeDBInstancesRequest)3 AmazonRdsClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonRdsClient)3 List (java.util.List)3 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)2 CloudFormationTemplateBuilder (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder)2 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)2 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)1 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)1 CREATE_FAILED (com.amazonaws.services.cloudformation.model.ResourceStatus.CREATE_FAILED)1 ModifyDBInstanceRequest (com.amazonaws.services.rds.model.ModifyDBInstanceRequest)1 StartDBInstanceRequest (com.amazonaws.services.rds.model.StartDBInstanceRequest)1 StopDBInstanceRequest (com.amazonaws.services.rds.model.StopDBInstanceRequest)1