From 227421854842c15fc8b0d33e7b4b128396b4320b Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 10 Jun 2016 08:38:54 -0400 Subject: [PATCH] Use terraform version 0.6.16. Add 'parallelism' parameter to limit the number of concurrent operations as Terraform walks its graph. Download glibc-2.21-r2.apk from 'github.com/sgerrand/alpine-pkg-glibc' as 'circle-artifacts.com/gh/andyshinn/alpine-pkg-glibc' is not available. Correct 'terraform apply' with parallelism option. Run 'go fmt'. --- DOCS.md | 23 ++++++++++++++++++++++- Dockerfile | 4 ++-- main.go | 36 +++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/DOCS.md b/DOCS.md index 1f33aab..1c58a15 100644 --- a/DOCS.md +++ b/DOCS.md @@ -8,8 +8,9 @@ Use the Terraform plugin to apply the infrastructure configuration contained wit =` option. * `ca_cert` - ca cert to add to your environment to allow terraform to use internal/private resources * `sensitive` (default: `false`) - Whether or not to suppress terraform commands to stdout. -* `role_arn_to_assume` - A role to assume before running the terraform commands +* `role_arn_to_assume` - A role to assume before running the terraform commands. * `root_dir` - The root directory where the terraform files live. When unset, the top level directory will be assumed. +* `parallelism` - The number of concurrent operations as Terraform walks its graph. The following is a sample Terraform configuration in your .drone.yml file: @@ -113,3 +114,23 @@ deploy: app_version: 1.0.0 root_dir: some/path/here ``` + +## Parallelism +You may want to limit the number of concurrent operations as Terraform walks its graph. +If you want to change Terraform's default parallelism (currently equal to 10) then set the `parallelism` parameter. + +```yaml +deploy: + terraform: + plan: false + remote: + backend: S3 + config: + bucket: my-terraform-config-bucket + key: tf-states/my-project + region: us-east-1 + vars: + app_name: my-project + app_version: 1.0.0 + parallelism: 2 +``` diff --git a/Dockerfile b/Dockerfile index 4010c61..ed99208 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,10 @@ FROM gliderlabs/alpine:3.2 RUN apk-install ca-certificates git -ENV TERRAFORM_VERSION 0.6.14 +ENV TERRAFORM_VERSION 0.6.16 RUN apk update && \ - wget -q "https://circle-artifacts.com/gh/andyshinn/alpine-pkg-glibc/6/artifacts/0/home/ubuntu/alpine-pkg-glibc/packages/x86_64/glibc-2.21-r2.apk" && \ + wget -q "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.21-r2/glibc-2.21-r2.apk" && \ apk add --allow-untrusted glibc-2.21-r2.apk && \ wget -q -O terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \ unzip terraform.zip -d /bin && \ diff --git a/main.go b/main.go index c1b5689..951494b 100644 --- a/main.go +++ b/main.go @@ -19,13 +19,14 @@ var ( ) type terraform struct { - Remote remote `json:"remote"` - Plan bool `json:"plan"` - Vars map[string]string `json:"vars"` - Cacert string `json:"ca_cert"` - Sensitive bool `json:"sensitive"` - RoleARN string `json:"role_arn_to_assume"` - RootDir string `json:"root_dir"` + Remote remote `json:"remote"` + Plan bool `json:"plan"` + Vars map[string]string `json:"vars"` + Cacert string `json:"ca_cert"` + Sensitive bool `json:"sensitive"` + RoleARN string `json:"role_arn_to_assume"` + RootDir string `json:"root_dir"` + Parallelism int `json:"parallelism"` } type remote struct { @@ -57,9 +58,9 @@ func main() { commands = append(commands, remoteConfigCommand(remote)) } commands = append(commands, getModules()) - commands = append(commands, planCommand(vargs.Vars)) + commands = append(commands, planCommand(vargs.Vars, vargs.Parallelism)) if !vargs.Plan { - commands = append(commands, applyCommand()) + commands = append(commands, applyCommand(vargs.Parallelism)) } commands = append(commands, deleteCache()) @@ -129,7 +130,7 @@ func getModules() *exec.Cmd { ) } -func planCommand(variables map[string]string) *exec.Cmd { +func planCommand(variables map[string]string, parallelism int) *exec.Cmd { args := []string{ "plan", "-out=plan.tfout", @@ -138,17 +139,26 @@ func planCommand(variables map[string]string) *exec.Cmd { args = append(args, "-var") args = append(args, fmt.Sprintf("%s=%s", k, v)) } + if parallelism > 0 { + args = append(args, fmt.Sprintf("-parallelism=%d", parallelism)) + } return exec.Command( "terraform", args..., ) } -func applyCommand() *exec.Cmd { +func applyCommand(parallelism int) *exec.Cmd { + args := []string{ + "apply", + } + if parallelism > 0 { + args = append(args, fmt.Sprintf("-parallelism=%d", parallelism)) + } + args = append(args, "plan.tfout") return exec.Command( "terraform", - "apply", - "plan.tfout", + args..., ) }