Commit e9278cc0 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/plugin: Randomly generate port to run on

parent b9e3eb1f
...@@ -10,19 +10,38 @@ import ( ...@@ -10,19 +10,38 @@ import (
"net/rpc" "net/rpc"
"os" "os"
packrpc "github.com/mitchellh/packer/packer/rpc" packrpc "github.com/mitchellh/packer/packer/rpc"
"strconv"
) )
// This serves a single RPC connection on the given RPC server on // This serves a single RPC connection on the given RPC server on
// a random port. // a random port.
func serve(server *rpc.Server) (err error) { func serve(server *rpc.Server) (err error) {
listener, err := net.Listen("tcp", ":2345") minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
if err != nil { if err != nil {
return return
} }
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
if err != nil {
return
}
var address string
var listener net.Listener
for port := minPort; port <= maxPort; port++ {
address = fmt.Sprintf(":%d", port)
listener, err = net.Listen("tcp", address)
if err != nil {
return
}
break
}
defer listener.Close() defer listener.Close()
// Output the address to stdout // Output the address to stdout
fmt.Println(":2345") fmt.Println(address)
os.Stdout.Sync() os.Stdout.Sync()
// Accept a connection // Accept a connection
......
...@@ -21,8 +21,14 @@ func (helperCommand) Synopsis() string { ...@@ -21,8 +21,14 @@ func (helperCommand) Synopsis() string {
func helperProcess(s... string) *exec.Cmd { func helperProcess(s... string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"} cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...) cs = append(cs, s...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
"PACKER_PLUGIN_MIN_PORT=10000",
"PACKER_PLUGIN_MAX_PORT=25000",
}
cmd := exec.Command(os.Args[0], cs...) cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) cmd.Env = append(env, os.Environ()...)
return cmd return cmd
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment