Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

de-opacify the logic behind how we choose to delete users from a hub db #5078

Merged
merged 10 commits into from
Sep 28, 2023
23 changes: 21 additions & 2 deletions scripts/delete-unused-users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ async def main():
'hub_url',
help='Fully qualified URL to the JupyterHub'
)
argparser.add_argument(
'--dry_run',
action='store_true',
help='Dry run without deleting users'
)
args = argparser.parse_args()

to_delete = []
Expand All @@ -36,15 +41,29 @@ async def main():
except:
print(user['last_activity'])
raise
if last_activity and datetime.now().astimezone() - last_activity < timedelta(hours=24) or user['server'] is not None:
if isinstance(last_activity, datetime):
was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24)
else:
print(f"For user {user['name']}, expected datetime.datetime class for last_activity but got {type(last_activity)} instead.")
raise

print(f"User: {user['name']}")
print(f"Last login: {last_activity}")
print(f"24hrs since last login: {was_active_last_day}")
print(f"Running server: {user['server']}")
if was_active_last_day or user['server'] is not None:
print(f"Not deleting {user['name']}")
else:
to_delete.append(user['name'])
print(f"Deleting {user['name']}")
print("")

for i, username in enumerate(to_delete):
print(f'{i+1} of {len(to_delete)}: deleting {username}')
await hub.delete_user(username)
if not args.dry_run:
await hub.delete_user(username)
else:
print('Skipped due to dry run.')

if __name__ == '__main__':
asyncio.run(main())