Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
Fix up some issues around the new model
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
  • Loading branch information
nathanleclaire committed Aug 21, 2015
1 parent e4e442e commit 7dbd979
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 39 deletions.
13 changes: 12 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/drivermaker"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
Expand All @@ -23,6 +24,10 @@ var (
)

func cmdCreate(c *cli.Context) {
var (
driver drivers.Driver
)

driverName := c.String("driver")
name := c.Args().First()
certInfo := getCertPathInfoFromContext(c)
Expand Down Expand Up @@ -82,10 +87,16 @@ func cmdCreate(c *cli.Context) {
},
}

h, err := store.NewHost(driverName, name)
driver, err := drivermaker.NewDriver(driverName, name, c.GlobalString("storage-path"))
if err != nil {
log.Fatalf("Error trying to get driver: %s", err)
}

h, err := store.NewHost(driver)
if err != nil {
log.Fatalf("Error getting new host: %s", err)
}

h.HostOptions = hostOptions

// TODO: This should be moved out of the driver and done in the
Expand Down
10 changes: 5 additions & 5 deletions libmachine/drivers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type BaseDriver struct {
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
StorePath string
ArtifactPath string
}

// GetSSHKeyPath -
func (d *BaseDriver) GetSSHKeyPath() string {
return filepath.Join(d.StorePath, "machines", d.MachineName, "id_rsa")
return filepath.Join(d.ArtifactPath, "machines", d.MachineName, "id_rsa")
}

// AuthorizePort -
Expand All @@ -40,9 +40,9 @@ func (d *BaseDriver) GetMachineName() string {
return d.MachineName
}

// ResolveStorePath -
func (d *BaseDriver) ResolveStorePath(file string) string {
return filepath.Join(d.StorePath, "machines", d.MachineName, file)
// GetLocalArtifactPath -
func (d *BaseDriver) LocalArtifactPath(file string) string {
return filepath.Join(d.ArtifactPath, "machines", d.MachineName, file)
}

// GetSSHPort -
Expand Down
26 changes: 26 additions & 0 deletions libmachine/drivers/drivermaker/maker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package drivermaker

import (
"fmt"

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/none"
"github.com/docker/machine/libmachine/drivers/virtualbox"
)

func NewDriver(driverName, hostName, artifactPath string) (drivers.Driver, error) {
var (
driver drivers.Driver
)

switch driverName {
case "virtualbox":
driver = virtualbox.NewDriver(hostName, artifactPath)
case "none":
driver = &none.Driver{}
default:
return nil, fmt.Errorf("Driver %s not recognized", driverName)
}

return driver, nil
}
15 changes: 12 additions & 3 deletions libmachine/drivers/virtualbox/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func init() {
})
}

func NewDriver(hostName, artifactPath string) *Driver {
return &Driver{
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
ArtifactPath: artifactPath,
},
}
}

// RegisterCreateFlags registers the flags this driver adds to
// "docker hosts create"
func GetCreateFlags() []cli.Flag {
Expand Down Expand Up @@ -227,7 +236,7 @@ func (d *Driver) Create() error {
}

if err := vbm("createvm",
"--basefolder", d.ResolveStorePath("."),
"--basefolder", d.LocalArtifactPath("."),
"--name", d.MachineName,
"--register"); err != nil {
return err
Expand Down Expand Up @@ -293,7 +302,7 @@ func (d *Driver) Create() error {
"--port", "0",
"--device", "0",
"--type", "dvddrive",
"--medium", d.ResolveStorePath("boot2docker.iso")); err != nil {
"--medium", d.LocalArtifactPath("boot2docker.iso")); err != nil {
return err
}

Expand Down Expand Up @@ -539,7 +548,7 @@ func (d *Driver) publicSSHKeyPath() string {
}

func (d *Driver) diskPath() string {
return d.ResolveStorePath("disk.vmdk")
return d.LocalArtifactPath("disk.vmdk")
}

// Make a boot2docker VM disk image.
Expand Down
48 changes: 28 additions & 20 deletions libmachine/examples/vbox_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,46 @@ package main
// Sample Virtualbox create independent of Machine CLI.
import (
"fmt"
"log"

"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers/virtualbox"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
)

func main() {
// returns the familiar store at $HOME/.docker/machine
store, _ := libmachine.GetDefaultStore()

h := &host.Host{
Name: "mycoolhost",
HostOptions: &host.HostOptions{
EngineOptions: &engine.EngineOptions{
StorageDriver: "overlay",
},
},
Driver: &virtualbox.Driver{
CPU: 2,
Memory: 2048,
},
store := libmachine.GetDefaultStore()

// over-ride this for now (don't want to muck with my default store)
store.Path = "/tmp/automatic"

hostName := "myfunhost"

// Set some options on the provider...
driver := virtualbox.NewDriver(hostName, "/tmp/automatic")
driver.CPU = 2
driver.Memory = 2048

h, err := store.NewHost(driver)
if err != nil {
log.Fatal(err)
}

// This method is a very high-level method which includes host creation,
// saving the newly created host in the store, and provisioning.
h, _ = libmachine.Create(store, h)
h.HostOptions.EngineOptions.StorageDriver = "overlay"

out, _ := h.RunSSHCommand("df -h")
if err := libmachine.Create(store, h); err != nil {
log.Fatal(err)
}

out, err := h.RunSSHCommand("df -h")
if err != nil {
log.Fatal(err)
}

fmt.Printf("Results of your disk space query:\n%s\n", out)

fmt.Println("Powering down machine now...")
_ := h.Stop()
if err := h.Stop(); err != nil {
log.Fatal(err)
}
}
14 changes: 5 additions & 9 deletions libmachine/persist/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers/drivermaker"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
Expand Down Expand Up @@ -174,13 +174,9 @@ func (s Filestore) GetProvider(name string) (provider.Provider, error) {
return nil, nil
}

func (s Filestore) NewHost(driverName, name string) (*host.Host, error) {
driver, err := drivermaker.NewDriver(driverName, name, s.Path)
if err != nil {
return nil, fmt.Errorf("Error attempting to get new driver: %s", err)
}

func (s Filestore) NewHost(driver drivers.Driver) (*host.Host, error) {
certPath := filepath.Join(s.Path, "certs")
name := driver.DriverName()

hostOptions := &host.HostOptions{
AuthOptions: &auth.AuthOptions{
Expand All @@ -200,9 +196,9 @@ func (s Filestore) NewHost(driverName, name string) (*host.Host, error) {

return &host.Host{
ConfigVersion: version.ConfigVersion,
Name: name,
Name: driver.GetMachineName(),
Driver: driver,
DriverName: driver.DriverName(),
DriverName: name,
HostOptions: hostOptions,
}, nil
}
3 changes: 2 additions & 1 deletion libmachine/persist/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package persist

import (
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/provider"
)
Expand All @@ -10,7 +11,7 @@ type Store interface {
Exists(name string) (bool, error)

// NewHost will initialize a new host machine
NewHost(driverName, name string) (*host.Host, error)
NewHost(driver drivers.Driver) (*host.Host, error)

// GetActive returns the active host
GetActive() (*host.Host, error)
Expand Down

0 comments on commit 7dbd979

Please sign in to comment.