Example usage for com.amazonaws.services.cloudformation AmazonCloudFormation describeStacks

List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormation describeStacks

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation AmazonCloudFormation describeStacks.

Prototype

DescribeStacksResult describeStacks(DescribeStacksRequest describeStacksRequest);

Source Link

Document

Returns the description for the specified stack; if no stack name was specified, then it returns the description for all the stacks created.

Usage

From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationExecuteChangeSetTask.java

License:Apache License

@TaskAction
public void executeChangeSet() throws InterruptedException, IOException {
    // to enable conventionMappings feature
    String stackName = getStackName();

    if (stackName == null) {
        throw new GradleException("stackName is not specified");
    }//  w  w w.j a  v a  2s.c  om

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);

        if (stableStatuses.contains(stack.getStackStatus())) {
            Optional<ChangeSetSummary> summary = getLatestChangeSetSummary(cfn);
            String changeSetName = summary
                    .orElseThrow(
                            () -> new GradleException("ChangeSet for stack " + stackName + " was not found."))
                    .getChangeSetName();
            ExecuteChangeSetRequest req = new ExecuteChangeSetRequest().withStackName(stackName)
                    .withChangeSetName(changeSetName);
            cfn.executeChangeSet(req);
            getLogger().info("ChangeSet is executed : {}, {}", stackName, changeSetName);
        } else {
            throw new GradleException("invalid status for update: " + stack.getStackStatus());
        }
    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().warn("stack {} not found", stackName);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            getLogger().trace(e.getMessage());
        } else {
            throw e;
        }
    }

}

From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java

License:Apache License

@TaskAction
public void createOrUpdateStack() throws InterruptedException, IOException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    String cfnTemplateUrl = getCfnTemplateUrl();
    File cfnTemplateFile = getCfnTemplateFile();
    List<String> stableStatuses = getStableStatuses();

    if (stackName == null) {
        throw new GradleException("stackName is not specified");
    }//from   ww  w  .jav  a  2  s  . c o  m
    if (cfnTemplateUrl == null && cfnTemplateFile == null) {
        throw new GradleException("cfnTemplateUrl or cfnTemplateFile must be provided");
    }

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);
        if (stack.getStackStatus().equals("DELETE_COMPLETE")) {
            getLogger().warn("deleted stack {} already exists", stackName);
            deleteStack(cfn);
            createStack(cfn);
        } else if (stableStatuses.contains(stack.getStackStatus())) {
            updateStack(cfn);
        } else {
            throw new GradleException("invalid status for update: " + stack.getStackStatus());
        }
    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().warn("stack {} not found", stackName);
            createStack(cfn);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            getLogger().trace(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationWaitStackStatusTask.java

License:Apache License

@TaskAction
public void waitStackForStatus() throws InterruptedException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    List<String> successStatuses = getSuccessStatuses();
    List<String> waitStatuses = getWaitStatuses();
    int loopTimeout = getLoopTimeout();
    int loopWait = getLoopWait();

    if (stackName == null) {
        throw new GradleException("stackName is not specified");
    }//  w ww.  j  a v a2s  . co  m

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    long start = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis() > start + (loopTimeout * 1000)) {
            throw new GradleException("Timeout");
        }
        try {
            DescribeStacksResult describeStackResult = cfn
                    .describeStacks(new DescribeStacksRequest().withStackName(stackName));
            Stack stack = describeStackResult.getStacks().get(0);
            if (stack == null) {
                throw new GradleException("stack " + stackName + " is not exists");
            }
            found = true;
            lastStatus = stack.getStackStatus();
            if (successStatuses.contains(lastStatus)) {
                getLogger().info("Status of stack {} is now {}.", stackName, lastStatus);
                printOutputs(stack);
                break;
            } else if (waitStatuses.contains(lastStatus)) {
                getLogger().info("Status of stack {} is {}...", stackName, lastStatus);
                Thread.sleep(loopWait * 1000);
            } else {
                // fail if not contains in successStatus or waitStatus
                throw new GradleException(
                        "Status of stack " + stackName + " is " + lastStatus + ".  It seems to be failed.");
            }
        } catch (AmazonServiceException e) {
            if (found) {
                break;
            } else {
                throw new GradleException("Fail to describe stack: " + stackName, e);
            }
        }
    }
}

From source file:org.terracotta.TerracottaCloudFormationSample.java

License:Open Source License

public static String waitForCompletion(AmazonCloudFormation stackbuilder, String stackName) throws Exception {

    DescribeStacksRequest wait = new DescribeStacksRequest();
    wait.setStackName(stackName);//  ww  w .  j a v a 2s  . co m
    Boolean completed = false;
    String stackStatus = "Unknown";
    String stackReason = "";

    System.out.print("Waiting");

    while (!completed) {
        List<Stack> stacks = stackbuilder.describeStacks(wait).getStacks();
        if (stacks.isEmpty()) {
            completed = true;
            stackStatus = "NO_SUCH_STACK";
            stackReason = "Stack has been deleted";
        } else {
            for (Stack stack : stacks) {
                if (stack.getStackStatus().equals(StackStatus.CREATE_COMPLETE.toString())
                        || stack.getStackStatus().equals(StackStatus.CREATE_FAILED.toString())
                        || stack.getStackStatus().equals(StackStatus.ROLLBACK_FAILED.toString())
                        || stack.getStackStatus().equals(StackStatus.DELETE_FAILED.toString())) {
                    completed = true;
                    stackStatus = stack.getStackStatus();
                    stackReason = stack.getStackStatusReason();
                }
            }
        }

        // Show we are waiting
        System.out.print(".");

        // Not done yet so sleep for 30 seconds.
        if (!completed)
            Thread.sleep(30000);
    }

    // Show we are done
    System.out.print("done\n");

    return stackStatus + " (" + stackReason + ")";
}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationCreateStackTask.java

License:BSD License

@TaskAction
public void createStack() throws InterruptedException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    if (stackName == null)
        throw new GradleException("stackName is not specified");

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {//from w  ww.ja v a 2s.co m
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);
        throw new GradleException("Stack exists. invalid status for create: " + stack.getStackStatus());

    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().info("stack {} not found", stackName);
            createStack(cfn);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            // ignore
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java

License:BSD License

@TaskAction
public void createOrUpdateStack() throws InterruptedException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    String templateBody = getTemplateBody();
    List<String> stableStatuses = getStableStatuses();

    if (stackName == null)
        throw new GradleException("stackName is not specified");

    if (templateBody == null && !usePreviousTemplate)
        throw new GradleException("templateBody is not specified");
    if (templateBody != null && usePreviousTemplate)
        throw new GradleException("templateBody cannot be specified with usePreviousTemplate");

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {/*from w  w  w. j a va2s  .c om*/
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);
        if (stack.getStackStatus().equals("DELETE_COMPLETE")) {
            getLogger().warn("deleted stack {} already exists", stackName);
            deleteStack(cfn);
            createStack(cfn);
        } else if (stableStatuses.contains(stack.getStackStatus())) {
            updateStack(cfn);
        } else {
            throw new GradleException("invalid status for update: " + stack.getStackStatus());
        }
    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().warn("stack {} not found", stackName);
            createStack(cfn);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            // ignore
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationUpdateStackTask.java

License:BSD License

@TaskAction
public void updateStack() throws InterruptedException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    String cfnTemplateUrl = getCfnTemplateUrl();
    // String cfnTemplateBody = getCnfTemplateBody();
    List<String> stableStatuses = getStableStatuses();

    if (stackName == null)
        throw new GradleException("stackName is not specified");

    if (templateBody == null && !usePreviousTemplate)
        throw new GradleException("templateBody is not specified");
    if (templateBody != null && usePreviousTemplate)
        throw new GradleException("templateBody cannot be specified with usePreviousTemplate");

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {//from  w  ww .  j  a  v  a2  s . c  om
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);
        if (stack.getStackStatus().equals("DELETE_COMPLETE")) {
            getLogger().warn("deleted stack {} already exists - recreating", stackName);
            deleteStack(cfn);
            createStack(cfn);
        } else if (stableStatuses.contains(stack.getStackStatus())) {
            updateStack(cfn);
        } else {
            throw new GradleException("invalid status for update: " + stack.getStackStatus());
        }
    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().warn("stack {} not found", stackName);
            createStack(cfn);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            // ignore
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationWaitStackStatusTask.java

License:BSD License

@TaskAction
public void waitStackForStatus() throws InterruptedException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    List<String> successStatuses = getSuccessStatuses();
    List<String> waitStatuses = getWaitStatuses();
    int loopTimeout = getLoopTimeout();
    int loopWait = getLoopWait();

    if (stackName == null)
        throw new GradleException("stackName is not specified");

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    long start = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis() > start + (loopTimeout * 1000)) {
            throw new GradleException("Timeout");
        }/*from  w  ww.j ava 2 s. c o m*/
        try {
            DescribeStacksResult describeStackResult = cfn
                    .describeStacks(new DescribeStacksRequest().withStackName(stackName));
            Stack stack = describeStackResult.getStacks().get(0);
            if (stack == null) {
                throw new GradleException("stack " + stackName + " is not exists");
            }
            found = true;
            lastStatus = stack.getStackStatus();
            if (successStatuses.contains(lastStatus)) {
                getLogger().info("Status of stack {} is now {}.", stackName, lastStatus);
                printOutputs(stack);
                break;
            } else if (waitStatuses.contains(lastStatus)) {
                getLogger().info("Status of stack {} is {}...", stackName, lastStatus);
                Thread.sleep(loopWait * 1000);
            } else {
                // waitStatuses?successStatuses???fail??
                throw new GradleException(
                        "Status of stack " + stackName + " is " + lastStatus + ".  It seems to be failed.");
            }
        } catch (AmazonServiceException e) {
            if (found) {
                break;
            } else {
                throw new GradleException("Fail to describe stack: " + stackName, e);
            }
        }
    }
}