diff --git a/builtin/provisioners/chef/resource_provisioner.go b/builtin/provisioners/chef/resource_provisioner.go index 36d3eb88d..cfcf8485a 100644 --- a/builtin/provisioners/chef/resource_provisioner.go +++ b/builtin/provisioners/chef/resource_provisioner.go @@ -312,11 +312,11 @@ func applyFn(ctx context.Context) error { return err } - ctx, cancel := context.WithTimeout(ctx, comm.Timeout()) + retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout()) defer cancel() // Wait and retry until we establish the connection - err = communicator.Retry(ctx, func() error { + err = communicator.Retry(retryCtx, func() error { return comm.Connect(o) }) if err != nil { diff --git a/builtin/provisioners/file/resource_provisioner.go b/builtin/provisioners/file/resource_provisioner.go index 5514250d7..26f2f4daf 100644 --- a/builtin/provisioners/file/resource_provisioner.go +++ b/builtin/provisioners/file/resource_provisioner.go @@ -48,9 +48,6 @@ func applyFn(ctx context.Context) error { return err } - ctx, cancel := context.WithTimeout(ctx, comm.Timeout()) - defer cancel() - // Get the source src, deleteSource, err := getSrc(data) if err != nil { @@ -99,8 +96,11 @@ func getSrc(data *schema.ResourceData) (string, bool, error) { // copyFiles is used to copy the files from a source to a destination func copyFiles(ctx context.Context, comm communicator.Communicator, src, dst string) error { + retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout()) + defer cancel() + // Wait and retry until we establish the connection - err := communicator.Retry(ctx, func() error { + err := communicator.Retry(retryCtx, func() error { return comm.Connect(nil) }) if err != nil { diff --git a/builtin/provisioners/habitat/resource_provisioner.go b/builtin/provisioners/habitat/resource_provisioner.go index ada06a88c..8d9403ee6 100644 --- a/builtin/provisioners/habitat/resource_provisioner.go +++ b/builtin/provisioners/habitat/resource_provisioner.go @@ -231,10 +231,10 @@ func applyFn(ctx context.Context) error { return err } - ctx, cancel := context.WithTimeout(ctx, comm.Timeout()) + retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout()) defer cancel() - err = communicator.Retry(ctx, func() error { + err = communicator.Retry(retryCtx, func() error { return comm.Connect(o) }) diff --git a/builtin/provisioners/remote-exec/resource_provisioner.go b/builtin/provisioners/remote-exec/resource_provisioner.go index 8c3d671b9..ca769767d 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner.go +++ b/builtin/provisioners/remote-exec/resource_provisioner.go @@ -157,20 +157,23 @@ func runScripts( comm communicator.Communicator, scripts []io.ReadCloser) error { - // Wait for the context to end and then disconnect - go func() { - <-ctx.Done() - comm.Disconnect() - }() + retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout()) + defer cancel() // Wait and retry until we establish the connection - err := communicator.Retry(ctx, func() error { + err := communicator.Retry(retryCtx, func() error { return comm.Connect(o) }) if err != nil { return err } + // Wait for the context to end and then disconnect + go func() { + <-ctx.Done() + comm.Disconnect() + }() + for _, script := range scripts { var cmd *remote.Cmd diff --git a/builtin/provisioners/salt-masterless/resource_provisioner.go b/builtin/provisioners/salt-masterless/resource_provisioner.go index dd19d419e..1d7594523 100644 --- a/builtin/provisioners/salt-masterless/resource_provisioner.go +++ b/builtin/provisioners/salt-masterless/resource_provisioner.go @@ -131,17 +131,11 @@ func applyFn(ctx context.Context) error { return err } - ctx, cancelFunc := context.WithTimeout(ctx, comm.Timeout()) - defer cancelFunc() - - // Wait for the context to end and then disconnect - go func() { - <-ctx.Done() - comm.Disconnect() - }() + retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout()) + defer cancel() // Wait and retry until we establish the connection - err = communicator.Retry(ctx, func() error { + err = communicator.Retry(retryCtx, func() error { return comm.Connect(o) }) @@ -149,6 +143,12 @@ func applyFn(ctx context.Context) error { return err } + // Wait for the context to end and then disconnect + go func() { + <-ctx.Done() + comm.Disconnect() + }() + var src, dst string o.Output("Provisioning with Salt...")