Skip to content

Commit

Permalink
Story 228: Purge Completed task records by pulp-admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham90 committed Jul 31, 2015
1 parent 80ef778 commit e647705
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 5 deletions.
4 changes: 2 additions & 2 deletions bindings/pulp/bindings/server.py
Expand Up @@ -84,8 +84,8 @@ def __init__(self,
self.verify_ssl = verify_ssl
self.ca_path = ca_path

def DELETE(self, path, body=None, log_request_body=True):
return self._request('DELETE', path, body=body, log_request_body=log_request_body)
def DELETE(self, path, body=None, log_request_body=True, queries=()):
return self._request('DELETE', path, queries, body=body, log_request_body=log_request_body)

def GET(self, path, queries=()):
return self._request('GET', path, queries)
Expand Down
19 changes: 19 additions & 0 deletions bindings/pulp/bindings/tasks.py
@@ -1,6 +1,7 @@
"""
This module contains bindings to the Task APIs.
"""

from pulp.bindings.base import PulpAPI
from pulp.bindings.responses import Task
from pulp.bindings.search import SearchAPI
Expand Down Expand Up @@ -70,6 +71,24 @@ def get_all_tasks(self, tags=()):
response.response_body = tasks
return response

def purge_tasks(self, states=()):
"""
Deletes completed tasks (except tasks in state 'canceled') in the system.
If states are specified, only tasks that are in given states are purged.
By default, all the completed tasks except tasks in state 'canceled', are purged.
:param states: tuple of states given by user through command line
Default to return all completed tasks except 'canceled'
:type states: tuple
:return: HttpResponse from REST API;
:rtype: Response
"""
path = '/v2/tasks/'
state = [('state', s) for s in states]
response = self.server.DELETE(path, queries=state)

return response

def get_repo_tasks(self, repo_id):
"""
Retrieves all tasks for the given repository.
Expand Down
10 changes: 10 additions & 0 deletions bindings/test/unit/test_tasks.py
Expand Up @@ -76,6 +76,16 @@ def test_return_type(self):
self.assertTrue(isinstance(task, responses.Task))


class TestPurgeTasks(unittest.TestCase):
def setUp(self):
self.server = mock.MagicMock()
self.api = tasks.TasksAPI(self.server)

def test_default(self):
states = ['finished', 'error', 'skipped']
self.api.purge_tasks(states=states)
self.server.DELETE.assert_called_once()

TASKS = [
{
'exception': None,
Expand Down
49 changes: 48 additions & 1 deletion client_admin/pulp/client/admin/tasks.py
@@ -1,15 +1,20 @@
from gettext import gettext as _

from okaara.cli import CommandUsage

import pulp.common.tags as tag_utils
from pulp.bindings.exceptions import PulpServerException
from pulp.client import parsers
from pulp.client.extensions.extensions import PulpCliSection, PulpCliFlag, PulpCliOption
from pulp.common.constants import CALL_COMPLETE_STATES, CALL_CANCELED_STATE


# Guidance for render_document_list on how to display task info
TASK_DETAILS_DOC_ORDER = ['operations', 'resources', 'state', 'start_time', 'finish_time',
'result', 'task_id']
TASK_LIST_DOC_ORDER = ['operations', 'resources', 'state', 'start_time', 'finish_time', 'task_id']
# This constant set is used for purging the completed tasks from the collection.
VALID_STATES = set(filter(lambda state: state != CALL_CANCELED_STATE, CALL_COMPLETE_STATES))


def initialize(context):
Expand All @@ -36,7 +41,7 @@ class BaseTasksSection(PulpCliSection):
state_option = PulpCliOption('--state',
_('comma-separated list of tasks states desired to be '
'shown. Example: "running,waiting,canceled,successful,failed". '
'Do not include spaces'), aliases=['-s'], required=False,
'Do not include spaces.'), aliases=['-s'], required=False,
parse_func=parsers.csv)

def __init__(self, context, name, description):
Expand Down Expand Up @@ -244,13 +249,27 @@ def retrieve_tasks(self):
class AllTasksSection(BaseTasksSection):
FIELDS = ('tags', 'task_id', 'state', 'start_time', 'finish_time')

# Flags for purge command
purge_all = PulpCliFlag('--all', _('if specified, all tasks in "successful, '
'skipped and failed" states will be purged'),
aliases=['-a'])
purge_state = PulpCliOption('--state', _('comma-separated list of tasks states desired to be '
'purged. Example: "successful,failed". Do not include spaces.'),
aliases=['-s'], required=False, parse_func=parsers.csv)

def __init__(self, context, name, description):
BaseTasksSection.__init__(self, context, name, description)

self.list_command.add_option(self.all_flag)

self.list_command.add_option(self.state_option)

self.purge_command = self.create_command('purge', _(
'purge tasks in one or more completed states'), self.purge)

self.purge_command.add_option(self.purge_all)
self.purge_command.add_option(self.purge_state)

def retrieve_tasks(self, **kwargs):
"""
:return: list of pulp.bindings.responses.Task instances
Expand All @@ -271,6 +290,34 @@ def retrieve_tasks(self, **kwargs):
filters={'state': {'$in': ['running', 'waiting']}}, fields=self.FIELDS)
return tasks

def purge(self, **kwargs):
"""
Attempts to purge completed tasks with states successful, failed and/or skipped.
If a state does not support purging, message will be raised from the server.
Otherwise a success message is displayed and nothing is returned from the server.
If no valid flag is mentioned, it raises the CommandUsage Exception.
:raises CommandUsage Exception when incorrect arguments given to command line
"""

self.context.prompt.render_title('Purge Completed Tasks')

