From 3d002187fe0af8ebaa3dd9c0a10dfb7972f074da Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Wed, 27 May 2015 14:12:36 -0400 Subject: [PATCH] Refactoring drivers to embed drivers.DefaultDriver Signed-off-by: Dave Henderson --- commands/inspect_test.go | 16 +++++- docs/DRIVER_SPEC.md | 19 ++----- drivers/amazonec2/amazonec2.go | 45 ++-------------- drivers/azure/azure.go | 49 +++-------------- drivers/base.go | 76 +++++++++++++++++++++++++++ drivers/digitalocean/digitalocean.go | 56 ++------------------ drivers/exoscale/exoscale.go | 34 ++---------- drivers/fakedriver/fakedriver.go | 9 +--- drivers/generic/generic.go | 56 +++----------------- drivers/google/compute_util.go | 2 +- drivers/google/google.go | 58 ++++---------------- drivers/hyperv/hyperv_windows.go | 52 ++++-------------- drivers/none/none.go | 15 ++---- drivers/openstack/openstack.go | 53 ++----------------- drivers/softlayer/driver.go | 55 +++---------------- drivers/virtualbox/virtualbox.go | 40 +++----------- drivers/vmwarefusion/fusion_darwin.go | 55 ++++--------------- drivers/vmwarevcloudair/vcloudair.go | 76 ++++++--------------------- drivers/vmwarevsphere/vsphere.go | 42 ++------------- 19 files changed, 196 insertions(+), 612 deletions(-) create mode 100644 drivers/base.go diff --git a/commands/inspect_test.go b/commands/inspect_test.go index 04fe5d9356..ac9db3746a 100644 --- a/commands/inspect_test.go +++ b/commands/inspect_test.go @@ -29,7 +29,21 @@ func TestCmdInspectFormat(t *testing.T) { assert.Equal(t, "\"none\"", actual) actual, _ = runInspectCommand(t, []string{"--format", "{{prettyjson .Driver}}", "test-a"}) - assert.Equal(t, "{\n \"IPAddress\": \"\",\n \"URL\": \"unix:///var/run/docker.sock\"\n}", actual) + type ExpectedDriver struct { + CaCertPath string + IPAddress string + MachineName string + PrivateKeyPath string + SSHPort int + SSHUser string + SwarmDiscovery string + SwarmHost string + SwarmMaster bool + URL string + } + expected, err := json.MarshalIndent(&ExpectedDriver{MachineName: "test-a", URL: "unix:///var/run/docker.sock"}, "", " ") + assert.NoError(t, err) + assert.Equal(t, string(expected), actual) } func runInspectCommand(t *testing.T, args []string) (string, *libmachine.Host) { diff --git a/docs/DRIVER_SPEC.md b/docs/DRIVER_SPEC.md index 762eddbd9e..a0f7549f5e 100644 --- a/docs/DRIVER_SPEC.md +++ b/docs/DRIVER_SPEC.md @@ -38,7 +38,7 @@ level maintenance. ## Maintainer To be supported as an official driver, it will need to be maintained. There -can be multiple driver maintainers and they will be identified in the +can be multiple driver maintainers and they will be identified in the maintainers file. # Provider Operations @@ -102,21 +102,12 @@ operations such as `Create`, `Remove`, `Start`, `Stop` etc. For details see the [Driver Interface](https://github.com/docker/machine/blob/master/drivers/drivers.go#L24). -To provide this functionality, most drivers use a struct similar to the following: +To provide this functionality, you should embed the `drivers.BaseDriver` struct, similar to the following: ``` type Driver struct { - MachineName string - IPAddress string - SSHUser string - SSHPort int - CaCertPath string - PrivateKeyPath string - DriverKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string + *drivers.BaseDriver + DriverSpecificField string } ``` @@ -142,7 +133,7 @@ func GetCreateFlags() []cli.Flag { EnvVar: "DRIVERNAME_TOKEN", Name: "drivername-token", Usage: "Provider access token", - + }, cli.StringFlag{ EnvVar: "DRIVERNAME_IMAGE", diff --git a/drivers/amazonec2/amazonec2.go b/drivers/amazonec2/amazonec2.go index 60e9199bd0..cee4730d9e 100644 --- a/drivers/amazonec2/amazonec2.go +++ b/drivers/amazonec2/amazonec2.go @@ -7,7 +7,6 @@ import ( "io" "io/ioutil" "net/url" - "path/filepath" "strconv" "strings" "time" @@ -36,6 +35,7 @@ var ( ) type Driver struct { + *drivers.BaseDriver Id string AccessKey string SecretKey string @@ -43,14 +43,10 @@ type Driver struct { Region string AMI string SSHKeyID int - SSHUser string - SSHPort int KeyName string InstanceId string InstanceType string - IPAddress string PrivateIPAddress string - MachineName string SecurityGroupId string SecurityGroupName string ReservationId string @@ -59,12 +55,6 @@ type Driver struct { VpcId string SubnetId string Zone string - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string keyPath string RequestSpotInstance bool SpotPrice string @@ -179,23 +169,13 @@ func GetCreateFlags() []cli.Flag { func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { id := generateId() + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) return &Driver{ - Id: id, - MachineName: machineName, - storePath: storePath, - CaCertPath: caCert, - PrivateKeyPath: privateKey, + Id: id, + BaseDriver: inner, }, nil } -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { region, err := validateAwsRegion(flags.String("amazonec2-region")) if err != nil { @@ -260,10 +240,6 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { return nil } -func (d *Driver) GetMachineName() string { - return d.MachineName -} - func (d *Driver) DriverName() string { return driverName } @@ -464,19 +440,12 @@ func (d *Driver) GetState() (state.State, error) { return state.None, nil } +// GetSSHHostname - func (d *Driver) GetSSHHostname() (string, error) { // TODO: use @nathanleclaire retry func here (ehazlett) return d.GetIP() } -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "ubuntu" @@ -537,10 +506,6 @@ func (d *Driver) getClient() *amz.EC2 { return amz.NewEC2(auth, d.Region) } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - func (d *Driver) getInstance() (*amz.EC2Instance, error) { instance, err := d.getClient().GetInstance(d.InstanceId) if err != nil { diff --git a/drivers/azure/azure.go b/drivers/azure/azure.go index 73ef4774db..58d24fa926 100644 --- a/drivers/azure/azure.go +++ b/drivers/azure/azure.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" azure "github.com/MSOpenTech/azure-sdk-for-go" @@ -20,8 +19,7 @@ import ( ) type Driver struct { - IPAddress string - MachineName string + *drivers.BaseDriver SubscriptionID string SubscriptionCert string PublishSettingsFilePath string @@ -29,15 +27,7 @@ type Driver struct { Size string UserPassword string Image string - SSHUser string - SSHPort int DockerPort int - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string } func init() { @@ -107,38 +97,15 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - d := &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey} + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + d := &Driver{BaseDriver: inner} return d, nil } -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName -} - func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "ubuntu" @@ -426,20 +393,16 @@ func (d *Driver) addDockerEndpoint(vmConfig *vmClient.Role) error { } func (d *Driver) generateCertForAzure() error { - if err := ssh.GenerateSSHKey(d.sshKeyPath()); err != nil { + if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil { return err } - cmd := exec.Command("openssl", "req", "-x509", "-key", d.sshKeyPath(), "-nodes", "-days", "365", "-newkey", "rsa:2048", "-out", d.azureCertPath(), "-subj", "/C=AU/ST=Some-State/O=InternetWidgitsPtyLtd/CN=\\*") + cmd := exec.Command("openssl", "req", "-x509", "-key", d.GetSSHKeyPath(), "-nodes", "-days", "365", "-newkey", "rsa:2048", "-out", d.azureCertPath(), "-subj", "/C=AU/ST=Some-State/O=InternetWidgitsPtyLtd/CN=\\*") return cmd.Run() } -func (d *Driver) sshKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - func (d *Driver) azureCertPath() string { - return filepath.Join(d.storePath, "azure_cert.pem") + return d.ResolveStorePath("azure_cert.pem") } func (d *Driver) getHostname() string { diff --git a/drivers/base.go b/drivers/base.go new file mode 100644 index 0000000000..dcfd1b82fb --- /dev/null +++ b/drivers/base.go @@ -0,0 +1,76 @@ +package drivers + +import "path/filepath" + +// BaseDriver - Embed this struct into drivers to provide the common set +// of fields and functions. +type BaseDriver struct { + storePath string + IPAddress string + SSHUser string + SSHPort int + MachineName string + CaCertPath string + PrivateKeyPath string + SwarmMaster bool + SwarmHost string + SwarmDiscovery string +} + +// NewBaseDriver - Get an instance of a BaseDriver +func NewBaseDriver(machineName string, storePath string, caCert string, privateKey string) *BaseDriver { + return &BaseDriver{ + MachineName: machineName, + storePath: storePath, + CaCertPath: caCert, + PrivateKeyPath: privateKey, + } +} + +// GetSSHKeyPath - +func (d *BaseDriver) GetSSHKeyPath() string { + return d.ResolveStorePath("id_rsa") +} + +// ResolveStorePath - +func (d *BaseDriver) ResolveStorePath(path string) string { + return filepath.Join(d.storePath, path) +} + +// AuthorizePort - +func (d *BaseDriver) AuthorizePort(ports []*Port) error { + return nil +} + +// DeauthorizePort - +func (d *BaseDriver) DeauthorizePort(ports []*Port) error { + return nil +} + +// DriverName - This must be implemented in every driver +func (d *BaseDriver) DriverName() string { + return "unknown" +} + +// GetMachineName - +func (d *BaseDriver) GetMachineName() string { + return d.MachineName +} + +// GetSSHPort - +func (d *BaseDriver) GetSSHPort() (int, error) { + if d.SSHPort == 0 { + d.SSHPort = 22 + } + + return d.SSHPort, nil +} + +// GetSSHUsername - +func (d *BaseDriver) GetSSHUsername() string { + if d.SSHUser == "" { + d.SSHUser = "root" + } + + return d.SSHUser +} diff --git a/drivers/digitalocean/digitalocean.go b/drivers/digitalocean/digitalocean.go index 3b65ca26f2..dd7e05d044 100644 --- a/drivers/digitalocean/digitalocean.go +++ b/drivers/digitalocean/digitalocean.go @@ -3,7 +3,6 @@ package digitalocean import ( "fmt" "io/ioutil" - "path/filepath" "time" "code.google.com/p/goauth2/oauth" @@ -16,27 +15,17 @@ import ( ) type Driver struct { + *drivers.BaseDriver AccessToken string DropletID int DropletName string Image string - MachineName string - IPAddress string Region string SSHKeyID int - SSHUser string - SSHPort int Size string IPv6 bool Backups bool PrivateNetworking bool - CaCertPath string - PrivateKeyPath string - DriverKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string } func init() { @@ -92,45 +81,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - -func (d *Driver) GetSSHUsername() string { - if d.SSHUser == "" { - d.SSHUser = "root" - } - - return d.SSHUser -} - func (d *Driver) DriverName() string { return "digitalocean" } @@ -229,7 +187,7 @@ func (d *Driver) Create() error { } func (d *Driver) createSSHKey() (*godo.Key, error) { - if err := ssh.GenerateSSHKey(d.sshKeyPath()); err != nil { + if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil { return nil, err } @@ -329,10 +287,6 @@ func (d *Driver) getClient() *godo.Client { return godo.NewClient(t.Client()) } -func (d *Driver) sshKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - func (d *Driver) publicSSHKeyPath() string { - return d.sshKeyPath() + ".pub" + return d.GetSSHKeyPath() + ".pub" } diff --git a/drivers/exoscale/exoscale.go b/drivers/exoscale/exoscale.go index 5a62d3cdda..902d1d2786 100644 --- a/drivers/exoscale/exoscale.go +++ b/drivers/exoscale/exoscale.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io/ioutil" - "path/filepath" "strings" "text/template" "time" @@ -18,6 +17,7 @@ import ( ) type Driver struct { + *drivers.BaseDriver URL string ApiKey string ApiSecretKey string @@ -26,18 +26,9 @@ type Driver struct { Image string SecurityGroup string AvailabilityZone string - MachineName string KeyPair string - IPAddress string PublicKey string Id string - CaCertPath string - PrivateKeyPath string - DriverKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string } func init() { @@ -100,33 +91,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - return 22, nil -} - func (d *Driver) GetSSHUsername() string { return "ubuntu" } diff --git a/drivers/fakedriver/fakedriver.go b/drivers/fakedriver/fakedriver.go index 13eae5a109..b2ff5a75e1 100644 --- a/drivers/fakedriver/fakedriver.go +++ b/drivers/fakedriver/fakedriver.go @@ -6,6 +6,7 @@ import ( ) type FakeDriver struct { + *drivers.BaseDriver MockState state.State } @@ -13,14 +14,6 @@ func (d *FakeDriver) DriverName() string { return "fakedriver" } -func (d *FakeDriver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *FakeDriver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - func (d *FakeDriver) SetConfigFromFlags(flags drivers.DriverOptions) error { return nil } diff --git a/drivers/generic/generic.go b/drivers/generic/generic.go index ce7ba301fa..b79618ca80 100644 --- a/drivers/generic/generic.go +++ b/drivers/generic/generic.go @@ -15,18 +15,8 @@ import ( ) type Driver struct { - MachineName string - IPAddress string - SSHKey string - SSHUser string - SSHPort int - CaCertPath string - PrivateKeyPath string - DriverKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string + *drivers.BaseDriver + SSHKey string } const ( @@ -68,46 +58,18 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{ - MachineName: machineName, - storePath: storePath, - CaCertPath: caCert, - PrivateKeyPath: privateKey, - }, nil + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) DriverName() string { return "generic" } -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName -} - func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { return d.SSHUser } @@ -136,11 +98,11 @@ func (d *Driver) PreCreateCheck() error { func (d *Driver) Create() error { log.Infof("Importing SSH key...") - if err := utils.CopyFile(d.SSHKey, d.sshKeyPath()); err != nil { + if err := utils.CopyFile(d.SSHKey, d.GetSSHKeyPath()); err != nil { return fmt.Errorf("unable to copy ssh key: %s", err) } - if err := os.Chmod(d.sshKeyPath(), 0600); err != nil { + if err := os.Chmod(d.GetSSHKeyPath(), 0600); err != nil { return err } @@ -208,10 +170,6 @@ func (d *Driver) Kill() error { return nil } -func (d *Driver) sshKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - func (d *Driver) publicSSHKeyPath() string { - return d.sshKeyPath() + ".pub" + return d.GetSSHKeyPath() + ".pub" } diff --git a/drivers/google/compute_util.go b/drivers/google/compute_util.go index bdf26672d7..793506a090 100644 --- a/drivers/google/compute_util.go +++ b/drivers/google/compute_util.go @@ -40,7 +40,7 @@ const ( // NewComputeUtil creates and initializes a ComputeUtil. func newComputeUtil(driver *Driver) (*ComputeUtil, error) { - service, err := newGCEService(driver.storePath, driver.AuthTokenPath) + service, err := newGCEService(driver.ResolveStorePath("."), driver.AuthTokenPath) if err != nil { return nil, err } diff --git a/drivers/google/google.go b/drivers/google/google.go index 4ad7c62ca6..2a535265f6 100644 --- a/drivers/google/google.go +++ b/drivers/google/google.go @@ -2,7 +2,6 @@ package google import ( "fmt" - "path/filepath" "github.com/codegangsta/cli" "github.com/docker/machine/drivers" @@ -13,23 +12,14 @@ import ( // Driver is a struct compatible with the docker.hosts.drivers.Driver interface. type Driver struct { - IPAddress string - MachineName string - SSHUser string - SSHPort int - Zone string - MachineType string - DiskType string - Scopes string - DiskSize int - AuthTokenPath string - storePath string - Project string - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string + *drivers.BaseDriver + Zone string + MachineType string + DiskType string + Scopes string + DiskSize int + AuthTokenPath string + Project string } func init() { @@ -94,42 +84,14 @@ func GetCreateFlags() []cli.Flag { // NewDriver creates a Driver with the specified storePath. func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{ - MachineName: machineName, - storePath: storePath, - CaCertPath: caCert, - PrivateKeyPath: privateKey, - }, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "docker-user" diff --git a/drivers/hyperv/hyperv_windows.go b/drivers/hyperv/hyperv_windows.go index c64d594a7e..849cbb8ab2 100644 --- a/drivers/hyperv/hyperv_windows.go +++ b/drivers/hyperv/hyperv_windows.go @@ -18,22 +18,13 @@ import ( ) type Driver struct { - IPAddress string - SSHUser string - SSHPort int - storePath string + *drivers.BaseDriver boot2DockerURL string boot2DockerLoc string vSwitch string - MachineName string diskImage string diskSize int memSize int - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string } func init() { @@ -87,37 +78,14 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "docker" @@ -186,7 +154,7 @@ func (d *Driver) Create() error { if d.boot2DockerURL != "" { isoURL = d.boot2DockerURL log.Infof("Downloading boot2docker.iso from %s...", isoURL) - if err := b2dutils.DownloadISO(d.storePath, "boot2docker.iso", isoURL); err != nil { + if err := b2dutils.DownloadISO(d.ResolveStorePath("."), "boot2docker.iso", isoURL); err != nil { return err } } else { @@ -217,14 +185,14 @@ func (d *Driver) Create() error { } } - isoDest := filepath.Join(d.storePath, "boot2docker.iso") + isoDest := d.ResolveStorePath("boot2docker.iso") if err := utils.CopyFile(commonIsoPath, isoDest); err != nil { return err } } } else { - if err := utils.CopyFile(d.boot2DockerLoc, filepath.Join(d.storePath, "boot2docker.iso")); err != nil { + if err := utils.CopyFile(d.boot2DockerLoc, d.ResolveStorePath("boot2docker.iso")); err != nil { return err } } @@ -250,7 +218,7 @@ func (d *Driver) Create() error { command := []string{ "New-VM", "-Name", d.MachineName, - "-Path", fmt.Sprintf("'%s'", d.storePath), + "-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")), "-MemoryStartupBytes", fmt.Sprintf("%dMB", d.memSize)} _, err = execute(command) if err != nil { @@ -260,7 +228,7 @@ func (d *Driver) Create() error { command = []string{ "Set-VMDvdDrive", "-VMName", d.MachineName, - "-Path", fmt.Sprintf("'%s'", filepath.Join(d.storePath, "boot2docker.iso"))} + "-Path", fmt.Sprintf("'%s'", d.ResolveStorePath("boot2docker.iso"))} _, err = execute(command) if err != nil { return err @@ -444,8 +412,8 @@ func (d *Driver) generateDiskImage() error { // Create a small fixed vhd, put the tar in, // convert to dynamic, then resize - d.diskImage = filepath.Join(d.storePath, "disk.vhd") - fixed := filepath.Join(d.storePath, "fixed.vhd") + d.diskImage = d.ResolveStorePath("disk.vhd") + fixed := d.ResolveStorePath("fixed.vhd") log.Infof("Creating VHD") command := []string{ "New-VHD", diff --git a/drivers/none/none.go b/drivers/none/none.go index f816f456ff..94dd7e7efc 100644 --- a/drivers/none/none.go +++ b/drivers/none/none.go @@ -13,8 +13,8 @@ import ( // connect to existing Docker hosts by specifying the URL of the host as // an option. type Driver struct { - IPAddress string - URL string + *drivers.BaseDriver + URL string } func init() { @@ -35,21 +35,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{inner, ""}, nil } func (d *Driver) Create() error { return nil } -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - func (d *Driver) DriverName() string { return "none" } diff --git a/drivers/openstack/openstack.go b/drivers/openstack/openstack.go index 3bae763e9a..71becd1cb7 100644 --- a/drivers/openstack/openstack.go +++ b/drivers/openstack/openstack.go @@ -3,7 +3,6 @@ package openstack import ( "fmt" "io/ioutil" - "path/filepath" "strings" "time" @@ -16,6 +15,7 @@ import ( ) type Driver struct { + *drivers.BaseDriver AuthUrl string Insecure bool DomainID string @@ -27,7 +27,6 @@ type Driver struct { Region string AvailabilityZone string EndpointType string - MachineName string MachineId string FlavorName string FlavorId string @@ -39,15 +38,6 @@ type Driver struct { SecurityGroups []string FloatingIpPool string FloatingIpPoolId string - SSHUser string - SSHPort int - IPAddress string - CaCertPath string - PrivateKeyPath string - storePath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string client Client } @@ -189,51 +179,14 @@ func NewDriver(machineName string, storePath string, caCert string, privateKey s } func NewDerivedDriver(machineName string, storePath string, client Client, caCert string, privateKey string) (*Driver, error) { - return &Driver{ - MachineName: machineName, - storePath: storePath, - client: client, - CaCertPath: caCert, - PrivateKeyPath: privateKey, - }, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner, client: client}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - -func (d *Driver) GetSSHUsername() string { - if d.SSHUser == "" { - d.SSHUser = "root" - } - - return d.SSHUser -} - func (d *Driver) DriverName() string { return "openstack" } diff --git a/drivers/softlayer/driver.go b/drivers/softlayer/driver.go index 87d0a4de38..077b3911d6 100644 --- a/drivers/softlayer/driver.go +++ b/drivers/softlayer/driver.go @@ -4,7 +4,6 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" "regexp" "time" @@ -20,20 +19,11 @@ const ( ) type Driver struct { - storePath string - IPAddress string - deviceConfig *deviceConfig - Id int - Client *Client - SSHUser string - SSHPort int - MachineName string - CaCertPath string - PrivateKeyPath string - SSHKeyID int - SwarmMaster bool - SwarmHost string - SwarmDiscovery string + *drivers.BaseDriver + deviceConfig *deviceConfig + Id int + Client *Client + SSHKeyID int } type deviceConfig struct { @@ -59,45 +49,14 @@ func init() { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - -func (d *Driver) GetSSHUsername() string { - if d.SSHUser == "" { - d.SSHUser = "root" - } - - return d.SSHUser -} - func GetCreateFlags() []cli.Flag { // Set hourly billing to true by default since codegangsta cli doesn't take default bool values if os.Getenv("SOFTLAYER_HOURLY_BILLING") == "" { diff --git a/drivers/virtualbox/virtualbox.go b/drivers/virtualbox/virtualbox.go index 0b12303c1a..ce1e155d04 100644 --- a/drivers/virtualbox/virtualbox.go +++ b/drivers/virtualbox/virtualbox.go @@ -36,20 +36,11 @@ var ( ) type Driver struct { - IPAddress string + *drivers.BaseDriver CPU int - MachineName string - SSHUser string - SSHPort int Memory int DiskSize int Boot2DockerURL string - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - storePath string Boot2DockerImportVM string HostOnlyCIDR string } @@ -104,33 +95,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return "localhost", nil } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "docker" @@ -237,7 +209,7 @@ func (d *Driver) Create() error { } if err := vbm("createvm", - "--basefolder", d.storePath, + "--basefolder", d.ResolveStorePath("."), "--name", d.MachineName, "--register"); err != nil { return err @@ -304,7 +276,7 @@ func (d *Driver) Create() error { "--port", "0", "--device", "0", "--type", "dvddrive", - "--medium", filepath.Join(d.storePath, "boot2docker.iso")); err != nil { + "--medium", d.ResolveStorePath("boot2docker.iso")); err != nil { return err } @@ -553,7 +525,7 @@ func (d *Driver) publicSSHKeyPath() string { } func (d *Driver) diskPath() string { - return filepath.Join(d.storePath, "disk.vmdk") + return d.ResolveStorePath("disk.vmdk") } // Make a boot2docker VM disk image. diff --git a/drivers/vmwarefusion/fusion_darwin.go b/drivers/vmwarefusion/fusion_darwin.go index 09bab79e41..30cc20c252 100644 --- a/drivers/vmwarefusion/fusion_darwin.go +++ b/drivers/vmwarefusion/fusion_darwin.go @@ -9,8 +9,6 @@ import ( "fmt" "io/ioutil" "os" - "path" - "path/filepath" "regexp" "runtime" "strings" @@ -33,23 +31,13 @@ const ( // Driver for VMware Fusion type Driver struct { - MachineName string - IPAddress string + *drivers.BaseDriver Memory int DiskSize int CPU int ISO string Boot2DockerURL string - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string CPUS int - SSHUser string - SSHPort int - - storePath string } func init() { @@ -90,37 +78,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "docker" @@ -138,7 +103,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { d.CPU = flags.Int("vmwarefusion-cpu-count") d.DiskSize = flags.Int("vmwarefusion-disk-size") d.Boot2DockerURL = flags.String("vmwarefusion-boot2docker-url") - d.ISO = path.Join(d.storePath, isoFilename) + d.ISO = d.ResolveStorePath(isoFilename) d.SwarmMaster = flags.Bool("swarm-master") d.SwarmHost = flags.String("swarm-host") d.SwarmDiscovery = flags.String("swarm-discovery") @@ -207,7 +172,7 @@ func (d *Driver) Create() error { } log.Infof("Creating VM...") - if err := os.MkdirAll(d.storePath, 0755); err != nil { + if err := os.MkdirAll(d.ResolveStorePath("."), 0755); err != nil { return err } @@ -224,7 +189,7 @@ func (d *Driver) Create() error { vmxt.Execute(vmxfile, d) // Generate vmdk file - diskImg := filepath.Join(d.storePath, fmt.Sprintf("%s.vmdk", d.MachineName)) + diskImg := d.ResolveStorePath(fmt.Sprintf("%s.vmdk", d.MachineName)) if _, err := os.Stat(diskImg); err != nil { if !os.IsNotExist(err) { return err @@ -271,7 +236,7 @@ func (d *Driver) Create() error { vmrun("-gu", B2DUser, "-gp", B2DPass, "directoryExistsInGuest", d.vmxPath(), "/var/lib/boot2docker") // Copy SSH keys bundle - vmrun("-gu", B2DUser, "-gp", B2DPass, "CopyFileFromHostToGuest", d.vmxPath(), path.Join(d.storePath, "userdata.tar"), "/home/docker/userdata.tar") + vmrun("-gu", B2DUser, "-gp", B2DPass, "CopyFileFromHostToGuest", d.vmxPath(), d.ResolveStorePath("userdata.tar"), "/home/docker/userdata.tar") // Expand tar file. vmrun("-gu", B2DUser, "-gp", B2DPass, "runScriptInGuest", d.vmxPath(), "/bin/sh", "sudo /bin/mv /home/docker/userdata.tar /var/lib/boot2docker/userdata.tar && sudo tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/ > /var/log/userdata.log 2>&1 && sudo chown -R docker:staff /home/docker") @@ -360,11 +325,11 @@ func (d *Driver) Upgrade() error { } func (d *Driver) vmxPath() string { - return path.Join(d.storePath, fmt.Sprintf("%s.vmx", d.MachineName)) + return d.ResolveStorePath(fmt.Sprintf("%s.vmx", d.MachineName)) } func (d *Driver) vmdkPath() string { - return path.Join(d.storePath, fmt.Sprintf("%s.vmdk", d.MachineName)) + return d.ResolveStorePath(fmt.Sprintf("%s.vmdk", d.MachineName)) } func (d *Driver) getIPfromDHCPLease() (string, error) { @@ -459,7 +424,7 @@ func (d *Driver) generateKeyBundle() error { magicString := "boot2docker, this is vmware speaking" - tf, err := os.Create(path.Join(d.storePath, "userdata.tar")) + tf, err := os.Create(d.ResolveStorePath("userdata.tar")) if err != nil { return err } diff --git a/drivers/vmwarevcloudair/vcloudair.go b/drivers/vmwarevcloudair/vcloudair.go index c2604b3d36..3749246476 100644 --- a/drivers/vmwarevcloudair/vcloudair.go +++ b/drivers/vmwarevcloudair/vcloudair.go @@ -7,7 +7,6 @@ package vmwarevcloudair import ( "fmt" "io/ioutil" - "path/filepath" "strings" "github.com/vmware/govcloudair" @@ -21,30 +20,21 @@ import ( ) type Driver struct { - IPAddress string - UserName string - UserPassword string - ComputeID string - VDCID string - OrgVDCNet string - EdgeGateway string - PublicIP string - Catalog string - CatalogItem string - MachineName string - SSHUser string - SSHPort int - DockerPort int - Provision bool - CPUCount int - MemorySize int - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string - VAppID string - storePath string + *drivers.BaseDriver + UserName string + UserPassword string + ComputeID string + VDCID string + OrgVDCNet string + EdgeGateway string + PublicIP string + Catalog string + CatalogItem string + DockerPort int + Provision bool + CPUCount int + MemorySize int + VAppID string } func init() { @@ -141,46 +131,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - driver := &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey} - return driver, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - -func (d *Driver) GetSSHUsername() string { - if d.SSHUser == "" { - d.SSHUser = "root" - } - - return d.SSHUser -} - // Driver interface implementation func (d *Driver) DriverName() string { return "vmwarevcloudair" diff --git a/drivers/vmwarevsphere/vsphere.go b/drivers/vmwarevsphere/vsphere.go index a357ac69f2..829cd5e24e 100644 --- a/drivers/vmwarevsphere/vsphere.go +++ b/drivers/vmwarevsphere/vsphere.go @@ -33,10 +33,7 @@ const ( ) type Driver struct { - IPAddress string - MachineName string - SSHUser string - SSHPort int + *drivers.BaseDriver CPU int Memory int DiskSize int @@ -49,13 +46,7 @@ type Driver struct { Datacenter string Pool string HostIP string - storePath string ISO string - CaCertPath string - PrivateKeyPath string - SwarmMaster bool - SwarmHost string - SwarmDiscovery string } func init() { @@ -136,37 +127,14 @@ func GetCreateFlags() []cli.Flag { } func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { - return &Driver{MachineName: machineName, storePath: storePath, CaCertPath: caCert, PrivateKeyPath: privateKey}, nil -} - -func (d *Driver) AuthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) DeauthorizePort(ports []*drivers.Port) error { - return nil -} - -func (d *Driver) GetMachineName() string { - return d.MachineName + inner := drivers.NewBaseDriver(machineName, storePath, caCert, privateKey) + return &Driver{BaseDriver: inner}, nil } func (d *Driver) GetSSHHostname() (string, error) { return d.GetIP() } -func (d *Driver) GetSSHKeyPath() string { - return filepath.Join(d.storePath, "id_rsa") -} - -func (d *Driver) GetSSHPort() (int, error) { - if d.SSHPort == 0 { - d.SSHPort = 22 - } - - return d.SSHPort, nil -} - func (d *Driver) GetSSHUsername() string { if d.SSHUser == "" { d.SSHUser = "docker" @@ -306,7 +274,7 @@ func (d *Driver) Create() error { } // Copy SSH keys bundle - if err := vcConn.GuestUpload(B2DUser, B2DPass, path.Join(d.storePath, "userdata.tar"), "/home/docker/userdata.tar"); err != nil { + if err := vcConn.GuestUpload(B2DUser, B2DPass, d.ResolveStorePath("userdata.tar"), "/home/docker/userdata.tar"); err != nil { return err } @@ -454,7 +422,7 @@ func (d *Driver) generateKeyBundle() error { magicString := "boot2docker, this is vmware speaking" - tf, err := os.Create(path.Join(d.storePath, "userdata.tar")) + tf, err := os.Create(d.ResolveStorePath("userdata.tar")) if err != nil { return err }