From 31a380115a0f05d9709e016e349fbf91f1c20e2a Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Thu, 30 Jan 2014 16:12:34 +0000 Subject: Connect to ephemeral port when debugging Rather than use a random, possibly used port, have the server connect to an ephemeral port and report via a file, which port it connected to. --- lorry-controller-webapp | 31 +++++++++++++++++++++++++------ yarns.webapp/900-implementations.yarn | 15 ++++++--------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 6acbac0..9064d17 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -20,6 +20,7 @@ import logging import os import sqlite3 import time +import wsgiref.simple_server import bottle import cliapp @@ -132,11 +133,12 @@ class WEBAPP(cliapp.Application): ['wsgi'], 'run in wsgi mode (default is debug mode, for development)') - self.settings.integer( - ['debug-port'], - 'listen on PORT when in debug mode (i.e., not running under WSGI)', - metavar='PORT', - default=8888) + self.settings.string( + ['debug-port-file'], + 'write listening port to FILE when in debug mode ' + '(i.e., not running under WSGI)', + metavar='FILE', + default='webapp.port') self.settings.string( ['debug-host'], @@ -182,10 +184,27 @@ class WEBAPP(cliapp.Application): if self.settings['wsgi']: WSGIServer(webapp).run() else: + server_port_file = self.settings['debug-port-file'] + class DebugServer(wsgiref.simple_server.WSGIServer): + '''WSGI-like server that uses an ephemeral port. + + Rather than use a specified port, or default, the + DebugServer connects to an ephemeral port and writes + its number to debug-port-file, so a non-racy temporary + port can be used. + + ''' + + def __init__(self, (host, port), *args, **kwargs): + wsgiref.simple_server.WSGIServer.__init__( + self, (host, 0), *args, **kwargs) + with open(server_port_file, 'w') as f: + f.write(str(self.server_port) + '\n') + bottle.run( webapp, host=self.settings['debug-host'], - port=self.settings['debug-port'], + server_class=DebugServer, quiet=True, debug=True) diff --git a/yarns.webapp/900-implementations.yarn b/yarns.webapp/900-implementations.yarn index 7bfb6bb..19d4cfc 100644 --- a/yarns.webapp/900-implementations.yarn +++ b/yarns.webapp/900-implementations.yarn @@ -24,14 +24,6 @@ but the shell doesn't wait for it to terminate. This way, WEBAPP will be running until it crashes or is explicitly killed. IMPLEMENTS GIVEN a running WEBAPP - # Pick a random port beyond 1024 (i.e., an unreserved one). - port=0 - while [ "$port" -le 1024 ] - do - port=$RANDOM - done - echo "$port" > "$DATADIR/webapp.port" - start-stop-daemon -S -x "$SRCDIR/lorry-controller-webapp" \ -b -p "$DATADIR/webapp.pid" -m --verbose \ -- \ @@ -39,7 +31,12 @@ be running until it crashes or is explicitly killed. --log-level debug \ --log "$DATADIR/webapp.log" \ --debug-host 127.0.0.1 \ - --debug-port "$port" + --debug-port-file "$DATADIR/webapp.port" + + while [ ! -e "$DATADIR/webapp.port" ]; do + sleep 1 + done + port=$(cat "$DATADIR/webapp.port") # Wait for the WEBAPP to actually be ready, i.e., that it's # listening on its assigned port. -- cgit v1.2.1