List of usage examples for com.amazonaws.services.cloudformation.model UpdateStackRequest setTemplateURL
public void setTemplateURL(String templateURL)
Location of file containing the template body.
From source file:com.mweagle.tereus.commands.UpdateCommand.java
License:Open Source License
protected void updateStack(UpdateInput updateInput, String transformedTemplate, String stackTargetName) throws UnsupportedEncodingException { if (updateInput.dryRun) { updateInput.logger.info("Dry run requested (-n/--noop). Stack update bypassed."); } else {//from w w w . j a v a 2s . co m // Fetch the stack parameters final DescribeStacksRequest stackRequest = new DescribeStacksRequest().withStackName(stackTargetName); final AmazonCloudFormationAsyncClient awsClient = new AmazonCloudFormationAsyncClient( updateInput.awsCredentials); awsClient.setRegion(updateInput.awsRegion); final DescribeStacksResult result = awsClient.describeStacks(stackRequest); final Stack existantStack = result.getStacks().get(0); final Optional<Parameter> s3Bucket = findNamedParameter(CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME, existantStack.getParameters()); Preconditions.checkArgument(s3Bucket.isPresent(), "Failed to determine S3 BucketName from existant template via parameter name: " + CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME); // Super, now put the new content to S3, update the parameter list // to include the new URL, and submit the updated stack. final byte[] templateBytes = transformedTemplate.getBytes("UTF-8"); final InputStream is = new ByteArrayInputStream(templateBytes); final String templateDigest = DigestUtils.sha256Hex(templateBytes); final String keyName = String.format("%s-tereus.cf.template", templateDigest); try (S3Resource resource = new S3Resource(s3Bucket.get().getParameterValue(), keyName, is, Optional.of(Long.valueOf(templateBytes.length)))) { // Upload the template resource.upload(); // Go ahead and create the stack. final UpdateStackRequest request = new UpdateStackRequest().withStackName(stackTargetName); request.setTemplateURL(resource.getResourceURL().get()); request.setParameters(existantStack.getParameters()); request.setCapabilities(Arrays.asList("CAPABILITY_IAM")); updateInput.logger.debug("Updating stack: {}", stackTargetName); final Optional<DescribeStacksResult> updateResult = new CloudFormation().updateStack(request, updateInput.awsRegion, updateInput.logger); // If everything worked out, then release the template // URL s.t. subsequent ASG instantiated instances have access // to the template content if (updateResult.isPresent()) { updateInput.logger.info("Stack successfully updated"); updateInput.logger.info(updateResult.get().toString()); resource.setReleased(true); } } } }
From source file:de.taimos.pipeline.aws.cloudformation.CloudFormationStack.java
License:Apache License
public void update(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, long pollIntervallMillis, String roleArn) throws ExecutionException { try {/*from ww w . j a v a2 s. com*/ UpdateStackRequest req = new UpdateStackRequest(); req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM); if (templateBody != null && !templateBody.isEmpty()) { req.setTemplateBody(templateBody); } else if (templateUrl != null && !templateUrl.isEmpty()) { req.setTemplateURL(templateUrl); } else { req.setUsePreviousTemplate(true); } req.withParameters(params).withTags(tags).withRoleARN(roleArn); this.client.updateStack(req); new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, this.client.waiters().stackUpdateComplete(), pollIntervallMillis); this.listener.getLogger().format("Updated CloudFormation stack %s %n", this.stack); } catch (AmazonCloudFormationException e) { if (e.getMessage().contains("No updates are to be performed")) { this.listener.getLogger().format("No updates were needed for CloudFormation stack %s %n", this.stack); return; } this.listener.getLogger().format("Failed to update CloudFormation stack %s %n", this.stack); throw e; } }
From source file:jetbrains.buildServer.runner.cloudformation.AWSClient.java
License:Apache License
/** * Uploads application revision archive to S3 bucket named s3BucketName with * the provided key and bundle type.// w w w . java 2 s.c o m * <p> * For performing this operation target AWSClient must have corresponding S3 * permissions. * * @param s3BucketName * valid S3 bucket name * @param s3ObjectKey * valid S3 object key */ public void initiateCFN(@NotNull String stackName, @NotNull String region, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String cfnAction, @NotNull String onFailure) { try { String templateURL; Region reg = Region.getRegion(Regions.fromName(region)); myCloudFormationClient.setRegion(reg); templateURL = getTemplateUrl(reg, s3BucketName, s3ObjectKey); System.out.println("The template url is " + templateURL); if (cfnAction.equalsIgnoreCase("Create")) { System.out.println("The CFN action is " + cfnAction); myListener.createStackStarted(stackName, region, s3BucketName, s3ObjectKey, cfnAction); CreateStackRequest createRequest = new CreateStackRequest(); createRequest.setStackName(stackName); if (!onFailure.equalsIgnoreCase("null")) createRequest.setOnFailure(onFailure); createRequest.setTemplateURL(templateURL); myCloudFormationClient.createStack(createRequest); waitForCompletion(myCloudFormationClient, stackName); } else if (cfnAction.equalsIgnoreCase("Delete")) { myListener.deleteStarted(stackName, region); DeleteStackRequest deleteStackRequest = new DeleteStackRequest(); deleteStackRequest.setStackName(stackName); myCloudFormationClient.deleteStack(deleteStackRequest); waitForDelete(myCloudFormationClient, stackName); } else if (cfnAction.equalsIgnoreCase("Validate")) { myListener.validateStarted(stackName); ValidateTemplateRequest validatetempRequest = new ValidateTemplateRequest(); validatetempRequest.setTemplateURL(templateURL); myListener.validateFinished( myCloudFormationClient.validateTemplate(validatetempRequest).getParameters().toString()); } else if (cfnAction.equalsIgnoreCase("Update")) { myListener.updateInProgress(stackName); UpdateStackRequest updateStackRequest = new UpdateStackRequest(); updateStackRequest.setStackName(stackName); updateStackRequest.setTemplateURL(templateURL); myCloudFormationClient.updateStack(updateStackRequest); waitForCompletion(myCloudFormationClient, stackName); } } catch (Throwable t) { processFailure(t); } }
From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java
License:Apache License
private void updateStack(AmazonCloudFormation cfn) throws IOException { // to enable conventionMappings feature String stackName = getStackName(); String cfnTemplateUrl = getCfnTemplateUrl(); File cfnTemplateFile = getCfnTemplateFile(); List<Parameter> cfnStackParams = getCfnStackParams(); List<Tag> cfnStackTags = getCfnStackTags(); String cfnStackPolicyUrl = getCfnStackPolicyUrl(); File cfnStackPolicyFile = getCfnStackPolicyFile(); getLogger().info("Update stack: {}", stackName); UpdateStackRequest req = new UpdateStackRequest().withStackName(stackName).withParameters(cfnStackParams) .withTags(cfnStackTags);/* w w w .ja v a 2 s . c om*/ // If template URL is specified, then use it if (Strings.isNullOrEmpty(cfnTemplateUrl) == false) { req.setTemplateURL(cfnTemplateUrl); getLogger().info("Using template url: {}", cfnTemplateUrl); // Else, use the template file body } else { req.setTemplateBody(FileUtils.readFileToString(cfnTemplateFile)); getLogger().info("Using template file: {}", "$cfnTemplateFile.canonicalPath"); } if (isCapabilityIam()) { Capability selectedCapability = (getUseCapabilityIam() == null) ? Capability.CAPABILITY_IAM : getUseCapabilityIam(); getLogger().info("Using IAM capability: " + selectedCapability); req.setCapabilities(Arrays.asList(selectedCapability.toString())); } // If stack policy is specified, then use it if (Strings.isNullOrEmpty(cfnStackPolicyUrl) == false) { req.setStackPolicyURL(cfnStackPolicyUrl); // Else, use the stack policy file body if present } else if (cfnStackPolicyFile != null) { req.setStackPolicyBody(FileUtils.readFileToString(cfnStackPolicyFile)); } UpdateStackResult updateStackResult = cfn.updateStack(req); getLogger().info("Update requested: {}", updateStackResult.getStackId()); }
From source file:org.xmlsh.aws.cfnUpdateStack.java
License:BSD License
private int updateStack(List<XValue> args, Options opts) throws IOException, XMLStreamException, SaxonApiException, CoreException { OutputPort stdout = getStdout();//from w ww .j a v a 2 s .com mWriter = new SafeXMLStreamWriter(stdout.asXMLStreamWriter(getSerializeOpts())); startDocument(); startElement(getName()); UpdateStackRequest request = new UpdateStackRequest(); // "capability:+,disable-rollback,notification-arn:+,name:,template:,timeout:,tag:+"); if (opts.hasOpt("capability")) request.setCapabilities(Util.toStringList(opts.getOptValues("capability"))); request.setStackName(opts.getOptStringRequired("name")); if (opts.hasOpt("template-file")) request.setTemplateBody(Util.readString(mShell.getFile(opts.getOptValue("template-file")), getSerializeOpts().getInput_text_encoding())); else request.setTemplateURL(opts.getOptStringRequired("template-url")); request.setParameters(getParameters(args)); traceCall("updateStack"); UpdateStackResult result = getAWSClient().updateStack(request); writeStackResult(result); endElement(); endDocument(); closeWriter(); stdout.writeSequenceTerminator(getSerializeOpts()); return 0; }