# HG changeset patch # User Marcin Kuzminski # Date 2017-05-11 10:34:41 # Node ID 1fa4621220b0e85e322eb6c75b915c0d0fc2a4db # Parent 97b4b51b78819929b2dd6f837b010988c0651ecb api: added maintenance command into API. diff --git a/rhodecode/api/views/repo_api.py b/rhodecode/api/views/repo_api.py --- a/rhodecode/api/views/repo_api.py +++ b/rhodecode/api/views/repo_api.py @@ -29,6 +29,7 @@ from rhodecode.api.utils import ( get_user_group_or_error, get_user_or_error, validate_repo_permissions, get_perm_or_error, parse_args, get_origin, build_commit_data, validate_set_owner_permissions) +from rhodecode.lib import repo_maintenance from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi from rhodecode.lib.utils2 import str2bool, time_to_datetime from rhodecode.lib.ext_json import json @@ -1986,3 +1987,65 @@ def set_repo_settings(request, apiuser, # Indicate success. return True + + +@jsonrpc_method() +def maintenance(request, apiuser, repoid): + """ + Triggers a maintenance on the given repository. + + This command can only be run using an |authtoken| with admin + rights to the specified repository. For more information, + see :ref:`config-token-ref`. + + This command takes the following options: + + :param apiuser: This is filled automatically from the |authtoken|. + :type apiuser: AuthUser + :param repoid: The repository name or repository ID. + :type repoid: str or int + + Example output: + + .. code-block:: bash + + id : + result : { + "msg": "executed maintenance command", + "executed_actions": [ + , ... + ], + "repository": "" + } + error : null + + Example error output: + + .. code-block:: bash + + id : + result : null + error : { + "Unable to execute maintenance on ``" + } + + """ + + repo = get_repo_or_error(repoid) + if not has_superadmin_permission(apiuser): + _perms = ('repository.admin',) + validate_repo_permissions(apiuser, repoid, repo, _perms) + + try: + maintenance = repo_maintenance.RepoMaintenance() + executed_actions = maintenance.execute(repo) + + return { + 'msg': 'executed maintenance command', + 'executed_actions': executed_actions, + 'repository': repo.repo_name + } + except Exception: + log.exception("Exception occurred while trying to run maintenance") + raise JSONRPCError( + 'Unable to execute maintenance on `%s`' % repo.repo_name) diff --git a/rhodecode/templates/admin/repos/repo_edit_maintenance.mako b/rhodecode/templates/admin/repos/repo_edit_maintenance.mako --- a/rhodecode/templates/admin/repos/repo_edit_maintenance.mako +++ b/rhodecode/templates/admin/repos/repo_edit_maintenance.mako @@ -4,19 +4,27 @@
+ % if c.executable_tasks: +

${_('Perform maintenance tasks for this repo')}

+ + ${_('Following tasks will be performed')}: +
    + % for task in c.executable_tasks: +
  1. ${task}
  2. + % endfor +

- % if c.executable_tasks: - ${_('Perform maintenance tasks for this repo, following tasks will be performed')}: -

    - % for task in c.executable_tasks: -
  1. ${task}
  2. - % endfor -
- % else: - ${_('No maintenance tasks for this repo available')} - % endif + ${_('Maintenance can be automated by such api call. Can be called periodically in crontab etc.')} +
+ + ${h.api_call_example(method='maintenance', args={"repoid": c.repo_info.repo_name})} +

+ % else: +

${_('No maintenance tasks for this repo available')}

+ % endif + % if c.executable_tasks: