Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/admin.py
Show First 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | def user_exists(ctx, username): | ||||
""" | """ | ||||
# to avoid loading too early django namespaces | # to avoid loading too early django namespaces | ||||
from swh.deposit.models import DepositClient | from swh.deposit.models import DepositClient | ||||
try: | try: | ||||
DepositClient.objects.get(username=username) | DepositClient.objects.get(username=username) | ||||
click.echo('User %s exists.' % username) | click.echo('User %s exists.' % username) | ||||
ctx.exit(0) | ctx.exit(0) | ||||
except DepositClient.DoesNotExist: | except DepositClient.DoesNotExist: | ||||
click.echo('User %s does not exists.' % username) | click.echo('User %s does not exist.' % username) | ||||
ctx.exit(1) | ctx.exit(1) | ||||
@admin.group('collection') | @admin.group('collection') | ||||
@click.pass_context | @click.pass_context | ||||
def collection(ctx): | def collection(ctx): | ||||
"""Manipulate collections.""" | """Manipulate collections.""" | ||||
pass | pass | ||||
Show All 18 Lines | def collection_list(ctx): | ||||
# to avoid loading too early django namespaces | # to avoid loading too early django namespaces | ||||
from swh.deposit.models import DepositCollection | from swh.deposit.models import DepositCollection | ||||
collections = DepositCollection.objects.all() | collections = DepositCollection.objects.all() | ||||
if not collections: | if not collections: | ||||
output = 'Empty collection list' | output = 'Empty collection list' | ||||
else: | else: | ||||
output = '\n'.join((col.name for col in collections)) | output = '\n'.join((col.name for col in collections)) | ||||
click.echo(output) | click.echo(output) | ||||
@admin.group('deposit') | |||||
@click.pass_context | |||||
def deposit(ctx): | |||||
"""Manipulate deposit.""" | |||||
pass | |||||
@deposit.command('reschedule') | |||||
@click.option('--deposit-id', required=True, help="Deposit identifier") | |||||
@click.pass_context | |||||
def deposit_reschedule(ctx, deposit_id): | |||||
"""Reschedule the deposit loading | |||||
This will: | |||||
- check the deposit's status to something reasonable (failed or done). That | |||||
means that the checks have passed alright but something went wrong during | |||||
the loading (failed: loading failed, done: loading ok, still for some | |||||
reasons as in bugs, we need to reschedule it) | |||||
- reset the deposit's status to 'verified' (prior to any loading but after | |||||
the checks which are fine) and removes the different archives' identifiers | |||||
(swh-id, ...) | |||||
- trigger back the loading task through the scheduler | |||||
""" | |||||
# to avoid loading too early django namespaces | |||||
from datetime import datetime | |||||
from swh.deposit.models import Deposit | |||||
from swh.deposit.config import ( | |||||
DEPOSIT_STATUS_LOAD_SUCCESS, DEPOSIT_STATUS_LOAD_FAILURE, | |||||
DEPOSIT_STATUS_VERIFIED, SWHDefaultConfig, | |||||
) | |||||
try: | |||||
deposit = Deposit.objects.get(pk=deposit_id) | |||||
except Deposit.DoesNotExist: | |||||
click.echo('Deposit %s does not exist.' % deposit_id) | |||||
ctx.exit(1) | |||||
# Check the deposit is in a reasonable state | |||||
accepted_statuses = [ | |||||
DEPOSIT_STATUS_LOAD_SUCCESS, DEPOSIT_STATUS_LOAD_FAILURE | |||||
] | |||||
if deposit.status == DEPOSIT_STATUS_VERIFIED: | |||||
click.echo('Deposit %s\'s status already set for rescheduling.' % ( | |||||
deposit_id)) | |||||
ctx.exit(0) | |||||
if deposit.status not in accepted_statuses: | |||||
click.echo('Deposit %s\'s status be one of %s.' % ( | |||||
deposit_id, ', '.join(accepted_statuses))) | |||||
ctx.exit(1) | |||||
task_id = deposit.load_task_id | |||||
if not task_id: | |||||
click.echo('Deposit %s cannot be rescheduled. It misses the ' | |||||
'associated task.' % deposit_id) | |||||
ctx.exit(1) | |||||
# Reset the deposit's state | |||||
deposit.swh_id = None | |||||
deposit.swh_id_context = None | |||||
deposit.swh_anchor_id = None | |||||
deposit.swh_anchor_id_context = None | |||||
deposit.status = DEPOSIT_STATUS_VERIFIED | |||||
deposit.save() | |||||
# Trigger back the deposit | |||||
scheduler = SWHDefaultConfig().scheduler | |||||
scheduler.set_status_tasks( | |||||
[task_id], status='next_run_not_scheduled', | |||||
next_run=datetime.now()) |