forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
host_ip_vmnetnatconf.go
67 lines (54 loc) · 1.28 KB
/
host_ip_vmnetnatconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package iso
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
)
// VMnetNatConfIPFinder finds the IP address of the host machine by
// retrieving the IP from the vmnetnat.conf. This isn't a full proof
// technique but so far it has not failed.
type VMnetNatConfIPFinder struct{}
func (*VMnetNatConfIPFinder) HostIP() (string, error) {
driver := &vmwcommon.Workstation9Driver{}
vmnetnat := driver.VmnetnatConfPath()
if vmnetnat == "" {
return "", errors.New("Could not find NAT vmnet conf file")
}
if _, err := os.Stat(vmnetnat); err != nil {
return "", fmt.Errorf("Could not find NAT vmnet conf file: %s", vmnetnat)
}
f, err := os.Open(vmnetnat)
if err != nil {
return "", err
}
defer f.Close()
ipRe := regexp.MustCompile(`^\s*ip\s*=\s*(.+?)\s*$`)
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if line != "" {
matches := ipRe.FindStringSubmatch(line)
if matches != nil {
ip := matches[1]
dotIndex := strings.LastIndex(ip, ".")
if dotIndex == -1 {
continue
}
ip = ip[0:dotIndex] + ".1"
return ip, nil
}
}
if err == io.EOF {
break
}
if err != nil {
return "", err
}
}
return "", errors.New("host IP not found in " + vmnetnat)
}