diff --git a/builtin/provisioners/remote-exec/resource_provisioner.go b/builtin/provisioners/remote-exec/resource_provisioner.go index f3524a31ce70..69f7beb4ba8f 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner.go +++ b/builtin/provisioners/remote-exec/resource_provisioner.go @@ -176,8 +176,9 @@ 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,17 +193,11 @@ 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 { cmd.Wait() + if cmd.ExitStatus != 0 { err = fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus) } @@ -214,6 +209,13 @@ 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 { + return fmt.Errorf("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