Move code to use SSH "backends"

Default to shelling out to SSH when available.

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire
2015-05-14 15:02:23 -07:00
parent 15e022219f
commit 2f78b7f92a
15 changed files with 309 additions and 165 deletions

View File

@@ -2,10 +2,18 @@ package libmachine
import (
"errors"
"fmt"
)
var (
ErrHostDoesNotExist = errors.New("Host does not exist")
ErrInvalidHostname = errors.New("Invalid hostname specified")
ErrUnknownProviderType = errors.New("Unknown hypervisor type")
)
type ErrHostDoesNotExist struct {
Name string
}
func (e ErrHostDoesNotExist) Error() string {
return fmt.Sprintf("Error: Host does not exist: %s", e.Name)
}

View File

@@ -25,7 +25,9 @@ func NewFilestore(rootPath string, caCert string, privateKey string) *Filestore
func (s Filestore) loadHost(name string) (*Host, error) {
hostPath := filepath.Join(utils.GetMachineDir(), name)
if _, err := os.Stat(hostPath); os.IsNotExist(err) {
return nil, ErrHostDoesNotExist
return nil, ErrHostDoesNotExist{
Name: name,
}
}
host := &Host{Name: name, StorePath: hostPath}

View File

@@ -137,26 +137,30 @@ func (h *Host) Create(name string) error {
return nil
}
func (h *Host) RunSSHCommand(command string) (ssh.Output, error) {
func (h *Host) RunSSHCommand(command string) (string, error) {
return drivers.RunSSHCommandFromDriver(h.Driver, command)
}
func (h *Host) CreateSSHShell() error {
func (h *Host) CreateSSHClient() (ssh.Client, error) {
addr, err := h.Driver.GetSSHHostname()
if err != nil {
return err
return ssh.ExternalClient{}, err
}
port, err := h.Driver.GetSSHPort()
if err != nil {
return err
return ssh.ExternalClient{}, err
}
auth := &ssh.Auth{
Keys: []string{h.Driver.GetSSHKeyPath()},
}
client, err := ssh.NewClient(h.Driver.GetSSHUsername(), addr, port, auth)
return ssh.NewClient(h.Driver.GetSSHUsername(), addr, port, auth)
}
func (h *Host) CreateSSHShell() error {
client, err := h.CreateSSHClient()
if err != nil {
return err
}

View File

@@ -13,7 +13,6 @@ import (
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/log"
"github.com/docker/machine/ssh"
"github.com/docker/machine/state"
"github.com/docker/machine/utils"
)
@@ -102,17 +101,7 @@ func (provisioner *Boot2DockerProvisioner) Package(name string, action pkgaction
}
func (provisioner *Boot2DockerProvisioner) Hostname() (string, error) {
output, err := provisioner.SSHCommand(fmt.Sprintf("hostname"))
if err != nil {
return "", err
}
var so bytes.Buffer
if _, err := so.ReadFrom(output.Stdout); err != nil {
return "", err
}
return so.String(), nil
return provisioner.SSHCommand("hostname")
}
func (provisioner *Boot2DockerProvisioner) SetHostname(hostname string) error {
@@ -231,7 +220,7 @@ func (provisioner *Boot2DockerProvisioner) Provision(swarmOptions swarm.SwarmOpt
return nil
}
func (provisioner *Boot2DockerProvisioner) SSHCommand(args string) (ssh.Output, error) {
func (provisioner *Boot2DockerProvisioner) SSHCommand(args string) (string, error) {
return drivers.RunSSHCommandFromDriver(provisioner.Driver, args)
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/ssh"
)
type GenericProvisioner struct {
@@ -25,17 +24,7 @@ type GenericProvisioner struct {
}
func (provisioner *GenericProvisioner) Hostname() (string, error) {
output, err := provisioner.SSHCommand("hostname")
if err != nil {
return "", err
}
var so bytes.Buffer
if _, err := so.ReadFrom(output.Stdout); err != nil {
return "", err
}
return so.String(), nil
return provisioner.SSHCommand("hostname")
}
func (provisioner *GenericProvisioner) SetHostname(hostname string) error {
@@ -63,7 +52,7 @@ func (provisioner *GenericProvisioner) GetDockerOptionsDir() string {
return provisioner.DockerOptionsDir
}
func (provisioner *GenericProvisioner) SSHCommand(args string) (ssh.Output, error) {
func (provisioner *GenericProvisioner) SSHCommand(args string) (string, error) {
return drivers.RunSSHCommandFromDriver(provisioner.Driver, args)
}

View File

@@ -1,7 +1,6 @@
package provision
import (
"bytes"
"fmt"
"github.com/docker/machine/drivers"
@@ -9,7 +8,6 @@ import (
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/ssh"
)
var provisioners = make(map[string]*RegisteredProvisioner)
@@ -52,7 +50,7 @@ type Provisioner interface {
GetDriver() drivers.Driver
// Short-hand for accessing an SSH command from the driver.
SSHCommand(args string) (ssh.Output, error)
SSHCommand(args string) (string, error)
// Set the OS Release info depending on how it's represented
// internally
@@ -69,19 +67,12 @@ func Register(name string, p *RegisteredProvisioner) {
}
func DetectProvisioner(d drivers.Driver) (Provisioner, error) {
var (
osReleaseOut bytes.Buffer
)
catOsReleaseOutput, err := drivers.RunSSHCommandFromDriver(d, "cat /etc/os-release")
osReleaseOut, err := drivers.RunSSHCommandFromDriver(d, "cat /etc/os-release")
if err != nil {
return nil, fmt.Errorf("Error getting SSH command: %s", err)
}
if _, err := osReleaseOut.ReadFrom(catOsReleaseOutput.Stdout); err != nil {
return nil, err
}
osReleaseInfo, err := NewOsRelease(osReleaseOut.Bytes())
osReleaseInfo, err := NewOsRelease([]byte(osReleaseOut))
if err != nil {
return nil, fmt.Errorf("Error parsing /etc/os-release file: %s", err)
}

View File

@@ -1,7 +1,6 @@
package provision
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
@@ -26,12 +25,7 @@ func installDockerGeneric(p Provisioner) error {
// install docker - until cloudinit we use ubuntu everywhere so we
// just install it using the docker repos
if output, err := p.SSHCommand("if ! type docker; then curl -sSL https://get.docker.com | sh -; fi"); err != nil {
var buf bytes.Buffer
if _, err := buf.ReadFrom(output.Stderr); err != nil {
return err
}
return fmt.Errorf("error installing docker: %s\n", buf.String())
return fmt.Errorf("error installing docker: %s\n", output)
}
return nil