|
|
|
|
@ -4,6 +4,8 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
|
"log"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
@ -25,27 +27,32 @@ func (s *stepHTTPServer) Run(state map[string]interface{}) multistep.StepAction
|
|
|
|
|
config := state["config"].(*config)
|
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
|
|
httpPort := 0
|
|
|
|
|
if config.HTTPDir != "" {
|
|
|
|
|
httpPort = 8080
|
|
|
|
|
httpAddr := fmt.Sprintf(":%d", httpPort)
|
|
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Starting HTTP server on port %d", httpPort))
|
|
|
|
|
var httpPort uint = 0
|
|
|
|
|
if config.HTTPDir == "" {
|
|
|
|
|
state["http_port"] = httpPort
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the TCP listener
|
|
|
|
|
// Find an available TCP port for our HTTP server
|
|
|
|
|
var httpAddr string
|
|
|
|
|
portRange := int(config.HTTPPortMax - config.HTTPPortMin)
|
|
|
|
|
for {
|
|
|
|
|
var err error
|
|
|
|
|
httpPort = uint(rand.Intn(portRange)) + config.HTTPPortMin
|
|
|
|
|
httpAddr = fmt.Sprintf(":%d", httpPort)
|
|
|
|
|
log.Printf("Trying port: %d", httpPort)
|
|
|
|
|
s.l, err = net.Listen("tcp", httpAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ui.Error(fmt.Sprintf("Error starting HTTP server: %s", err))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
if err == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the HTTP server and run it in the background
|
|
|
|
|
fileServer := http.FileServer(http.Dir(config.HTTPDir))
|
|
|
|
|
server := &http.Server{Addr: httpAddr, Handler: fileServer}
|
|
|
|
|
go server.Serve(s.l)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Starting HTTP server on port %d", httpPort))
|
|
|
|
|
|
|
|
|
|
// Start the HTTP server and run it in the background
|
|
|
|
|
fileServer := http.FileServer(http.Dir(config.HTTPDir))
|
|
|
|
|
server := &http.Server{Addr: httpAddr, Handler: fileServer}
|
|
|
|
|
go server.Serve(s.l)
|
|
|
|
|
|
|
|
|
|
// Save the address into the state so it can be accessed in the future
|
|
|
|
|
state["http_port"] = httpPort
|
|
|
|
|
|
|
|
|
|
|