Browse Source

Add init command for TF 0.9.x

pull/41/head
marcin.suterski 8 years ago
parent
commit
ceefb39d61
No known key found for this signature in database GPG Key ID: A6CF8B51D99C0C92
  1. 11
      main.go
  2. 58
      plugin.go

11
main.go

@ -29,9 +29,9 @@ func main() {
EnvVar: "PLUGIN_PLAN", EnvVar: "PLUGIN_PLAN",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "remote", Name: "init_options",
Usage: "contains the configuration for the Terraform remote state tracking", Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html",
EnvVar: "PLUGIN_REMOTE", EnvVar: "PLUGIN_INIT_OPTIONS",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "vars", Name: "vars",
@ -111,12 +111,15 @@ func run(c *cli.Context) error {
} }
} }
initOptions := InitOptions{}
json.Unmarshal([]byte(c.String("init_options")), &initOptions)
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
Remote: remote,
Plan: c.Bool("plan"), Plan: c.Bool("plan"),
Vars: vars, Vars: vars,
Secrets: secrets, Secrets: secrets,
InitOptions: initOptions,
Cacert: c.String("ca_cert"), Cacert: c.String("ca_cert"),
Sensitive: c.Bool("sensitive"), Sensitive: c.Bool("sensitive"),
RoleARN: c.String("role_arn_to_assume"), RoleARN: c.String("role_arn_to_assume"),

58
plugin.go

@ -2,24 +2,25 @@ package main
import ( import (
"fmt" "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" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"time" "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 ( type (
Config struct { Config struct {
Remote Remote
Plan bool Plan bool
Vars map[string]string Vars map[string]string
Secrets map[string]string Secrets map[string]string
InitOptions InitOptions
Cacert string Cacert string
Sensitive bool Sensitive bool
RoleARN string RoleARN string
@ -33,6 +34,12 @@ type (
Config map[string]string `json:"config"` 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 { Plugin struct {
Config Config Config Config
} }
@ -49,14 +56,14 @@ func (p Plugin) Exec() error {
exportSecrets(p.Config.Secrets) exportSecrets(p.Config.Secrets)
} }
remote := p.Config.Remote
if p.Config.Cacert != "" { if p.Config.Cacert != "" {
commands = append(commands, installCaCert(p.Config.Cacert)) commands = append(commands, installCaCert(p.Config.Cacert))
} }
if remote.Backend != "" {
commands = append(commands, deleteCache()) commands = append(commands, deleteCache())
commands = append(commands, remoteConfigCommand(remote))
} commands = append(commands, initCommand(p.Config.InitOptions))
commands = append(commands, getModules()) commands = append(commands, getModules())
commands = append(commands, validateCommand()) commands = append(commands, validateCommand())
commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets)) 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) trace(c)
} }
logrus.WithFields(logrus.Fields{
"command": c.Args,
}).Debug("Running")
err := c.Run() err := c.Run()
if err != nil { if err != nil {
logrus.WithFields(logrus.Fields{ 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{ args := []string{
"remote", "init",
"config",
fmt.Sprintf("-backend=%s", config.Backend),
} }
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( return exec.Command(
"terraform", "terraform",
args..., args...,

Loading…
Cancel
Save