Skip to content
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

[QT-570] Add support for running enos with providers running in debug mode #98

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion internal/operation/runner_terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/hashicorp/enos/internal/diagnostics"
"github.com/hashicorp/enos/internal/operation/terraform"
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
)

Expand Down Expand Up @@ -59,7 +60,22 @@ func (r *Runner) terraformApply(
applyOut := NewTextOutput()
tf.SetStdout(applyOut.Stdout)
tf.SetStderr(applyOut.Stderr)
err = tf.Apply(ctx, r.TFConfig.ApplyOptions()...)

options := r.TFConfig.ApplyOptions()
if reattachInfo, ok := terraform.LookupReattachInfoFromEnv(); ok {
reattachOpt, err := terraform.UnMarshallReattachInfo(reattachInfo)
if err != nil {
res.Diagnostics = append(res.Diagnostics, &pb.Diagnostic{
Severity: pb.Diagnostic_SEVERITY_WARNING,
Summary: "Failed to configure Reattach Providers option",
Detail: err.Error(),
})
log.Error("failed to configure Reattach Providers option", "error", err)
} else {
options = append(options, reattachOpt)
}
}
err = tf.Apply(ctx, options...)
res.Stderr = applyOut.Stderr.String()
if err != nil {
notifyFail(diagnostics.FromErr(err))
Expand Down
18 changes: 17 additions & 1 deletion internal/operation/runner_terraform_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/hashicorp/enos/internal/diagnostics"
"github.com/hashicorp/enos/internal/operation/terraform"
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
tfjson "github.com/hashicorp/terraform-json"
)
Expand Down Expand Up @@ -81,7 +82,22 @@ func (r *Runner) terraformDestroy(
destroyOut := NewTextOutput()
tf.SetStdout(destroyOut.Stdout)
tf.SetStderr(destroyOut.Stderr)
err = tf.Destroy(ctx, r.TFConfig.DestroyOptions()...)

options := r.TFConfig.DestroyOptions()
if reattachInfo, ok := terraform.LookupReattachInfoFromEnv(); ok {
reattachOpt, err := terraform.UnMarshallReattachInfo(reattachInfo)
if err != nil {
res.Diagnostics = append(res.Diagnostics, &pb.Diagnostic{
Severity: pb.Diagnostic_SEVERITY_WARNING,
Summary: "Failed to configure Reattach Providers option",
Detail: err.Error(),
})
log.Error("failed to configure Reattach Providers option", "error", err)
} else {
options = append(options, reattachOpt)
}
}
err = tf.Destroy(ctx, options...)
res.Stderr = destroyOut.Stderr.String()
if err != nil {
notifyFail(diagnostics.FromErr(err))
Expand Down
19 changes: 18 additions & 1 deletion internal/operation/runner_terraform_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/hashicorp/enos/internal/diagnostics"
"github.com/hashicorp/enos/internal/operation/terraform"
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
)

Expand Down Expand Up @@ -59,7 +60,23 @@ func (r *Runner) terraformInit(
initOut := NewTextOutput()
tf.SetStdout(initOut.Stdout)
tf.SetStderr(initOut.Stderr)
err = tf.Init(ctx, r.TFConfig.InitOptions()...)

options := r.TFConfig.InitOptions()
if reattachInfo, ok := terraform.LookupReattachInfoFromEnv(); ok {
reattachOpt, err := terraform.UnMarshallReattachInfo(reattachInfo)
if err != nil {
res.Diagnostics = append(res.Diagnostics, &pb.Diagnostic{
Severity: pb.Diagnostic_SEVERITY_WARNING,
Summary: "Failed to configure Reattach Providers option",
Detail: err.Error(),
})
log.Error("failed to configure Reattach Providers option", "error", err)
} else {
options = append(options, reattachOpt)
}
}

err = tf.Init(ctx, options...)
res.Stderr = initOut.Stderr.String()
if err != nil {
notifyFail(diagnostics.FromErr(err))
Expand Down
19 changes: 18 additions & 1 deletion internal/operation/runner_terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/hashicorp/enos/internal/diagnostics"
"github.com/hashicorp/enos/internal/operation/terraform"
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
)

Expand Down Expand Up @@ -59,7 +60,23 @@ func (r *Runner) terraformPlan(
planOut := NewTextOutput()
tf.SetStdout(planOut.Stdout)
tf.SetStderr(planOut.Stderr)
changes, err := tf.Plan(ctx, r.TFConfig.PlanOptions()...)

options := r.TFConfig.PlanOptions()
if reattachInfo, ok := terraform.LookupReattachInfoFromEnv(); ok {
reattachOpt, err := terraform.UnMarshallReattachInfo(reattachInfo)
if err != nil {
res.Diagnostics = append(res.Diagnostics, &pb.Diagnostic{
Severity: pb.Diagnostic_SEVERITY_WARNING,
Summary: "Failed to configure Reattach Providers option",
Detail: err.Error(),
})
log.Error("failed to configure Reattach Providers option", "error", err)
} else {
options = append(options, reattachOpt)
}
}

changes, err := tf.Plan(ctx, options...)
res.ChangesPresent = changes
res.Stderr = planOut.Stderr.String()
if err != nil {
Expand Down
19 changes: 18 additions & 1 deletion internal/operation/runner_terraform_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"

"github.com/hashicorp/enos/internal/diagnostics"
"github.com/hashicorp/enos/internal/operation/terraform"
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
)

Expand Down Expand Up @@ -60,7 +61,23 @@ func (r *Runner) terraformShow(
showOut := NewTextOutput()
tf.SetStdout(showOut.Stdout)
tf.SetStderr(showOut.Stderr)
state, err := tf.Show(ctx, r.TFConfig.ShowOptions()...)

options := r.TFConfig.ShowOptions()
if reattachInfo, ok := terraform.LookupReattachInfoFromEnv(); ok {
reattachOpt, err := terraform.UnMarshallReattachInfo(reattachInfo)
if err != nil {
res.Diagnostics = append(res.Diagnostics, &pb.Diagnostic{
Severity: pb.Diagnostic_SEVERITY_WARNING,
Summary: "Failed to configure Reattach Providers option",
Detail: err.Error(),
})
log.Error("failed to configure Reattach Providers option", "error", err)
} else {
options = append(options, reattachOpt)
}
}

state, err := tf.Show(ctx, options...)
if err != nil {
notifyFail(diagnostics.FromErr(err))
return res
Expand Down
18 changes: 18 additions & 0 deletions internal/operation/terraform/terraform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terraform

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -343,3 +344,20 @@ func (c *Config) OutputOptions() []tfexec.OutputOption {
func (c *Config) ShowOptions() []tfexec.ShowOption {
return []tfexec.ShowOption{}
}

// LookupReattachInfoFromEnv gets the Terraform Reattach Providers configuration from the TF_REATTACH_PROVIDERS
// environment variable. Returns the value and a boolean which if true indicates that the value is
// configured.
func LookupReattachInfoFromEnv() (string, bool) {
return os.LookupEnv("TF_REATTACH_PROVIDERS")
}

// UnMarshallReattachInfo unmarshalls the reattach providers configuration string into a tfexec.ReattachOption.
func UnMarshallReattachInfo(reattachInfo string) (*tfexec.ReattachOption, error) {
var info tfexec.ReattachInfo
err := json.Unmarshal([]byte(reattachInfo), &info)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is Unmarshal but you've used Unmarshall in the other func names, we should be consistent (also english is terrible)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point...

if err != nil {
return nil, fmt.Errorf("failed to unmarshall, reattach providers config, due to: %w", err)
}
return tfexec.Reattach(info), nil
}
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
//
// Version must conform to the format expected by github.com/hashicorp/go-version
// for tests to work.
Version = "0.0.19"
Version = "0.0.20"

// VersionPrerelease is a pre-release marker for the version. If this is ""
// (empty string) then it means that it is a final release. Otherwise, this
Expand Down