This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from zeeyang/master
Create driver model and refactor virtualbox dependencies into driver
- Loading branch information
Showing
13 changed files
with
601 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package virtualbox | ||
package driver | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.