This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/migrate/cleanup_old_robots.py
Joseph Schorr f06eec8a35 Fix issue where we didn't delete robots immediately under a namespace
This could result in "hanging" robot accounts, although that would only leak the names of said accounts. Now we delete them immediately AND we proactively delete them before replacing the namespace (just to be sure)
2018-05-09 17:53:30 +03:00

28 lines
883 B
Python

from data.database import User
from util.names import parse_robot_username
def cleanup_old_robots(page_size=50):
""" Deletes any robots that live under namespaces that no longer exist. """
# Collect the robot accounts to delete.
page_number = 1
to_delete = []
while True:
found_bots = False
for robot in list(User.select().where(User.robot == True).paginate(page_number, page_size)):
found_bots = True
namespace, _ = parse_robot_username(robot.username)
try:
User.get(username=namespace)
except User.DoesNotExist:
# Save the robot account for deletion.
to_delete.append(robot)
if not found_bots:
break
page_number = page_number + 1
# Cleanup any robot accounts whose corresponding namespace doesn't exist.
for robot in to_delete:
robot.delete_instance(recursive=True, delete_nullable=True)