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-352] Always output exec information #76

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func (r *acceptanceRunner) run(ctx context.Context, subCommand string) ([]byte,
if err != nil {
return nil, nil
}
cmd := exec.CommandContext(ctx, path, strings.Split(subCommand, " ")...)

cmdParts := strings.Split(subCommand, " ")
// Don't specify a port so we can execute tests in parallel
cmdParts = append(cmdParts, "--listen-grpc", "http://localhost")

cmd := exec.CommandContext(ctx, path, cmdParts...)
cmd.Env = os.Environ()
return cmd.CombinedOutput()
}
Expand Down
6 changes: 4 additions & 2 deletions internal/operation/operator_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ func (o *LocalOperator) Stop() error {
defer o.mu.Unlock()
o.running = false // Prevent new work requests from being dispatched
o.drainWorkReqQueue() // Drain any queued operations
o.ctxCancel() // Cancel in-flight operations, kill operation workers, drain the event queue
o.publisher.Stop() // Turn off event publisher
if o.ctxCancel != nil {
o.ctxCancel() // Cancel in-flight operations, kill operation workers, drain the event queue
}
o.publisher.Stop() // Turn off event publisher
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion internal/operation/runner_scenario_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func DestroyScenario(req *pb.Operation_Request) WorkFunc {

// Destroy the scenario
resVal.Destroy.Destroy = runner.terraformDestroy(ctx, req, events, state)
res.Status = diagnostics.Status(runner.TFConfig.FailOnWarnings, resVal.Destroy.Destroy.GetDiagnostics()...)

// Determine our final status from all operations
res.Status = diagnostics.OperationStatus(runner.TFConfig.FailOnWarnings, res)

return res
}
Expand Down
28 changes: 27 additions & 1 deletion internal/operation/runner_scenario_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,34 @@ func ExecScenario(req *pb.Operation_Request) WorkFunc {
WithLogger(log),
)

// Exec is tricky in that the sub-command may or may not actually need
// to execute in the context of a generated scenario Terraform module.
// As such, we'll try and configure it with the module but rewrite any
// failure diags as warnings. If the sub-command needs to the module and
// it doesn't exist the sub-command failure will be reported.

// Try and configure the runner with the module
mod, diags := moduleForReq(req)

if len(diags) > 0 {
// Rewrite failure diags to warnings since we might not need the module
for i := range diags {
if diags[i].Severity == pb.Diagnostic_SEVERITY_ERROR {
diags[i].Severity = pb.Diagnostic_SEVERITY_WARNING
}
}
}

resVal.Exec.Diagnostics = append(resVal.Exec.GetDiagnostics(), diags...)

// Configure our Terraform executor to use module that may or may not exist
runner.TFConfig.WithModule(mod)

// Execute the exec command
resVal.Exec.Exec = runner.terraformExec(ctx, req, events)
res.Status = diagnostics.Status(runner.TFConfig.FailOnWarnings, resVal.Exec.Exec.GetDiagnostics()...)

// Determine our final status from all operations
res.Status = diagnostics.OperationStatus(runner.TFConfig.FailOnWarnings, res)

return res
}
Expand Down
4 changes: 3 additions & 1 deletion internal/operation/runner_scenario_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func GenerateScenario(req *pb.Operation_Request) WorkFunc {
// Run generate and update the generic status
resVal := runner.moduleGenerate(ctx, req, events)
res.Value = resVal
res.Status = diagnostics.Status(runner.TFConfig.FailOnWarnings, resVal.Generate.GetDiagnostics()...)

// Determine our final status from all operations
res.Status = diagnostics.OperationStatus(runner.TFConfig.FailOnWarnings, res)

return res
}
Expand Down
6 changes: 5 additions & 1 deletion internal/operation/runner_scenario_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func OutputScenario(req *pb.Operation_Request) WorkFunc {
WithLogger(log),
)

// Configure the runner with the existing Terraform module. If it doesn't
// exit there's nothing to output.
mod, diags := moduleForReq(req)
resVal.Output.Diagnostics = append(resVal.Output.GetDiagnostics(), diags...)

Expand All @@ -57,7 +59,9 @@ func OutputScenario(req *pb.Operation_Request) WorkFunc {
// Run the output command in the context of the module that should already
// exist
resVal.Output.Output = runner.terraformOutput(ctx, req, events)
res.Status = diagnostics.Status(runner.TFConfig.FailOnWarnings, resVal.Output.Output.GetDiagnostics()...)

// Determine our final status from all operations
res.Status = diagnostics.OperationStatus(runner.TFConfig.FailOnWarnings, res)

return res
}
Expand Down
28 changes: 25 additions & 3 deletions internal/ui/basic/show_op_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func (v *View) showOperationResponse(res *pb.Operation_Response, fullOnComplete
scenario.FromRef(res.GetOp().GetScenario())
v.ui.Info(fmt.Sprintf("Scenario: %s %s", scenario.String(), v.opStatusString(res.GetStatus())))

// If we have a successful operation and fullOnComplete has not been set to
// true we'll move on after printing a single line for the scenario.
if res.GetStatus() == pb.Operation_STATUS_COMPLETED && !fullOnComplete {
if !shouldShowFullResponse(res, fullOnComplete) {
return nil
}

Expand Down Expand Up @@ -162,3 +160,27 @@ func (v *View) showOperationResponse(res *pb.Operation_Response, fullOnComplete

return nil
}

// shouldShowFullResponse determines whether or not we should display the entire
// operation response. We show the full response if something went wrong, if
// a full response has been requested, or if there is operation information for
// for sub-operations that have output like exec or output.
func shouldShowFullResponse(res *pb.Operation_Response, fullOnComplete bool) bool {
if fullOnComplete {
return true
}

if res.GetStatus() != pb.Operation_STATUS_COMPLETED {
return true
}

if res.GetExec() != nil {
return true
}

if res.GetOutput() != nil {
return true
}

return false
}
3 changes: 2 additions & 1 deletion internal/ui/basic/tf_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (v *View) writeExecResponse(exec *pb.Terraform_Command_Exec_Response) {
}

v.ui.Info(exec.GetStdout())
v.ui.Error(exec.GetStderr())
v.ui.Debug(fmt.Sprintf(" Sub-command: %s", exec.GetSubCommand()))
v.WriteDiagnostics(exec.GetDiagnostics())
}
Expand All @@ -97,7 +98,7 @@ func (v *View) writeOutputResponse(res *pb.Operation_Response) {

scenario := flightplan.NewScenario()
scenario.FromRef(res.GetOp().GetScenario())
v.ui.Info(fmt.Sprintf("Scenario: %s", scenario.String()))
v.ui.Info(fmt.Sprintf("Scenario: %s %s", scenario.String(), v.opStatusString(res.GetStatus())))

if status.HasFailed(v.settings.FailOnWarnings, out) {
msg := " Output: failed!"
Expand Down
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.13"
Version = "0.0.14"

// 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