Skip to content
This repository has been archived by the owner on Feb 27, 2018. It is now read-only.

Commit

Permalink
Merge pull request #184 from zeeyang/master
Browse files Browse the repository at this point in the history
Create driver model and refactor virtualbox dependencies into driver
  • Loading branch information
Sven Dowideit authored and Sven Dowideit committed Jul 18, 2014
2 parents b19e4eb + 360ad9a commit 711cc7d
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 430 deletions.
259 changes: 31 additions & 228 deletions cmds.go

Large diffs are not rendered by default.

45 changes: 4 additions & 41 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,14 @@ import (
"runtime"

toml "github.com/BurntSushi/toml"
vbx "github.com/boot2docker/boot2docker-cli/virtualbox"
"github.com/boot2docker/boot2docker-cli/driver"
flag "github.com/ogier/pflag"
)

// boot2docker config.
var B2D struct {
// NOTE: separate sections with blank lines so gofmt doesn't change
// indentation all the time.

// Gereral flags.
Verbose bool
VBM string

// basic config
SSH string // SSH client executable
SSHGen string // SSH keygen executable
SSHKey string // SSH key to send to the vm
VM string // virtual machine name
Dir string // boot2docker directory
ISO string // boot2docker ISO image path
VMDK string // base VMDK to use as persistent disk
DiskSize uint // VM disk image size (MB)
Memory uint // VM memory size (MB)

// NAT network: port forwarding
SSHPort uint16 // host SSH port (forward to port 22 in VM)
DockerPort uint16 // host Docker port (forward to port 2375 in VM)

// host-only network
HostIP net.IP
DHCPIP net.IP
NetMask net.IPMask
LowerIP net.IP
UpperIP net.IP
DHCPEnabled bool

// Serial console pipe/socket
Serial bool
SerialFile string
}

var (
// Pattern to parse a key=value line in config profile.
reFlagLine = regexp.MustCompile(`^\s*(\w+)\s*=\s*([^#;]+)`)
B2D = driver.MachineConfig{}
)

func getCfgDir(name string) (string, error) {
Expand Down Expand Up @@ -124,8 +88,10 @@ func config() (*flag.FlagSet, error) {
}
vbm = filepath.Join(p, "VBoxManage.exe")
}
flags.BoolVarP(&B2D.Init, "init", "i", false, "auto initialize vm instance.")
flags.StringVar(&B2D.VBM, "vbm", vbm, "path to VirtualBox management utility.")
flags.BoolVarP(&B2D.Verbose, "verbose", "v", false, "display verbose command invocations.")
flags.StringVar(&B2D.Driver, "driver", "virtualbox", "hypervisor driver.")
flags.StringVar(&B2D.SSH, "ssh", "ssh", "path to SSH client utility.")
flags.StringVar(&B2D.SSHGen, "ssh-keygen", "ssh-keygen", "path to ssh-keygen utility.")

Expand Down Expand Up @@ -174,9 +140,6 @@ func config() (*flag.FlagSet, error) {
return nil, err
}

vbx.Verbose = B2D.Verbose
vbx.VBM = B2D.VBM

