From 787e18174d8ab22b3ca9447592a8e31f0fb54d65 Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Fri, 5 May 2017 13:02:10 -0400 Subject: [PATCH 1/7] Update to terraform version 0.9.4 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 50bb94d..b62e4ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN apk -U add \ wget && \ rm -rf /var/cache/apk/* -ENV TERRAFORM_VERSION 0.8.8 +ENV TERRAFORM_VERSION 0.9.4 RUN wget -q https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -O terraform.zip && \ unzip terraform.zip -d /bin && \ rm -f terraform.zip From ceefb39d615a125d52732a6914a55626ef4e0777 Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Mon, 8 May 2017 11:40:14 -0400 Subject: [PATCH 2/7] Add init command for TF 0.9.x --- main.go | 11 +++++++---- plugin.go | 58 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 6f609e0..0ab0b30 100644 --- a/main.go +++ b/main.go @@ -29,9 +29,9 @@ func main() { EnvVar: "PLUGIN_PLAN", }, cli.StringFlag{ - Name: "remote", - Usage: "contains the configuration for the Terraform remote state tracking", - EnvVar: "PLUGIN_REMOTE", + Name: "init_options", + Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html", + EnvVar: "PLUGIN_INIT_OPTIONS", }, cli.StringFlag{ Name: "vars", @@ -111,12 +111,15 @@ func run(c *cli.Context) error { } } + initOptions := InitOptions{} + json.Unmarshal([]byte(c.String("init_options")), &initOptions) + plugin := Plugin{ Config: Config{ - Remote: remote, Plan: c.Bool("plan"), Vars: vars, Secrets: secrets, + InitOptions: initOptions, Cacert: c.String("ca_cert"), Sensitive: c.Bool("sensitive"), RoleARN: c.String("role_arn_to_assume"), diff --git a/plugin.go b/plugin.go index b90d39e..49fdd5e 100644 --- a/plugin.go +++ b/plugin.go @@ -2,24 +2,25 @@ package main import ( "fmt" - "github.com/Sirupsen/logrus" - "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/credentials/stscreds" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/sts" "io/ioutil" "os" "os/exec" "strings" "time" + + "github.com/Sirupsen/logrus" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/credentials/stscreds" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/sts" ) type ( Config struct { - Remote Remote Plan bool Vars map[string]string Secrets map[string]string + InitOptions InitOptions Cacert string Sensitive bool RoleARN string @@ -33,6 +34,12 @@ type ( Config map[string]string `json:"config"` } + InitOptions struct { + BackendConfig string `json:"backend_config"` + Lock *bool `json:"lock_state"` + LockTimeout string `json:"lock_timeout"` + } + Plugin struct { Config Config } @@ -49,14 +56,14 @@ func (p Plugin) Exec() error { exportSecrets(p.Config.Secrets) } - remote := p.Config.Remote if p.Config.Cacert != "" { commands = append(commands, installCaCert(p.Config.Cacert)) } - if remote.Backend != "" { - commands = append(commands, deleteCache()) - commands = append(commands, remoteConfigCommand(remote)) - } + + commands = append(commands, deleteCache()) + + commands = append(commands, initCommand(p.Config.InitOptions)) + commands = append(commands, getModules()) commands = append(commands, validateCommand()) commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets)) @@ -81,6 +88,10 @@ func (p Plugin) Exec() error { trace(c) } + logrus.WithFields(logrus.Fields{ + "command": c.Args, + }).Debug("Running") + err := c.Run() if err != nil { logrus.WithFields(logrus.Fields{ @@ -114,15 +125,28 @@ func deleteCache() *exec.Cmd { ) } -func remoteConfigCommand(config Remote) *exec.Cmd { +func initCommand(config InitOptions) *exec.Cmd { args := []string{ - "remote", - "config", - fmt.Sprintf("-backend=%s", config.Backend), + "init", } - for k, v := range config.Config { - args = append(args, fmt.Sprintf("-backend-config=%s=%s", k, v)) + + if config.BackendConfig != "" { + args = append(args, fmt.Sprintf("-backend-config=%s", config.BackendConfig)) } + + // Fasle is default + if config.Lock != nil { + args = append(args, fmt.Sprintf("-lock=%t", *config.Lock)) + } + + // "0s" is default + if config.LockTimeout != "" { + args = append(args, fmt.Sprintf("-lock-timeout=%s", config.LockTimeout)) + } + + // Fail Terraform execution on prompt + args = append(args, "-input=false") + return exec.Command( "terraform", args..., From 037235f5f549a7c84eb7d5f78cab646f3e4170d0 Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Mon, 8 May 2017 11:42:06 -0400 Subject: [PATCH 3/7] Remove debug --- plugin.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugin.go b/plugin.go index 49fdd5e..3ce3529 100644 --- a/plugin.go +++ b/plugin.go @@ -88,10 +88,6 @@ func (p Plugin) Exec() error { trace(c) } - logrus.WithFields(logrus.Fields{ - "command": c.Args, - }).Debug("Running") - err := c.Run() if err != nil { logrus.WithFields(logrus.Fields{ From f50b50b2687a9eb9a53f431291768b9757a2365b Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Mon, 8 May 2017 11:44:08 -0400 Subject: [PATCH 4/7] Fix spelling --- plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.go b/plugin.go index 3ce3529..dbb2fb2 100644 --- a/plugin.go +++ b/plugin.go @@ -130,7 +130,7 @@ func initCommand(config InitOptions) *exec.Cmd { args = append(args, fmt.Sprintf("-backend-config=%s", config.BackendConfig)) } - // Fasle is default + // False is default if config.Lock != nil { args = append(args, fmt.Sprintf("-lock=%t", *config.Lock)) } From cbbcf0bd6506d35f0d1925305d4f440d1ea2dc19 Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Thu, 11 May 2017 12:20:22 -0400 Subject: [PATCH 5/7] Match plugin option names with terraform's CLI options --- plugin.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin.go b/plugin.go index dbb2fb2..9f890ba 100644 --- a/plugin.go +++ b/plugin.go @@ -35,9 +35,9 @@ type ( } InitOptions struct { - BackendConfig string `json:"backend_config"` - Lock *bool `json:"lock_state"` - LockTimeout string `json:"lock_timeout"` + BackendConfig string `json:"backend-config"` + Lock *bool `json:"lock-state"` + LockTimeout string `json:"lock-timeout"` } Plugin struct { From 73851ed5d86f4fb225f1704e233c61ac8a1f332c Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Thu, 11 May 2017 12:34:37 -0400 Subject: [PATCH 6/7] Remove remote struct and option lookup since they are not being used any more --- main.go | 3 --- plugin.go | 5 ----- 2 files changed, 8 deletions(-) diff --git a/main.go b/main.go index 0ab0b30..64bd31b 100644 --- a/main.go +++ b/main.go @@ -95,9 +95,6 @@ func run(c *cli.Context) error { _ = godotenv.Load(c.String("env-file")) } - remote := Remote{} - json.Unmarshal([]byte(c.String("remote")), &remote) - var vars map[string]string if c.String("vars") != "" { if err := json.Unmarshal([]byte(c.String("vars")), &vars); err != nil { diff --git a/plugin.go b/plugin.go index 9f890ba..4ff90be 100644 --- a/plugin.go +++ b/plugin.go @@ -29,11 +29,6 @@ type ( Targets []string } - Remote struct { - Backend string `json:"backend"` - Config map[string]string `json:"config"` - } - InitOptions struct { BackendConfig string `json:"backend-config"` Lock *bool `json:"lock-state"` From 346612f2fad46085f0c989ddc6212c893fedb82a Mon Sep 17 00:00:00 2001 From: "marcin.suterski" Date: Thu, 11 May 2017 12:40:08 -0400 Subject: [PATCH 7/7] Match lock option name to TF CLI --- plugin.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin.go b/plugin.go index 4ff90be..b30e89a 100644 --- a/plugin.go +++ b/plugin.go @@ -31,7 +31,7 @@ type ( InitOptions struct { BackendConfig string `json:"backend-config"` - Lock *bool `json:"lock-state"` + Lock *bool `json:"lock"` LockTimeout string `json:"lock-timeout"` } @@ -125,12 +125,12 @@ func initCommand(config InitOptions) *exec.Cmd { args = append(args, fmt.Sprintf("-backend-config=%s", config.BackendConfig)) } - // False is default + // True is default in TF if config.Lock != nil { args = append(args, fmt.Sprintf("-lock=%t", *config.Lock)) } - // "0s" is default + // "0s" is default in TF if config.LockTimeout != "" { args = append(args, fmt.Sprintf("-lock-timeout=%s", config.LockTimeout)) }