From 01060c72158893863162acec85f576c442fc49ff Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 1 Oct 2014 16:24:47 +0000 Subject: distbuild: allow daemons to bind to ephemeral ports You can bind to an ephemeral port by passing 0 as the port number. To work out which port you actually got, you need to call getsockname(). To facilitate being able to spawn multiple copies of the daemons for testing environments, you can pass a -file option, which will make the daemon write which port it actually bound to. If this path is a fifo, reading from it in the spawner process will allow synchronisation of only spawning services that require that port to be ready after it is. --- distbuild/sockserv.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'distbuild') diff --git a/distbuild/sockserv.py b/distbuild/sockserv.py index a5215e79..68991a93 100644 --- a/distbuild/sockserv.py +++ b/distbuild/sockserv.py @@ -26,15 +26,20 @@ class ListenServer(StateMachine): '''Listen for new connections on a port, send events for them.''' - def __init__(self, addr, port, machine, extra_args=None): + def __init__(self, addr, port, machine, extra_args=None, port_file=''): StateMachine.__init__(self, 'listening') self._addr = addr self._port = port self._machine = machine self._extra_args = extra_args or [] + self._port_file = port_file def setup(self): src = ListeningSocketEventSource(self._addr, self._port) + if self._port_file: + host, port = src.sock.getsockname() + with open(self._port_file, 'w') as f: + f.write(port) self.mainloop.add_event_source(src) spec = [ -- cgit v1.2.1