2015-04-09 21:58:00 +02:00
|
|
|
package communicator
|
|
|
|
|
|
|
|
import (
|
2018-02-15 00:18:12 +01:00
|
|
|
"context"
|
2015-04-09 21:58:00 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-02-15 00:18:12 +01:00
|
|
|
"log"
|
|
|
|
"sync/atomic"
|
2015-04-09 21:58:00 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
|
|
|
"github.com/hashicorp/terraform/communicator/ssh"
|
2015-04-10 20:34:46 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator/winrm"
|
2020-11-18 16:06:28 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/terraform"
|
2015-04-09 21:58:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Communicator is an interface that must be implemented by all communicators
|
|
|
|
// used for any of the provisioners
|
|
|
|
type Communicator interface {
|
|
|
|
// Connect is used to setup the connection
|
|
|
|
Connect(terraform.UIOutput) error
|
|
|
|
|
|
|
|
// Disconnect is used to terminate the connection
|
|
|
|
Disconnect() error
|
|
|
|
|
|
|
|
// Timeout returns the configured connection timeout
|
|
|
|
Timeout() time.Duration
|
|
|
|
|
|
|
|
// ScriptPath returns the configured script path
|
|
|
|
ScriptPath() string
|
|
|
|
|
|
|
|
// Start executes a remote command in a new session
|
|
|
|
Start(*remote.Cmd) error
|
|
|
|
|
|
|
|
// Upload is used to upload a single file
|
|
|
|
Upload(string, io.Reader) error
|
|
|
|
|
2019-03-21 22:05:41 +01:00
|
|
|
// UploadScript is used to upload a file as an executable script
|
2015-04-09 21:58:00 +02:00
|
|
|
UploadScript(string, io.Reader) error
|
|
|
|
|
|
|
|
// UploadDir is used to upload a directory
|
2015-04-10 20:34:46 +02:00
|
|
|
UploadDir(string, string) error
|
2015-04-09 21:58:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a configured Communicator or an error if the connection type is not supported
|
|
|
|
func New(s *terraform.InstanceState) (Communicator, error) {
|
|
|
|
connType := s.Ephemeral.ConnInfo["type"]
|
|
|
|
switch connType {
|
|
|
|
case "ssh", "": // The default connection type is ssh, so if connType is empty use ssh
|
|
|
|
return ssh.New(s)
|
2015-04-10 20:34:46 +02:00
|
|
|
case "winrm":
|
|
|
|
return winrm.New(s)
|
2015-04-09 21:58:00 +02:00
|
|
|
default:
|
2015-04-10 20:34:46 +02:00
|
|
|
return nil, fmt.Errorf("connection type '%s' not supported", connType)
|
2015-04-09 21:58:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 00:18:12 +01:00
|
|
|
|
2018-02-15 20:04:17 +01:00
|
|
|
// maxBackoffDelay is the maximum delay between retry attempts
|
|
|
|
var maxBackoffDelay = 20 * time.Second
|
2018-02-15 00:18:12 +01:00
|
|
|
var initialBackoffDelay = time.Second
|
|
|
|
|
2018-02-15 20:04:17 +01:00
|
|
|
// Fatal is an interface that error values can return to halt Retry
|
2018-02-15 00:18:12 +01:00
|
|
|
type Fatal interface {
|
|
|
|
FatalError() error
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:04:17 +01:00
|
|
|
// Retry retries the function f until it returns a nil error, a Fatal error, or
|
|
|
|
// the context expires.
|
2018-02-15 00:18:12 +01:00
|
|
|
func Retry(ctx context.Context, f func() error) error {
|
|
|
|
// container for atomic error value
|
|
|
|
type errWrap struct {
|
|
|
|
E error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the function in a goroutine
|
|
|
|
var errVal atomic.Value
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
delay := time.Duration(0)
|
|
|
|
for {
|
|
|
|
// If our context ended, we want to exit right away.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(delay):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the function call
|
|
|
|
err := f()
|
|
|
|
|
|
|
|
// return if we have no error, or a FatalError
|
|
|
|
done := false
|
|
|
|
switch e := err.(type) {
|
|
|
|
case nil:
|
|
|
|
done = true
|
|
|
|
case Fatal:
|
|
|
|
err = e.FatalError()
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:04:17 +01:00
|
|
|
errVal.Store(errWrap{err})
|
2018-02-15 00:18:12 +01:00
|
|
|
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[WARN] retryable error: %v", err)
|
|
|
|
|
|
|
|
delay *= 2
|
|
|
|
|
|
|
|
if delay == 0 {
|
|
|
|
delay = initialBackoffDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
if delay > maxBackoffDelay {
|
|
|
|
delay = maxBackoffDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] sleeping for %s", delay)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for completion
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastErr error
|
|
|
|
// Check if we got an error executing
|
|
|
|
if ev, ok := errVal.Load().(errWrap); ok {
|
|
|
|
lastErr = ev.E
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a context error to check if we're interrupted or timeout
|
|
|
|
switch ctx.Err() {
|
|
|
|
case context.Canceled:
|
|
|
|
return fmt.Errorf("interrupted - last error: %v", lastErr)
|
|
|
|
case context.DeadlineExceeded:
|
|
|
|
return fmt.Errorf("timeout - last error: %v", lastErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastErr != nil {
|
|
|
|
return lastErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|