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 diff --git a/main.go b/main.go index 5d00736..a50c4a3 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", @@ -101,9 +101,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 { @@ -117,12 +114,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 a20cf9d..739aae1 100644 --- a/plugin.go +++ b/plugin.go @@ -17,10 +17,10 @@ import ( type ( Config struct { - Remote Remote Plan bool Vars map[string]string Secrets map[string]string + InitOptions InitOptions Cacert string Sensitive bool RoleARN string @@ -30,9 +30,10 @@ type ( VarFiles []string } - Remote struct { - Backend string `json:"backend"` - Config map[string]string `json:"config"` + InitOptions struct { + BackendConfig string `json:"backend-config"` + Lock *bool `json:"lock"` + LockTimeout string `json:"lock-timeout"` } Plugin struct { @@ -51,14 +52,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)) @@ -116,15 +117,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)) } + + // True is default in TF + if config.Lock != nil { + args = append(args, fmt.Sprintf("-lock=%t", *config.Lock)) + } + + // "0s" is default in TF + 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...,