2020-01-27 10:49:31 -05:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-03 09:47:03 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-01-27 10:49:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Step to discover the http ip
|
|
|
|
// which guests use to reach the vm host
|
|
|
|
// To make sure the IP is set before boot command and http server steps
|
|
|
|
type stepHTTPIPDiscover struct{}
|
|
|
|
|
|
|
|
func (s *stepHTTPIPDiscover) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-05-03 09:47:03 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-05-03 09:47:03 -04:00
|
|
|
|
|
|
|
hostIP := ""
|
|
|
|
|
|
|
|
if config.NetBridge == "" {
|
|
|
|
hostIP = "10.0.2.2"
|
|
|
|
} else {
|
|
|
|
bridgeInterface, err := net.InterfaceByName(config.NetBridge)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error getting the bridge %s interface: %s", config.NetBridge, err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
addrs, err := bridgeInterface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error getting the bridge %s interface addresses: %s", config.NetBridge, err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
var ip net.IP
|
|
|
|
switch v := addr.(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = v.IP
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = v.IP
|
|
|
|
}
|
|
|
|
if ip == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip = ip.To4()
|
|
|
|
if ip == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hostIP = ip.String()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hostIP == "" {
|
|
|
|
err := fmt.Errorf("Error getting an IPv4 address from the bridge %s: cannot find any IPv4 address", config.NetBridge)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("http_ip", hostIP)
|
2020-01-27 10:49:31 -05:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepHTTPIPDiscover) Cleanup(state multistep.StateBag) {}
|