arrfab / centos / cbs-tools

Forked from centos/cbs-tools 5 years ago
Clone

Blame scripts/koji-group-sync.py

b4342a
#!/usr/bin/python
c4b337
import ConfigParser
c4b337
import sys
b4342a
c4b337
from centos import AccountSystem
978cd4
from centos.client import AuthError
b4342a
b4342a
b4342a
def group_users(account_system_handle):
b4342a
    fas = account_system_handle
b4342a
    groups = fas.group_data().keys()
b4342a
b4342a
    group_users = {}
b4342a
    for groupname in groups:
b4342a
        if not groupname.startswith(GROUP_INCLUDE_PREFIX):
b4342a
            continue
b4342a
        group_users[groupname] = [member_entry['username'] for member_entry in
b4342a
                                  fas.group_members(groupname)]
b4342a
    return group_users
b4342a
b4342a
c4b337
def write_file(group_membership, filename):
b4342a
    with open(filename, 'w') as groupfile:
b4342a
        for groupname, users in group_membership.iteritems():
b4342a
            signame = groupname[len(GROUP_INCLUDE_PREFIX):]
2a8b5f
            print >>groupfile, "{0}:{1}".format(signame, ','.join(users))
b4342a
b4342a
b4342a
if __name__ == '__main__':
c4b337
    config = ConfigParser.SafeConfigParser()
c4b337
    config.read('/etc/bsadmin/bsadmin.conf')
c4b337
c4b337
    try:
c4b337
        FAS_TOPURL = config.get('fas', 'topurl')
c4b337
        FAS_USERNAME = config.get('fas', 'username')
c4b337
        FAS_PASSWORD = config.get('fas', 'password')
c4b337
        IGNORE_CERT_VALIDATION = config.getboolean('fas', 'ignore_selfsigned')
c4b337
        GROUP_INCLUDE_PREFIX = config.get('fas', 'group_prefix')
c4b337
        GROUP_FILE = config.get('fas', 'group_file')
c4b337
    except ConfigParser.NoOptionError as e:
c4b337
        print >> sys.stderr, e.msg
978cd4
        sys.exit(1)
c4b337
978cd4
    try:
978cd4
        fas = AccountSystem(base_url=FAS_TOPURL,
978cd4
                            username=FAS_USERNAME,
978cd4
                            password=FAS_PASSWORD,
978cd4
                            insecure=IGNORE_CERT_VALIDATION)
978cd4
    except AuthError as e:
978cd4
        print >> sys.stderr, e.msg
978cd4
        sys.exit(1)
b4342a
c4b337
    write_file(group_users(fas), GROUP_FILE)