diff options
author | Richard Maw <richard.maw@gmail.com> | 2014-10-01 16:59:53 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2017-10-02 10:51:38 +0100 |
commit | 228b683d377161f62fea40389259ca089da0fb74 (patch) | |
tree | 13bc5faea69032f9fe1cc6f1ce344b1ff6f69bed /morph-cache-server | |
parent | acefe33868585cf31cda53474a3004da42e00896 (diff) | |
download | morph-cache-server-228b683d377161f62fea40389259ca089da0fb74.tar.gz |
Allow morph-cache-server to be bound to an ephemeral port
Diffstat (limited to 'morph-cache-server')
-rwxr-xr-x | morph-cache-server | 30 |
1 files changed, 28 insertions, 2 deletions
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) |