Пример #1
0
def sysuser_set_groups(self, username, groups, update=True, primary_group=""):
    """ set sysuser groups """
    __LOG.log_d("setting groups for user %s" % (username))
    config = retrieve_config_infos(self)[1]

    if type(groups) != list:
        raise TypeError("type of groups should be list")
    if type(primary_group) != str:
        raise TypeError("type of primary_group should be str")

    # cleaning user from all groups if not update
    # adding user "already member groups" if update
    if not update:
        __fapi.sed_file(self.srv_ip,
                        config['groupfile'],
                        r'([,:])%s(,([^:]*))*' % (username), r'\1\3',
                        use_sudo=config.has_key('use_sudo'))
    else: groups = list(set(groups + sysuser_get_groups(self, username)[1]))

    # set primary group with first group if needed
    if len(primary_group):
        __LOG.log_d("primary group: %s" % (primary_group))
        primary_group_opt = config['primary_group_opt'] % (
            {'primary_group': primary_group})
    else: primary_group_opt = ""

    if len(groups):
        __LOG.log_d("secondary groups: %s" % (groups))
        secondary_groups = ",".join(groups)
        secondary_groups_opt = config['secondary_groups_opt'] % (
            {'secondary_groups': secondary_groups})
    else: secondary_groups = secondary_groups_opt = ""

    context = {
        'username': username,
        'primary_group': primary_group,
        'secondary_groups': secondary_groups,
        'primary_group_opt': primary_group_opt,
        'secondary_groups_opt': secondary_groups_opt
        }
    ret = user_group_ctl(self, config, context)
    __LOG.log_d("groups setted: %s" % (repr(ret)))

    return ret
Пример #2
0
def sed_file(self, filename, pat_src, pat_dst, use_sudo = True):
    """ sed the content of file """
    return __fapi.sed_file(self.srv_ip, filename,
                           pat_src, pat_dst,
                           use_sudo = use_sudo)