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

Commit 233e407

Browse files
committed
Add Fusion driver and vmx template
1 parent 0a7bcf3 commit 233e407

4 files changed

Lines changed: 308 additions & 0 deletions

File tree

cmds.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
_ "github.com/boot2docker/boot2docker-cli/dummy"
1414
_ "github.com/boot2docker/boot2docker-cli/virtualbox"
15+
_ "github.com/boot2docker/boot2docker-cli/vmware"
1516

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

vmware/machine.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
package vmware
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"text/template"
9+
10+
"github.com/boot2docker/boot2docker-cli/driver"
11+
"github.com/ogier/pflag"
12+
)
13+
14+
var (
15+
verbose bool // Verbose mode (Local copy of B2D.Verbose).
16+
cfg DriverCfg
17+
)
18+
19+
type DriverCfg struct {
20+
VMRUN string // Path to VBoxManage utility.
21+
VMDK string // base VMDK to use as persistent disk.
22+
}
23+
24+
func init() {
25+
if err := driver.Register("fusion", InitFunc); err != nil {
26+
fmt.Fprintf(os.Stderr, "Failed to initialize driver. Error : %s", err.Error())
27+
os.Exit(1)
28+
}
29+
if err := driver.RegisterConfig("fusion", ConfigFlags); err != nil {
30+
fmt.Fprintf(os.Stderr, "Failed to initialize driver config. Error : %s", err.Error())
31+
os.Exit(1)
32+
}
33+
}
34+
35+
// Initialize the Machine.
36+
func InitFunc(mc *driver.MachineConfig) (driver.Machine, error) {
37+
verbose = mc.Verbose
38+
39+
m, err := GetMachine(getVMX(mc))
40+
if err != nil && mc.Init == true {
41+
return CreateMachine(mc)
42+
}
43+
return m, err
44+
}
45+
46+
// Add cmdline params for this driver
47+
func ConfigFlags(mc *driver.MachineConfig, flags *pflag.FlagSet) error {
48+
cfg.VMRUN = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
49+
flags.StringVar(&cfg.VMRUN, "vmrun", cfg.VMRUN, "path to vmrun management utility.")
50+
51+
return nil
52+
}
53+
54+
// Machine information.
55+
type Machine struct {
56+
Name string
57+
State driver.MachineState
58+
CPUs uint
59+
Memory uint // main memory (in MB)
60+
VRAM uint // video memory (in MB)
61+
VMX string
62+
OSType string
63+
BootOrder []string // max 4 slots, each in {none|floppy|dvd|disk|net}
64+
DockerPort uint
65+
SSHPort uint
66+
}
67+
68+
// Refresh reloads the machine information.
69+
func (m *Machine) Refresh() error {
70+
mm, err := GetMachine(m.Name)
71+
mm.State = driver.Running
72+
if err != nil {
73+
return err
74+
}
75+
*m = *mm
76+
return nil
77+
}
78+
79+
// Start starts the machine.
80+
func (m *Machine) Start() error {
81+
m.State = driver.Running
82+
vmrun("start", m.VMX, "nogui")
83+
return nil
84+
}
85+
86+
// Suspend suspends the machine and saves its state to disk.
87+
func (m *Machine) Save() error {
88+
m.State = driver.Saved
89+
fmt.Printf("Save %s: %s\n", m.Name, m.State)
90+
return nil
91+
}
92+
93+
// Pause pauses the execution of the machine.
94+
func (m *Machine) Pause() error {
95+
m.State = driver.Paused
96+
fmt.Printf("Pause %s: %s\n", m.Name, m.State)
97+
return nil
98+
}
99+
100+
// Stop gracefully stops the machine.
101+
func (m *Machine) Stop() error {
102+
m.State = driver.Poweroff
103+
vmrun("stop", m.VMX, "nogui")
104+
return nil
105+
}
106+
107+
// Poweroff forcefully stops the machine. State is lost and might corrupt the disk image.
108+
func (m *Machine) Poweroff() error {
109+
m.State = driver.Poweroff
110+
fmt.Printf("Poweroff %s: %s\n", m.Name, m.State)
111+
return nil
112+
}
113+
114+
// Restart gracefully restarts the machine.
115+
func (m *Machine) Restart() error {
116+
m.State = driver.Running
117+
fmt.Printf("Restart %s: %s\n", m.Name, m.State)
118+
return nil
119+
}
120+
121+
// Reset forcefully restarts the machine. State is lost and might corrupt the disk image.
122+
func (m *Machine) Reset() error {
123+
m.State = driver.Running
124+
fmt.Printf("Reset %s: %s\n", m.Name, m.State)
125+
return nil
126+
}
127+
128+
// Get vm name
129+
func (m *Machine) GetName() string {
130+
return m.Name
131+
}
132+
133+
// Get current state
134+
func (m *Machine) GetState() driver.MachineState {
135+
return m.State
136+
}
137+
138+
// Get serial file
139+
func (m *Machine) GetSerialFile() string {
140+
return ""
141+
}
142+
143+
// Get Docker port
144+
func (m *Machine) GetDockerPort() uint {
145+
return m.DockerPort
146+
}
147+
148+
// Get SSH port
149+
func (m *Machine) GetSSHPort() uint {
150+
return m.SSHPort
151+
}
152+
153+
// Delete deletes the machine and associated disk images.
154+
func (m *Machine) Delete() error {
155+
fmt.Printf("Delete %s: %s\n", m.Name, m.State)
156+
return nil
157+
}
158+
159+
// Modify changes the settings of the machine.
160+
func (m *Machine) Modify() error {
161+
fmt.Printf("Modify %s: %s\n", m.Name, m.State)
162+
return m.Refresh()
163+
}
164+
165+
// AddNATPF adds a NAT port forarding rule to the n-th NIC with the given name.
166+
func (m *Machine) AddNATPF(n int, name string, rule driver.PFRule) error {
167+
fmt.Println("Add NAT PF")
168+
return nil
169+
}
170+
171+
// DelNATPF deletes the NAT port forwarding rule with the given name from the n-th NIC.
172+
func (m *Machine) DelNATPF(n int, name string) error {
173+
fmt.Println("Del NAT PF")
174+
return nil
175+
}
176+
177+
// SetNIC set the n-th NIC.
178+
func (m *Machine) SetNIC(n int, nic driver.NIC) error {
179+
fmt.Println("Set NIC")
180+
return nil
181+
}
182+
183+
// AddStorageCtl adds a storage controller with the given name.
184+
func (m *Machine) AddStorageCtl(name string, ctl driver.StorageController) error {
185+
fmt.Println("Add storage ctl")
186+
return nil
187+
}
188+
189+
// DelStorageCtl deletes the storage controller with the given name.
190+
func (m *Machine) DelStorageCtl(name string) error {
191+
fmt.Println("Del storage ctl")
192+
return nil
193+
}
194+
195+
// AttachStorage attaches a storage medium to the named storage controller.
196+
func (m *Machine) AttachStorage(ctlName string, medium driver.StorageMedium) error {
197+
fmt.Println("Attach storage")
198+
return nil
199+
}
200+
201+
// GetMachine finds a machine.
202+
func GetMachine(vmx string) (*Machine, error) {
203+
if _, err := os.Stat(vmx); os.IsNotExist(err) {
204+
return nil, ErrMachineNotExist
205+
}
206+
m := &Machine{VMX: vmx}
207+
return m, nil
208+
}
209+
210+
// CreateMachine creates a new virtual machine.
211+
func CreateMachine(mc *driver.MachineConfig) (*Machine, error) {
212+
if err := os.MkdirAll(getBaseFolder(mc), 0755); err != nil {
213+
return nil, err
214+
}
215+
216+
if _, err := os.Stat(getVMX(mc)); err == nil {
217+
return nil, ErrMachineExist
218+
}
219+
220+
vmxt := template.Must(template.New("vmx").Parse(vmx))
221+
vmxfile, err := os.Create(getVMX(mc))
222+
if err != nil {
223+
return nil, err
224+
}
225+
vmxt.Execute(vmxfile, mc)
226+
return nil, nil
227+
}
228+
229+
func getBaseFolder(mc *driver.MachineConfig) string {
230+
return filepath.Join(mc.Dir, mc.VM)
231+
}
232+
func getVMX(mc *driver.MachineConfig) string {
233+
return filepath.Join(getBaseFolder(mc), strings.Join([]string{mc.VM, "vmx"}, "."))
234+
}

