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

Removing tf show output when streaming logs #104

Merged
merged 6 commits into from
Sep 16, 2021
Merged
Changes from 4 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
26 changes: 21 additions & 5 deletions server/events/terraform/terraform_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/runatlantis/atlantis/server/logging"
)

var LogStreamingValidCmds = [...]string{"init", "plan", "apply"}

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_terraform_client.go Client

type Client interface {
Expand Down Expand Up @@ -442,11 +444,16 @@ func (c *DefaultClient) RunCommandAsync(ctx models.ProjectCommandContext, path s

// Asynchronously copy from stdout/err to outCh.
go func() {
s := bufio.NewScanner(stdout)
for s.Scan() {
message := s.Text()
outCh <- Line{Line: message}
c.projectCmdOutputHandler.Send(ctx, message)
// Don't stream terraform show output to outCh
cmds := strings.Split(tfCmd, " ")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to actually only allow plan and apply specifically instead of just having a deny list?

Upstream has support for using terraform version. and we might want to eventually support terraform import.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I think it makes sense to have an allow list for better control over what's being streamed to the web UI. I've added a LogStreamingValidCmds with init, plan, and apply commands for now. We can add more commands as we go :)

// OverrideTF used for testing with non terraform commands.
if c.overrideTF != "" || isValidCommand(cmds[1]) {
s := bufio.NewScanner(stdout)
for s.Scan() {
message := s.Text()
outCh <- Line{Line: message}
Copy link

Choose a reason for hiding this comment

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

This needs to always run. outCh is used to display the terraform command output on the PR. isValidCommand is only relevant for c.projectCmdOutputHandler.Send(ctx, message)

Copy link
Author

@Aayyush Aayyush Sep 15, 2021

Choose a reason for hiding this comment

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

True! Thanks for the catch :) I've made the change you suggested.

c.projectCmdOutputHandler.Send(ctx, message)
}
}
wg.Done()
}()
Expand Down Expand Up @@ -558,6 +565,15 @@ func generateRCFile(tfeToken string, tfeHostname string, home string) error {
return nil
}

func isValidCommand(cmd string) bool {
for _, validCmd := range LogStreamingValidCmds {
if validCmd == cmd {
return true
}
}
return false
}

func getVersion(tfBinary string) (*version.Version, error) {
versionOutBytes, err := exec.Command(tfBinary, "version").Output() // #nosec
versionOutput := string(versionOutBytes)
Expand Down