def test_add_stream_logger(self, GL): from kamaki.cli.logger import add_stream_logger with patch('kamaki.cli.logger._add_logger', return_value='AL') as AL: for name, level, fmt in product( ('my name'), ('my level', None), ('my fmt', None)): self.assertEqual(add_stream_logger(name, level, fmt), 'AL') self.assertEqual(AL.mock_calls[-1], call(name, level, fmt=fmt)) with patch('kamaki.cli.logger._add_logger', side_effect=Exception): self.assertEqual(add_stream_logger('X'), 'my get logger ret') GL.assert_called_once_with('X')
def test_add_stream_logger(self, GL): from kamaki.cli.logger import add_stream_logger with patch('kamaki.cli.logger._add_logger', return_value='AL') as AL: for name, level, fmt in product(('my name'), ('my level', None), ('my fmt', None)): self.assertEqual(add_stream_logger(name, level, fmt), 'AL') self.assertEqual(AL.mock_calls[-1], call(name, level, fmt=fmt)) with patch('kamaki.cli.logger._add_logger', side_effect=Exception): self.assertEqual(add_stream_logger('X'), 'my get logger ret') GL.assert_called_once_with('X')
# # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.cli.config import Config cnf = Config() CLOUD = cnf.get('global', 'default_cloud') URL = cnf.get_cloud(CLOUD, 'url') TOKEN = cnf.get_cloud(CLOUD, 'token') from kamaki.cli import logger logger.add_stream_logger('kamaki.clients.recv', fmt='< %(message)s') logger.add_stream_logger('kamaki.clients.send', fmt='> %(message)s') from kamaki.clients import astakos, pithos, cyclades, image from kamaki.clients.utils import https https.patch_with_certs('/etc/ssl/certs/ca-certificates.crt') identity_client = astakos.AstakosClient(URL, TOKEN) pithosURL = identity_client.get_endpoint_url(pithos.PithosClient.service_type) storage_client = pithos.PithosClient(pithosURL, TOKEN) storage_client.account = identity_client.user_info['id'] storage_client.container = 'pithos' imageURL = identity_client.get_endpoint_url(image.ImageClient.service_type)
def wrap(): try: exe = basename(argv[0]) internal_argv = [] for i, a in enumerate(argv): try: internal_argv.append(a.decode(pref_enc)) except UnicodeDecodeError as ude: raise CLIError( 'Invalid encoding in command', importance=3, details=[ 'The invalid term is #%s (with "%s" being 0)' % (i, exe), 'Encoding is invalid with current locale settings ' '(%s)' % pref_enc, '( %s )' % ude ]) for i, a in enumerate(internal_argv): argv[i] = a logger.add_stream_logger( __name__, logging.WARNING, fmt='%(levelname)s (%(name)s): %(message)s') _config_arg = ConfigArgument('Path to a custom config file') parser = ArgumentParseManager( exe, arguments=dict( config=_config_arg, cloud=ValueArgument('Chose a cloud to connect to', ('--cloud')), help=Argument(0, 'Show help message', ('-h', '--help')), debug=FlagArgument('Include debug output', ('-d', '--debug')), verbose=FlagArgument( 'Show HTTP requests and responses, without HTTP body', ('-v', '--verbose')), verbose_with_data=FlagArgument( 'Show HTTP requests and responses, including HTTP body', ('-vv', '--verbose-with-data')), version=VersionArgument('Print current version', ('-V', '--version')), options=RuntimeConfigArgument( _config_arg, 'Override a config option (not persistent)', ('-o', '--options')), ignore_ssl=FlagArgument( 'Allow connections to SSL sites without certs', ('-k', '--ignore-ssl', '--insecure')), ca_file=ValueArgument( 'CA certificates for SSL authentication', '--ca-certs'), )) if parser.arguments['version'].value: exit(0) _cnf = parser.arguments['config'] log_file = _cnf.get('global', 'log_file') if log_file: logger.set_log_filename(log_file) filelog = logger.add_file_logger(__name__.split('.')[0]) filelog.info('%s\n- - -' % ' '.join(argv)) _colors = _cnf.value.get('global', 'colors') exclude = ['ansicolors'] if not _colors == 'on' else [] suggest_missing(exclude=exclude) func(exe, parser) except CLIError as err: print_error_message(err) if _debug: raise err exit(1) except KamakiSSLError as err: ca_arg = parser.arguments.get('ca_file') ca = ca_arg.value if ca_arg and ca_arg.value else _cnf.get( 'global', 'ca_certs') stderr.write(red('SSL Authentication failed\n')) if ca: stderr.write('Path used for CA certifications file: %s\n' % (escape_ctrl_chars(ca))) stderr.write('Please make sure the path is correct\n') if not (ca_arg and ca_arg.value): stderr.write('| To set the correct path:\n') stderr.write('| kamaki config set ca_certs CA_FILE\n') else: stderr.write('| To use a CA certifications file:\n') stderr.write('| kamaki config set ca_certs CA_FILE\n') stderr.write('| OR run with --ca-certs=FILE_LOCATION\n') stderr.write('| To ignore SSL errors and move on (%s):\n' % (red('insecure'))) stderr.write('| kamaki config set ignore_ssl on\n') stderr.write('| OR run with --ignore-ssl\n') stderr.flush() if _debug: raise stderr.write('| %s: %s\n' % (type(err), escape_ctrl_chars('%s' % err))) stderr.flush() exit(1) except KeyboardInterrupt: print('Canceled by user') exit(1) except Exception as er: print('Unknown Error: %s' % er) if _debug: raise exit(1)
def _setup_logging(debug=False, verbose=False, _verbose_with_data=False): """handle logging for clients package""" sfmt, rfmt = '> %(message)s', '< %(message)s' if debug: print('Logging location: %s' % logger.get_log_filename()) logger.add_stream_logger('kamaki.clients.send', logging.DEBUG, sfmt) logger.add_stream_logger('kamaki.clients.recv', logging.DEBUG, rfmt) logger.add_stream_logger(__name__, logging.DEBUG) elif verbose: logger.add_stream_logger('kamaki.clients.send', DEBUGV, sfmt) logger.add_stream_logger('kamaki.clients.recv', DEBUGV, rfmt) logger.add_stream_logger(__name__, DEBUGV) # else: # logger.add_stream_logger(__name__, logging.WARNING) if _verbose_with_data: from kamaki import clients clients.Client.LOG_DATA = True global kloger kloger = logger.get_logger(__name__)
def wrap(): try: exe = basename(argv[0]) internal_argv = [] for i, a in enumerate(argv): try: internal_argv.append(a.decode(pref_enc)) except UnicodeDecodeError as ude: raise CLIError( 'Invalid encoding in command', importance=3, details=[ 'The invalid term is #%s (with "%s" being 0)' % ( i, exe), 'Encoding is invalid with current locale settings ' '(%s)' % pref_enc, '( %s )' % ude]) for i, a in enumerate(internal_argv): argv[i] = a logger.add_stream_logger( __name__, logging.WARNING, fmt='%(levelname)s (%(name)s): %(message)s') _config_arg = ConfigArgument('Path to config file') parser = ArgumentParseManager(exe, arguments=dict( config=_config_arg, cloud=ValueArgument( 'Chose a cloud to connect to', ('--cloud')), help=Argument(0, 'Show help message', ('-h', '--help')), debug=FlagArgument('Include debug output', ('-d', '--debug')), verbose=FlagArgument( 'More info at response', ('-v', '--verbose')), version=VersionArgument( 'Print current version', ('-V', '--version')), options=RuntimeConfigArgument( _config_arg, 'Override a config value', ('-o', '--options')), ignore_ssl=FlagArgument( 'Allow connections to SSL sites without certs', ('-k', '--ignore-ssl', '--insecure')), ca_file=ValueArgument( 'CA certificates for SSL authentication', '--ca-certs'),) ) if parser.arguments['version'].value: exit(0) _cnf = parser.arguments['config'] log_file = _cnf.get('global', 'log_file') if log_file: logger.set_log_filename(log_file) filelog = logger.add_file_logger(__name__.split('.')[0]) filelog.info('%s\n- - -' % ' '.join(argv)) _colors = _cnf.value.get('global', 'colors') exclude = ['ansicolors'] if not _colors == 'on' else [] suggest_missing(exclude=exclude) func(exe, parser) except CLIError as err: print_error_message(err) if _debug: raise err exit(1) except KamakiSSLError as err: ca_arg = parser.arguments.get('ca_file') ca = ca_arg.value if ca_arg and ca_arg.value else _cnf.get( 'global', 'ca_certs') stderr.write(red('SSL Authentication failed\n')) if ca: stderr.write('Path used for CA certifications file: %s\n' % ( escape_ctrl_chars(ca))) stderr.write('Please make sure the path is correct\n') if not (ca_arg and ca_arg.value): stderr.write('| To set the correct path:\n') stderr.write('| kamaki config set ca_certs CA_FILE\n') else: stderr.write('| To use a CA certifications file:\n') stderr.write('| kamaki config set ca_certs CA_FILE\n') stderr.write('| OR run with --ca-certs=FILE_LOCATION\n') stderr.write('| To ignore SSL errors and move on (%s):\n' % ( red('insecure'))) stderr.write('| kamaki config set ignore_ssl on\n') stderr.write('| OR run with --ignore-ssl\n') stderr.flush() if _debug: raise stderr.write('| %s: %s\n' % ( type(err), escape_ctrl_chars('%s' % err))) stderr.flush() exit(1) except KeyboardInterrupt: print('Canceled by user') exit(1) except Exception as er: print('Unknown Error: %s' % er) if _debug: raise exit(1)
def _setup_logging(debug=False, verbose=False): """handle logging for clients package""" sfmt, rfmt = '> %(message)s', '< %(message)s' if debug: print('Logging location: %s' % logger.get_log_filename()) logger.add_stream_logger('kamaki.clients.send', logging.DEBUG, sfmt) logger.add_stream_logger('kamaki.clients.recv', logging.DEBUG, rfmt) logger.add_stream_logger(__name__, logging.DEBUG) elif verbose: logger.add_stream_logger('kamaki.clients.send', logging.INFO, sfmt) logger.add_stream_logger('kamaki.clients.recv', logging.INFO, rfmt) logger.add_stream_logger(__name__, logging.INFO) # else: # logger.add_stream_logger(__name__, logging.WARNING) global kloger kloger = logger.get_logger(__name__)
# The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.cli.config import Config cnf = Config() CLOUD = cnf.get('global', 'default_cloud') URL = cnf.get_cloud(CLOUD, 'url') TOKEN = cnf.get_cloud(CLOUD, 'token') from kamaki.cli import logger logger.add_stream_logger('kamaki.clients.recv', fmt='< %(message)s') logger.add_stream_logger('kamaki.clients.send', fmt='> %(message)s') from kamaki.clients import astakos, pithos, cyclades, image from kamaki.clients.utils import https https.patch_with_certs('/etc/ssl/certs/ca-certificates.crt') identity_client = astakos.AstakosClient(URL, TOKEN) pithosURL = identity_client.get_endpoint_url(pithos.PithosClient.service_type) storage_client = pithos.PithosClient(pithosURL, TOKEN) storage_client.account = identity_client.user_info['id'] storage_client.container = 'pithos'