1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

Add a cleanup_authtokens management command

We also will now clean these up during cleanup_deleted
This commit is contained in:
Matthew Jones 2015-10-12 13:44:22 -04:00
parent ae3f5c220e
commit 6cd39efb85
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,38 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
# Python
import datetime
import logging
from optparse import make_option
# Django
from django.db import transaction
from django.core.management.base import BaseCommand
from django.utils.timezone import now
# AWX
from awx.main.models import * # noqa
class Command(BaseCommand):
'''
Management command to cleanup expired auth tokens
'''
help = 'Cleanup expired auth tokens.'
def init_logging(self):
log_levels = dict(enumerate([logging.ERROR, logging.INFO,
logging.DEBUG, 0]))
self.logger = logging.getLogger('awx.main.commands.cleanup_authtokens')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(message)s'))
self.logger.addHandler(handler)
self.logger.propagate = False
@transaction.atomic
def handle(self, *args, **options):
self.init_logging()
tokens_removed = AuthToken.objects.filter(expires__lt=now())
self.logger.log(99, "Removing %d expired auth tokens" % tokens_removed.count())
tokens_removed.delete()

View File

@ -118,3 +118,10 @@ class Command(BaseCommand):
self.logger.log(99, "Removed %d items", n_deleted_items)
else:
self.logger.log(99, "Would have removed %d items", n_deleted_items)
tokens_removed = AuthToken.objects.filter(expires__lt=now())
if not self.dry_run:
self.logger.log(99, "Removed %d expired auth tokens" % tokens_removed.count())
tokens_removed.delete()
else:
self.logger.log(99, "Would have removed %d expired auth tokens" % tokens_removed.count())