Commit 921d9325 authored by Marco Mariani's avatar Marco Mariani

use requests package for 'slapos cache lookup'

parent 5ecc02c2
...@@ -45,6 +45,7 @@ setup(name=name, ...@@ -45,6 +45,7 @@ setup(name=name,
'zope.interface', # slap library implementes interfaces 'zope.interface', # slap library implementes interfaces
'zc.buildout', 'zc.buildout',
'cliff', 'cliff',
'requests',
] + additional_install_requires, ] + additional_install_requires,
extra_requires={'docs': ('Sphinx', 'repoze.sphinx.autointerface'),}, extra_requires={'docs': ('Sphinx', 'repoze.sphinx.autointerface'),},
tests_require=[ tests_require=[
......
...@@ -4,35 +4,43 @@ import ast ...@@ -4,35 +4,43 @@ import ast
import hashlib import hashlib
import json import json
import re import re
import requests
import sys import sys
import urllib2
from slapos.grid import networkcache from slapos.grid import networkcache
from slapos.grid.distribution import patched_linux_distribution from slapos.grid.distribution import patched_linux_distribution
def maybe_md5(s): def looks_like_md5(s):
"""
Return True if the parameter looks like an hashed value.
Not 100% precise, but we're actually more interested in filtering out URLs and pathnames.
"""
return re.match('[0-9a-f]{32}', s) return re.match('[0-9a-f]{32}', s)
def do_lookup(configp, software_url): def do_lookup(configp, software_url):
cache_dir = configp.get('networkcache', 'download-binary-dir-url') cache_dir = configp.get('networkcache', 'download-binary-dir-url')
if maybe_md5(software_url): if looks_like_md5(software_url):
md5 = software_url md5 = software_url
else: else:
md5 = hashlib.md5(software_url).hexdigest() md5 = hashlib.md5(software_url).hexdigest()
try: try:
response = urllib2.urlopen('%s/%s' % (cache_dir, md5)) req = requests.get('%s/%s' % (cache_dir, md5))
except urllib2.HTTPError as e: except requests.ConnectionError:
if e.code == 404: print 'Cannot connect to cache at %s' % cache_dir
sys.exit(10)
if not req.ok:
if req.status_code == 404:
print 'Object not in cache: %s' % software_url print 'Object not in cache: %s' % software_url
else: else:
print 'Error during cache lookup: %s (%s)' % (e.code, e.reason) print 'Error while looking object %s: %s' % (software_url, req.reason)
sys.exit(10) sys.exit(10)
entries = json.loads(response.read()) entries = req.json()
linux_distribution = patched_linux_distribution() linux_distribution = patched_linux_distribution()
......
...@@ -10,6 +10,10 @@ import cliff.commandmanager ...@@ -10,6 +10,10 @@ import cliff.commandmanager
import slapos.version import slapos.version
# silence messages like 'Starting connection' that are logged with INFO
urllib3_logger = logging.getLogger('requests.packages.urllib3')
urllib3_logger.setLevel(logging.WARNING)
class SlapOSCommandManager(cliff.commandmanager.CommandManager): class SlapOSCommandManager(cliff.commandmanager.CommandManager):
......
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