packer-plugin-xenserver/builder/xenserver/common/find_port.go
Cheng Sun 152bdee7f2 Initial work on splitting ISO and XVA builders
Compiles -- but xva create_instance is incomplete

Several changes were rolled into this commit, including:

- "export_format" is now "format", and "keep_instance" is "keep_vm"
- gox is used for building (with the intention of allowing
  cross-compilation in the future)
2014-12-31 16:09:23 +00:00

26 lines
628 B
Go

package common
import (
"fmt"
"log"
"net"
)
// FindPort finds and starts listening on a port in the range [portMin, portMax]
// returns the listener and the port number on success, or nil, 0 on failure
func FindPort(portMin uint, portMax uint) (net.Listener, uint) {
log.Printf("Looking for an available port between %d and %d", portMin, portMax)
for port := portMin; port <= portMax; port++ {
log.Printf("Trying port: %d", port)
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err == nil {
return l, port
} else {
log.Printf("Port %d unavailable: %s", port, err.Error())
}
}
return nil, 0
}