#!/opt/imh-python/bin/python3
"""Adds cPanel users to a common supplementary group"""
from subprocess import check_call, CalledProcessError
COMMON_GROUP = 'cpanel-users'
# cron config appends stdout/err to /var/log/maint/secgroups.log
logger = rads.setup_logging(
fmt='%(asctime)s [%(levelname)s] %(message)s',
cpanel_users = set(rads.all_cpusers())
group_members = grp.getgrnam(COMMON_GROUP).gr_mem
missing_users = cpanel_users.difference(group_members)
for user in missing_users:
logger.info('%s not in %s', user, COMMON_GROUP)
check_call(['usermod', '-G', COMMON_GROUP, user])
except CalledProcessError:
logger.critical('Failed to add %s to %s', user, COMMON_GROUP)
logger.info('Added %s to %s', user, COMMON_GROUP)
if __name__ == '__main__':