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 (
"net/rpc"
"os"
packrpc "github.com/mitchellh/packer/packer/rpc"
"strconv"
)
// This serves a single RPC connection on the given RPC server on
// a random port.
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 {
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()
// Output the address to stdout
fmt.Println(":2345")
fmt.Println(address)
os.Stdout.Sync()
// Accept a connection
......
......@@ -21,8 +21,14 @@ func (helperCommand) Synopsis() string {
func helperProcess(s... string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"}
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.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...)
cmd.Env = append(env, os.Environ()...)
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