-
Notifications
You must be signed in to change notification settings - Fork 562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes issues with AWSAssumeRole in Blocks for Terraform being passed in #1720
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"os" | ||
"path" | ||
"regexp" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
|
@@ -217,6 +218,7 @@ func (d DiggerExecutor) Plan() (*terraform_utils.TerraformSummary, bool, bool, s | |
} | ||
} | ||
for _, step := range planSteps { | ||
log.Printf(" Running step: %v\n", step.Action) | ||
if step.Action == "init" { | ||
_, stderr, err := d.TerraformExecutor.Init(step.ExtraArgs, d.StateEnvVars) | ||
if err != nil { | ||
|
@@ -531,3 +533,15 @@ func cleanupTerraformPlan(nonEmptyPlan bool, planError error, stdout string, std | |
func (d DiggerExecutor) projectId() string { | ||
return d.ProjectNamespace + "#" + d.ProjectName | ||
} | ||
|
||
// this will log an exit code and error based on the executor of the executor drivers are by filename | ||
func logCommandFail(exitCode int, err error) { | ||
|
||
_, filename, _, ok := runtime.Caller(1); | ||
if ok { | ||
executor := strings.TrimSuffix(path.Base(filename), path.Ext(filename)) | ||
log.Printf("Command failed in %v with exit code %v and error %v", executor, exitCode, err) | ||
} else { | ||
log.Printf("Command failed in unknown executor with exit code %v and error %v", exitCode, err) | ||
} | ||
} | ||
Comment on lines
+538
to
+547
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks so much for this improvement, I remember terragrunt jobs always had obscure error messages especially when they failed |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,24 +45,34 @@ func (job *Job) PopulateAwsCredentialsEnvVarsForJob() error { | |
log.Printf("Project-level AWS role detected, Assuming role for project: %v", job.ProjectName) | ||
var err error | ||
backendConfigArgs, err := populateretrieveBackendConfigArgs(*job.StateEnvProvider) | ||
if err != nil { | ||
log.Printf("Failed to get keys from role: %v", err) | ||
return fmt.Errorf("Failed to get (state) keys from role: %v", err) | ||
} | ||
|
||
if job.PlanStage != nil { | ||
// TODO: check that the first step is infact the terraform "init" step | ||
job.PlanStage.Steps[0].ExtraArgs = append(job.PlanStage.Steps[0].ExtraArgs, backendConfigArgs...) | ||
} | ||
if job.ApplyStage != nil { | ||
// TODO: check that the first step is infact the terraform "init" step | ||
job.ApplyStage.Steps[0].ExtraArgs = append(job.ApplyStage.Steps[0].ExtraArgs, backendConfigArgs...) | ||
} | ||
if err != nil { | ||
log.Printf("Failed to get keys from role: %v", err) | ||
return fmt.Errorf("Failed to get (state) keys from role: %v", err) | ||
// Terragrunt will cause a backend configuration problem if backend-config options are passed and envs of the same key are passed. | ||
// which will trigger a request to init with --reconfigure, so do not use backend-config for terragrunt | ||
if job.Terragrunt != true { | ||
ben-of-codecraft marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. today I learned something new, so with terragrunt does it pass those options to terraform based on terragrunt.hcl configuration or what is the source of the duplicates? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the docs I read it should not happen but in practice I tested it out and it consistently happened, if I set aws credentials in the my environment and then also pass in backend-config options for credentials causes that request on state change when it runs the plan. The So the option was to make it pass backend-config to all steps or just exclude it. I chose to exclude it path, as it does not seem to impact terraform. backend-config is not recommended on the TF site, I figured it was best at least for TG to just ignore their use. I did not go much deeper than that, but I know the above does work. |
||
if err != nil { | ||
log.Printf("Failed to get keys from role: %v", err) | ||
return fmt.Errorf("Failed to get (state) keys from role: %v", err) | ||
} | ||
|
||
if job.PlanStage != nil { | ||
// TODO: check that the first step is infact the terraform "init" step | ||
job.PlanStage.Steps[0].ExtraArgs = append(job.PlanStage.Steps[0].ExtraArgs, backendConfigArgs...) | ||
} | ||
if job.ApplyStage != nil { | ||
// TODO: check that the first step is infact the terraform "init" step | ||
job.ApplyStage.Steps[0].ExtraArgs = append(job.ApplyStage.Steps[0].ExtraArgs, backendConfigArgs...) | ||
} | ||
if err != nil { | ||
log.Printf("Failed to get keys from role: %v", err) | ||
return fmt.Errorf("Failed to get (state) keys from role: %v", err) | ||
} | ||
} else { | ||
job.StateEnvVars, err = populateKeys(job.StateEnvVars, *job.StateEnvProvider) | ||
if err != nil { | ||
log.Printf("Failed to get keys from role (StateEnvProvider): %v", err) | ||
return fmt.Errorf("Failed to get (state) keys from role: %v", err) | ||
} | ||
} | ||
|
||
} | ||
|
||
if job.CommandEnvProvider != nil { | ||
|
@@ -73,6 +83,16 @@ func (job *Job) PopulateAwsCredentialsEnvVarsForJob() error { | |
return fmt.Errorf("Failed to get (command) keys from role: %v", err) | ||
} | ||
} | ||
|
||
// If state environment variables are not set them to match command env vars | ||
if len(job.StateEnvVars) == 0 && len(job.CommandEnvVars) != 0 { | ||
job.StateEnvVars = job.CommandEnvVars | ||
} | ||
|
||
if len(job.StateEnvVars) != 0 && len(job.CommandEnvVars) == 0 { | ||
job.CommandEnvVars = job.StateEnvVars | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry another nit, why not just pass it in the struct above directly? :D