|
| 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 | +} |
0 commit comments