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

Commit 7fb5cbd

Browse files
committed
Add vm control commands and get hostname
1 parent 1dfab34 commit 7fb5cbd

8 files changed

Lines changed: 87 additions & 43 deletions

File tree

cmds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212

1313
_ "github.com/boot2docker/boot2docker-cli/dummy"
14+
_ "github.com/boot2docker/boot2docker-cli/fusion"
1415
_ "github.com/boot2docker/boot2docker-cli/virtualbox"
15-
_ "github.com/boot2docker/boot2docker-cli/vmware"
1616

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

driver/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Machine interface {
4242
AttachStorage(ctlName string, medium StorageMedium) error
4343
GetState() MachineState
4444
GetName() string
45+
GetHostname() string
4546
GetSerialFile() string
4647
GetDockerPort() uint
4748
GetSSHPort() uint

dummy/machine.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ func ConfigFlags(B2D *driver.MachineConfig, flags *flag.FlagSet) error {
4747

4848
// Machine information.
4949
type Machine struct {
50-
Name string
5150
UUID string
51+
Name string
52+
Hostname string
5253
State driver.MachineState
5354
CPUs uint
5455
Memory uint // main memory (in MB)
@@ -122,6 +123,11 @@ func (m *Machine) GetName() string {
122123
return m.Name
123124
}
124125

126+
// Get machine hostname
127+
func (m *Machine) GetHostname() string {
128+
return m.Hostname
129+
}
130+
125131
// Get current state
126132
func (m *Machine) GetState() driver.MachineState {
127133
return m.State
Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package vmware
1+
package fusion
22

33
import (
4+
"bufio"
45
"fmt"
56
"os"
67
"path/filepath"
8+
"strconv"
9+
"strings"
710
"text/template"
811

912
"github.com/boot2docker/boot2docker-cli/driver"
@@ -55,22 +58,17 @@ func ConfigFlags(mc *driver.MachineConfig, flags *pflag.FlagSet) error {
5558

5659
// Machine information.
5760
type Machine struct {
58-
Name string
59-
State driver.MachineState
60-
CPUs uint
61-
Memory uint // main memory (in MB)
62-
VRAM uint // video memory (in MB)
63-
VMX string
64-
OSType string
65-
BootOrder []string // max 4 slots, each in {none|floppy|dvd|disk|net}
66-
DockerPort uint
67-
SSHPort uint
61+
Name string
62+
State driver.MachineState
63+
CPUs uint64
64+
Memory uint64 // main memory (in MB)
65+
VMX string
66+
OSType string
6867
}
6968

7069
// Refresh reloads the machine information.
7170
func (m *Machine) Refresh() error {
7271
mm, err := GetMachine(m.VMX)
73-
mm.State = driver.Running
7472
if err != nil {
7573
return err
7674
}
@@ -80,50 +78,43 @@ func (m *Machine) Refresh() error {
8078

8179
// Start starts the machine.
8280
func (m *Machine) Start() error {
83-
m.State = driver.Running
8481
vmrun("start", m.VMX, "nogui")
8582
return nil
8683
}
8784

8885
// Suspend suspends the machine and saves its state to disk.
8986
func (m *Machine) Save() error {
90-
m.State = driver.Saved
91-
fmt.Printf("Save %s: %s\n", m.Name, m.State)
87+
vmrun("suspend", m.VMX, "nogui")
9288
return nil
9389
}
9490

9591
// Pause pauses the execution of the machine.
9692
func (m *Machine) Pause() error {
97-
m.State = driver.Paused
98-
fmt.Printf("Pause %s: %s\n", m.Name, m.State)
93+
vmrun("pause", m.VMX, "nogui")
9994
return nil
10095
}
10196

10297
// Stop gracefully stops the machine.
10398
func (m *Machine) Stop() error {
104-
m.State = driver.Poweroff
10599
vmrun("stop", m.VMX, "nogui")
106100
return nil
107101
}
108102

109103
// Poweroff forcefully stops the machine. State is lost and might corrupt the disk image.
110104
func (m *Machine) Poweroff() error {
111-
m.State = driver.Poweroff
112-
fmt.Printf("Poweroff %s: %s\n", m.Name, m.State)
105+
vmrun("stop", m.VMX, "nogui")
113106
return nil
114107
}
115108

116109
// Restart gracefully restarts the machine.
117110
func (m *Machine) Restart() error {
118-
m.State = driver.Running
119-
fmt.Printf("Restart %s: %s\n", m.Name, m.State)
111+
vmrun("reset", m.VMX, "nogui")
120112
return nil
121113
}
122114

123115
// Reset forcefully restarts the machine. State is lost and might corrupt the disk image.
124116
func (m *Machine) Reset() error {
125-
m.State = driver.Running
126-
fmt.Printf("Reset %s: %s\n", m.Name, m.State)
117+
vmrun("reset", m.VMX, "nogui")
127118
return nil
128119
}
129120

@@ -132,6 +123,12 @@ func (m *Machine) GetName() string {
132123
return m.Name
133124
}
134125

126+
// Get vm hostname
127+
func (m *Machine) GetHostname() string {
128+
stdout, _, _ := vmrun("getGuestIPAddress", m.VMX)
129+
return strings.TrimSpace(stdout)
130+
}
131+
135132
// Get current state
136133
func (m *Machine) GetState() driver.MachineState {
137134
return m.State
@@ -144,23 +141,23 @@ func (m *Machine) GetSerialFile() string {
144141

145142
// Get Docker port
146143
func (m *Machine) GetDockerPort() uint {
147-
return m.DockerPort
144+
return 2375
148145
}
149146

150147
// Get SSH port
151148
func (m *Machine) GetSSHPort() uint {
152-
return m.SSHPort
149+
return 22
153150
}
154151

155152
// Delete deletes the machine and associated disk images.
156153
func (m *Machine) Delete() error {
157-
fmt.Printf("Delete %s: %s\n", m.Name, m.State)
154+
vmrun("deleteVM", m.VMX, "nogui")
158155
return nil
159156
}
160157

161158
// Modify changes the settings of the machine.
162159
func (m *Machine) Modify() error {
163-
fmt.Printf("Modify %s: %s\n", m.Name, m.State)
160+
fmt.Printf("Hot modify not supported")
164161
return m.Refresh()
165162
}
166163

@@ -205,7 +202,38 @@ func GetMachine(vmx string) (*Machine, error) {
205202
if _, err := os.Stat(vmx); os.IsNotExist(err) {
206203
return nil, ErrMachineNotExist
207204
}
208-
m := &Machine{VMX: vmx}
205+
206+
m := &Machine{VMX: vmx, State: driver.Poweroff}
207+
208+
// VMRUN only tells use if the vm is running or not
209+
if stdout, _, _ := vmrun("list"); strings.Contains(stdout, m.VMX) {
210+
m.State = driver.Running
211+
}
212+
213+
// Parse the vmx file
214+
vmxfile, err := os.Open(vmx)
215+
if err != nil {
216+
return m, err
217+
}
218+
defer vmxfile.Close()
219+
220+
vmxscan := bufio.NewScanner(vmxfile)
221+
for vmxscan.Scan() {
222+
if vmxtokens := strings.Split(vmxscan.Text(), " = "); len(vmxtokens) > 1 {
223+
vmxkey := strings.TrimSpace(vmxtokens[0])
224+
vmxvalue, _ := strconv.Unquote(vmxtokens[1])
225+
switch vmxkey {
226+
case "displayName":
227+
m.Name = vmxvalue
228+
case "guestOS":
229+
m.OSType = vmxvalue
230+
case "memsize":
231+
m.Memory, _ = strconv.ParseUint(vmxvalue, 10, 0)
232+
case "numvcpus":
233+
m.CPUs, _ = strconv.ParseUint(vmxvalue, 10, 0)
234+
}
235+
}
236+
}
209237
return m, nil
210238
}
211239

vmware/vmrun.go renamed to fusion/vmrun.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package vmware
1+
package fusion
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"log"
7-
"os"
88
"os/exec"
99
"strings"
1010
)
@@ -15,19 +15,24 @@ var (
1515
ErrVMRUNNotFound = errors.New("VMRUN not found")
1616
)
1717

18-
func vmrun(args ...string) error {
18+
func vmrun(args ...string) (string, string, error) {
1919
cmd := exec.Command(cfg.VMRUN, args...)
2020
if verbose {
21-
cmd.Stdout = os.Stdout
22-
cmd.Stderr = os.Stderr
2321
log.Printf("executing: %v %v", cfg.VMRUN, strings.Join(args, " "))
2422
}
25-
if stdout := cmd.Run(); stdout != nil {
26-
if ee, ok := stdout.(*exec.Error); ok && ee == exec.ErrNotFound {
27-
return ErrVMRUNNotFound
23+
24+
var stdout bytes.Buffer
25+
var stderr bytes.Buffer
26+
cmd.Stdout, cmd.Stderr = &stdout, &stderr
27+
28+
err := cmd.Run()
29+
if err != nil {
30+
if ee, ok := err.(*exec.Error); ok && ee == exec.ErrNotFound {
31+
err = ErrVMRUNNotFound
2832
}
2933
}
30-
return nil
34+
35+
return stdout.String(), stderr.String(), err
3136
}
3237

3338
// Make a vmdk disk image with the given size (in MB).

vmware/vmx.go renamed to fusion/vmx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package vmware
1+
package fusion
22

33
const vmx = `
44
.encoding = "UTF-8"

util.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,14 @@ func reader(r io.Reader) {
168168
}
169169

170170
func getSSHCommand(m driver.Machine, args ...string) *exec.Cmd {
171-
172171
DefaultSSHArgs := []string{
173172
"-o", "IdentitiesOnly=yes",
174173
"-o", "StrictHostKeyChecking=no",
175174
"-o", "UserKnownHostsFile=/dev/null",
176175
"-o", "LogLevel=quiet", // suppress "Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts."
177176
"-p", fmt.Sprintf("%d", m.GetSSHPort()),
178177
"-i", B2D.SSHKey,
179-
"docker@localhost",
178+
fmt.Sprintf("docker@%s", m.GetHostname()),
180179
}
181180

182181
sshArgs := append(DefaultSSHArgs, args...)

virtualbox/machine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ func (m *Machine) GetName() string {
305305
return m.Name
306306
}
307307

308+
// Get machine hostname
309+
func (m *Machine) GetHostname() string {
310+
return "localhost"
311+
}
312+
308313
// Get current state
309314
func (m *Machine) GetState() driver.MachineState {
310315
return m.State

0 commit comments

Comments
 (0)