Browse Source

(Mostly) alphabetize functions

pull/58/head
Jacob McCann 7 years ago
parent
commit
5401f4f43f
  1. 140
      plugin.go

140
plugin.go

@ -125,13 +125,6 @@ func (p Plugin) Exec() error {
return nil return nil
} }
func installCaCert(cacert string) *exec.Cmd {
ioutil.WriteFile("/usr/local/share/ca-certificates/ca_cert.crt", []byte(cacert), 0644)
return exec.Command(
"update-ca-certificates",
)
}
// CopyTfEnv creates copies of TF_VAR_ to lowercase // CopyTfEnv creates copies of TF_VAR_ to lowercase
func CopyTfEnv() { func CopyTfEnv() {
tfVar := regexp.MustCompile(`^TF_VAR_.*$`) tfVar := regexp.MustCompile(`^TF_VAR_.*$`)
@ -144,6 +137,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",
@ -152,6 +166,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",
@ -180,51 +201,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 tfValidate(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 tfPlan(config Config, destroy bool) *exec.Cmd { func tfApply(config Config) *exec.Cmd {
args := []string{ args := []string{
"plan", "apply",
}
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, "--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,18 +228,19 @@ func tfPlan(config Config, destroy bool) *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 tfApply(config Config) *exec.Cmd { func tfDestroy(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))
@ -256,19 +251,33 @@ func tfApply(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 tfDestroy(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))
@ -279,34 +288,25 @@ func tfDestroy(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...,
)
} }

Loading…
Cancel
Save