Skip to content

Instantly share code, notes, and snippets.

@relrod
Created Jan 10, 2020
Embed
What would you like to do?
#!/usr/bin/env python3
from fedora.client.fas2 import AccountSystem
from getpass import getpass
fas_user = input('Admin username: ')
fas_password = getpass('Admin password (not echoed): ')
src = input('Source group name: ')
dest = input('Destination group name: ')
preserve_s = input('Preserve roles? (n=all users are just members) [Y/n]: ')
unapproved_s = input('Import unapproved members? (they will stay unapproved) [Y/n]: ')
preserve = False
if preserve_s in ['Y', 'y', '']:
preserve = True
unapproved = False
if unapproved_s in ['Y', 'y', '']:
unapproved = True
fas = AccountSystem(
'https://admin.fedoraproject.org/accounts',
username=fas_user,
password=fas_password,
)
print('Obtaining list of all users, hope you have some free RAM!')
users_req = fas.send_request(
'/user/list',
req_params={'search': '*'},
auth=True,
timeout=240
)
# Be able to key on user id
users = {}
for user in users_req['people']:
users[user['id']] = user
print('Got list of users. Obtaining ids of members in source group.')
src_res = fas.send_request('/group/view/%s' % src, auth=True)
print('And dest group, to avoid duplicates.')
dest_res = fas.send_request('/group/view/%s' % dest, auth=True)
print('Requesting membership for members of the source group -> new group.')
for member in src_res['members']:
uid = member['person_id']
username = users[uid]['username']
if uid in [a['person_id'] for a in dest_res['members']]:
print('%s already in destination group, skipping.' % username)
continue
if member['role_status'] == 'unapproved' and not unapproved:
print('Skipping unapproved member %s' % username)
continue
print('Applying for %s' % username)
fas.send_request(
'/group/apply/%s/%s' % (dest, username))
if member['role_status'] != 'unapproved':
print('Sponsoring %s' % username)
fas.send_request(
'/group/sponsor/%s/%s' % (dest, username))
if preserve:
# It should be safe to call upgrade here as needed because we
# already bailed out above if the user was already a member.
# At this point the user can only be a member because they were
# just now added. So we don't risk upgrading people who shouldn't
# be upgraded.
if member['role_type'] in ['sponsor', 'administrator']:
print('Upgrading %s.' % username)
fas.send_request(
'/group/upgrade/%s/%s' % (dest, username))
if member['role_type'] == 'administrator':
print('Upgrading %s again.' % username)
fas.send_request(
'/group/upgrade/%s/%s' % (dest, username))
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment