Skip to content

Commit

Permalink
Added test flag to create_users.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
DraTeots committed Jun 30, 2024
1 parent 01c40bf commit 738a9a3
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions scripts/users_create/users_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
--groupid - POSIX Group ID to get users from. E.g. on ifarm halld is 267
--test - Don't do real changes in DB if this flag is given. To be used for scripts and cronjob testing purposes
Examples:
#recreate user list from halld group
Expand All @@ -41,15 +43,18 @@ def get_provider(con_str):
return provider


def delete_users(provider):
def delete_users(provider, is_test):
god_list = ["anonymous", "test_user"]
assert isinstance(provider, AlchemyProvider)

users = provider.get_users()
deleted_count = 0
for user in users:
if not user.name in god_list:
provider.delete_user(user.name)
if is_test:
print(f"Should have deleted: {user.name} (--test flag is given, user won't be deleted) ")
else:
provider.delete_user(user.name)
deleted_count += 1
print("Users deleted {}".format(deleted_count))

Expand Down Expand Up @@ -99,7 +104,7 @@ def get_user_names_from_ypmatch(ypstr):
return [token for token in lexer]


def create_users(provider, user_names):
def create_users(provider, user_names, is_test):
"""
@param provider:Connected alchemy provider
Expand All @@ -108,7 +113,10 @@ def create_users(provider, user_names):
count = 0
for name in user_names:
try:
provider.create_user(name)
if is_test:
print(f"Should have created: '{name}' (--test flag is given, user won't be created) ")
else:
provider.create_user(name)
count += 1
except UserExistsError as err:
print(err.message)
Expand All @@ -121,44 +129,38 @@ def parse_arguments():
parser.add_argument('connection_string', type=str, help='Connection string for the database, e.g., mysql://ccdb_user@localhost/ccdb')
parser.add_argument('--recreate', action='store_true', help='Delete all users before creating new ones')
parser.add_argument('--groupid', default=267, help='POSIX Group ID to get usernames from')
parser.add_argument('--test', action='store_true', help="Don't do real changes in DB, allowing testing of this script ")
args = parser.parse_args()
return args.connection_string, args.recreate, args.groupid
return args.connection_string, args.recreate, args.groupid, args.test


def main():
# Parse command line arguments
connection_str, recreate, groupid = parse_arguments()
connection_str, recreate, group_id, is_test = parse_arguments()

# Check stdin for usernames
names = get_names(groupid)
names = get_names(group_id)
if not names:
print("No user names found in stdin (pipe). Script continues to delete somebody if --recreate flag is given...")
print("No user names found. Script continues to delete somebody if --recreate flag is given...")
exit(1)

# light parse arguments
for token in sys.argv[1:]:
if token == "--recreate":
recreate = True
else:
connection_str = token

print("Connecting to '" + connection_str + "'")

provider = get_provider(connection_str)
provider.get_root_directory() # this will load directories and ensure that ccdb structure is in place

# delete old users if needed
if recreate:
print ("Deleting users...")
print("Deleting users ...")
try:
delete_users(provider)
delete_users(provider, is_test)
except Exception as ex:
print("User deletion failed with error of type {} : {}".format(type(ex), str(ex)))
sys.exit(2)

# create new users
try:
create_users(provider, names)
create_users(provider, names, is_test)
except Exception as ex:
print("User creation failed with error of type {} : {}".format(type(ex), str(ex)))
sys.exit(3)
Expand Down

0 comments on commit 738a9a3

Please sign in to comment.