From aa2418cf01b1dcba2c4f192e317abcb90cd1018b Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 17 Aug 2020 11:35:42 -0700 Subject: [PATCH] =?UTF-8?q?add=20progress=20tracker=20to=20vmware-esx=20is?= =?UTF-8?q?o=20upload.=20Add=20colored=20prefix=20to=20=E2=80=A6=20(#9779)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add progress tracker to vmware-esx iso upload. Add colored prefix to tracker to make it clear which build a tracker belongs to. * fix mock; fix tests --- builder/vmware/common/driver_esx5.go | 22 +++++++++++++++++---- builder/vmware/common/remote_driver.go | 8 ++++++-- builder/vmware/common/remote_driver_mock.go | 8 ++++++-- builder/vmware/common/step_remote_upload.go | 2 +- builder/vmware/common/step_upload_vmx.go | 2 +- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/builder/vmware/common/driver_esx5.go b/builder/vmware/common/driver_esx5.go index 22bd0be8c..b3ef21cf0 100644 --- a/builder/vmware/common/driver_esx5.go +++ b/builder/vmware/common/driver_esx5.go @@ -154,7 +154,7 @@ func (d *ESX5Driver) Stop(vmxPathLocal string) error { func (d *ESX5Driver) Register(vmxPathLocal string) error { vmxPath := filepath.ToSlash(filepath.Join(d.outputDir, filepath.Base(vmxPathLocal))) - if err := d.upload(vmxPath, vmxPathLocal); err != nil { + if err := d.upload(vmxPath, vmxPathLocal, nil); err != nil { return err } r, err := d.run(nil, "vim-cmd", "solo/registervm", strconv.Quote(vmxPath)) @@ -185,7 +185,7 @@ func (d *ESX5Driver) IsDestroyed() (bool, error) { return true, err } -func (d *ESX5Driver) UploadISO(localPath string, checksum string) (string, error) { +func (d *ESX5Driver) UploadISO(localPath string, checksum string, ui packer.Ui) (string, error) { finalPath := d.CachePath(localPath) if err := d.mkdir(filepath.ToSlash(filepath.Dir(finalPath))); err != nil { return "", err @@ -198,7 +198,7 @@ func (d *ESX5Driver) UploadISO(localPath string, checksum string) (string, error } log.Println("Initial checksum did not match, uploading.") - if err := d.upload(finalPath, localPath); err != nil { + if err := d.upload(finalPath, localPath, ui); err != nil { return "", err } @@ -667,12 +667,26 @@ func (d *ESX5Driver) mkdir(path string) error { return d.sh("mkdir", "-p", strconv.Quote(path)) } -func (d *ESX5Driver) upload(dst, src string) error { +func (d *ESX5Driver) upload(dst, src string, ui packer.Ui) error { + // Get size so we can set up progress tracker + info, err := os.Stat(src) + if err != nil { + return err + } + f, err := os.Open(src) if err != nil { return err } defer f.Close() + + if ui != nil { + pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f) + defer pf.Close() + + return d.comm.Upload(dst, pf, &info) + } + return d.comm.Upload(dst, f, nil) } diff --git a/builder/vmware/common/remote_driver.go b/builder/vmware/common/remote_driver.go index f3e6aee86..f2c16cf12 100644 --- a/builder/vmware/common/remote_driver.go +++ b/builder/vmware/common/remote_driver.go @@ -1,12 +1,16 @@ package common +import ( + "github.com/hashicorp/packer/packer" +) + type RemoteDriver interface { Driver // UploadISO uploads a local ISO to the remote side and returns the // new path that should be used in the VMX along with an error if it // exists. - UploadISO(path string, checksum string) (string, error) + UploadISO(path string, checksum string, ui packer.Ui) (string, error) // RemoveCache deletes localPath from the remote cache. RemoveCache(localPath string) error @@ -24,7 +28,7 @@ type RemoteDriver interface { IsDestroyed() (bool, error) // Uploads a local file to remote side. - upload(dst, src string) error + upload(dst, src string, ui packer.Ui) error // Download a remote file to a local file. Download(src, dst string) error diff --git a/builder/vmware/common/remote_driver_mock.go b/builder/vmware/common/remote_driver_mock.go index 8c3d53d16..8bcc1f8a8 100644 --- a/builder/vmware/common/remote_driver_mock.go +++ b/builder/vmware/common/remote_driver_mock.go @@ -1,5 +1,9 @@ package common +import ( + "github.com/hashicorp/packer/packer" +) + type RemoteDriverMock struct { DriverMock @@ -32,7 +36,7 @@ type RemoteDriverMock struct { ReloadVMErr error } -func (d *RemoteDriverMock) UploadISO(path string, checksum string) (string, error) { +func (d *RemoteDriverMock) UploadISO(path string, checksum string, ui packer.Ui) (string, error) { d.UploadISOCalled = true d.UploadISOPath = path return d.UploadISOResult, d.UploadISOErr @@ -60,7 +64,7 @@ func (d *RemoteDriverMock) IsDestroyed() (bool, error) { return d.IsDestroyedResult, d.IsDestroyedErr } -func (d *RemoteDriverMock) upload(dst, src string) error { +func (d *RemoteDriverMock) upload(dst, src string, ui packer.Ui) error { return d.UploadErr } diff --git a/builder/vmware/common/step_remote_upload.go b/builder/vmware/common/step_remote_upload.go index 23c7e312e..8790c2659 100644 --- a/builder/vmware/common/step_remote_upload.go +++ b/builder/vmware/common/step_remote_upload.go @@ -45,7 +45,7 @@ func (s *StepRemoteUpload) Run(ctx context.Context, state multistep.StateBag) mu ui.Say(s.Message) log.Printf("Remote uploading: %s", path) - newPath, err := remote.UploadISO(path, s.Checksum) + newPath, err := remote.UploadISO(path, s.Checksum, ui) if err != nil { err := fmt.Errorf("Error uploading file: %s", err) state.Put("error", err) diff --git a/builder/vmware/common/step_upload_vmx.go b/builder/vmware/common/step_upload_vmx.go index b08c6dcbf..861ab900e 100644 --- a/builder/vmware/common/step_upload_vmx.go +++ b/builder/vmware/common/step_upload_vmx.go @@ -32,7 +32,7 @@ func (c *StepUploadVMX) Run(ctx context.Context, state multistep.StateBag) multi remoteDriver, ok := driver.(RemoteDriver) if ok { remoteVmxPath := filepath.ToSlash(filepath.Join(fmt.Sprintf("%s", remoteDriver), filepath.Base(vmxPath))) - if err := remoteDriver.upload(remoteVmxPath, vmxPath); err != nil { + if err := remoteDriver.upload(remoteVmxPath, vmxPath, nil); err != nil { state.Put("error", fmt.Errorf("Error writing VMX: %s", err)) return multistep.ActionHalt }