2013-06-05 17:24:48 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-07 18:20:39 -04:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
2013-06-05 17:24:48 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates and runs the HTTP server that is serving the files
|
|
|
|
// specified by the 'http_files` configuration parameter in the template.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// http_port int - The port the HTTP server started on.
|
2013-06-05 18:48:13 -04:00
|
|
|
type stepHTTPServer struct {
|
2013-06-05 17:24:48 -04:00
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepHTTPServer) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
config := state["config"].(*config)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
2013-06-07 18:20:39 -04:00
|
|
|
var httpPort uint = 0
|
|
|
|
if config.HTTPDir == "" {
|
|
|
|
state["http_port"] = httpPort
|
2013-06-07 18:50:54 -04:00
|
|
|
return multistep.ActionContinue
|
2013-06-07 18:20:39 -04:00
|
|
|
}
|
2013-06-06 17:42:23 -04:00
|
|
|
|
2013-06-07 18:20:39 -04:00
|
|
|
// Find an available TCP port for our HTTP server
|
|
|
|
var httpAddr string
|
|
|
|
portRange := int(config.HTTPPortMax - config.HTTPPortMin)
|
|
|
|
for {
|
2013-06-06 17:42:23 -04:00
|
|
|
var err error
|
2013-06-07 18:20:39 -04:00
|
|
|
httpPort = uint(rand.Intn(portRange)) + config.HTTPPortMin
|
|
|
|
httpAddr = fmt.Sprintf(":%d", httpPort)
|
|
|
|
log.Printf("Trying port: %d", httpPort)
|
2013-06-06 17:42:23 -04:00
|
|
|
s.l, err = net.Listen("tcp", httpAddr)
|
2013-06-07 18:20:39 -04:00
|
|
|
if err == nil {
|
|
|
|
break
|
2013-06-06 17:42:23 -04:00
|
|
|
}
|
2013-06-05 17:24:48 -04:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:20:39 -04:00
|
|
|
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)
|
|
|
|
|
2013-06-05 17:24:48 -04:00
|
|
|
// Save the address into the state so it can be accessed in the future
|
|
|
|
state["http_port"] = httpPort
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepHTTPServer) Cleanup(map[string]interface{}) {
|
|
|
|
if s.l != nil {
|
|
|
|
// Close the listener so that the HTTP server stops
|
|
|
|
s.l.Close()
|
|
|
|
}
|
|
|
|
}
|