packer/plugin: Randomly generate port to run on

This commit is contained in:
Mitchell Hashimoto 2013-05-06 15:27:44 -07:00
parent b9e3eb1ff7
commit e9278cc0bd
2 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -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
}