diff --git a/builder/hyperv/common/step_type_boot_command.go b/builder/hyperv/common/step_type_boot_command.go index 4af792eff..2955813d4 100644 --- a/builder/hyperv/common/step_type_boot_command.go +++ b/builder/hyperv/common/step_type_boot_command.go @@ -8,6 +8,7 @@ import ( "unicode/utf8" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template/interpolate" ) @@ -42,6 +43,7 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction ui.Say(fmt.Sprintf("Host IP for the HyperV machine: %s", hostIp)) + common.SetHTTPIP(hostIp) s.Ctx.Data = &bootCommandTemplateData{ hostIp, httpPort, diff --git a/builder/parallels/common/step_type_boot_command.go b/builder/parallels/common/step_type_boot_command.go index 5d116f077..01f9743cb 100644 --- a/builder/parallels/common/step_type_boot_command.go +++ b/builder/parallels/common/step_type_boot_command.go @@ -9,6 +9,7 @@ import ( "unicode/utf8" "github.com/mitchellh/multistep" + packer_common "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template/interpolate" ) @@ -67,6 +68,7 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction ui.Say(fmt.Sprintf("Host IP for the Parallels machine: %s", hostIP)) + packer_common.SetHTTPIP(hostIP) s.Ctx.Data = &bootCommandTemplateData{ hostIP, httpPort, diff --git a/builder/qemu/step_type_boot_command.go b/builder/qemu/step_type_boot_command.go index af9334818..2ec581685 100644 --- a/builder/qemu/step_type_boot_command.go +++ b/builder/qemu/step_type_boot_command.go @@ -72,9 +72,11 @@ func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction log.Printf("Connected to VNC desktop: %s", c.DesktopName) + hostIP := "10.0.2.2" + common.SetHTTPIP(hostIP) ctx := config.ctx ctx.Data = &bootCommandTemplateData{ - "10.0.2.2", + hostIP, httpPort, config.VMName, } diff --git a/builder/virtualbox/common/step_type_boot_command.go b/builder/virtualbox/common/step_type_boot_command.go index 7bccc70c6..26d5a9658 100644 --- a/builder/virtualbox/common/step_type_boot_command.go +++ b/builder/virtualbox/common/step_type_boot_command.go @@ -9,6 +9,7 @@ import ( "unicode/utf8" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template/interpolate" ) @@ -49,8 +50,10 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn) } + hostIP := "10.0.2.2" + common.SetHTTPIP(hostIP) s.Ctx.Data = &bootCommandTemplateData{ - "10.0.2.2", + hostIP, httpPort, s.VMName, } diff --git a/builder/vmware/common/step_type_boot_command.go b/builder/vmware/common/step_type_boot_command.go index b3cc02b79..f4d7367c8 100644 --- a/builder/vmware/common/step_type_boot_command.go +++ b/builder/vmware/common/step_type_boot_command.go @@ -95,7 +95,7 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction ipFinder = &IfconfigIPFinder{Device: "vmnet8"} } - hostIp, err := ipFinder.HostIP() + hostIP, err := ipFinder.HostIP() if err != nil { err := fmt.Errorf("Error detecting host IP: %s", err) state.Put("error", err) @@ -103,10 +103,11 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction return multistep.ActionHalt } - log.Printf("Host IP for the VMware machine: %s", hostIp) + log.Printf("Host IP for the VMware machine: %s", hostIP) + common.SetHTTPIP(hostIP) s.Ctx.Data = &bootCommandTemplateData{ - hostIp, + hostIP, httpPort, s.VMName, } diff --git a/common/step_http_server.go b/common/step_http_server.go index 55874992e..6dbef6011 100644 --- a/common/step_http_server.go +++ b/common/step_http_server.go @@ -2,12 +2,16 @@ package common import ( "fmt" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" + "io/ioutil" "log" "math/rand" "net" "net/http" + "os" + "path/filepath" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) // This step creates and runs the HTTP server that is serving files from the @@ -66,13 +70,41 @@ func (s *StepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { // Save the address into the state so it can be accessed in the future state.Put("http_port", httpPort) + SetHTTPPort(fmt.Sprintf("%d", httpPort)) return multistep.ActionContinue } +func httpAddrFilename(suffix string) string { + uuid := os.Getenv("PACKER_RUN_UUID") + return filepath.Join(os.TempDir(), fmt.Sprintf("packer-%s-%s", uuid, suffix)) +} + +func SetHTTPPort(port string) error { + return ioutil.WriteFile(httpAddrFilename("port"), []byte(port), 0644) +} + +func SetHTTPIP(ip string) error { + return ioutil.WriteFile(httpAddrFilename("ip"), []byte(ip), 0644) +} + +func GetHTTPAddr() string { + ip, err := ioutil.ReadFile(httpAddrFilename("ip")) + if err != nil { + return "" + } + port, err := ioutil.ReadFile(httpAddrFilename("port")) + if err != nil { + return "" + } + return fmt.Sprintf("%s:%s", ip, port) +} + func (s *StepHTTPServer) Cleanup(multistep.StateBag) { if s.l != nil { // Close the listener so that the HTTP server stops s.l.Close() } + os.Remove(httpAddrFilename("port")) + os.Remove(httpAddrFilename("ip")) } diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 00a81bf30..a6018d69c 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -229,10 +229,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } // Build our variables up by adding in the build name and builder type - envVars := make([]string, len(p.config.Vars)+2) + envVars := make([]string, len(p.config.Vars)+3) envVars[0] = fmt.Sprintf("PACKER_BUILD_NAME='%s'", p.config.PackerBuildName) envVars[1] = fmt.Sprintf("PACKER_BUILDER_TYPE='%s'", p.config.PackerBuilderType) - copy(envVars[2:], p.config.Vars) + envVars[2] = fmt.Sprintf("PACKER_HTTP_ADDR=%s", common.GetHTTPAddr()) + + copy(envVars[3:], p.config.Vars) for _, path := range scripts { ui.Say(fmt.Sprintf("Provisioning with shell script: %s", path))