diff --git a/common/step_download_test.go b/common/step_download_test.go index fa0e690d9..26cd026e9 100644 --- a/common/step_download_test.go +++ b/common/step_download_test.go @@ -245,6 +245,7 @@ func TestStepDownload_download(t *testing.T) { ui := &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), + PB: &packer.NoopProgressTracker{}, } dir := createTempDir(t) diff --git a/common/step_output_dir_test.go b/common/step_output_dir_test.go index 197977151..7a5f58d95 100644 --- a/common/step_output_dir_test.go +++ b/common/step_output_dir_test.go @@ -16,6 +16,7 @@ func testState(t *testing.T) multistep.StateBag { state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), + PB: &packer.NoopProgressTracker{}, }) return state } diff --git a/go.mod b/go.mod index 91be5ff3d..5398a74bf 100644 --- a/go.mod +++ b/go.mod @@ -39,9 +39,6 @@ require ( github.com/go-ole/go-ole v1.2.4 // indirect github.com/go-resty/resty/v2 v2.3.0 github.com/gobwas/glob v0.2.3 - github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect - github.com/gobwas/pool v0.2.1 // indirect - github.com/gobwas/ws v1.0.4 // indirect github.com/gofrs/flock v0.7.3 github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 github.com/golang/protobuf v1.4.2 // indirect @@ -52,7 +49,6 @@ require ( github.com/gophercloud/gophercloud v0.12.0 github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect - github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 github.com/hashicorp/consul/api v1.4.0 diff --git a/go.sum b/go.sum index 5a7642f72..51d2cb63f 100644 --- a/go.sum +++ b/go.sum @@ -159,8 +159,6 @@ github.com/digitalocean/godo v1.11.1 h1:OsTh37YFKk+g6DnAOrkXJ9oDArTkRx5UTkBJ2EWA github.com/digitalocean/godo v1.11.1/go.mod h1:h6faOIcZ8lWIwNQ+DN7b3CgX4Kwby5T+nbpNqkUIozU= github.com/dimchansky/utfbom v1.1.0 h1:FcM3g+nofKgUteL8dm/UpdRXNC9KmADgTpLKsu0TRo4= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= -github.com/diskfs/go-diskfs v1.1.1 h1:rMjLpaydtXGVZb7mdkRGK1+//30i76nKAit89zUzeaI= -github.com/diskfs/go-diskfs v1.1.1/go.mod h1:afUPxxu+x1snp4aCY2bKR0CoZ/YFJewV3X2UEr2nPZE= github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY= github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= github.com/dylanmei/iso8601 v0.1.0 h1:812NGQDBcqquTfH5Yeo7lwR0nzx/cKdsmf3qMjPURUI= @@ -204,12 +202,6 @@ github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= -github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= -github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -github.com/gobwas/ws v1.0.4 h1:5eXU1CZhpQdq5kXbKb+sECH5Ia5KiO6CYzIzdlVx6Bs= -github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= github.com/gofrs/flock v0.7.3 h1:I0EKY9l8HZCXTMYC4F80vwT6KNypV9uYKP3Alm/hjmQ= github.com/gofrs/flock v0.7.3/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= @@ -286,8 +278,6 @@ github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c h1:iawx2ojEQA7c+ github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c/go.mod h1:ehWUbLQJPqS0Ep+CxeD559hsm9pthPXadJNKwZkp43w= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 h1:JIM+OacoOJRU30xpjMf8sulYqjr0ViA3WDrTX6j/yDI= -github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE= github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 h1:BpJ2o0OR5FV7vrkDYfXYVJQeMNWa8RhklZOpW2ITAIQ= @@ -589,14 +579,6 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/sylviamoss/go-vnc v0.0.0-20200908143403-27ce26f2bed9 h1:uEIT4Mi8C8O+S44150F3lug+WaZ01aJ0sLHvN4npMsc= -github.com/sylviamoss/go-vnc v0.0.0-20200908143403-27ce26f2bed9/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908143844-452d6786ec66 h1:4pQ2Ex/3Fxn7WDvpa85XqHLkc+cSoceIefX/8mD518o= -github.com/sylviamoss/go-vnc v0.0.0-20200908143844-452d6786ec66/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908144818-694f171108bf h1:HUHmcnxuPAJktJM1acw5AckmPvOWz/BoenBLB+IMuo0= -github.com/sylviamoss/go-vnc v0.0.0-20200908144818-694f171108bf/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908145922-2061e37ffa00 h1:mURYI59JxRHmjEKY4ZZING6qm6e/R91tUqdwFmHoAjY= -github.com/sylviamoss/go-vnc v0.0.0-20200908145922-2061e37ffa00/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= github.com/tencentcloud/tencentcloud-sdk-go v3.0.222+incompatible h1:bs+0lcG4RELNbE8PsBC9oaPP0/qExr0DuEGnZyocm84= github.com/tencentcloud/tencentcloud-sdk-go v3.0.222+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8= @@ -765,7 +747,6 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -886,8 +867,6 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.27 h1:kJdccidYzt3CaHD1crCFTS1hxyhSi059NhOFUf03YFo= gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= -gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM= -gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk= gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/main.go b/main.go index 8d4c5ee38..47f69d364 100644 --- a/main.go +++ b/main.go @@ -196,6 +196,7 @@ func wrappedMain() int { Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, + PB: &packer.NoopProgressTracker{}, } ui = basicUi if !inPlugin { @@ -211,6 +212,7 @@ func wrappedMain() int { fmt.Fprintf(os.Stderr, "No tty available: %s\n", err) } else { basicUi.TTY = TTY + basicUi.PB = &packer.UiProgressBar{} defer TTY.Close() } } diff --git a/packer/progressbar.go b/packer/progressbar.go index 3328ca9ce..97e781d74 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -15,25 +15,17 @@ func ProgressBarConfig(bar *pb.ProgressBar, prefix string) { bar.Prefix(prefix) } -var defaultUiProgressBar = &uiProgressBar{} - -// uiProgressBar is a self managed progress bar singleton. -// decorate your struct with a *uiProgressBar to -// give it TrackProgress capabilities. -// In TrackProgress if uiProgressBar is nil -// defaultUiProgressBar will be used as -// the progress bar. -type uiProgressBar struct { +// UiProgressBar is a progress bar compatible with go-getter used in our +// UI structs. +type UiProgressBar struct { lock sync.Mutex - pool *pb.Pool - - pbs int + pbs int } -func (p *uiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { +func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { if p == nil { - return defaultUiProgressBar.TrackProgress(src, currentSize, totalSize, stream) + return stream } p.lock.Lock() defer p.lock.Unlock() diff --git a/packer/progressbar_solaris.go b/packer/progressbar_solaris.go index 75b40ad06..868037c38 100644 --- a/packer/progressbar_solaris.go +++ b/packer/progressbar_solaris.go @@ -1,3 +1,3 @@ package packer -type uiProgressBar = NoopProgressTracker +type UiProgressBar = NoopProgressTracker diff --git a/packer/progressbar_test.go b/packer/progressbar_test.go index 5039c48a9..40928d273 100644 --- a/packer/progressbar_test.go +++ b/packer/progressbar_test.go @@ -11,7 +11,7 @@ import ( // The following tests rarelly just happen. So we run them 100 times. func TestProgressTracking_open_close(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar tracker := bar.TrackProgress("1,", 1, 42, ioutil.NopCloser(nil)) tracker.Close() @@ -21,7 +21,7 @@ func TestProgressTracking_open_close(t *testing.T) { } func TestProgressTracking_multi_open_close(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar g := errgroup.Group{} for i := 0; i < 100; i++ { @@ -36,7 +36,7 @@ func TestProgressTracking_multi_open_close(t *testing.T) { } func TestProgressTracking_races(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar g := errgroup.Group{} for i := 0; i < 100; i++ { diff --git a/packer/ui.go b/packer/ui.go index 5d8cb89ed..46bfbf235 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -40,11 +40,12 @@ type Ui interface { Message(string) Error(string) Machine(string, ...string) + // TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) getter.ProgressTracker } type NoopUi struct { - NoopProgressTracker + PB NoopProgressTracker } var _ Ui = new(NoopUi) @@ -54,13 +55,16 @@ func (*NoopUi) Say(string) { return } func (*NoopUi) Message(string) { return } func (*NoopUi) Error(string) { return } func (*NoopUi) Machine(string, ...string) { return } +func (u *NoopUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { + return u.PB.TrackProgress(src, currentSize, totalSize, stream) +} // ColoredUi is a UI that is colored using terminal colors. type ColoredUi struct { Color UiColor ErrorColor UiColor Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(ColoredUi) @@ -189,7 +193,7 @@ type BasicUi struct { l sync.Mutex interrupted bool TTY TTY - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(BasicUi) @@ -304,11 +308,15 @@ func (rw *BasicUi) Machine(t string, args ...string) { log.Printf("machine readable: %s %#v", t, args) } +func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return rw.PB.TrackProgress(src, currentSize, totalSize, stream) +} + // MachineReadableUi is a UI that only outputs machine-readable output // to the given Writer. type MachineReadableUi struct { Writer io.Writer - NoopProgressTracker + PB NoopProgressTracker } var _ Ui = new(MachineReadableUi) @@ -366,11 +374,15 @@ func (u *MachineReadableUi) Machine(category string, args ...string) { log.Printf("%d,%s,%s,%s\n", now.Unix(), target, category, argsString) } +func (u *MachineReadableUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return u.PB.TrackProgress(src, currentSize, totalSize, stream) +} + // TimestampedUi is a UI that wraps another UI implementation and // prefixes each message with an RFC3339 timestamp type TimestampedUi struct { Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(TimestampedUi) @@ -395,6 +407,10 @@ func (u *TimestampedUi) Machine(message string, args ...string) { u.Ui.Machine(message, args...) } +func (u *TimestampedUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return u.Ui.TrackProgress(src, currentSize, totalSize, stream) +} + func (u *TimestampedUi) timestampLine(string string) string { return fmt.Sprintf("%v: %v", time.Now().Format(time.RFC3339), string) } @@ -404,7 +420,7 @@ func (u *TimestampedUi) timestampLine(string string) string { type SafeUi struct { Sem chan int Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(SafeUi) @@ -440,3 +456,11 @@ func (u *SafeUi) Machine(t string, args ...string) { u.Ui.Machine(t, args...) <-u.Sem } + +func (u *SafeUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + u.Sem <- 1 + ret := u.Ui.TrackProgress(src, currentSize, totalSize, stream) + <-u.Sem + + return ret +} diff --git a/packer/ui_test.go b/packer/ui_test.go index bae2ca570..9684aaf34 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -49,7 +49,7 @@ func (tty *testTTY) ReadString() (string, error) { func TestColoredUi(t *testing.T) { bufferUi := testUi() - ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, defaultUiProgressBar} + ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, &UiProgressBar{}} if !ui.supportsColors() { t.Skip("skipping for ui without color support") @@ -81,7 +81,7 @@ func TestColoredUi(t *testing.T) { func TestColoredUi_noColorEnv(t *testing.T) { bufferUi := testUi() - ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, defaultUiProgressBar} + ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, &UiProgressBar{}} // Set the env var to get rid of the color oldenv := os.Getenv("PACKER_NO_COLOR") diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 0986e1e05..dd1d0325c 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -126,6 +126,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -184,6 +185,7 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -253,6 +255,7 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -304,6 +307,7 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -361,6 +365,7 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.ProvisionDownload(ui, comm)