if B2D.SerialFile == "" {
if runtime.GOOS == "windows" {
//SerialFile ~~ filepath.Join(dir, B2D.vm+".sock")
Expand Down
39 changes: 39 additions & 0 deletions driver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package driver

import "net"

// Machine config.
type MachineConfig struct {
// Gereral flags.
Init bool
Verbose bool
VBM string
Driver string

// basic config
SSH string // SSH client executable
SSHGen string // SSH keygen executable
SSHKey string // SSH key to send to the vm
VM string // virtual machine name
Dir string // boot2docker directory
ISO string // boot2docker ISO image path
VMDK string // base VMDK to use as persistent disk
DiskSize uint // VM disk image size (MB)
Memory uint // VM memory size (MB)

// NAT network: port forwarding
SSHPort uint16 // host SSH port (forward to port 22 in VM)
DockerPort uint16 // host Docker port (forward to port 2375 in VM)

// host-only network
HostIP net.IP
DHCPIP net.IP
NetMask net.IPMask
LowerIP net.IP
UpperIP net.IP
DHCPEnabled bool

// Serial console pipe/socket
Serial bool
SerialFile string
}
12 changes: 12 additions & 0 deletions driver/dhcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package driver

import "net"

// DHCP server info.
type DHCP struct {
NetworkName string
IPv4 net.IPNet
LowerIP net.IP
UpperIP net.IP
Enabled bool
}
76 changes: 76 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package driver

import (
"errors"
"fmt"
)

type InitFunc func(i *MachineConfig) (Machine, error)

type MachineState string

const (
// Known ports
SSHPort = 22
DockerPort = 2375

// VM states
Poweroff = MachineState("poweroff")
Running = MachineState("running")
Paused = MachineState("paused")
Saved = MachineState("saved")
Aborted = MachineState("aborted")
)

// Machine represents a virtual machine instance
type Machine interface {
Start() error
Save() error
Pause() error
Stop() error
Refresh() error
Poweroff() error
Restart() error
Reset() error
Delete() error
Modify() error
AddNATPF(n int, name string, rule PFRule) error
DelNATPF(n int, name string) error
SetNIC(n int, nic NIC) error
AddStorageCtl(name string, ctl StorageController) error
DelStorageCtl(name string) error
AttachStorage(ctlName string, medium StorageMedium) error
GetState() MachineState
GetSerialFile() string
GetDockerPort() uint
GetSSHPort() uint
}

var (
// All registred machines
machines map[string]InitFunc

ErrNotSupported = errors.New("machine not supported")
ErrMachineNotExist = errors.New("machine not exist")
ErrPrerequisites = errors.New("prerequisites for machine not satisfied (hypervisor installed?)")
)

func init() {
machines = make(map[string]InitFunc)
}

func Register(driver string, initFunc InitFunc) error {
if _, exists := machines[driver]; exists {
return fmt.Errorf("Driver already registered %s", driver)
}
machines[driver] = initFunc

return nil
}

func GetMachine(mc *MachineConfig) (Machine, error) {
if initFunc, exists := machines[mc.Driver]; exists {
return initFunc(mc)
}
return nil, ErrNotSupported
}
2 changes: 1 addition & 1 deletion virtualbox/nic.go → driver/nic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package driver

// NIC represents a virtualized network interface card.
type NIC struct {
Expand Down
2 changes: 1 addition & 1 deletion virtualbox/pfrule.go → driver/pfrule.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package driver

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion virtualbox/storage.go → driver/storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package driver

// StorageController represents a virtualized storage controller.
type StorageController struct {
Expand Down
157 changes: 157 additions & 0 deletions dummy/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package dummy

import (
"fmt"

"github.com/boot2docker/boot2docker-cli/driver"
)

func init() {
driver.Register("dummy", InitFunc)
}

// Initialize the Machine.
func InitFunc(i *driver.MachineConfig) (driver.Machine, error) {
fmt.Printf("Init dummy %s\n", i.VM)
return &Machine{Name: i.VM, State: driver.Poweroff}, nil
}

// Machine information.
type Machine struct {
Name string
UUID string
State driver.MachineState
CPUs uint
Memory uint // main memory (in MB)
VRAM uint // video memory (in MB)
CfgFile string
BaseFolder string
OSType string
BootOrder []string // max 4 slots, each in {none|floppy|dvd|disk|net}
DockerPort uint
SSHPort uint
SerialFile string
}

// Refresh reloads the machine information.
func (m *Machine) Refresh() error {
fmt.Printf("Refresh %s: %s\n", m.Name, m.State)
return nil
}

// Start starts the machine.
func (m *Machine) Start() error {
m.State = driver.Running
fmt.Printf("Start %s: %s\n", m.Name, m.State)
return nil
}

// Suspend suspends the machine and saves its state to disk.
func (m *Machine) Save() error {
m.State = driver.Saved
fmt.Printf("Save %s: %s\n", m.Name, m.State)
return nil
}

// Pause pauses the execution of the machine.
func (m *Machine) Pause() error {
m.State = driver.Paused
fmt.Printf("Pause %s: %s\n", m.Name, m.State)
return nil
}

// Stop gracefully stops the machine.
func (m *Machine) Stop() error {
m.State = driver.Poweroff
fmt.Printf("Stop %s: %s\n", m.Name, m.State)
return nil
}

// Poweroff forcefully stops the machine. State is lost and might corrupt the disk image.
func (m *Machine) Poweroff() error {
m.State = driver.Poweroff
fmt.Printf("Poweroff %s: %s\n", m.Name, m.State)
return nil
}

// Restart gracefully restarts the machine.
func (m *Machine) Restart() error {
m.State = driver.Running
fmt.Printf("Restart %s: %s\n", m.Name, m.State)
return nil
}

// Reset forcefully restarts the machine. State is lost and might corrupt the disk image.
func (m *Machine) Reset() error {
m.State = driver.Running
fmt.Printf("Reset %s: %s\n", m.Name, m.State)
return nil
}

// Get current state
func (m *Machine) GetState() driver.MachineState {
return m.State
}

// Get serial file
func (m *Machine) GetSerialFile() string {
return m.SerialFile
}

// Get Docker port
func (m *Machine) GetDockerPort() uint {
return m.DockerPort
}

// Get SSH port
func (m *Machine) GetSSHPort() uint {
return m.SSHPort
}

// Delete deletes the machine and associated disk images.
func (m *Machine) Delete() error {
fmt.Printf("Delete %s: %s\n", m.Name, m.State)
return nil
}

// Modify changes the settings of the machine.
func (m *Machine) Modify() error {
fmt.Printf("Modify %s: %s\n", m.Name, m.State)
return m.Refresh()
}

// AddNATPF adds a NAT port forarding rule to the n-th NIC with the given name.
func (m *Machine) AddNATPF(n int, name string, rule driver.PFRule) error {
fmt.Println("Add NAT PF")
return nil
}

// DelNATPF deletes the NAT port forwarding rule with the given name from the n-th NIC.
func (m *Machine) DelNATPF(n int, name string) error {
fmt.Println("Del NAT PF")
return nil
}

// SetNIC set the n-th NIC.
func (m *Machine) SetNIC(n int, nic driver.NIC) error {
fmt.Println("Set NIC")
return nil
}

// AddStorageCtl adds a storage controller with the given name.
func (m *Machine) AddStorageCtl(name string, ctl driver.StorageController) error {
fmt.Println("Add storage ctl")
return nil
}

// DelStorageCtl deletes the storage controller with the given name.
func (m *Machine) DelStorageCtl(name string) error {
fmt.Println("Del storage ctl")
return nil
}

// AttachStorage attaches a storage medium to the named storage controller.
func (m *Machine) AttachStorage(ctlName string, medium driver.StorageMedium) error {
fmt.Println("Attach storage")
return nil
}
Loading

0 comments on commit 711cc7d

Please sign in to comment.