vmware/vmrun.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package vmware
2+
3+
import (
4+
"errors"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"strings"
9+
)
10+
11+
var (
12+
ErrMachineExist = errors.New("machine already exists")
13+
ErrMachineNotExist = errors.New("machine does not exist")
14+
ErrVMRUNNotFound = errors.New("VMRUN not found")
15+
)
16+
17+
func vmrun(args ...string) error {
18+
cmd := exec.Command(cfg.VMRUN, args...)
19+
if verbose {
20+
cmd.Stdout = os.Stdout
21+
cmd.Stderr = os.Stderr
22+
log.Printf("executing: %v %v", cfg.VMRUN, strings.Join(args, " "))
23+
}
24+
if stdout := cmd.Run(); stdout != nil {
25+
if ee, ok := stdout.(*exec.Error); ok && ee == exec.ErrNotFound {
26+
return ErrVMRUNNotFound
27+
}
28+
}
29+
return nil
30+
}

vmware/vmx.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package vmware
2+
3+
const vmx = `
4+
.encoding = "UTF-8"
5+
bios.bootOrder = "CDROM"
6+
config.version = "8"
7+
displayName = "{{.VM}}"
8+
ethernet0.addressType = "static"
9+
ethernet0.connectionType = "nat"
10+
ethernet0.address = "00:0c:29:2c:fe:e5"
11+
ethernet0.linkStatePropagation.enable = "FALSE"
12+
ethernet0.pciSlotNumber = "33"
13+
ethernet0.present = "TRUE"
14+
ethernet0.virtualDev = "e1000"
15+
ethernet0.wakeOnPcktRcv = "FALSE"
16+
ethernet1.addressType = "static"
17+
ethernet1.connectionType = "hostonly"
18+
ethernet1.address = "00:0c:29:2c:fe:ef"
19+
ethernet1.pciSlotNumber = "34"
20+
ethernet1.present = "TRUE"
21+
ethernet1.virtualDev = "e1000"
22+
ethernet1.wakeOnPcktRcv = "FALSE"
23+
floppy0.present = "FALSE"
24+
guestOS = "other26xlinux-64"
25+
hpet0.present = "TRUE"
26+
ide1:0.deviceType = "cdrom-image"
27+
ide1:0.fileName = "{{.ISO}}"
28+
ide1:0.present = "TRUE"
29+
mem.hotadd = "TRUE"
30+
memsize = "1024"
31+
powerType.powerOff = "soft"
32+
powerType.powerOn = "soft"
33+
powerType.reset = "soft"
34+
powerType.suspend = "soft"
35+
sata0.present = "TRUE"
36+
sata0:0.fileName = "{{.VMDK}}"
37+
sata0:0.present = "TRUE"
38+
scsi0.pciSlotNumber = "16"
39+
scsi0.present = "FALSE"
40+
scsi0.virtualDev = "lsilogic"
41+
scsi0:0.present = "FALSE"
42+
virtualHW.version = "10"
43+
`

0 commit comments

Comments
 (0)