Skip to content

Commit a410e76

Browse files
authored
Merge pull request #31 from middagj/vmrun-ip
Use vmrun getGuestIPAddress to get IP (Big Sur)
2 parents aaccce5 + a335ac9 commit a410e76

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Download the the binary that corresponds to your OS into a directory residing in
2424

2525
### From Homebrew
2626

27-
The driver is available for easy installation via Homebrew on macOS.
27+
The driver is available for easy installation via Homebrew on macOS.
2828

2929
```shell
3030
$ brew install docker-machine-driver-vmware
@@ -60,6 +60,7 @@ $ docker-machine create --driver=vmware default
6060
- `--vmware-no-share`: Disable the mount of your home directory
6161
- `--vmware-ssh-password`: SSH password
6262
- `--vmware-ssh-user`: SSH user
63+
- `--vmware-wait-ip`: Time to wait for vmrun to get an ip (in milliseconds)
6364

6465
#### Environment variables and default values
6566

@@ -73,6 +74,7 @@ $ docker-machine create --driver=vmware default
7374
| `--vmware-no-share` | VMWARE_NO_SHARE | - |
7475
| `--vmware-ssh-password` | VMWARE_SSH_PASSWORD | `tcuser` |
7576
| `--vmware-ssh-user` | VMWARE_SSH_USER | `docker` |
77+
| `--vmware-wait-ip` | VMWARE_WAIT_IP | `30000` |
7678

7779

7880
## License

pkg/drivers/vmware/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
defaultDiskSize = 20000
3232
defaultCPU = 1
3333
defaultMemory = 1024
34+
defaultWaitIP = 30000
3435
)
3536

3637
// Config specifies the configuration of driver VMware
@@ -47,6 +48,8 @@ type Config struct {
4748
ConfigDriveISO string
4849
ConfigDriveURL string
4950
NoShare bool
51+
52+
WaitIP int
5053
}
5154

5255
// NewConfig creates a new Config
@@ -56,6 +59,7 @@ func NewConfig(hostname, storePath string) *Config {
5659
Memory: defaultMemory,
5760
DiskSize: defaultDiskSize,
5861
SSHPassword: defaultSSHPass,
62+
WaitIP: defaultWaitIP,
5963
BaseDriver: &drivers.BaseDriver{
6064
SSHUser: defaultSSHUser,
6165
MachineName: hostname,
@@ -115,5 +119,11 @@ func (c *Config) GetCreateFlags() []mcnflag.Flag {
115119
Name: "vmware-no-share",
116120
Usage: "Disable the mount of your home directory",
117121
},
122+
mcnflag.IntFlag{
123+
EnvVar: "VMWARE_WAIT_IP",
124+
Name: "vmware-wait-ip",
125+
Usage: "time to wait for vmrun to get an ip (in milliseconds)",
126+
Value: defaultWaitIP,
127+
},
118128
}
119129
}

pkg/drivers/vmware/driver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
8989
d.SSHPassword = flags.String("vmware-ssh-password")
9090
d.SSHPort = 22
9191
d.NoShare = flags.Bool("vmware-no-share")
92+
d.WaitIP = flags.Int("vmware-wait-ip")
9293

9394
// We support a maximum of 16 cpu to be consistent with Virtual Hardware 10
9495
// specs.
@@ -123,6 +124,11 @@ func (d *Driver) GetIP() (ip string, err error) {
123124
return "", drivers.ErrHostIsNotRunning
124125
}
125126

127+
// attempt to find the address from vmrun
128+
if ip, err := d.getIPfromVmrun(); err == nil {
129+
return ip, err
130+
}
131+
126132
// determine MAC address for VM
127133
macaddr, err := d.getMacAddressFromVmx()
128134
if err != nil {
@@ -401,6 +407,18 @@ func (d *Driver) getMacAddressFromVmx() (string, error) {
401407
return macaddr, nil
402408
}
403409

410+
func (d *Driver) getIPfromVmrun() (string, error) {
411+
vmx := d.vmxPath()
412+
413+
ip := regexp.MustCompile(`\d+\.\d+\.\d+\.\d+`)
414+
stdout, _, _ := vmrun_wait(time.Duration(d.WaitIP)*time.Millisecond, "getGuestIPAddress", vmx, "-wait")
415+
if match := ip.FindString(stdout); match != "" {
416+
return match, nil
417+
}
418+
419+
return "", fmt.Errorf("could not get IP from vmrun")
420+
}
421+
404422
func (d *Driver) getIPfromVmnetConfiguration(macaddr string) (string, error) {
405423

406424
// DHCP lease table for NAT vmnet interface

pkg/drivers/vmware/vmrun.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ package vmware
2222

2323
import (
2424
"bytes"
25+
"context"
2526
"errors"
2627
"fmt"
2728
"io"
2829
"os"
2930
"os/exec"
3031
"strings"
32+
"time"
3133

3234
"github.com/docker/machine/libmachine/log"
3335
)
@@ -55,7 +57,18 @@ func isMachineDebugEnabled() bool {
5557

5658
func vmrun(args ...string) (string, string, error) {
5759
cmd := exec.Command(vmrunbin, args...)
60+
return vmrun_cmd(cmd)
61+
}
62+
63+
func vmrun_wait(timeout time.Duration, args ...string) (string, string, error) {
64+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
65+
defer cancel()
66+
67+
cmd := exec.CommandContext(ctx, vmrunbin, args...)
68+
return vmrun_cmd(cmd)
69+
}
5870

71+
func vmrun_cmd(cmd *exec.Cmd) (string, string, error) {
5972
var stdout bytes.Buffer
6073
var stderr bytes.Buffer
6174
cmd.Stdout, cmd.Stderr = &stdout, &stderr
@@ -66,7 +79,7 @@ func vmrun(args ...string) (string, string, error) {
6679
cmd.Stderr = io.MultiWriter(os.Stderr, cmd.Stderr)
6780
}
6881

69-
log.Debugf("executing: %v %v", vmrunbin, strings.Join(args, " "))
82+
log.Debugf("executing: %v", strings.Join(cmd.Args, " "))
7083

7184
err := cmd.Run()
7285
if err != nil {

0 commit comments

Comments
 (0)