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
18 changes: 16 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,24 @@ 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:
delta_bool = datetime.now().astimezone() - last_activity < timedelta(hours=24)
print(f"user: {user['name']}")
print(f"last login: {last_activity}")
print(f"24hrs since last login: {delta_bool}")
print(f"server: {user['server']}")
if (last_activity and delta_bool) or (user['server'] is not None):
shaneknapp marked this conversation as resolved.
Show resolved Hide resolved
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())