def logs(args): """`conduct logs` command""" log = logging.getLogger(__name__) request_url = conduct_url.url( 'bundles/{}/logs?count={}'.format(quote_plus(args.bundle), args.lines), args) # At the time when this comment is being written, we need to pass the Host header when making HTTP request due to # a bug with requests python library not working properly when IPv6 address is supplied: # https://github.com/kennethreitz/requests/issues/3002 # The workaround for this problem is to explicitly set the Host header when making HTTP request. # This fix is benign and backward compatible as the library would do this when making HTTP request anyway. response = requests.get(request_url, timeout=DEFAULT_HTTP_TIMEOUT, headers=conduct_url.request_headers(args)) validation.raise_for_status_inc_3xx(response) data = [{ 'time': validation.format_timestamp(event['timestamp'], args), 'host': event['host'], 'log': event['message'] } for event in json.loads(response.text)] data.insert(0, {'time': 'TIME', 'host': 'HOST', 'log': 'LOG'}) padding = 2 column_widths = dict(screen_utils.calc_column_widths(data), **{'padding': ' ' * padding}) for row in data: log.screen('''\ {time: <{time_width}}{padding}\ {host: <{host_width}}{padding}\ {log: <{log_width}}{padding}'''.format(**dict(row, **column_widths)).rstrip()) return True
def test_format_date_timestamp_utc(self): timestamp = '2015-08-24T01:16:22.327Z' args = MagicMock() args.date = True args.utc = True result = validation.format_timestamp(timestamp, args) self.assertEqual('2015-08-24T01:16:22Z', result)
def test_format_date_timestamp(self): timestamp = '2015-08-24T01:16:22.327Z' args = MagicMock(**{'utc': False}) result = validation.format_timestamp(timestamp, args) expected_result = arrow.get(timestamp).to('local').strftime( '%a %Y-%m-%dT%H:%M:%S%z') self.assertEqual(expected_result, result)
def logs(args): """`conduct logs` command""" log = logging.getLogger(__name__) request_url = conduct_url.url('bundles/{}/logs?count={}'.format(quote_plus(args.bundle), args.lines), args) # At the time when this comment is being written, we need to pass the Host header when making HTTP request due to # a bug with requests python library not working properly when IPv6 address is supplied: # https://github.com/kennethreitz/requests/issues/3002 # The workaround for this problem is to explicitly set the Host header when making HTTP request. # This fix is benign and backward compatible as the library would do this when making HTTP request anyway. response = requests.get(request_url, timeout=DEFAULT_HTTP_TIMEOUT, headers=conduct_url.request_headers(args)) validation.raise_for_status_inc_3xx(response) data = [ { 'time': validation.format_timestamp(event['timestamp'], args), 'host': event['host'], 'log': event['message'] } for event in json.loads(response.text) ] data.insert(0, {'time': 'TIME', 'host': 'HOST', 'log': 'LOG'}) padding = 2 column_widths = dict(screen_utils.calc_column_widths(data), **{'padding': ' ' * padding}) for row in data: log.screen('''\ {time: <{time_width}}{padding}\ {host: <{host_width}}{padding}\ {log: <{log_width}}{padding}'''.format(**dict(row, **column_widths)).rstrip()) return True
def test_format_timestamp_utc(self): timestamp = '2015-08-24T01:16:22.327Z' args = MagicMock() args.date = False args.utc = True result = validation.format_timestamp(timestamp, args) self.assertEqual('01:16:22Z', result)
def events(args): """`conduct events` command""" log = logging.getLogger(__name__) request_url = conduct_url.url('bundles/{}/events?count={}'.format(quote_plus(args.bundle), args.lines), args) response = conduct_request.get(args.dcos_mode, conductr_host(args), request_url, auth=args.conductr_auth, verify=args.server_verification_file, timeout=DEFAULT_HTTP_TIMEOUT) validation.raise_for_status_inc_3xx(response) data = [ { 'time': validation.format_timestamp(event['timestamp'], args), 'event': event['event'], 'description': event['description'] } for event in json.loads(response.text) ] data.insert(0, {'time': 'TIME', 'event': 'EVENT', 'description': 'DESC'}) padding = 2 column_widths = dict(screen_utils.calc_column_widths(data), **{'padding': ' ' * padding}) for row in data: log.screen('''\ {time: <{time_width}}{padding}\ {event: <{event_width}}{padding}\ {description: <{description_width}}{padding}'''.format(**dict(row, **column_widths)).rstrip()) return True
def events(args): """`conduct events` command""" log = logging.getLogger(__name__) request_url = conduct_url.url( 'bundles/{}/events?count={}'.format(quote_plus(args.bundle), args.lines), args) response = conduct_request.get(args.dcos_mode, conductr_host(args), request_url, auth=args.conductr_auth, verify=args.server_verification_file, timeout=DEFAULT_HTTP_TIMEOUT) validation.raise_for_status_inc_3xx(response) data = [{ 'time': validation.format_timestamp(event['timestamp'], args), 'event': event['event'], 'description': event['description'] } for event in json.loads(response.text)] data.insert(0, {'time': 'TIME', 'event': 'EVENT', 'description': 'DESC'}) padding = 2 column_widths = dict(screen_utils.calc_column_widths(data), **{'padding': ' ' * padding}) for row in data: log.screen('''\ {time: <{time_width}}{padding}\ {event: <{event_width}}{padding}\ {description: <{description_width}}{padding}'''.format( **dict(row, **column_widths)).rstrip()) return True
def test_format_timestamp(self): timestamp = '2015-08-24T01:16:22.327Z' args = MagicMock() args.date = False args.utc = False result = validation.format_timestamp(timestamp, args) expected_result = arrow.get(timestamp).to('local').datetime.strftime('%X') self.assertEqual(expected_result, result)
def test_format_date_timestamp_utc(self): timestamp = '2015-08-24T01:16:22.327Z' args = MagicMock(**{'utc': True}) result = validation.format_timestamp(timestamp, args) self.assertEqual('Mon 2015-08-24T01:16:22Z', result)