Commit e533396a authored by Marco Mariani's avatar Marco Mariani

cli refactoring: cache-lookup

parent 7c5f3e89
......@@ -44,6 +44,7 @@ setup(name=name,
'zope.interface', # slap library implementes interfaces
] + additional_install_requires,
extra_requires={'docs': ('Sphinx', 'repoze.sphinx.autointerface'),},
......@@ -69,6 +70,10 @@ setup(name=name,
'slapgrid-supervisorctl = slapos.grid.svcbackend:supervisorctl',
'slapgrid-supervisord = slapos.grid.svcbackend:supervisord',
'bang = slapos.bang:main',
'slap2 = slapos.cli.entry:main',
'slapos.cli': [
'cache lookup = slapos.cli.cache:CacheLookup',
......@@ -16,29 +16,20 @@ from slapos.grid.distribution import patched_linux_distribution
def maybe_md5(s):
return re.match('[0-9a-f]{32}', s)
def cache_lookup():
parser = argparse.ArgumentParser()
parser.add_argument("configuration_file", help="SlapOS configuration file")
parser.add_argument("software_url", help="Your software url or MD5 hash")
args = parser.parse_args()
configuration_parser = ConfigParser.SafeConfigParser()
def do_lookup(config, software_url):
cache_dir = config.get('networkcache', 'download-binary-dir-url')
cache_dir = configuration_parser.get('networkcache', 'download-binary-dir-url')
if maybe_md5(args.software_url):
md5 = args.software_url
if maybe_md5(software_url):
md5 = software_url
md5 = hashlib.md5(args.software_url).hexdigest()
md5 = hashlib.md5(software_url).hexdigest()
response = urllib2.urlopen('%s/%s' % (cache_dir, md5))
except urllib2.HTTPError as e:
if e.code == 404:
print 'Object not in cache: %s' % args.software_url
print 'Object not in cache: %s' % software_url
print 'Error during cache lookup: %s (%s)' % (e.code, e.reason)
......@@ -68,3 +59,14 @@ def cache_lookup():
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)
def cache_lookup():
parser = argparse.ArgumentParser()
parser.add_argument("configuration_file", help="SlapOS configuration file")
parser.add_argument("software_url", help="Your software url or MD5 hash")
args = parser.parse_args()
config = ConfigParser.SafeConfigParser()
do_lookup(config, args.software_url)
# -*- coding: utf-8 -*-
import logging
from slapos.cli.config import ConfigCommand
from slapos.cache import do_lookup
class CacheLookup(ConfigCommand):
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(CacheLookup, self).get_parser(prog_name)
# XXX this argument could use a better name
help="Your software url or MD5 hash")
return parser
def take_action(self, args):
config = self.fetch_config(args)
do_lookup(config, args.software_url)
# -*- coding: utf-8 -*-
import ConfigParser
import os
from cliff.command import Command
class ConfigError(Exception):
class ConfigCommand(Command):
"Base class for commands that require a configuration file"
# TODO: what commands need slapos.cfg / slapos_client.cfg?
# XXX: always use SafeConfigParser, or sometimes RawConfigParser?
log = None
default_config_var = 'SLAPOS_CONFIGURATION'
# use this is default_config_var does not exist
default_config_path = '/etc/opt/slapos/slapos.cfg'
def get_parser(self, prog_name):
parser = super(ConfigCommand, self).get_parser(prog_name)
parser.add_argument('--cfg', help='SlapOS configuration file')
return parser
def _get_config(self, cfg_path, required=False):
Returns a configuration object if file exists/readable/valid,
None otherwise.
Will raise an error instead of returning None if required is True.
Even if required is False, may still raise an exception from the
configparser if the configuration content is very broken.
We don't catch that exception as it will clearly show what is
wrong with the file.
if not os.path.exists(cfg_path):
if required:
raise ConfigError('Configuration file does not exist: %s' % cfg_path)
return None
config = ConfigParser.SafeConfigParser()
if != [cfg_path]:
# bad permission, etc.
if required:
raise ConfigError('Cannot parse configuration file: %s' % cfg_path)
return None
return config
def fetch_config(self, args):
if args.cfg:
cfg_path = os.path.expanduser(args.cfg)
cfg_path = os.environ.get(self.default_config_var, self.default_config_path)
self.log.debug('Loading config: %s' % cfg_path)
return self._get_config(cfg_path, required=True)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import sys
import cliff
import cliff.commandmanager
import slapos.version
class SlapOSCommandManager(cliff.commandmanager.CommandManager):
def find_command(self, argv):
# XXX a little cheating, 'slapos node' is not documented
# by the help command
if argv == ['node']:
argv = ['node', 'status']
return super(SlapOSCommandManager, self).find_command(argv)
class SlapOSApp(
log = logging.getLogger(__name__)
def __init__(self):
super(SlapOSApp, self).__init__(
description='SlapOS client',
def initialize_app(self, argv):
def prepare_to_run_command(self, cmd):
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
def clean_up(self, cmd, result, err):
self.log.debug('clean_up %s', cmd.__class__.__name__)
if err:
self.log.debug('got an error: %s', err)
def main(argv=sys.argv[1:]):
app = SlapOSApp()
if __name__ == '__main__':
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