start on refactor for driver interface

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-03-09 10:12:19 -04:00
parent 9d7c6874fe
commit 007d83319d
No known key found for this signature in database
GPG Key ID: A519480096146526
4 changed files with 79 additions and 132 deletions

View File

@ -3,7 +3,6 @@ package drivers
import (
"errors"
"fmt"
"os/exec"
"sort"
"github.com/codegangsta/cli"
@ -14,62 +13,57 @@ import (
// driver represent different ways hosts can be created (e.g. different
// hypervisors, different cloud providers)
type Driver interface {
// Create a host using the driver's config
Create() error
// DriverName returns the name of the driver as it is registered
DriverName() string
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
SetConfigFromFlags(flags DriverOptions) error
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
GetURL() (string, error)
// GetIP returns an IP or hostname that this host is available at
// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net
GetIP() (string, error)
// GetSSHHostname returns hostname for use with ssh
GetSSHHostname() (string, error)
// GetSSHKeyPath returns key path for use with ssh
GetSSHKeyPath() string
// GetSSHPort returns port for use with ssh
GetSSHPort() (int, error)
// GetSSHUsername returns username for use with ssh
GetSSHUsername() string
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
GetURL() (string, error)
// GetState returns the state that the host is in (running, stopped, etc)
GetState() (state.State, error)
// PreCreate allows for pre-create operations to make sure a driver is ready for creation
PreCreateCheck() error
// Kill stops a host forcefully
Kill() error
// Create a host using the driver's config
Create() error
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
PreCreateCheck() error
// Remove a host
Remove() error
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
Restart() error
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
SetConfigFromFlags(flags DriverOptions) error
// Start a host
Start() error
// Stop a host gracefully
Stop() error
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
Restart() error
// Kill stops a host forcefully
Kill() error
// StartDocker starts a Docker daemon on the machine
StartDocker() error
// StopDocker stops a Docker daemon on the machine
StopDocker() error
// Upgrade the version of Docker on the host to the latest version
Upgrade() error
// GetDockerConfigDir returns the config directory for storing daemon configs
GetDockerConfigDir() string
// GetSSHCommand returns a command for SSH pointing at the correct user, host
// and keys for the host with args appended. If no args are passed, it will
// initiate an interactive SSH session as if SSH were passed no args.
GetSSHCommand(args ...string) (*exec.Cmd, error)
}
// RegisteredDriver is used to register a driver with the Register function.

View File

@ -38,10 +38,58 @@ func NewDriver(machineName string, storePath string, caCert string, privateKey s
return &Driver{}, nil
}
func (d *Driver) Create() error {
return nil
}
func (d *Driver) DriverName() string {
return "none"
}
func (d *Driver) GetIP() (string, error) {
return "", nil
}
func (d *Driver) GetSSHHostname() (string, error) {
return "", nil
}
func (d *Driver) GetSSHKeyPath() string {
return ""
}
func (d *Driver) GetSSHPort() int {
return 0
}
func (d *Driver) GetSSHUsername() string {
return ""
}
func (d *Driver) GetURL() (string, error) {
return d.URL, nil
}
func (d *Driver) GetState() (state.State, error) {
return state.None, nil
}
func (d *Driver) Kill() error {
return fmt.Errorf("hosts without a driver cannot be killed")
}
func (d *Driver) PreCreateCheck() error {
return nil
}
func (d *Driver) Remove() error {
return nil
}
func (d *Driver) Restart() error {
return fmt.Errorf("hosts without a driver cannot be restarted")
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
url := flags.String("url")
@ -57,26 +105,6 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func (d *Driver) GetURL() (string, error) {
return d.URL, nil
}
func (d *Driver) GetIP() (string, error) {
return "", nil
}
func (d *Driver) GetState() (state.State, error) {
return state.None, nil
}
func (d *Driver) PreCreateCheck() error {
return nil
}
func (d *Driver) Create() error {
return nil
}
func (d *Driver) Start() error {
return fmt.Errorf("hosts without a driver cannot be started")
}
@ -84,35 +112,3 @@ func (d *Driver) Start() error {
func (d *Driver) Stop() error {
return fmt.Errorf("hosts without a driver cannot be stopped")
}
func (d *Driver) Remove() error {
return nil
}
func (d *Driver) Restart() error {
return fmt.Errorf("hosts without a driver cannot be restarted")
}
func (d *Driver) Kill() error {
return fmt.Errorf("hosts without a driver cannot be killed")
}
func (d *Driver) StartDocker() error {
return fmt.Errorf("hosts without a driver cannot start docker")
}
func (d *Driver) StopDocker() error {
return fmt.Errorf("hosts without a driver cannot stop docker")
}
func (d *Driver) GetDockerConfigDir() string {
return ""
}
func (d *Driver) Upgrade() error {
return fmt.Errorf("hosts without a driver cannot be upgraded")
}
func (d *Driver) GetSSHCommand(args ...string) (*exec.Cmd, error) {
return nil, fmt.Errorf("hosts without a driver do not support SSH")
}

View File

@ -1,42 +0,0 @@
package drivers
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/docker/machine/utils"
)
func PublicKeyPath() string {
return filepath.Join(utils.GetDockerDir(), "public-key.json")
}
func AddPublicKeyToAuthorizedHosts(d Driver, authorizedKeysPath string) error {
f, err := os.Open(PublicKeyPath())
if err != nil {
return err
}
defer f.Close()
// Use path.Join here, want to create unix path even when running on Windows.
cmdString := fmt.Sprintf("mkdir -p %q && cat > %q", authorizedKeysPath,
path.Join(authorizedKeysPath, "docker-host.json"))
cmd, err := d.GetSSHCommand(cmdString)
if err != nil {
return err
}
cmd.Stdin = f
return cmd.Run()
}
func PublicKeyExists() (bool, error) {
_, err := os.Stat(PublicKeyPath())
if err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
}
return false, err
}

View File

@ -11,7 +11,6 @@ import (
)
func GetSSHCommand(host string, port int, user string, sshKey string, args ...string) *exec.Cmd {
defaultSSHArgs := []string{
"-o", "IdentitiesOnly=yes",
"-o", "StrictHostKeyChecking=no",