X-Git-Url: https://code.grnet.gr/git/kamaki/blobdiff_plain/144b3551ba1117d59097c08226e9a72141ac46f5..df0045d8e5c4a697bbdb8d324a7ced41ab24bd6b:/kamaki/cli/__init__.py diff --git a/kamaki/cli/__init__.py b/kamaki/cli/__init__.py index dc36e04..b9ef171 100644 --- a/kamaki/cli/__init__.py +++ b/kamaki/cli/__init__.py @@ -33,7 +33,7 @@ import logging from sys import argv, exit, stdout -from os.path import basename +from os.path import basename, exists from inspect import getargspec from kamaki.cli.argument import ArgumentParseManager @@ -194,10 +194,9 @@ def _setup_logging(silent=False, debug=False, verbose=False, include=False): def _check_config_version(cnf): guess = cnf.guess_version() - if guess < 3.0: - print('Config file format version >= 3.0 is required') - print('Configuration file "%s" format is not up to date' % ( - cnf.path)) + if exists(cnf.path) and guess < 0.9: + print('Config file format version >= 9.0 is required') + print('Configuration file: %s' % cnf.path) print('but kamaki can fix this:') print('Calculating changes while preserving information') lost_terms = cnf.rescue_old_file() @@ -234,6 +233,10 @@ def _init_session(arguments, is_non_API=False): global _verbose _verbose = arguments['verbose'].value _cnf = arguments['config'] + + if _help or is_non_API: + return None, None + _check_config_version(_cnf.value) global _colors @@ -244,28 +247,49 @@ def _init_session(arguments, is_non_API=False): _silent = arguments['silent'].value _setup_logging(_silent, _debug, _verbose, _include) - if _help or is_non_API: - return None, None - - cloud = arguments['cloud'].value or 'default' + cloud = arguments['cloud'].value or _cnf.value.get( + 'global', 'default_cloud') + if not cloud: + num_of_clouds = len(_cnf.value.keys('cloud')) + if num_of_clouds == 1: + cloud = _cnf.value.keys('cloud')[0] + elif num_of_clouds > 1: + raise CLIError( + 'Found %s clouds but none of them is set as default' % ( + num_of_clouds), + importance=2, details=[ + 'Please, choose one of the following cloud names:', + ', '.join(_cnf.value.keys('cloud')), + 'To see all cloud settings:', + ' kamaki config get cloud.', + 'To set a default cloud:', + ' kamaki config set default_cloud ', + 'To pick a cloud for the current session, use --cloud:', + ' kamaki --cloud= ...']) if not cloud in _cnf.value.keys('cloud'): raise CLIError( - 'No cloud "%s" is configured' % cloud, + 'No cloud%s is configured' % ((' "%s"' % cloud) if cloud else ''), importance=3, details=[ - 'To configure a new cloud, find and set the', + 'To configure a new cloud "%s", find and set the' % ( + cloud or ''), 'single authentication URL and token:', - ' kamaki config set cloud.%s.url ' % cloud, - ' kamaki config set cloud.%s.token ' % cloud]) + ' kamaki config set cloud.%s.url ' % ( + cloud or ''), + ' kamaki config set cloud.%s.token ' % ( + cloud or '')]) auth_args = dict() for term in ('url', 'token'): - auth_args[term] = _cnf.get_cloud(cloud, term) + try: + auth_args[term] = _cnf.get_cloud(cloud, term) + except KeyError: + auth_args[term] = '' if not auth_args[term]: raise CLIError( - 'No authentication %s provided for %s cloud' % (term, cloud), + 'No authentication %s provided for cloud "%s"' % (term, cloud), importance=3, details=[ - 'Get and set a %s for %s cloud:' % (term, cloud), - ' kamaki config set cloud.%s.%s ' % (term, cloud) - ]) + 'Set a %s for cloud %s:' % (term, cloud), + ' kamaki config set cloud.%s.%s <%s>' % ( + cloud, term, term)]) from kamaki.clients.astakos import AstakosClient as AuthCachedClient try: @@ -461,7 +485,9 @@ def main(): auth_base, cloud = _init_session(parser.arguments, is_non_API(parser)) from kamaki.cli.utils import suggest_missing - suggest_missing() + global _colors + exclude = ['ansicolors'] if not _colors == 'on' else [] + suggest_missing(exclude=exclude) if parser.unparsed: run_one_cmd(exe, parser, auth_base, cloud)