From 0b7e1fac70d3a9bf5e596030b10faabcc0f755b2 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Mon, 21 Mar 2016 09:53:54 -0500 Subject: [PATCH] provisioner/remote-exec: Move script cleanup after command wait The script cleanup step added in #5577 was positioned before the `cmd.Wait()` call to ensure the command completes. This was causing non-deterministic failures, especially for longer running scripts. Fixes #5699 Fixes #5737 --- .../remote-exec/resource_provisioner.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/builtin/provisioners/remote-exec/resource_provisioner.go b/builtin/provisioners/remote-exec/resource_provisioner.go index f3524a31ce70..2f784a4eaa0f 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner.go +++ b/builtin/provisioners/remote-exec/resource_provisioner.go @@ -176,8 +176,8 @@ func (p *ResourceProvisioner) runScripts( go p.copyOutput(o, outR, outDoneCh) go p.copyOutput(o, errR, errDoneCh) + remotePath := comm.ScriptPath() err = retryFunc(comm.Timeout(), func() error { - remotePath := comm.ScriptPath() if err := comm.UploadScript(remotePath, script); err != nil { return fmt.Errorf("Failed to upload script: %v", err) @@ -192,13 +192,6 @@ func (p *ResourceProvisioner) runScripts( return fmt.Errorf("Error starting script: %v", err) } - // Upload a blank follow up file in the same path to prevent residual - // script contents from remaining on remote machine - empty := bytes.NewReader([]byte("")) - if err := comm.Upload(remotePath, empty); err != nil { - return fmt.Errorf("Failed to upload empty follow up script: %v", err) - } - return nil }) if err == nil { @@ -214,6 +207,14 @@ func (p *ResourceProvisioner) runScripts( <-outDoneCh <-errDoneCh + // Upload a blank follow up file in the same path to prevent residual + // script contents from remaining on remote machine + empty := bytes.NewReader([]byte("")) + if err := comm.Upload(remotePath, empty); err != nil { + // This feature is best-effort. + log.Printf("[WARN] Failed to upload empty follow up script: %v", err) + } + // If we have an error, return it out now that we've cleaned up if err != nil { return err