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:
parent
ae3f5c220e
commit
6cd39efb85
38
awx/main/management/commands/cleanup_authtokens.py
Normal file
38
awx/main/management/commands/cleanup_authtokens.py
Normal 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()
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user