Skip to content

Commit

Permalink
Add new custom task type for multi-region sam deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
fieldju committed Apr 11, 2017
1 parent ec25df9 commit 84b6acc
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.fieldju
artifactId=gradle-aws-sam-deployer-plugin
version=1.3.1
version=1.4.0
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: "$awsSDKVersion"
compile group: 'com.amazonaws', name: 'aws-java-sdk-cloudformation', version: "$awsSDKVersion"
compile group: 'com.amazonaws', name: 'aws-java-sdk-sts', version: "$awsSDKVersion"

compile 'com.fieldju:commons:1.0.0'

testCompile "junit:junit:4.12"
testCompile "org.mockito:mockito-all:1.10.19"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@ class AwsSamDeployerExtension {
Map<String, String> parameterOverrides = [:]
boolean forceUploads = false


@Override
public String toString() {
return "AwsSamDeployerExtension{" +
"region='" + region + '\'' +
", s3Bucket='" + s3Bucket + '\'' +
", s3Prefix='" + s3Prefix + '\'' +
", kmsKeyId='" + kmsKeyId + '\'' +
", samTemplatePath='" + samTemplatePath + '\'' +
", stackName='" + stackName + '\'' +
", tokenArtifactMap=" + tokenArtifactMap +
", parameterOverrides=" + parameterOverrides +
", forceUploads=" + forceUploads +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class DeploySamTask extends SamTask {
@Input
Map<String, String> parameterOverrides

@Input
String templatePath = "${project.buildDir.absolutePath}${File.separator}sam${File.separator}sam-deploy.yaml"

DeploySamTask() {
group = TASK_GROUP
}
Expand All @@ -32,7 +35,11 @@ class DeploySamTask extends SamTask {
.build() as AmazonCloudFormationClient
)

String samTemplate = new File("${project.buildDir.absolutePath}${File.separator}sam${File.separator}sam-deploy.yaml").text
File template = new File(templatePath)
if (! template.exists() || ! template.isFile()) {
throw new GradleException("The deployable cloudformation template: ${templatePath} did not exist or was not a file")
}
String samTemplate = template.text

List<TemplateParameter> templateDefinedParameters = deployer.getTemplateParameters(samTemplate)
List<Parameter> parameterOverrides = mergeParameters(parameterOverrides, templateDefinedParameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fieldju.gradle.plugins.lambdasam.tasks

import com.fieldju.commons.StringUtils
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

class MultiRegionSamDeployTask extends SamTask {

@Input
@Optional
String templatePath

@Input
@Optional
def regionTemplatePathMap = [:]

@Input
def regions = []

@Input
String stackName

@Input
Map<String, Map<String, String>> regionToParameterOverridesMap = [:]

@Override
void taskAction() {
regions.each { region ->
Map<String, String> parameterOverrides
if (regionToParameterOverridesMap.containsKey(region)) {
parameterOverrides = regionToParameterOverridesMap."${region}"
} else {
logger.lifecycle("regionToParameterOverridesMap does not contain an entry for region ${region} defaulting to empty map")
parameterOverrides = [:]
}

String templatePath
if (StringUtils.isNotBlank(this.templatePath)) {
templatePath = this.templatePath
} else if (regionTemplatePathMap.containsKey(region)) {
templatePath = regionTemplatePathMap."${region}"
} else {
throw new GradleException("templatePath or regionTemplatePathMap.'\${region}' for " +
"region: ${region} must be set")
}

DeploySamTask deploySamTask = new DeploySamTask()
deploySamTask.region = region
deploySamTask.stackName = stackName
deploySamTask.parameterOverrides = parameterOverrides
deploySamTask.templatePath = templatePath

logger.lifecycle("Creating and executing changeset for ${templatePath} with stackname ${stackName} in region ${region} with overrides ${parameterOverrides}")
deploySamTask.taskAction()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@ public abstract class SamTask extends DefaultTask {
protected void logExtraDetails() {
AwsSamDeployerExtension extension = project.extensions.getByName(AwsSamDeployerPlugin.EXTENSION_NAME) as AwsSamDeployerExtension
try {

def stsClient = AWSSecurityTokenServiceClient.builder().standard().withRegion(extension.getRegion()).build()
def res = stsClient.getCallerIdentity(new GetCallerIdentityRequest())
logger.lifecycle("Running Command: ${this.name} with AWS Identity info, ARN: ${res.arn}, Account: ${res.account}, UserId: ${res.userId}")
} catch (Throwable t) {
logger.error("Failed to get caller identity via AWS STS", t)
}

logger.info("region: ${extension.region}")
logger.info("s3Bucket: ${extension.s3Bucket}")
logger.info("s3Prefix: ${extension.s3Prefix}")
logger.info("kmsKeyId: ${extension.kmsKeyId}")
logger.info("samTemplatePath: ${extension.samTemplatePath}")
logger.info("stackName: ${extension.stackName}")
logger.info("tokenArtifactMap: ${extension.tokenArtifactMap}")
logger.info("parameterOverrides: ${extension.parameterOverrides}")
logger.info("forceUploads: ${extension.forceUploads}")
logger.info("extention: ${extension.toString()}")
}

@TaskAction
Expand Down

0 comments on commit 84b6acc

Please sign in to comment.