set PACKER_HTTP_ADDR env var when available.

If using a builder that has an http server set up for file transfer,
expose the connection info to the shell provisioner through the environment
variable PACKER_HTTP_ADDR.

Closes #2869
This commit is contained in:
Matthew Hooker 2017-01-16 23:19:52 -08:00
parent eb60387aad
commit d2e59e4e92
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
7 changed files with 53 additions and 9 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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,
}

View File

@ -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,
}

View File

@ -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,
}

View File

@ -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"))
}

View File

@ -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))