2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
// Taken from hashicorp/packer/builder/qemu/step_http_server.go
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
import (
|
2021-01-03 16:08:24 -06:00
|
|
|
"context"
|
2014-11-10 12:16:02 -06:00
|
|
|
"fmt"
|
2014-12-30 09:01:53 -06:00
|
|
|
"log"
|
2014-11-10 12:16:02 -06:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-01-03 16:08:24 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-11-10 12:16:02 -06: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.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// http_port int - The port the HTTP server started on.
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepHTTPServer struct {
|
2014-12-30 09:01:53 -06:00
|
|
|
Chan chan<- string
|
|
|
|
|
2014-11-10 12:16:02 -06:00
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
2014-12-30 09:01:53 -06:00
|
|
|
type IPSnooper struct {
|
|
|
|
ch chan<- string
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (snooper IPSnooper) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Printf("HTTP: %s %s %s", req.RemoteAddr, req.Method, req.URL)
|
|
|
|
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
|
|
if err == nil && ip != "" {
|
|
|
|
select {
|
|
|
|
case snooper.ch <- ip:
|
|
|
|
log.Printf("Remembering remote address '%s'", ip)
|
|
|
|
default:
|
|
|
|
// if ch is already full, don't block waiting to send the address, just drop it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snooper.handler.ServeHTTP(resp, req)
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-12-29 06:46:54 -06:00
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2014-11-10 12:16:02 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
var httpPort uint = 0
|
|
|
|
if config.HTTPDir == "" {
|
|
|
|
state.Put("http_port", httpPort)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-10 08:18:18 -06:00
|
|
|
s.l, httpPort = FindPort(config.HTTPPortMin, config.HTTPPortMax)
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-10 08:18:18 -06:00
|
|
|
if s.l == nil || httpPort == 0 {
|
|
|
|
ui.Error("Error: unable to find free HTTP server port. Try providing a larger range [http_port_min, http_port_max]")
|
|
|
|
return multistep.ActionHalt
|
2014-11-10 12:16:02 -06: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))
|
2014-12-30 09:01:53 -06:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%d", httpPort),
|
|
|
|
Handler: IPSnooper{
|
|
|
|
ch: s.Chan,
|
|
|
|
handler: fileServer,
|
|
|
|
},
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
go server.Serve(s.l)
|
|
|
|
|
|
|
|
// Save the address into the state so it can be accessed in the future
|
|
|
|
state.Put("http_port", httpPort)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (s *StepHTTPServer) Cleanup(multistep.StateBag) {
|
2014-11-10 12:16:02 -06:00
|
|
|
if s.l != nil {
|
|
|
|
// Close the listener so that the HTTP server stops
|
|
|
|
s.l.Close()
|
|
|
|
}
|
|
|
|
}
|