diff --git a/ara/cli/task.py b/ara/cli/task.py new file mode 100644 index 00000000..89d4ac15 --- /dev/null +++ b/ara/cli/task.py @@ -0,0 +1,108 @@ +# Copyright (c) 2020 The ARA Records Ansible authors +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +import logging + +from cliff.lister import Lister + +from ara.cli.base import global_arguments +from ara.clients.utils import get_client + + +class TaskList(Lister): + """ Returns a list of tasks based on search queries """ + + log = logging.getLogger(__name__) + + def get_parser(self, prog_name): + parser = super(TaskList, self).get_parser(prog_name) + parser = global_arguments(parser) + # fmt: off + # Task search arguments and ordering as per ara.api.filters.TaskFilter + parser.add_argument( + "--playbook", + metavar="", + default=None, + help=("List tasks for a specified playbook id"), + ) + parser.add_argument( + "--status", + metavar="", + default=None, + help=("List tasks matching a specific status ('completed', 'running' or 'unknown')") + ) + parser.add_argument( + "--name", + metavar="", + default=None, + help=("List tasks matching the provided name (full or partial)"), + ) + parser.add_argument( + "--path", + metavar="", + default=None, + help=("List tasks matching the provided path (full or partial)"), + ) + parser.add_argument( + "--action", + metavar="", + default=None, + help=("List tasks matching a specific action/ansible module (ex: 'debug', 'package', 'set_fact')"), + ) + parser.add_argument( + "--order", + metavar="", + default="-started", + help=( + "Orders tasks by a field ('id', 'created', 'updated', 'started', 'ended', 'duration')\n" + "Defaults to '-started' descending so the most recent task is at the top.\n" + "The order can be reversed by omitting the '-': ara task list --order=started" + ), + ) + parser.add_argument( + "--limit", + metavar="", + default=100, + help=("Returns the first determined by the ordering. Defaults to 100.") + ) + # fmt: on + return parser + + def take_action(self, args): + client = get_client( + client=args.client, + endpoint=args.server, + timeout=args.timeout, + username=args.username, + password=args.password, + verify=False if args.insecure else True, + ) + query = {} + if args.playbook is not None: + query["playbook"] = args.playbook + + if args.status is not None: + query["status"] = args.status + + if args.name is not None: + query["name"] = args.name + + if args.path is not None: + query["path"] = args.path + + if args.action is not None: + query["action"] = args.action + + query["order"] = args.order + query["limit"] = args.limit + + tasks = client.get("/api/v1/tasks", **query) + columns = ("id", "playbook", "status", "action", "name", "started", "duration") + # fmt: off + return ( + columns, ( + [task[column] for column in columns] + for task in tasks["results"] + ) + ) + # fmt: on diff --git a/doc/source/cli.rst b/doc/source/cli.rst index 25f745bf..66ec090e 100644 --- a/doc/source/cli.rst +++ b/doc/source/cli.rst @@ -173,6 +173,38 @@ ara result delete .. command-output:: ara result delete --help +ara task list +------------- + +.. command-output:: ara task list --help + +.. note:: + + ara doesn't have the concept of roles but it is possible to search for + them by path, for example: ``ara task list --path "roles/install_apache"`` + + Role names are included in the task names and it is possible to search for + role-specific tasks there as well: ``ara task list --name install_apache``. + +Examples: + +.. code-block:: bash + + # Return the top 25 longest running tasks + ara task list --order=-duration --limit 25 + + # Return tasks from a specific playbook + ara task list --playbook 9001 + + # Return tasks for the package action + ara task list --action package + + # Return tasks matching a path (partial or full) + ara task list --path="roles/install_apache" + + # Return tasks matching a name (partial or full) + ara task list --name install_apache + CLI: ara-manage (django) ======================== diff --git a/setup.cfg b/setup.cfg index 7aeb9d86..8881563e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,6 +44,7 @@ ara.cli = result list = ara.cli.result:ResultList result show = ara.cli.result:ResultShow result delete = ara.cli.result:ResultDelete + task list = ara.cli.task:TaskList [extras] server=