X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=curlwrapper.py;h=21b499c2d12fd998e8afd5634761adaf6f73a654;hb=475168d8b0b35536e3a4d48c8e29523c03e718b1;hp=ce273a304968ec9683ab3f75ced80641a9a5c8e8;hpb=191f762aee7f7412e3d3b3840de914b9326aa888;p=nodemanager.git diff --git a/curlwrapper.py b/curlwrapper.py index ce273a3..21b499c 100644 --- a/curlwrapper.py +++ b/curlwrapper.py @@ -1,16 +1,43 @@ +# Note +# in spring 2010, an attempt was made to use pycurl instead of forking curl +# it turned out, however, that after around 10 cycles of the nodemanager, +# attempts to call GetSlivers were failing with a curl error 60 +# we are thus reverting to the version from tag curlwrapper.py-NodeManager-2.0-8 +# the (broekn) pycurl version can be found in tags 2.0-9 and 2.0-10 + from subprocess import PIPE, Popen +from select import select +import xmlrpclib +import signal +import os +import logger -class CurlException(Exception): pass +class Sopen(Popen): + def kill(self, signal = signal.SIGTERM): + os.kill(self.pid, signal) -def retrieve(url, postdata=None): - options = ('/usr/bin/curl', '--cacert', '/usr/boot/cacert.pem') +def retrieve(url, cacert=None, postdata=None, timeout=90): +# options = ('/usr/bin/curl', '--fail', '--silent') + options = ('/usr/bin/curl', '--fail', ) + if cacert: options += ('--cacert', cacert) if postdata: options += ('--data', '@-') - p = Popen(options + (url,), stdin=PIPE, stdout=PIPE, stderr=PIPE) + if timeout: + options += ('--max-time', str(timeout)) + options += ('--connect-timeout', str(timeout)) + p = Sopen(options + (url,), stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) if postdata: p.stdin.write(postdata) p.stdin.close() + sout, sin, serr = select([p.stdout,p.stderr],[],[], timeout) + if len(sout) == 0 and len(sin) == 0 and len(serr) == 0: + logger.verbose("curlwrapper: timed out after %s" % timeout) + p.kill(signal.SIGKILL) data = p.stdout.read() err = p.stderr.read() rc = p.wait() - if rc != 0: raise CurlException(err) - else: return data + if rc != 0: + # when this triggers, the error sometimes doesn't get printed + logger.log ("curlwrapper: retrieve, got stderr <%s>"%err) + raise xmlrpclib.ProtocolError(url, rc, err, postdata) + else: + return data