List of usage examples for com.amazonaws.services.cloudformation.model Stack getStackStatus
public String getStackStatus()
Current status of the stack.
From source file:CloudFormation.java
License:Open Source License
public static void main(String[] args) throws Exception { /*// w w w. j a v a 2 s . c o m * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonCloudFormation stackbuilder = new AmazonCloudFormationClient( new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_EAST_1); stackbuilder.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with AWS CloudFormation"); System.out.println("===========================================\n"); String stackName = "CloudFormationSampleStack"; String logicalResourceName = "SampleNotificationTopic"; try { // Create a stack CreateStackRequest createRequest = new CreateStackRequest(); createRequest.setStackName(stackName); createRequest.setTemplateBody( convertStreamToString(CloudFormation.class.getResourceAsStream("CloudFormation.template"))); System.out.println("Creating a stack called " + createRequest.getStackName() + "."); stackbuilder.createStack(createRequest); // Wait for stack to be created // Note that you could use SNS notifications on the CreateStack call to track the progress of the stack creation System.out.println("Stack creation completed, the stack " + stackName + " completed with " + waitForCompletion(stackbuilder, stackName)); // Show all the stacks for this account along with the resources for each stack for (Stack stack : stackbuilder.describeStacks(new DescribeStacksRequest()).getStacks()) { System.out.println( "Stack : " + stack.getStackName() + " [" + stack.getStackStatus().toString() + "]"); DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest(); stackResourceRequest.setStackName(stack.getStackName()); for (StackResource resource : stackbuilder.describeStackResources(stackResourceRequest) .getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } } // Lookup a resource by its logical name DescribeStackResourcesRequest logicalNameResourceRequest = new DescribeStackResourcesRequest(); logicalNameResourceRequest.setStackName(stackName); logicalNameResourceRequest.setLogicalResourceId(logicalResourceName); System.out.format("Looking up resource name %1$s from stack %2$s\n", logicalNameResourceRequest.getLogicalResourceId(), logicalNameResourceRequest.getStackName()); for (StackResource resource : stackbuilder.describeStackResources(logicalNameResourceRequest) .getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } // Delete the stack DeleteStackRequest deleteRequest = new DeleteStackRequest(); deleteRequest.setStackName(stackName); System.out.println("Deleting the stack called " + deleteRequest.getStackName() + "."); stackbuilder.deleteStack(deleteRequest); // Wait for stack to be deleted // Note that you could used SNS notifications on the original CreateStack call to track the progress of the stack deletion System.out.println("Stack creation completed, the stack " + stackName + " completed with " + waitForCompletion(stackbuilder, stackName)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS CloudFormation, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS CloudFormation, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:CloudFormation.java
License:Open Source License
public static String waitForCompletion(AmazonCloudFormation stackbuilder, String stackName) throws Exception { DescribeStacksRequest wait = new DescribeStacksRequest(); wait.setStackName(stackName);// www.j a va 2 s .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 10 seconds. if (!completed) Thread.sleep(10000); } // Show we are done System.out.print("done\n"); return stackStatus + " (" + stackReason + ")"; }
From source file:CloudFormationSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*// w w w.j a va 2 s . c o m * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonCloudFormation stackbuilder = new AmazonCloudFormationClient(new PropertiesCredentials( CloudFormationSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("==========================================="); System.out.println("Getting Started with AWS CloudFormation"); System.out.println("===========================================\n"); String stackName = "CloudFormationSampleStack"; String logicalResourceName = "SampleNotificationTopic"; try { // Create a stack CreateStackRequest createRequest = new CreateStackRequest(); createRequest.setStackName(stackName); createRequest.setTemplateBody(convertStreamToString( CloudFormationSample.class.getResourceAsStream("CloudFormationSample.template"))); System.out.println("Creating a stack called " + createRequest.getStackName() + "."); stackbuilder.createStack(createRequest); // Wait for stack to be created // Note that you could use SNS notifications on the CreateStack call to track the progress of the stack creation System.out.println("Stack creation completed, the stack " + stackName + " completed with " + waitForCompletion(stackbuilder, stackName)); // Show all the stacks for this account along with the resources for each stack for (Stack stack : stackbuilder.describeStacks(new DescribeStacksRequest()).getStacks()) { System.out.println( "Stack : " + stack.getStackName() + " [" + stack.getStackStatus().toString() + "]"); DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest(); stackResourceRequest.setStackName(stack.getStackName()); for (StackResource resource : stackbuilder.describeStackResources(stackResourceRequest) .getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } } // Lookup a resource by its logical name DescribeStackResourcesRequest logicalNameResourceRequest = new DescribeStackResourcesRequest(); logicalNameResourceRequest.setStackName(stackName); logicalNameResourceRequest.setLogicalResourceId(logicalResourceName); System.out.format("Looking up resource name %1$s from stack %2$s\n", logicalNameResourceRequest.getLogicalResourceId(), logicalNameResourceRequest.getStackName()); for (StackResource resource : stackbuilder.describeStackResources(logicalNameResourceRequest) .getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } // Delete the stack DeleteStackRequest deleteRequest = new DeleteStackRequest(); deleteRequest.setStackName(stackName); System.out.println("Deleting the stack called " + deleteRequest.getStackName() + "."); stackbuilder.deleteStack(deleteRequest); // Wait for stack to be deleted // Note that you could used SNS notifications on the original CreateStack call to track the progress of the stack deletion System.out.println("Stack creation completed, the stack " + stackName + " completed with " + waitForCompletion(stackbuilder, stackName)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS CloudFormation, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS CloudFormation, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.carrotgarden.maven.aws.cfn.CarrotCloudForm.java
License:BSD License
private Stack waitForStackCreate() throws Exception { final long timeStart = System.currentTimeMillis(); while (true) { if (isTimeoutPending(timeStart)) { return newStackWithStatus(StackStatus.CREATE_FAILED, "stack create timeout"); }//from www. ja v a2s .c o m Stack stack = null; try { stack = findStack(); } catch (final Exception e) { return newStackWithStatus(StackStatus.CREATE_FAILED, e.toString()); } if (!isStackValid(stack)) { return newStackWithStatus(StackStatus.CREATE_FAILED, "stack create invalid/missing"); } final StackStatus status = StackStatus.fromValue(stack.getStackStatus()); switch (status) { case CREATE_IN_PROGRESS: final long timeCurrent = System.currentTimeMillis(); final long timeDiff = timeCurrent - timeStart; logger.info("stack create in progress; time=" + timeDiff / 1000); sleep(); continue; case CREATE_COMPLETE: logger.info("stack create success"); printStackEvents(); return stack; default: logger.error("stack create failure"); return stack; } } }
From source file:com.carrotgarden.maven.aws.cfn.CarrotCloudForm.java
License:BSD License
private Stack waitForStackDelete() throws Exception { final long timeStart = System.currentTimeMillis(); while (true) { if (isTimeoutPending(timeStart)) { return newStackWithStatus(StackStatus.DELETE_FAILED, "stack delete timeout"); }/*from w w w. j av a2 s. c om*/ Stack stack = null; try { stack = findStack(); } catch (final Exception e) { return newStackWithStatus(StackStatus.DELETE_FAILED, e.toString()); } if (!isStackValid(stack)) { return newStackWithStatus(StackStatus.DELETE_COMPLETE, "stack delete invalid/missing"); } final StackStatus status = StackStatus.fromValue(stack.getStackStatus()); switch (status) { case DELETE_IN_PROGRESS: final long timeCurrent = System.currentTimeMillis(); final long timeDiff = timeCurrent - timeStart; logger.info("stack delete in progress; time=" + timeDiff / 1000); sleep(); continue; case DELETE_COMPLETE: logger.info("stack delete complete"); printStackEvents(); return stack; default: logger.error("stack delete failed"); return stack; } } }
From source file:com.carrotgarden.maven.aws.cfn.CarrotCloudForm.java
License:BSD License
private Stack waitForStackUpdate() throws Exception { final long timeStart = System.currentTimeMillis(); while (true) { if (isTimeoutPending(timeStart)) { return newStackWithStatus(StackStatus.UPDATE_ROLLBACK_FAILED, "stack update timeout"); }//from w w w. ja va 2 s. com Stack stack = null; try { stack = findStack(); } catch (final Exception e) { return newStackWithStatus(StackStatus.UPDATE_ROLLBACK_FAILED, e.toString()); } if (!isStackValid(stack)) { return newStackWithStatus(StackStatus.UPDATE_ROLLBACK_FAILED, "stack update invalid/missing"); } final StackStatus status = StackStatus.fromValue(stack.getStackStatus()); switch (status) { case UPDATE_IN_PROGRESS: final long timeCurrent = System.currentTimeMillis(); final long timeDiff = timeCurrent - timeStart; logger.info("stack update in progress; time=" + timeDiff / 1000); sleep(); continue; case UPDATE_COMPLETE: logger.info("stack update complete"); printStackEvents(); return stack; default: logger.error("stack updtae failed"); return stack; } } }
From source file:com.carrotgarden.maven.aws.cfn.CloudFormCreateStack.java
License:BSD License
/** * {@inheritDoc}/*from w w w .j a v a2s. c o m*/ */ @Override public void execute() throws MojoFailureException { try { getLog().info("stack create init [" + stackName() + "]"); final Properties stackInputProps = Util.propsLoad(getLog(), stackPropertiesInputFile); final Map<String, String> pluginProps = mergePluginProps(stackInputProps, stackInputParams); final Map<String, String> stackTemplateParams = loadTemplateParameters(stackTemplateFile, pluginProps); final CarrotCloudForm formation = newCloudFormation(stackTemplateFile, stackTemplateParams); formation.logParamList(); final Stack stack = formation.stackCreate(); final StackStatus status = StackStatus.fromValue(stack.getStackStatus()); switch (status) { case CREATE_COMPLETE: break; default: throw new IllegalStateException("stack create failed"); } // getLog().info("stack create stack=\n" + stack); getLog().info("stack create output:"); final List<Output> outputList = stack.getOutputs(); final Properties outputProps = new Properties(); for (final Output output : outputList) { final String key = output.getOutputKey(); final String value = output.getOutputValue(); outputProps.put(key, value); getLog().info("\t" + key + "=" + value); } if (stackIsInjectOutputProperties) { project().getProperties().putAll(outputProps); getLog().info("stack create output is injected in project.properties]"); } if (stackIsPersistOutputProperties) { Util.propsSave(getLog(), outputProps, stackPropertiesOutputFile); getLog().info("stack create output is persisted to : " + stackPropertiesOutputFile); } // getLog().info("stack create done [" + stackName() + "]"); } catch (final Exception e) { throw new MojoFailureException("bada-boom", e); } }
From source file:com.carrotgarden.maven.aws.cfn.CloudFormDeleteStack.java
License:BSD License
/** * {@inheritDoc}//w w w .j av a2s .c o m */ @Override public void execute() throws MojoFailureException { try { getLog().info("stack delete init [" + stackName() + "]"); final CarrotCloudForm formation = newCloudFormation(null, null); final Stack stack = formation.stackDelete(); final StackStatus status = StackStatus.fromValue(stack.getStackStatus()); switch (status) { case DELETE_COMPLETE: break; default: throw new IllegalStateException("stack delete failed"); } getLog().info("stack delete stack=\n" + stack); getLog().info("stack delete done [" + stackName() + "]"); } catch (final Exception e) { throw new MojoFailureException("bada-boom", e); } }
From source file:com.deploymentio.cfnstacker.Status.java
License:Apache License
public static Status valueOf(Stack stack) { if (stack == null) { return Status.CREATABLE; }/* www .ja v a2 s . c o m*/ return stack.getStackStatus().endsWith("_IN_PROGRESS") ? Status.BUSY : Status.MODIFIABLE; }
From source file:com.haskins.cloudtrailviewer.dialog.resourcedetail.detailpanels.CfStackDetail.java
License:Open Source License
private void buildUI(DescribeStacksResult detail) { JTabbedPane tabs = new JTabbedPane(); tabs.add("Stack", primaryScrollPane); final JTable outputCheckTable = new JTable(outputTableModel); JScrollPane healthCheckScrollPane = new JScrollPane(outputCheckTable); tabs.add("Output", healthCheckScrollPane); final JTable paramsTable = new JTable(parametersTableModel); JScrollPane listenersScrollPane = new JScrollPane(paramsTable); tabs.add("Parameters", listenersScrollPane); this.add(tabs, BorderLayout.CENTER); if (!detail.getStacks().isEmpty()) { List<Stack> stacks = detail.getStacks(); Stack stack = stacks.get(0); if (stack.getCreationTime() != null) { primaryTableModel.addRow(new Object[] { "Created", getDateString(stack.getCreationTime()) }); }/*from w w w.j a v a 2 s.c o m*/ if (stack.getDescription() != null) { primaryTableModel.addRow(new Object[] { "Description", stack.getDescription() }); } if (stack.getDisableRollback() != null) { primaryTableModel.addRow(new Object[] { "Disable Rollback", stack.getDisableRollback() }); } if (stack.getLastUpdatedTime() != null) { primaryTableModel .addRow(new Object[] { "Last Updated", getDateString(stack.getLastUpdatedTime()) }); } if (stack.getNotificationARNs() != null) { primaryTableModel.addRow(new Object[] { "Notification Arns", stack.getNotificationARNs() }); } if (stack.getStackId() != null) { primaryTableModel.addRow(new Object[] { "Stacks Id", stack.getStackId() }); } if (stack.getStackName() != null) { primaryTableModel.addRow(new Object[] { "Stacks Name", stack.getStackName() }); } if (stack.getStackStatus() != null) { primaryTableModel.addRow(new Object[] { "Stacks Status", stack.getStackStatus() }); } if (stack.getStackStatusReason() != null) { primaryTableModel.addRow(new Object[] { "Stacks Status Reason", stack.getStackStatusReason() }); } if (stack.getTimeoutInMinutes() != null) { primaryTableModel.addRow(new Object[] { "Timeout (minutes)", stack.getTimeoutInMinutes() }); } /** * Tags */ List<Tag> tags = stack.getTags(); for (Tag tag : tags) { tagsTableModel.addRow(new Object[] { tag.getKey(), tag.getValue() }); } /** * Output */ outputTableModel.addColumn("Description"); outputTableModel.addColumn("Key"); outputTableModel.addColumn("Value"); List<Output> outputs = stack.getOutputs(); for (Output output : outputs) { tagsTableModel.addRow( new Object[] { output.getDescription(), output.getOutputKey(), output.getOutputValue() }); } /** * Parameters */ parametersTableModel.addColumn("Key"); parametersTableModel.addColumn("Value"); parametersTableModel.addColumn("User Previous Value"); List<Parameter> parameters = stack.getParameters(); for (Parameter parameter : parameters) { tagsTableModel.addRow(new Object[] { parameter.getParameterKey(), parameter.getParameterValue(), parameter.getUsePreviousValue() }); } } }