Commit 1ab56554 authored by David Trudgian's avatar David Trudgian
Browse files

Try polling web server until it's up

parent d17ffdb0
......@@ -6,7 +6,7 @@ from multiprocessing import Process
from . import VERSION
from .clair import check_clair, post_layer, get_report, format_report_text
from .util import sha256
from .util import sha256, wait_net_service
from .image import check_image, image_to_tgz, http_server
......@@ -42,6 +42,9 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
# so that Clair can retrieve it
httpd = Process(target=http_server, args=(tar_dir, bind_ip, bind_port, quiet))
httpd.start()
# Allow up to 30 seconds for the httpd to start and be answering requests
wait_net_service(bind_ip, bind_port, 30)
image_uri = 'http://%s:%d/%s' % (bind_ip, bind_port, path.basename(tar_file))
# Register the iamge with Clair as a docker layer that has no parent
......
......@@ -21,3 +21,62 @@ def err_and_exit(message, code=1):
"""Write error to STDERR and exit with supplied code"""
sys.stderr.write(message)
sys.exit(code)
# http://code.activestate.com/recipes/576655-wait-for-network-service-to-appear/
#
#
# Copyright (c) 2017 ActiveState Software Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
def wait_net_service(server, port, timeout=None):
""" Wait for network service to appear
@param timeout: in seconds, if None or 0 wait forever
@return: True of False, if timeout is None may return only True or
throw unhandled network exception
"""
import socket
import errno
s = socket.socket()
if timeout:
from time import time as now
# time module is needed to calc timeout shared between two exceptions
end = now() + timeout
while True:
try:
if timeout:
next_timeout = end - now()
if next_timeout < 0:
return False
else:
s.settimeout(next_timeout)
s.connect((server, port))
except socket.timeout, err:
# this exception occurs only if timeout is set
if timeout:
return False
except socket.error, err:
# catch timeout exception from underlying network library
# this one is different from socket.timeout
if type(err.args) != tuple or err[0] != errno.ETIMEDOUT:
raise
else:
s.close()
return True
\ No newline at end of file
......@@ -47,7 +47,9 @@ def test_http_server(testimage, tmpdir):
httpd = multiprocessing.Process(target=http_server,
args=(os.path.dirname(testimage), '127.0.0.1', 8088, False))
httpd.start()
time.sleep(2)
# Allow up to 30 seconds for the httpd to start and be answering requests
wait_net_service('127.0.0.1', 8088, 30)
r = requests.get('http://127.0.0.1:8088/vsoch-singularity-hello-world-master.img',
proxies={'http://127.0.0.1': ''}, stream=True)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment