github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/communicator/communicator.go (about)

     1  package communicator
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/communicator/remote"
     9  	"github.com/hashicorp/terraform/communicator/ssh"
    10  	"github.com/hashicorp/terraform/communicator/winrm"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  // Communicator is an interface that must be implemented by all communicators
    15  // used for any of the provisioners
    16  type Communicator interface {
    17  	// Connect is used to setup the connection
    18  	Connect(terraform.UIOutput) error
    19  
    20  	// Disconnect is used to terminate the connection
    21  	Disconnect() error
    22  
    23  	// Timeout returns the configured connection timeout
    24  	Timeout() time.Duration
    25  
    26  	// ScriptPath returns the configured script path
    27  	ScriptPath() string
    28  
    29  	// Start executes a remote command in a new session
    30  	Start(*remote.Cmd) error
    31  
    32  	// Upload is used to upload a single file
    33  	Upload(string, io.Reader) error
    34  
    35  	// UploadScript is used to upload a file as a executable script
    36  	UploadScript(string, io.Reader) error
    37  
    38  	// UploadDir is used to upload a directory
    39  	UploadDir(string, string) error
    40  }
    41  
    42  // New returns a configured Communicator or an error if the connection type is not supported
    43  func New(s *terraform.InstanceState) (Communicator, error) {
    44  	connType := s.Ephemeral.ConnInfo["type"]
    45  	switch connType {
    46  	case "ssh", "": // The default connection type is ssh, so if connType is empty use ssh
    47  		return ssh.New(s)
    48  	case "winrm":
    49  		return winrm.New(s)
    50  	default:
    51  		return nil, fmt.Errorf("connection type '%s' not supported", connType)
    52  	}
    53  }