Ejemplo n.º 1
0
def tabulate_token_services(services, token_etag, token_name):
    """Returns a table displaying the service info"""
    num_services = len(services)
    if num_services > 0:
        num_failing_services = len([s for s in services if s['status'] == 'Failing'])
        total_mem_usage = format_memory_amount(sum(s['service-description']['mem'] for s in services))
        total_cpu_usage = round(sum(s['service-description']['cpus'] for s in services), 2)
        table = [['# Services', num_services],
                 ['# Failing', num_failing_services],
                 ['Total Memory', total_mem_usage],
                 ['Total CPUs', total_cpu_usage]]
        summary_table = tabulate(table, tablefmt='plain')

        services = sorted(services, key=lambda s: s.get('last-request-time', None) or '', reverse=True)
        rows = [collections.OrderedDict([('Service Id', s['service-id']),
                                         ('Run as user', s['service-description']['run-as-user']),
                                         ('CPUs', s['service-description']['cpus']),
                                         ('Memory', format_mem_field(s['service-description'])),
                                         ('Version', s['service-description']['version']),
                                         ('Status', format_status(s['status'])),
                                         ('Last request', format_last_request_time(s)),
                                         ('Current?', format_using_current_token(s, token_etag, token_name))])
                for s in services]
        service_table = tabulate(rows, headers='keys', tablefmt='plain')
        return f'\n\n{summary_table}\n\n{service_table}'
    else:
        return ''
Ejemplo n.º 2
0
def tabulate_token(cluster_name, token, token_name, services, token_etag):
    """Given a token, returns a string containing tables for the fields"""
    table = [['Owner', token['owner']]]
    if token.get('name'):
        table.append(['Name', token['name']])
    if token.get('cpus'):
        table.append(['CPUs', token['cpus']])
    if token.get('mem'):
        table.append(['Memory', format_mem_field(token)])
    if token.get('ports'):
        table.append(['Ports requested', token['ports']])
    if token.get('cmd-type'):
        table.append(['Command type', token['cmd-type']])
    if token.get('health-check-url'):
        table.append(['Health check endpoint', token['health-check-url']])
    if token.get('permitted-user'):
        table.append(['Permitted user(s)', token['permitted-user']])

    explicit_keys = ('cmd', 'cmd-type', 'cpus', 'env', 'health-check-url',
                     'last-update-time', 'last-update-user', 'mem', 'name',
                     'owner', 'permitted-user', 'ports')
    ignored_keys = ('cluster', 'previous', 'root')
    for key, value in token.items():
        if key not in (explicit_keys + ignored_keys):
            table.append([format_field_name(key), value])

    command = token.get('cmd')
    if command:
        token_command = f'Command:\n{command}'
    else:
        token_command = '<No command specified>'

    if token.get('env') and len(token['env']) > 0:
        environment = '\n\nEnvironment:\n%s' % '\n'.join(
            ['%s=%s' % (k, v) for k, v in token['env'].items()])
    else:
        environment = ''

    table_text = tabulate(table, tablefmt='plain')
    last_update_time = format_timestamp_string(token['last-update-time'])
    last_update_user = f' ({token["last-update-user"]})' if 'last-update-user' in token else ''
    service_table = tabulate_token_services(services, token_etag, token_name)
    return f'\n' \
        f'=== {terminal.bold(cluster_name)} / {terminal.bold(token_name)} ===\n' \
        f'\n' \
        f'Last Updated: {last_update_time}{last_update_user}\n' \
        f'\n' \
        f'{table_text}\n' \
        f'\n' \
        f'{token_command}' \
        f'{environment}' \
        f'{service_table}'