From 04cc49b52339a234cf4fe4079c9959d326d885d4 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 24 Dec 2015 17:43:57 +0100 Subject: [PATCH] Support running code questions on Docker swarm --- course/page/code.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/course/page/code.py b/course/page/code.py index 0225d24b..1de192f9 100644 --- a/course/page/code.py +++ b/course/page/code.py @@ -123,13 +123,22 @@ def request_python_run(run_req, run_timeout, image=None): else: container_id = None + connect_host_ip = 'localhost' + try: # FIXME: Prohibit networking if container_id is not None: docker_cnx.start(container_id) - port_info, = docker_cnx.port(container_id, RUNPY_PORT) + container_props = docker_cnx.inspect_container(container_id) + (port_info,) = (container_props + ["NetworkSettings"]["Ports"]["%d/tcp" % RUNPY_PORT]) + port_host_ip = port_info.get("HostIp") + + if port_host_ip != "0.0.0.0": + connect_host_ip = port_host_ip + port = int(port_info["HostPort"]) else: port = RUNPY_PORT @@ -154,7 +163,7 @@ def request_python_run(run_req, run_timeout, image=None): while True: try: - connection = http_client.HTTPConnection('localhost', port) + connection = http_client.HTTPConnection(connect_host_ip, port) connection.request('GET', '/ping') @@ -187,7 +196,7 @@ def request_python_run(run_req, run_timeout, image=None): try: # Add a second to accommodate 'wire' delays - connection = http_client.HTTPConnection('localhost', port, + connection = http_client.HTTPConnection(connect_host_ip, port, timeout=1 + run_timeout) headers = {'Content-type': 'application/json'} -- GitLab