From 8c48f15a61266d0803345a35817293b82433916b Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 13:12:40 -0700 Subject: [PATCH 01/10] add some output verbiage and make deletion logic more clear --- scripts/delete-unused-users.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 0e2111b9e..09cf61658 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -36,15 +36,21 @@ 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 (bool): {delta_bool}") + print(f"server: {user['server']}") + if (last_activity and delta_bool) 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) + #await hub.delete_user(username) if __name__ == '__main__': asyncio.run(main()) From 16946bd8287bea06b23bc0d3e9fa2c83fc15a6eb Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 13:36:20 -0700 Subject: [PATCH 02/10] add dry run arg --- scripts/delete-unused-users.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 09cf61658..2a205e177 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -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 = [] @@ -39,7 +44,7 @@ async def main(): 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 (bool): {delta_bool}") + 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): print(f"Not deleting {user['name']}") @@ -50,7 +55,11 @@ async def main(): 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: + print('should not be here!!!') + await hub.delete_user(username) + else: + print('Skipped due to dry run.') if __name__ == '__main__': asyncio.run(main()) From 664a4517afd9eb1f7cca67ef5cb1ea4725893cdd Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 13:38:19 -0700 Subject: [PATCH 03/10] remove printf debugging --- scripts/delete-unused-users.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 2a205e177..d9516ae4f 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -56,7 +56,6 @@ async def main(): for i, username in enumerate(to_delete): print(f'{i+1} of {len(to_delete)}: deleting {username}') if not args.dry_run: - print('should not be here!!!') await hub.delete_user(username) else: print('Skipped due to dry run.') From 724e8218b474ba4d14ded869d426358184514cf2 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 14:25:50 -0700 Subject: [PATCH 04/10] type checking and better variable name --- scripts/delete-unused-users.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index d9516ae4f..a5cea5999 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -12,6 +12,7 @@ ok. """ import argparse +import inspect from jhub_client.api import JupyterHubAPI from dateutil.parser import parse import asyncio @@ -41,17 +42,24 @@ async def main(): except: print(user['last_activity']) raise - delta_bool = datetime.now().astimezone() - last_activity < timedelta(hours=24) + + if inspect.isclass(type(last_activity)): + was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24) + else: + activity_type = type(last_activity) + print(f"expected datetime.datetime class for last_activity, got {activity_type} instead.") + raise + 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): + print(f"24hrs since last login: {was_active_last_day}") + print(f"running server: {user['server']}") + if (last_activity and 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('') + print("") for i, username in enumerate(to_delete): print(f'{i+1} of {len(to_delete)}: deleting {username}') From 41e9a17d57d6a1360d8fc3309a7aa73e3bbdbe7e Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 14:36:45 -0700 Subject: [PATCH 05/10] better type checking --- scripts/delete-unused-users.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index a5cea5999..1532a5a95 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -12,7 +12,6 @@ ok. """ import argparse -import inspect from jhub_client.api import JupyterHubAPI from dateutil.parser import parse import asyncio @@ -43,7 +42,7 @@ async def main(): print(user['last_activity']) raise - if inspect.isclass(type(last_activity)): + if isinstance(last_activity, datetime): was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24) else: activity_type = type(last_activity) From b7bed9ce1ec28d9c8ee4cd0db8be24dec28da469 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 14:39:54 -0700 Subject: [PATCH 06/10] NoRmAlIzE cApItAlIzAtIoN --- scripts/delete-unused-users.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 1532a5a95..594f843bc 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -49,10 +49,10 @@ async def main(): print(f"expected datetime.datetime class for last_activity, got {activity_type} instead.") raise - print(f"user: {user['name']}") - print(f"last login: {last_activity}") + 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']}") + print(f"Running server: {user['server']}") if (last_activity and was_active_last_day) or (user['server'] is not None): print(f"Not deleting {user['name']}") else: From e5fabe60385eb7574596685009b5b684c81354c0 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 14:41:50 -0700 Subject: [PATCH 07/10] better exception handling --- scripts/delete-unused-users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 594f843bc..400adb215 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -46,7 +46,7 @@ async def main(): was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24) else: activity_type = type(last_activity) - print(f"expected datetime.datetime class for last_activity, got {activity_type} instead.") + print(f"For user {user['name']}, expected datetime.datetime class for last_activity but got {activity_type} instead.") raise print(f"User: {user['name']}") From 0ebf48b666871c711184d357ad96b11f9b71ac96 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 14:57:20 -0700 Subject: [PATCH 08/10] can't leave well enough alone lol --- scripts/delete-unused-users.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 400adb215..6dc8e2e71 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -41,12 +41,11 @@ async def main(): except: print(user['last_activity']) raise - + last_activity = asyncio if isinstance(last_activity, datetime): was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24) else: - activity_type = type(last_activity) - print(f"For user {user['name']}, expected datetime.datetime class for last_activity but got {activity_type} instead.") + 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']}") From 8c5f8e09ed7d98912b571d2f4443bb386afcad21 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 15:22:52 -0700 Subject: [PATCH 09/10] remove debugging code --- scripts/delete-unused-users.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 6dc8e2e71..315d32a23 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -41,7 +41,6 @@ async def main(): except: print(user['last_activity']) raise - last_activity = asyncio if isinstance(last_activity, datetime): was_active_last_day = datetime.now().astimezone() - last_activity < timedelta(hours=24) else: From 1918138e9da9422f6483ff6f1c22a99fae2b7a73 Mon Sep 17 00:00:00 2001 From: shane knapp Date: Wed, 27 Sep 2023 15:45:15 -0700 Subject: [PATCH 10/10] remove extra vars from logic --- scripts/delete-unused-users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/delete-unused-users.py b/scripts/delete-unused-users.py index 315d32a23..1192750e5 100755 --- a/scripts/delete-unused-users.py +++ b/scripts/delete-unused-users.py @@ -51,7 +51,7 @@ async def main(): print(f"Last login: {last_activity}") print(f"24hrs since last login: {was_active_last_day}") print(f"Running server: {user['server']}") - if (last_activity and was_active_last_day) or (user['server'] is not None): + if was_active_last_day or user['server'] is not None: print(f"Not deleting {user['name']}") else: to_delete.append(user['name'])