Skip to content

Commit

Permalink
Add a cleanup_authtokens management command
Browse files Browse the repository at this point in the history
We also will now clean these up during cleanup_deleted
  • Loading branch information
matburt committed Oct 12, 2015
1 parent ae3f5c2 commit 6cd39ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions awx/main/management/commands/cleanup_authtokens.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 7 additions & 0 deletions awx/main/management/commands/cleanup_deleted.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ def handle(self, *args, **options):
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())

0 comments on commit 6cd39ef

Please sign in to comment.