From 228b683d377161f62fea40389259ca089da0fb74 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 1 Oct 2014 16:59:53 +0000 Subject: Allow morph-cache-server to be bound to an ephemeral port --- morph-cache-server | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'morph-cache-server') diff --git a/morph-cache-server b/morph-cache-server index a3c3c97..4af3cee 100755 --- a/morph-cache-server +++ b/morph-cache-server @@ -45,6 +45,10 @@ class MorphCacheServer(cliapp.Application): 'port to listen on', metavar='PORTNUM', default=defaults['port']) + self.settings.string(['port-file'], + 'write port number to FILE', + metavar='FILE', + default='') self.settings.string(['repo-dir'], 'path to the repository cache directory', metavar='PATH', @@ -322,7 +326,8 @@ class MorphCacheServer(cliapp.Application): % artifact) return - filename = os.path.join(self.settings['artifact-dir'], artifact) + filename = os.path.join(self.settings['artifact-dir'], + artifact) results[artifact] = os.path.exists(filename) if results[artifact]: @@ -338,8 +343,29 @@ class MorphCacheServer(cliapp.Application): if self.settings['fcgi-server']: WSGIServer(root).run() + elif self.settings['port-file']: + import wsgiref.simple_server + + server_port_file = self.settings['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 binds to an ephemeral port on 127.0.0.1 + and writes its number to port-file, so a non-racy + temporary port can be used. + + ''' + + def __init__(self, (host, port), *args, **kwargs): + wsgiref.simple_server.WSGIServer.__init__( + self, ('127.0.0.1', 0), *args, **kwargs) + with open(server_port_file, 'w') as f: + f.write(str(self.server_port) + '\n') + run(root, server_class=DebugServer, debug=True) else: - run(root, host='0.0.0.0', port=self.settings['port'], reloader=True) + run(root, host='0.0.0.0', port=self.settings['port'], + reloader=True) def _unescape_parameter(self, param): return urllib.unquote(param) -- cgit v1.2.1