Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to add Galaxy users to groups according to their email domain
#!/usr/bin/env python
from __future__ import print_function
import bioblend.galaxy
def add_users_to_groups(gi, groups_to_check):
# Create a dictionary groups_dict mapping a group name to the list of user
# ids of its members
groups_dict = dict()
groups = gi.groups.get_groups()
for group in groups:
if group['name'] in groups_to_check:
group_users_ids = [_['id'] for _ in gi.groups.get_group_users(group['id'])]
groups_dict[group['name']] = {
'id': group['id'],
'user_ids': group_users_ids,
for group_name in groups_to_check:
if group_name not in groups_dict:
raise Exception("Group %s does not exist in Galaxy" % group_name)
users = gi.users.get_users()
for user in users:
user_id = user['id']
user_email = user['email']
for group_name, email_suffix in groups_to_check.items():
if user_email.endswith(email_suffix):
if user_id not in groups_dict[group_name]['user_ids']:
print("Adding user %s to group %s" % (user_email, group_name))
gi.groups.add_group_user(groups_dict[group_name]['id'], user_id)
galaxy_url = ''
galaxy_api_key = 'admin_secret_API_key'
groups_to_check = {
'Earlham': '',
'Quadram': '',
'JIC': '',
gi = bioblend.galaxy.GalaxyInstance(galaxy_url, galaxy_api_key)
print('Checking users and groups on the prod server')
add_users_to_groups(gi, groups_to_check)
except Exception as e:
print("add_users_to_groups() failed with exception: %s" % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment