mirror of https://github.com/hashicorp/packer
parent
34f4086963
commit
dfee3eb8ef
@ -0,0 +1,43 @@
|
||||
package vmware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Interface to help find the host IP that is available from within
|
||||
// the VMware virtual machines.
|
||||
type HostIPFinder interface {
|
||||
HostIP() (string, error)
|
||||
}
|
||||
|
||||
// IfconfigIPFinder finds the host IP based on the output of `ifconfig`.
|
||||
type IfconfigIPFinder struct {
|
||||
Device string
|
||||
}
|
||||
|
||||
func (f *IfconfigIPFinder) HostIP() (string, error) {
|
||||
ifconfigPath, err := exec.LookPath("ifconfig")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stdout := new(bytes.Buffer)
|
||||
|
||||
cmd := exec.Command(ifconfigPath, f.Device)
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = new(bytes.Buffer)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`inet\s*(.+?)\s`)
|
||||
matches := re.FindStringSubmatch(stdout.String())
|
||||
if matches == nil {
|
||||
return "", errors.New("IP not found in ifconfig output...")
|
||||
}
|
||||
|
||||
return matches[1], nil
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package vmware
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIfconfigIPFinder_Impl(t *testing.T) {
|
||||
var raw interface{}
|
||||
raw = &IfconfigIPFinder{}
|
||||
if _, ok := raw.(HostIPFinder); !ok {
|
||||
t.Fatalf("IfconfigIPFinder is not a host IP finder")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue