output buffer must be synchronized

This commit is contained in:
James Bardin 2020-09-30 13:44:07 -04:00
parent ab6d6f99ae
commit a3c9b33b7b
1 changed files with 13 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"sync"
"time" "time"
"github.com/hashicorp/terraform/builtin/provisioners/puppet/bolt" "github.com/hashicorp/terraform/builtin/provisioners/puppet/bolt"
@ -39,6 +40,8 @@ type provisioner struct {
instanceState *terraform.InstanceState instanceState *terraform.InstanceState
output terraform.UIOutput output terraform.UIOutput
comm communicator.Communicator comm communicator.Communicator
outputWG sync.WaitGroup
} }
type csrAttributes struct { type csrAttributes struct {
@ -297,8 +300,11 @@ func (p *provisioner) runCommand(command string) (stdout string, err error) {
outR, outW := io.Pipe() outR, outW := io.Pipe()
errR, errW := io.Pipe() errR, errW := io.Pipe()
outTee := io.TeeReader(outR, &stdoutBuffer) outTee := io.TeeReader(outR, &stdoutBuffer)
p.outputWG.Add(2)
go p.copyToOutput(outTee) go p.copyToOutput(outTee)
go p.copyToOutput(errR) go p.copyToOutput(errR)
defer outW.Close() defer outW.Close()
defer errW.Close() defer errW.Close()
@ -315,12 +321,19 @@ func (p *provisioner) runCommand(command string) (stdout string, err error) {
} }
err = cmd.Wait() err = cmd.Wait()
outW.Close()
errW.Close()
p.outputWG.Wait()
stdout = strings.TrimSpace(stdoutBuffer.String()) stdout = strings.TrimSpace(stdoutBuffer.String())
return stdout, err return stdout, err
} }
func (p *provisioner) copyToOutput(reader io.Reader) { func (p *provisioner) copyToOutput(reader io.Reader) {
defer p.outputWG.Done()
lr := linereader.New(reader) lr := linereader.New(reader)
for line := range lr.Ch { for line := range lr.Ch {
p.output.Output(line) p.output.Output(line)