0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-25 09:40:39 +00:00

Merge pull request #41 from msuterski/tf-0.9.4

Tf 0.9.4
This commit is contained in:
Jacob McCann 2017-05-12 08:39:29 -05:00 committed by GitHub
commit eb649f82d7
3 changed files with 37 additions and 23 deletions

View File

@ -10,7 +10,7 @@ RUN apk -U add \
wget && \ wget && \
rm -rf /var/cache/apk/* 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 && \ RUN wget -q https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -O terraform.zip && \
unzip terraform.zip -d /bin && \ unzip terraform.zip -d /bin && \
rm -f terraform.zip rm -f terraform.zip

14
main.go
View File

@ -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",
@ -101,9 +101,6 @@ func run(c *cli.Context) error {
_ = godotenv.Load(c.String("env-file")) _ = godotenv.Load(c.String("env-file"))
} }
remote := Remote{}
json.Unmarshal([]byte(c.String("remote")), &remote)
var vars map[string]string var vars map[string]string
if c.String("vars") != "" { if c.String("vars") != "" {
if err := json.Unmarshal([]byte(c.String("vars")), &vars); err != nil { 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{ 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"),

View File

@ -17,10 +17,10 @@ import (
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
@ -30,9 +30,10 @@ type (
VarFiles []string VarFiles []string
} }
Remote struct { InitOptions struct {
Backend string `json:"backend"` BackendConfig string `json:"backend-config"`
Config map[string]string `json:"config"` Lock *bool `json:"lock"`
LockTimeout string `json:"lock-timeout"`
} }
Plugin struct { Plugin struct {
@ -51,14 +52,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)) 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{ 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))
} }
// 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( return exec.Command(
"terraform", "terraform",
args..., args...,