arrfab / centos / cbs-tools

Forked from centos/cbs-tools 5 years ago
Clone
Blob Blame History Raw
#!/usr/bin/python
from centos import AccountSystem
from centos import defaults

FAS_USERNAME = ''
FAS_PASSWORD = ''
GROUP_INCLUDE_PREFIX = 'sig-'
GROUP_FILE = '/etc/bsadmin/groups'
IGNORE_CERT_VALIDATION = True


def group_users(account_system_handle):
    fas = account_system_handle
    groups = fas.group_data().keys()

    group_users = {}
    for groupname in groups:
        if not groupname.startswith(GROUP_INCLUDE_PREFIX):
            continue
        group_users[groupname] = [member_entry['username'] for member_entry in
                                  fas.group_members(groupname)]
    return group_users


def write_file(group_membership, filename=GROUP_FILE):
    with open(filename, 'w') as groupfile:
        for groupname, users in group_membership.iteritems():
            signame = groupname[len(GROUP_INCLUDE_PREFIX):]
            print >>groupfile, "{0}:{1}".format(signame, ','.join(users))


if __name__ == '__main__':
    fas = AccountSystem(base_url=defaults.FAS_TOPURL,
                        username=FAS_USERNAME,
                        password=FAS_PASSWORD,
                        insecure=IGNORE_CERT_VALIDATION)

    write_file(group_users(fas))