if kwargs.get(self.purge_all.keyword) and kwargs.get(self.purge_state.keyword):
msg = _('These arguments cannot be used together')
self.context.prompt.render_failure_message(msg)
return

if kwargs.get(self.purge_state.keyword):
states = kwargs[self.purge_state.keyword]
elif kwargs.get(self.purge_all.keyword):
states = VALID_STATES
else:
raise CommandUsage
states = self.translate_state_discrepancy(states)

self.context.server.tasks.purge_tasks(states=states)
self.context.prompt.render_success_message(_('Task purging is successfully initiated.'))

@staticmethod
def translate_state_discrepancy(states):
"""
Expand Down
58 changes: 58 additions & 0 deletions client_admin/test/unit/test_pulp_tasks_extension.py
@@ -1,11 +1,13 @@
import copy

import mock
from okaara.cli import CommandUsage

import base_builtins

from pulp.bindings.exceptions import NotFoundException, PulpServerException
from pulp.client.admin import tasks
from pulp.client.admin.tasks import VALID_STATES


EXAMPLE_CALL_REPORT = {
Expand Down Expand Up @@ -153,6 +155,62 @@ def test_list_all_state(self):
# Verify
self.assertTrue('These arguments cannot be used together\n' in self.recorder.lines)

@mock.patch('pulp.bindings.tasks.TasksAPI.purge_tasks')
def test_purge_default(self, mock_purge):
# Setup
data = {
'all': False,
'state': None
}

# Verify
self.assertRaises(CommandUsage, self.all_tasks_section.purge)

@mock.patch('pulp.bindings.tasks.TasksAPI.purge_tasks')
@mock.patch('pulp.client.admin.tasks.AllTasksSection.translate_state_discrepancy')
def test_purge_state(self, mock_translate, mock_purge):
# Setup
data = {
'all': False,
'state': 'successful,failed'
}
mock_translate.return_value = 'finished,error'

# Test
self.all_tasks_section.purge(**data)

# Verify
mock_translate.assert_called_once_with('successful,failed')
mock_purge.assert_called_once_with(states='finished,error')

@mock.patch('pulp.bindings.tasks.TasksAPI.purge_tasks')
def test_purge_all(self, mock_purge):
# Setup
data = {
'all': True,
'state': None
}

# Test
self.all_tasks_section.purge(**data)
states = VALID_STATES

# Verify
mock_purge.assert_called_once_with(states=states)

def test_purge_all_state(self):
# Setup
data = {
'all': True,
'state': 'finished,error'
}

# Test
self.all_tasks_section.purge(**data)

# Verify
self.assertTrue('These arguments cannot be used together\n' in self.recorder.lines)

def test_translate_state_discrepancy(self):
# Setup
state = ['successful', 'failed']
Expand Down
3 changes: 1 addition & 2 deletions common/pulp/common/error_codes.py
Expand Up @@ -114,6 +114,5 @@
PLP1009 = Error("PLP1009", _("The request body does not contain valid JSON"), [])
PLP1010 = Error("PLP1010", _("Provided value %(value)s for field %(field)s must be of type "
"%(field_type)s."), ["value", "field", "field_type"])
PLP1011 = Error("PLP1011", _("Invalid task state passed to parameters list: %(state)s. "
"Valid states are finished, error and skipped"), ["state"])
PLP1011 = Error("PLP1011", _("Invalid task state passed to purge: %(state)s."), ["state"])
PLP1012 = Error("PLP1012", _("No task state given to parameters list for delete."), [])
52 changes: 52 additions & 0 deletions docs/user-guide/admin-client/tasks.rst
Expand Up @@ -80,6 +80,58 @@ occur very frequently and as result the database can grow to an unreasonable siz
The ``monthly`` task is run every 30 days to clean up data referencing any repositories that no
longer exist.


Purging
-------

To purge the completed tasks on the server at any given time, the **pulp-admin**
command line client provides the ``tasks`` section and the ``purge`` command.

By giving a ``--all`` flag, the tasks in ``successful``, ``failed`` and ``skipped`` state
will be purged.

By giving a ``--state`` flag, specific states can be passed as an argument and all the
tasks belonging to these states will be purged. These states must be from the completed
state list(except ``canceled``) or the command will result in error message.

::

$ pulp-admin tasks purge
+----------------------------------------------------------------------+
Purge Completed Tasks
+----------------------------------------------------------------------+

Command: purge
Description: purge tasks in one or more completed states

Available Arguments:

--all, -a - if specified, all tasks in "successful, skipped and failed states
will be purged
--state, -s - comma-separated list of tasks states desired to be purged.
Example: "successful,failed". Do not include spaces.

$ pulp-admin tasks purge --all
+----------------------------------------------------------------------+
Purge Completed Tasks
+----------------------------------------------------------------------+

Task purging is successfully initiated.

$ pulp-admin tasks purge -s successful,failed
+----------------------------------------------------------------------+
Purge Completed Tasks
+----------------------------------------------------------------------+

Task purging is successfully initiated.

$ pulp-admin tasks purge -s canceled
+----------------------------------------------------------------------+
Purge Completed Tasks
+----------------------------------------------------------------------+

Invalid task state passed to parameters list: canceled.

Canceling a Task
----------------

Expand Down
2 changes: 2 additions & 0 deletions docs/user-guide/release-notes/master.rst
Expand Up @@ -17,6 +17,8 @@ Deprecation
Client Changes
--------------

* Tasks with complete states (except `canceled` state) can now be deleted. This can be done
using `pulp-admin tasks purge` command.

Agent Changes
-------------
Expand Down

0 comments on commit e647705

Please sign in to comment.