Commit 45ca8a58 authored by Marco Mariani's avatar Marco Mariani

cache lookup: removed old entry point, use prettytable

distribution check: added a few tests
parent cc3c6c7a
......@@ -7,6 +7,7 @@ import re
import requests
import sys
import prettytable
from slapos.grid import networkcache
from slapos.grid.distribution import patched_linux_distribution
......@@ -19,7 +20,7 @@ def looks_like_md5(s):
return re.match('[0-9a-f]{32}', s)
def do_lookup(configp, software_url):
def do_lookup(configp, software_url, logger):
cache_dir = configp.get('networkcache', 'download-binary-dir-url')
if looks_like_md5(software_url):
......@@ -28,39 +29,39 @@ def do_lookup(configp, software_url):
md5 = hashlib.md5(software_url).hexdigest()
try:
req = requests.get('%s/%s' % (cache_dir, md5))
except requests.ConnectionError:
print 'Cannot connect to cache at %s' % cache_dir
url = '%s/%s' % (cache_dir, md5)
logger.debug('Connecting to %s', url)
req = requests.get(url, timeout=5)
except (requests.Timeout, requests.ConnectionError):
logger.critical('Cannot connect to cache server at %s', url)
sys.exit(10)
if not req.ok:
if req.status_code == 404:
print 'Object not in cache: %s' % software_url
logger.critical('Object not in cache: %s', software_url)
else:
print 'Error while looking object %s: %s' % (software_url, req.reason)
logger.critical('Error while looking object %s: %s', software_url, req.reason)
sys.exit(10)
entries = req.json()
linux_distribution = patched_linux_distribution()
if not entries:
logger.info('Object found in cache, but has no binary entries.')
return
ostable = sorted(ast.literal_eval(json.loads(entry[0])['os']) for entry in entries)
pt = prettytable.PrettyTable(['distribution', 'version', 'id', 'compatible?'])
header_printed = False
ostable = []
for entry in entries:
meta = json.loads(entry[0])
os = ast.literal_eval(meta['os'])
if not header_printed:
print 'Software URL: %s' % meta['software_url']
print 'MD5: %s' % md5
print '-------------'
print 'Available for: '
print 'distribution | version | id | compatible?'
print '-----------------+--------------+----------------+-------------'
header_printed = True
ostable.append(os)
ostable.sort()
linux_distribution = patched_linux_distribution()
for os in ostable:
compatible = 'yes' if networkcache.os_matches(os, linux_distribution) else 'no'
print '%-16s | %12s | %s | %s' % (os[0], os[1], os[2].center(14), compatible)
pt.add_row([os[0], os[1], os[2], compatible])
meta = json.loads(entries[0][0])
logger.info('Software URL: %s', meta['software_url'])
logger.info('MD5: %s', md5)
for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line)
......@@ -27,4 +27,4 @@ class CacheLookupCommand(ConfigCommand):
def take_action(self, args):
configp = self.fetch_config(args)
do_lookup(configp, args.software_url)
do_lookup(configp, args.software_url, logger=self.app.log)
# -*- coding: utf-8 -*-
import argparse
import ConfigParser
from slapos.cache import do_lookup
def cache_lookup():
ap = argparse.ArgumentParser()
ap.add_argument("configuration_file", help="SlapOS configuration file")
ap.add_argument("software_url", help="Your software url or MD5 hash")
args = ap.parse_args()
configp = ConfigParser.SafeConfigParser()
configp.read(args.configuration_file)
do_lookup(configp, args.software_url)
......@@ -37,7 +37,6 @@ from slapos.cli_legacy.request import request
from slapos.cli_legacy.remove import remove
from slapos.cli_legacy.supply import supply
from slapos.cli_legacy.format import main as format
from slapos.cli_legacy.cache import cache_lookup
from slapos.cli_legacy.slapgrid import runComputerPartition as instance
from slapos.cli_legacy.slapgrid import runSoftwareRelease as software
from slapos.cli_legacy.slapgrid import runUsageReport as report
......@@ -165,8 +164,6 @@ def dispatch(command, is_node_command):
raise EntryPointNotImplementedError(command)
elif command == 'console':
call(console, config_path=USER_SLAPOS_CONFIGURATION)
elif command == 'cache-lookup':
call(cache_lookup, config_path=GLOBAL_SLAPOS_CONFIGURATION)
else:
return False
......@@ -193,7 +190,6 @@ Client subcommands usage:
slapos request <instance-name> <software-url> [--configuration arg1=value1 arg2=value2 ... argN=valueN]
slapos supply <software-url> <node-id>
slapos console
slapos cache-lookup <software-url-or-md5>
Node subcommands usage:
slapos node
slapos node register <node-id>
......
# -*- coding: utf-8 -*-
import unittest
from slapos.grid import distribution
class TestDebianize(unittest.TestCase):
def test_debian_major(self):
"""
On debian, we only care about major release.
All the other tuples are unchanged.
"""
for provided, expected in [
(('CentOS', '6.3', 'Final'), None),
(('Ubuntu', '12.04', 'precise'), None),
(('Ubuntu', '13.04', 'raring'), None),
(('Fedora', '17', 'Beefy Miracle'), None),
(('debian', '6.0.6', ''), ('debian', '6', '')),
(('debian', '7.0', ''), ('debian', '7', '')),
]:
self.assertEqual(distribution._debianize(provided), expected or provided)
class TestOSMatches(unittest.TestCase):
def test_centos(self):
self.assertFalse(distribution.os_matches(('CentOS', '6.3', 'Final'),
('Ubuntu', '13.04', 'raring')))
self.assertFalse(distribution.os_matches(('CentOS', '6.3', 'Final'),
('debian', '6.3', '')))
def test_ubuntu(self):
self.assertFalse(distribution.os_matches(('Ubuntu', '12.04', 'precise'),
('Ubuntu', '13.04', 'raring')))
self.assertTrue(distribution.os_matches(('Ubuntu', '13.04', 'raring'),
('Ubuntu', '13.04', 'raring')))
self.assertTrue(distribution.os_matches(('Ubuntu', '12.04', 'precise'),
('Ubuntu', '12.04', 'precise')))
def test_debian(self):
self.assertFalse(distribution.os_matches(('debian', '6.0.6', ''),
('debian', '7.0', '')))
self.assertTrue(distribution.os_matches(('debian', '6.0.6', ''),
('debian', '6.0.5', '')))
self.assertTrue(distribution.os_matches(('debian', '6.0.6', ''),
('debian', '6.1', '')))
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