2014-09-05 14:59:46 -04:00
|
|
|
package common
|
2013-06-05 17:24:48 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2014-05-06 11:03:51 -04:00
|
|
|
// This step creates and runs the HTTP server that is serving files from the
|
|
|
|
// directory specified by the 'http_directory` configuration parameter in the
|
|
|
|
// template.
|
2013-06-05 17:24:48 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// http_port int - The port the HTTP server started on.
|
2014-09-05 14:59:46 -04:00
|
|
|
type StepHTTPServer struct {
|
|
|
|
HTTPDir string
|
|
|
|
HTTPPortMin uint
|
|
|
|
HTTPPortMax uint
|
|
|
|
|
2013-06-05 17:24:48 -04:00
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:59:46 -04:00
|
|
|
func (s *StepHTTPServer) Run(state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-05 17:24:48 -04:00
|
|
|
|
2013-06-07 18:20:39 -04:00
|
|
|
var httpPort uint = 0
|
2014-09-05 14:59:46 -04:00
|
|
|
if s.HTTPDir == "" {
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("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
|
2014-09-05 14:59:46 -04:00
|
|
|
portRange := int(s.HTTPPortMax - s.HTTPPortMin)
|
2013-06-07 18:20:39 -04:00
|
|
|
for {
|
2013-06-06 17:42:23 -04:00
|
|
|
var err error
|
2013-06-28 21:59:29 -04:00
|
|
|
var offset uint = 0
|
|
|
|
|
|
|
|
if portRange > 0 {
|
|
|
|
// Intn will panic if portRange == 0, so we do a check.
|
|
|
|
offset = uint(rand.Intn(portRange))
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:59:46 -04:00
|
|
|
httpPort = offset + s.HTTPPortMin
|
2015-06-08 23:47:47 -04:00
|
|
|
httpAddr = fmt.Sprintf("0.0.0.0:%d", httpPort)
|
2013-06-07 18:20:39 -04:00
|
|
|
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
|
2014-09-05 14:59:46 -04:00
|
|
|
fileServer := http.FileServer(http.Dir(s.HTTPDir))
|
2013-06-07 18:20:39 -04:00
|
|
|
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
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("http_port", httpPort)
|
2013-06-05 17:24:48 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:59:46 -04:00
|
|
|
func (s *StepHTTPServer) Cleanup(multistep.StateBag) {
|
2013-06-05 17:24:48 -04:00
|
|
|
if s.l != nil {
|
|
|
|
// Close the listener so that the HTTP server stops
|
|
|
|
s.l.Close()
|
|
|
|
}
|
|
|
|
}
|