From 43aa7f7b3bb212ee50d6c4b313ff3cb88a969753 Mon Sep 17 00:00:00 2001 From: tamarrow Date: Fri, 7 Oct 2016 15:08:22 -0700 Subject: [PATCH] handle `dcos task` commands when no task is given (#796) --- cli/dcoscli/task/main.py | 25 +++++++++++++------------ dcos/mesos.py | 12 +++++++----- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cli/dcoscli/task/main.py b/cli/dcoscli/task/main.py index 5d3d193..3c484f9 100644 --- a/cli/dcoscli/task/main.py +++ b/cli/dcoscli/task/main.py @@ -86,9 +86,6 @@ def _task(task, completed, json_): :returns: process return code """ - if task is None: - task = "" - tasks = sorted(mesos.get_master().tasks(completed=completed, fltr=task), key=lambda t: t['name']) @@ -120,10 +117,7 @@ def _log(follow, completed, lines, task, file_): :rtype: int """ - if task is None: - fltr = "" - else: - fltr = task + fltr = task if file_ is None: file_ = 'stdout' @@ -138,7 +132,9 @@ def _log(follow, completed, lines, task, file_): tasks = master.tasks(completed=completed, fltr=fltr) if not tasks: - if not completed: + if not fltr: + raise DCOSException("No tasks found. Exiting.") + elif not completed: completed_tasks = master.tasks(completed=True, fltr=fltr) if completed_tasks: msg = 'No running tasks match ID [{}]; however, there '.format( @@ -154,6 +150,10 @@ def _log(follow, completed, lines, task, file_): mesos_files = _mesos_files(tasks, file_, client) if not mesos_files: + if fltr is None: + msg = "No tasks found. Exiting" + else: + msg = "No matching tasks. Exiting." raise DCOSException('No matching tasks. Exiting.') log.log_files(mesos_files, follow, lines) @@ -176,8 +176,6 @@ def _ls(task, path, long_, completed): :rtype: int """ - if task is None: - task = "" if path is None: path = '.' if path.startswith('/'): @@ -189,8 +187,11 @@ def _ls(task, path, long_, completed): completed=completed) if len(task_objects) == 0: - raise DCOSException( - 'Cannot find a task with ID containing "{}"'.format(task)) + if task is None: + raise DCOSException("No tasks found") + else: + raise DCOSException( + 'Cannot find a task with ID containing "{}"'.format(task)) try: all_files = [] diff --git a/dcos/mesos.py b/dcos/mesos.py index 7b5f1cc..f2d14f7 100644 --- a/dcos/mesos.py +++ b/dcos/mesos.py @@ -415,12 +415,12 @@ class Master(object): for slave in self.state()['slaves'] if fltr in slave['id']] - def tasks(self, fltr="", completed=False): + def tasks(self, fltr=None, completed=False): """Returns tasks running under the master - :param fltr: May be a substring or regex. Only return tasks - whose 'id' matches `fltr`. - :type fltr: str + :param fltr: May be None, a substring or regex. None returns all tasks, + else return tasks whose 'id' matches `fltr`. + :type fltr: str | None :param completed: also include completed tasks :type completed: bool :returns: a list of tasks @@ -434,7 +434,9 @@ class Master(object): tasks = [] for framework in self._framework_dicts(completed, completed): for task in _merge(framework, keys): - if fltr in task['id'] or fnmatch.fnmatchcase(task['id'], fltr): + if fltr is None or \ + fltr in task['id'] or \ + fnmatch.fnmatchcase(task['id'], fltr): task = self._framework_obj(framework).task(task['id']) tasks.append(task)