Skip to content

Commit

Permalink
use correct context for communicator.Retry
Browse files Browse the repository at this point in the history
The timeout for a provisioner is expected to only apply to the initial
connection. Keep the context for the communicator.Retry separate from
the global cancellation context.
  • Loading branch information
jbardin committed Mar 20, 2018
1 parent 38e6309 commit 9b4b5f2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions builtin/provisioners/chef/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions builtin/provisioners/file/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions builtin/provisioners/habitat/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
15 changes: 9 additions & 6 deletions builtin/provisioners/remote-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions builtin/provisioners/salt-masterless/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,24 @@ 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)
})

if err != nil {
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...")
Expand Down

0 comments on commit 9b4b5f2

Please sign in to comment.