|
@ -21,7 +21,7 @@ import ( |
|
|
type ( |
|
|
type ( |
|
|
// Config holds input parameters for the plugin
|
|
|
// Config holds input parameters for the plugin
|
|
|
Config struct { |
|
|
Config struct { |
|
|
Plan bool |
|
|
Actions []string |
|
|
Vars map[string]string |
|
|
Vars map[string]string |
|
|
Secrets map[string]string |
|
|
Secrets map[string]string |
|
|
InitOptions InitOptions |
|
|
InitOptions InitOptions |
|
@ -32,7 +32,6 @@ type ( |
|
|
Parallelism int |
|
|
Parallelism int |
|
|
Targets []string |
|
|
Targets []string |
|
|
VarFiles []string |
|
|
VarFiles []string |
|
|
Destroy bool |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Netrc struct { |
|
|
Netrc struct { |
|
@ -88,15 +87,27 @@ func (p Plugin) Exec() error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commands = append(commands, deleteCache()) |
|
|
commands = append(commands, deleteCache()) |
|
|
|
|
|
|
|
|
commands = append(commands, initCommand(p.Config.InitOptions)) |
|
|
commands = append(commands, initCommand(p.Config.InitOptions)) |
|
|
|
|
|
|
|
|
commands = append(commands, getModules()) |
|
|
commands = append(commands, getModules()) |
|
|
commands = append(commands, validateCommand(p.Config)) |
|
|
|
|
|
commands = append(commands, planCommand(p.Config)) |
|
|
// Add commands listed from Actions
|
|
|
if !p.Config.Plan { |
|
|
for _, action := range p.Config.Actions { |
|
|
commands = append(commands, terraformCommand(p.Config)) |
|
|
switch action { |
|
|
|
|
|
case "validate": |
|
|
|
|
|
commands = append(commands, tfValidate(p.Config)) |
|
|
|
|
|
case "plan": |
|
|
|
|
|
commands = append(commands, tfPlan(p.Config, false)) |
|
|
|
|
|
case "plan-destroy": |
|
|
|
|
|
commands = append(commands, tfPlan(p.Config, true)) |
|
|
|
|
|
case "apply": |
|
|
|
|
|
commands = append(commands, tfApply(p.Config)) |
|
|
|
|
|
case "destroy": |
|
|
|
|
|
commands = append(commands, tfDestroy(p.Config)) |
|
|
|
|
|
default: |
|
|
|
|
|
return fmt.Errorf("valid actions are: validate, plan, apply, plan-destroy, destroy. You provided %s", action) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commands = append(commands, deleteCache()) |
|
|
commands = append(commands, deleteCache()) |
|
|
|
|
|
|
|
|
for _, c := range commands { |
|
|
for _, c := range commands { |
|
@ -127,13 +138,7 @@ func (p Plugin) Exec() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func installCaCert(cacert string) *exec.Cmd { |
|
|
// CopyTfEnv creates copies of TF_VAR_ to lowercase
|
|
|
ioutil.WriteFile("/usr/local/share/ca-certificates/ca_cert.crt", []byte(cacert), 0644) |
|
|
|
|
|
return exec.Command( |
|
|
|
|
|
"update-ca-certificates", |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func CopyTfEnv() { |
|
|
func CopyTfEnv() { |
|
|
tfVar := regexp.MustCompile(`^TF_VAR_.*$`) |
|
|
tfVar := regexp.MustCompile(`^TF_VAR_.*$`) |
|
|
for _, e := range os.Environ() { |
|
|
for _, e := range os.Environ() { |
|
@ -145,6 +150,27 @@ func CopyTfEnv() { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func assumeRole(roleArn string) { |
|
|
|
|
|
client := sts.New(session.New()) |
|
|
|
|
|
duration := time.Hour * 1 |
|
|
|
|
|
stsProvider := &stscreds.AssumeRoleProvider{ |
|
|
|
|
|
Client: client, |
|
|
|
|
|
Duration: duration, |
|
|
|
|
|
RoleARN: roleArn, |
|
|
|
|
|
RoleSessionName: "drone", |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
value, err := credentials.NewCredentials(stsProvider).Get() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logrus.WithFields(logrus.Fields{ |
|
|
|
|
|
"error": err, |
|
|
|
|
|
}).Fatal("Error assuming role!") |
|
|
|
|
|
} |
|
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", value.AccessKeyID) |
|
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", value.SecretAccessKey) |
|
|
|
|
|
os.Setenv("AWS_SESSION_TOKEN", value.SessionToken) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func deleteCache() *exec.Cmd { |
|
|
func deleteCache() *exec.Cmd { |
|
|
return exec.Command( |
|
|
return exec.Command( |
|
|
"rm", |
|
|
"rm", |
|
@ -153,6 +179,13 @@ func deleteCache() *exec.Cmd { |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getModules() *exec.Cmd { |
|
|
|
|
|
return exec.Command( |
|
|
|
|
|
"terraform", |
|
|
|
|
|
"get", |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func initCommand(config InitOptions) *exec.Cmd { |
|
|
func initCommand(config InitOptions) *exec.Cmd { |
|
|
args := []string{ |
|
|
args := []string{ |
|
|
"init", |
|
|
"init", |
|
@ -181,50 +214,24 @@ func initCommand(config InitOptions) *exec.Cmd { |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getModules() *exec.Cmd { |
|
|
func installCaCert(cacert string) *exec.Cmd { |
|
|
|
|
|
ioutil.WriteFile("/usr/local/share/ca-certificates/ca_cert.crt", []byte(cacert), 0644) |
|
|
return exec.Command( |
|
|
return exec.Command( |
|
|
"terraform", |
|
|
"update-ca-certificates", |
|
|
"get", |
|
|
|
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func validateCommand(config Config) *exec.Cmd { |
|
|
func trace(cmd *exec.Cmd) { |
|
|
args := []string{ |
|
|
fmt.Println("$", strings.Join(cmd.Args, " ")) |
|
|
"validate", |
|
|
|
|
|
} |
|
|
|
|
|
for _, v := range config.VarFiles { |
|
|
|
|
|
args = append(args, "-var-file", fmt.Sprintf("%s", v)) |
|
|
|
|
|
} |
|
|
|
|
|
for k, v := range config.Vars { |
|
|
|
|
|
args = append(args, "-var") |
|
|
|
|
|
args = append(args, fmt.Sprintf("%s=%s", k, v)) |
|
|
|
|
|
} |
|
|
|
|
|
return exec.Command( |
|
|
|
|
|
"terraform", |
|
|
|
|
|
args..., |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func planCommand(config Config) *exec.Cmd { |
|
|
func tfApply(config Config) *exec.Cmd { |
|
|
args := []string{ |
|
|
args := []string{ |
|
|
"plan", |
|
|
"apply", |
|
|
} |
|
|
|
|
|
if config.Destroy { |
|
|
|
|
|
args = append(args, "-destroy") |
|
|
|
|
|
} else { |
|
|
|
|
|
args = append(args, "-out=plan.tfout") |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for _, v := range config.Targets { |
|
|
for _, v := range config.Targets { |
|
|
args = append(args, "--target", fmt.Sprintf("%s", v)) |
|
|
args = append(args, "--target", fmt.Sprintf("%s", v)) |
|
|
} |
|
|
} |
|
|
for _, v := range config.VarFiles { |
|
|
|
|
|
args = append(args, "-var-file", fmt.Sprintf("%s", v)) |
|
|
|
|
|
} |
|
|
|
|
|
for k, v := range config.Vars { |
|
|
|
|
|
args = append(args, "-var") |
|
|
|
|
|
args = append(args, fmt.Sprintf("%s=%s", k, v)) |
|
|
|
|
|
} |
|
|
|
|
|
if config.Parallelism > 0 { |
|
|
if config.Parallelism > 0 { |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
|
} |
|
|
} |
|
@ -234,26 +241,19 @@ func planCommand(config Config) *exec.Cmd { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
} |
|
|
} |
|
|
|
|
|
args = append(args, "plan.tfout") |
|
|
return exec.Command( |
|
|
return exec.Command( |
|
|
"terraform", |
|
|
"terraform", |
|
|
args..., |
|
|
args..., |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func terraformCommand(config Config) *exec.Cmd { |
|
|
func tfDestroy(config Config) *exec.Cmd { |
|
|
if config.Destroy { |
|
|
|
|
|
return destroyCommand(config) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return applyCommand(config) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyCommand(config Config) *exec.Cmd { |
|
|
|
|
|
args := []string{ |
|
|
args := []string{ |
|
|
"apply", |
|
|
"destroy", |
|
|
} |
|
|
} |
|
|
for _, v := range config.Targets { |
|
|
for _, v := range config.Targets { |
|
|
args = append(args, "--target", fmt.Sprintf("%s", v)) |
|
|
args = append(args, fmt.Sprintf("-target=%s", v)) |
|
|
} |
|
|
} |
|
|
if config.Parallelism > 0 { |
|
|
if config.Parallelism > 0 { |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
@ -264,19 +264,33 @@ func applyCommand(config Config) *exec.Cmd { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
} |
|
|
} |
|
|
args = append(args, "plan.tfout") |
|
|
args = append(args, "-force") |
|
|
return exec.Command( |
|
|
return exec.Command( |
|
|
"terraform", |
|
|
"terraform", |
|
|
args..., |
|
|
args..., |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func destroyCommand(config Config) *exec.Cmd { |
|
|
func tfPlan(config Config, destroy bool) *exec.Cmd { |
|
|
args := []string{ |
|
|
args := []string{ |
|
|
"destroy", |
|
|
"plan", |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if destroy { |
|
|
|
|
|
args = append(args, "-destroy") |
|
|
|
|
|
} else { |
|
|
|
|
|
args = append(args, "-out=plan.tfout") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for _, v := range config.Targets { |
|
|
for _, v := range config.Targets { |
|
|
args = append(args, fmt.Sprintf("-target=%s", v)) |
|
|
args = append(args, "--target", fmt.Sprintf("%s", v)) |
|
|
|
|
|
} |
|
|
|
|
|
for _, v := range config.VarFiles { |
|
|
|
|
|
args = append(args, "-var-file", fmt.Sprintf("%s", v)) |
|
|
|
|
|
} |
|
|
|
|
|
for k, v := range config.Vars { |
|
|
|
|
|
args = append(args, "-var") |
|
|
|
|
|
args = append(args, fmt.Sprintf("%s=%s", k, v)) |
|
|
} |
|
|
} |
|
|
if config.Parallelism > 0 { |
|
|
if config.Parallelism > 0 { |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) |
|
@ -287,36 +301,27 @@ func destroyCommand(config Config) *exec.Cmd { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
if config.InitOptions.LockTimeout != "" { |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout)) |
|
|
} |
|
|
} |
|
|
args = append(args, "-force") |
|
|
|
|
|
return exec.Command( |
|
|
return exec.Command( |
|
|
"terraform", |
|
|
"terraform", |
|
|
args..., |
|
|
args..., |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func assumeRole(roleArn string) { |
|
|
func tfValidate(config Config) *exec.Cmd { |
|
|
client := sts.New(session.New()) |
|
|
args := []string{ |
|
|
duration := time.Hour * 1 |
|
|
"validate", |
|
|
stsProvider := &stscreds.AssumeRoleProvider{ |
|
|
|
|
|
Client: client, |
|
|
|
|
|
Duration: duration, |
|
|
|
|
|
RoleARN: roleArn, |
|
|
|
|
|
RoleSessionName: "drone", |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
for _, v := range config.VarFiles { |
|
|
value, err := credentials.NewCredentials(stsProvider).Get() |
|
|
args = append(args, "-var-file", fmt.Sprintf("%s", v)) |
|
|
if err != nil { |
|
|
|
|
|
logrus.WithFields(logrus.Fields{ |
|
|
|
|
|
"error": err, |
|
|
|
|
|
}).Fatal("Error assuming role!") |
|
|
|
|
|
} |
|
|
} |
|
|
os.Setenv("AWS_ACCESS_KEY_ID", value.AccessKeyID) |
|
|
for k, v := range config.Vars { |
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", value.SecretAccessKey) |
|
|
args = append(args, "-var") |
|
|
os.Setenv("AWS_SESSION_TOKEN", value.SessionToken) |
|
|
args = append(args, fmt.Sprintf("%s=%s", k, v)) |
|
|
} |
|
|
} |
|
|
|
|
|
return exec.Command( |
|
|
func trace(cmd *exec.Cmd) { |
|
|
"terraform", |
|
|
fmt.Println("$", strings.Join(cmd.Args, " ")) |
|
|
args..., |
|
|
|
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// helper function to write a netrc file.
|
|
|
// helper function to write a netrc file.
|
|
|