Search in sources :

Example 6 with DescribeStacksRequest

use of com.amazonaws.services.cloudformation.model.DescribeStacksRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudFormationStack method executeChangeSet.

public void executeChangeSet(String changeSetName, long pollIntervallMillis) throws ExecutionException {
    if (!this.changeSetHasChanges(changeSetName)) {
        // If the change set has no changes we should simply delete it.
        this.listener.getLogger().format("Deleting empty change set %s for stack %s %n", changeSetName, this.stack);
        DeleteChangeSetRequest req = new DeleteChangeSetRequest().withChangeSetName(changeSetName).withStackName(this.stack);
        this.client.deleteChangeSet(req);
    } else {
        this.listener.getLogger().format("Executing change set %s for stack %s %n", changeSetName, this.stack);
        final Waiter<DescribeStacksRequest> waiter;
        if (this.exists()) {
            waiter = this.client.waiters().stackUpdateComplete();
        } else {
            waiter = this.client.waiters().stackCreateComplete();
        }
        ExecuteChangeSetRequest req = new ExecuteChangeSetRequest().withChangeSetName(changeSetName).withStackName(this.stack);
        this.client.executeChangeSet(req);
        new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, waiter, pollIntervallMillis);
        this.listener.getLogger().format("Executed change set %s for stack %s %n", changeSetName, this.stack);
    }
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) ExecuteChangeSetRequest(com.amazonaws.services.cloudformation.model.ExecuteChangeSetRequest) DeleteChangeSetRequest(com.amazonaws.services.cloudformation.model.DeleteChangeSetRequest)

Example 7 with DescribeStacksRequest

use of com.amazonaws.services.cloudformation.model.DescribeStacksRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
    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.createAccess(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    boolean existingVPC = awsNetworkView.isExistingVPC();
    boolean existingSubnet = awsNetworkView.isExistingSubnet();
    boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    try {
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        LOGGER.info("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        String cidr = stack.getNetwork().getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr;
        AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
        ModelContext modelContext = new ModelContext().withAuthenticatedContext(ac).withStack(stack).withExistingVpc(existingVPC).withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)).withExistingIGW(awsNetworkView.isExistingIGW()).withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null).withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()).withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()).withTemplate(stack.getTemplate()).withDefaultSubnet(subnet);
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
    }
    LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName);
    PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName);
    try {
        Boolean statePollerResult = task.call();
        if (!task.completed(statePollerResult)) {
            syncPollingScheduler.schedule(task);
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
    saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier);
    saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
    List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch);
    return check(ac, cloudResources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 8 with DescribeStacksRequest

use of com.amazonaws.services.cloudformation.model.DescribeStacksRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.info("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    if (resources != null && !resources.isEmpty()) {
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
        CloudResource stackResource = getCloudFormationStackResource(resources);
        if (stackResource == null) {
            return Collections.emptyList();
        }
        String cFStackName = stackResource.getName();
        LOGGER.info("Deleting CloudFormation stack for stack: {} [cf stack id: {}]", cFStackName, ac.getCloudContext().getId());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
        try {
            retryService.testWith2SecDelayMax5Times(() -> {
                try {
                    cfClient.describeStacks(describeStacksRequest);
                } catch (AmazonServiceException e) {
                    if (!e.getErrorMessage().contains(cFStackName + " does not exist")) {
                        throw e;
                    }
                    throw new ActionWentFailException("Stack not exists");
                }
                return Boolean.TRUE;
            });
        } catch (ActionWentFailException ignored) {
            LOGGER.info(String.format("Stack not found with name: %s", cFStackName));
            AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
            releaseReservedIp(amazonEC2Client, resources);
            return Collections.emptyList();
        }
        resumeAutoScalingPolicies(ac, stack);
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
        cfClient.deleteStack(deleteStackRequest);
        PollTask<Boolean> task = awsPollTaskFactory.newAwsTerminateStackStatusCheckerTask(ac, cfClient, DELETE_COMPLETE, DELETE_FAILED, ERROR_STATUSES, cFStackName);
        try {
            Boolean statePollerResult = task.call();
            if (!task.completed(statePollerResult)) {
                syncPollingScheduler.schedule(task);
            }
        } catch (Exception e) {
            throw new CloudConnectorException(e.getMessage(), e);
        }
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        deleteKeyPair(ac, stack);
    } else if (resources != null) {
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        LOGGER.info("No CloudFormation stack saved for stack.");
    } else {
        LOGGER.info("No resources to release.");
    }
    return check(ac, resources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Aggregations

DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)8 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)3 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)3 Output (com.amazonaws.services.cloudformation.model.Output)2 Stack (com.amazonaws.services.cloudformation.model.Stack)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AmazonCloudFormationException (com.amazonaws.services.cloudformation.model.AmazonCloudFormationException)1 DeleteChangeSetRequest (com.amazonaws.services.cloudformation.model.DeleteChangeSetRequest)1 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)1 DescribeStacksResult (com.amazonaws.services.cloudformation.model.DescribeStacksResult)1 ExecuteChangeSetRequest (com.amazonaws.services.cloudformation.model.ExecuteChangeSetRequest)1 StackStatus (com.amazonaws.services.cloudformation.model.StackStatus)1 WaiterHandler (com.amazonaws.waiters.WaiterHandler)1 ModelContext (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext)1 AwsInstanceProfileView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)1