From 539ffd60f0c7f6713b09b6c3b5395652b7d16d45 Mon Sep 17 00:00:00 2001 From: Michael Gummelt Date: Fri, 26 Jun 2015 17:10:50 -0700 Subject: [PATCH] better app status info in dcos marathon app list --- cli/dcoscli/marathon/main.py | 10 +++++- cli/dcoscli/tables.py | 57 ++++++++++++++++++++++++++++------- cli/tests/unit/data/app.txt | 4 +-- cli/tests/unit/test_tables.py | 8 +++-- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/cli/dcoscli/marathon/main.py b/cli/dcoscli/marathon/main.py index 30a8581..6615244 100644 --- a/cli/dcoscli/marathon/main.py +++ b/cli/dcoscli/marathon/main.py @@ -360,7 +360,15 @@ def _list(json_): client = marathon.create_client() apps = client.get_apps() - emitting.publish_table(emitter, apps, tables.app_table, json_) + if json_: + emitter.publish(apps) + else: + deployments = client.get_deployments() + table = tables.app_table(apps, deployments) + output = str(table) + if output: + emitter.publish(output) + return 0 diff --git a/cli/dcoscli/tables.py b/cli/dcoscli/tables.py index 83bb8f5..113fa86 100644 --- a/cli/dcoscli/tables.py +++ b/cli/dcoscli/tables.py @@ -3,6 +3,15 @@ from collections import OrderedDict from dcos import util +EMPTY_ENTRY = '---' + +DEPLOYMENT_DISPLAY = {'ResolveArtifacts': 'artifacts', + 'ScaleApplication': 'scale', + 'StartApplication': 'start', + 'StopApplication': 'stop', + 'RestartApplication': 'restart', + 'KillAllOldTasksOf': 'kill-tasks'} + def task_table(tasks): """Returns a PrettyTable representation of the provided mesos tasks. @@ -28,7 +37,7 @@ def task_table(tasks): return tb -def app_table(apps): +def app_table(apps, deployments): """Returns a PrettyTable representation of the provided apps. :param tasks: apps to render @@ -36,6 +45,10 @@ def app_table(apps): :rtype: PrettyTable """ + deployment_map = {} + for deployment in deployments: + deployment_map[deployment['id']] = deployment + def get_cmd(app): if app["cmd"] is not None: return app["cmd"] @@ -48,13 +61,40 @@ def app_table(apps): else: return "mesos" + def get_health(app): + if app["healthChecks"]: + return "{}/{}".format(app["tasksHealthy"], + app["tasksRunning"]) + else: + return EMPTY_ENTRY + + def get_deployment(app): + deployment_ids = {deployment['id'] + for deployment in app['deployments']} + + actions = [] + for deployment_id in deployment_ids: + deployment = deployment_map.get(deployment_id) + if deployment: + for action in deployment['currentActions']: + if action['app'] == app['id']: + actions.append(DEPLOYMENT_DISPLAY[action['action']]) + + if len(actions) == 0: + return EMPTY_ENTRY + elif len(actions) == 1: + return actions[0] + else: + return "({})".format(", ".join(actions)) + fields = OrderedDict([ ("ID", lambda a: a["id"]), ("MEM", lambda a: a["mem"]), ("CPUS", lambda a: a["cpus"]), - ("DEPLOYMENTS", lambda a: len(a["deployments"])), ("TASKS", lambda a: "{}/{}".format(a["tasksRunning"], a["instances"])), + ("HEALTH", get_health), + ("DEPLOYMENT", get_deployment), ("CONTAINER", get_container), ("CMD", get_cmd) ]) @@ -101,12 +141,6 @@ def deployment_table(deployments): """ def get_action(deployment): - action_map = {'ResolveArtifacts': 'artifacts', - 'ScaleApplication': 'scale', - 'StartApplication': 'start', - 'StopApplication': 'stop', - 'RestartApplication': 'restart', - 'KillAllOldTasksOf': 'kill-tasks'} multiple_apps = len({action['app'] for action in deployment['currentActions']}) > 1 @@ -114,7 +148,7 @@ def deployment_table(deployments): ret = [] for action in deployment['currentActions']: try: - action_display = action_map[action['action']] + action_display = DEPLOYMENT_DISPLAY[action['action']] except KeyError: raise ValueError( 'Unknown Marathon action: {}'.format(action['action'])) @@ -227,9 +261,10 @@ def package_table(packages): fields = OrderedDict([ ('NAME', lambda p: p['name']), - ('APP', lambda p: '\n'.join(p['apps']) if p.get('apps') else '---'), + ('APP', + lambda p: '\n'.join(p['apps']) if p.get('apps') else EMPTY_ENTRY), ('COMMAND', - lambda p: p['command']['name'] if 'command' in p else '---'), + lambda p: p['command']['name'] if 'command' in p else EMPTY_ENTRY), ('DESCRIPTION', lambda p: p['description']) ]) diff --git a/cli/tests/unit/data/app.txt b/cli/tests/unit/data/app.txt index 542ac36..64240dc 100644 --- a/cli/tests/unit/data/app.txt +++ b/cli/tests/unit/data/app.txt @@ -1,2 +1,2 @@ - ID MEM CPUS DEPLOYMENTS TASKS CONTAINER CMD - /test-app 16.0 0.1 0 1/1 mesos sleep 1000 \ No newline at end of file + ID MEM CPUS TASKS HEALTH DEPLOYMENT CONTAINER CMD + /test-app 16.0 0.1 1/1 --- --- mesos sleep 1000 \ No newline at end of file diff --git a/cli/tests/unit/test_tables.py b/cli/tests/unit/test_tables.py index e696d4e..08a41d6 100644 --- a/cli/tests/unit/test_tables.py +++ b/cli/tests/unit/test_tables.py @@ -14,9 +14,11 @@ def test_task_table(): def test_app_table(): - _test_table(tables.app_table, - app_fixture, - 'tests/unit/data/app.txt') + apps = [app_fixture()] + deployments = [] + table = tables.app_table(apps, deployments) + with open('tests/unit/data/app.txt') as f: + assert str(table) == f.read() def test_deployment_table():