Skip to content

Commit

Permalink
Merge pull request #6 from omervk/feature/support-template-urls
Browse files Browse the repository at this point in the history
[JENKINS-44487] Added the ability to use a URL in cfnUpdate
  • Loading branch information
hoegertn committed Jun 2, 2017
2 parents 53e1551 + 5440166 commit a99da2c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -118,6 +118,14 @@ The step returns the outputs of the stack as a map.
def outputs = cfnUpdate(stack:'my-stack', file:'template.yaml', params:['InstanceType=t2.nano'], keepParams:['Version'], timeoutInMinutes:10, tags:['TagName=Value'])
```

Alternatively, you can specify a URL to a template on S3 (you'll need this if you hit the 51200 byte limit on template):

```
def outputs = cfnUpdate(stack:'my-stack', url:'https://s3.amazonaws.com/my-templates-bucket/template.yaml')
```

Note: When creating a stack, either `file` or `url` are required. When updating it, omitting both parameters will keep the stack's current template.

## cfnDelete

Remove the given stack from CloudFormation.
Expand Down
Expand Up @@ -51,17 +51,17 @@
public class CFNUpdateStep extends AbstractStepImpl {

private final String stack;
private final String file;
private String file;
private String url;
private String[] params;
private String[] keepParams;
private String[] tags;
private String paramsFile;
private Integer timeoutInMinutes;

@DataBoundConstructor
public CFNUpdateStep(String stack, String file) {
public CFNUpdateStep(String stack) {
this.stack = stack;
this.file = file;
}

public String getStack() {
Expand All @@ -72,6 +72,18 @@ public String getFile() {
return this.file;
}

public void setFile(String file) {
this.file = file;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String[] getParams() {
return this.params != null ? this.params.clone() : null;
}
Expand Down Expand Up @@ -150,6 +162,7 @@ public static class Execution extends AbstractStepExecutionImpl {
public boolean start() throws Exception {
final String stack = this.step.getStack();
final String file = this.step.getFile();
final String url = this.step.getUrl();

final Collection<Parameter> params= this.parseParamsFile(this.step.getParamsFile());
params.addAll(this.parseParams(this.step.getParams()));
Expand All @@ -171,9 +184,9 @@ public void run() {
if (cfnStack.exists()) {
ArrayList<Parameter> parameters = new ArrayList<>(params);
parameters.addAll(keepParams);
cfnStack.update(Execution.this.readTemplate(file), parameters, tags);
cfnStack.update(Execution.this.readTemplate(file), url, parameters, tags);
} else {
cfnStack.create(Execution.this.readTemplate(file), params, tags, timeoutInMinutes);
cfnStack.create(Execution.this.readTemplate(file), url, params, tags, timeoutInMinutes);
}
Execution.this.listener.getLogger().println("Stack update complete");
Execution.this.getContext().onSuccess(cfnStack.describeOutputs());
Expand Down Expand Up @@ -206,6 +219,10 @@ private Collection<Parameter> parseParamsFile(String paramsFile) {
}

private String readTemplate(String file) {
if (file == null) {
return null;
}

FilePath child = this.workspace.child(file);
try {
return child.readToString();
Expand Down
Expand Up @@ -72,20 +72,34 @@ public Map<String, String> describeOutputs() {
return map;
}

public void create(String templateBody, Collection<Parameter> params, Collection<Tag> tags, Integer timeoutInMinutes) throws ExecutionException {
public void create(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, Integer timeoutInMinutes) throws ExecutionException {
if ((templateBody != null && !templateBody.isEmpty()) || (templateUrl != null && !templateUrl.isEmpty())) {
throw new IllegalArgumentException("Either a file or url for the template must be specified");
}

CreateStackRequest req = new CreateStackRequest();
req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM);
req.withTemplateBody(templateBody).withParameters(params).withTags(tags).withTimeoutInMinutes(timeoutInMinutes);
req.withTemplateBody(templateBody).withTemplateURL(templateUrl).withParameters(params).withTags(tags).withTimeoutInMinutes(timeoutInMinutes);
this.client.createStack(req);

new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, this.client.waiters().stackCreateComplete());
}

public void update(String templateBody, Collection<Parameter> params, Collection<Tag> tags) throws ExecutionException {
public void update(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags) throws ExecutionException {
try {
UpdateStackRequest req = new UpdateStackRequest();
req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM);
req.withTemplateBody(templateBody).withParameters(params).withTags(tags);

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);

this.client.updateStack(req);

new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, this.client.waiters().stackUpdateComplete());
Expand Down

0 comments on commit a99da2c

Please sign in to comment.