Exemplo n.º 1
0
def create_users(argvs):
    """
    create user
    :param argvs:
    :return:
    """
    if '-f' in argvs:
        user_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreateusers -f <the new users file>", logout=True)
        return

    source = yaml_parser(user_file)
    if source:
        logger.debug("source:\n%s" % source)
        for key, val in source.items():
            logger.debug("%s:%s" % (key, val))
            obj = models.UserProfile(username=key, password=val.get('password'))
            logger.info(obj)
            # if val.get('groups'):
            #     groups = session.query(models.Group).filter(models.Group.name.in_(val.get('groups'))).all()
            #     if not groups:
            #         print_err("none of [%s] exist in group table." % val.get('groups'),quit=True)
            #     obj.groups = groups
            # if val.get('bind_hosts'):
            #     bind_hosts = common_filters.bind_hosts_filter(val)
            #     obj.bind_hosts = bind_hosts
            # print(obj)
            session.add(obj)
        session.commit()
        logger.info("create user sucess!")
Exemplo n.º 2
0
def create_groups(argvs):
    """
    create groups
    :param argvs:
    :return:
    """
    if '-f' in argvs:
        group_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreategroups -f <the new groups file>", logout=True)
        return
    source = yaml_parser(group_file)
    if source:
        logger.debug("source:\n%s" % source)
        for key, val in source.items():
            logger.debug("%s:%s" % (key, val))
            obj = models.HostGroup(name=key)
            logger.info(obj)
            # if val.get('bind_hosts'):
            #     bind_hosts = common_filters.bind_hosts_filter(val)
            #     obj.bind_hosts = bind_hosts
            #
            if val.get('user_profiles'):  # 用户与主机分组关系
                user_profiles = common_filters.user_profiles_filter(val)
                obj.user_profiles = user_profiles
            session.add(obj)
        session.commit()
        logger.info("create groups sucess!")
Exemplo n.º 3
0
def create_bindhosts(argvs):
    """
    create bind hosts
    主机及该主机上的账户信息
    :param argvs:
    :return:
    """
    if '-f' in argvs:
        bindhosts_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreate_bindhosts -f <the new bindhosts file>", logout=True)
        return
    source = yaml_parser(bindhosts_file)
    if source:
        logger.debug("source:\n%s" % source)
        for key, val in source.items():
            logger.debug("%s:%s" % (key, val))
            # 要Bind的主机信息
            host_obj = session.query(models.Host).filter(models.Host.hostname == val.get('hostname')).first()
            logger.debug("host_obj---\n%s" % host_obj)
            assert host_obj
            for item in val['remote_users']:  # 要bind到该主机上的账户信息
                logger.debug(item)
                assert item.get('auth_type')
                if item.get('auth_type') == 'ssh-password':
                    remoteuser_obj = session.query(models.RemoteUser).filter(
                        models.RemoteUser.username == item.get('username'),
                        models.RemoteUser.password == item.get('password')
                    ).first()
                else:
                    remoteuser_obj = session.query(models.RemoteUser).filter(
                        models.RemoteUser.username == item.get('username'),
                        models.RemoteUser.auth_type == item.get('auth_type'),
                    ).first()
                if not remoteuser_obj:
                    print_err("RemoteUser obj %s does not exist." % item, logout=True)
                bindhost_obj = models.BindHost(host_id=host_obj.id, remoteuser_id=remoteuser_obj.id)  # 设定bind关系
                session.add(bindhost_obj)
                # for groups this host binds to 该主机bind到主机组
                if source[key].get('groups'):
                    group_objs = session.query(models.HostGroup).filter(
                        models.HostGroup.name.in_(source[key].get('groups'))).all()
                    assert group_objs
                    logger.info('groups:%s' % group_objs)
                    bindhost_obj.host_groups = group_objs
                # for user_profiles this host binds to  该主机bind到的用户
                if source[key].get('user_profiles'):
                    userprofile_objs = session.query(models.UserProfile).filter(models.UserProfile.username.in_(
                        source[key].get('user_profiles')
                    )).all()
                    logger.debug(userprofile_objs)
                    assert userprofile_objs
                    logger.info("userprofiles:%s" % userprofile_objs)
                    bindhost_obj.user_profiles = userprofile_objs
                    # print(bindhost_obj)
        session.commit()
        logger.info("create bindhosts sucess!")
Exemplo n.º 4
0
def create_hosts(argvs):
    """
    create hosts
    :param argvs:
    :return:
    """
    if '-f' in argvs:
        hosts_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreate_hosts -f <the new hosts file>", logout=True)
        return
    source = yaml_parser(hosts_file)
    if source:
        logger.debug("source:\n%s" % source)
        for key, val in source.items():
            logger.debug("%s:%s" % (key, val))
            obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22)
            logger.info(obj)
            session.add(obj)
        session.commit()
        logger.info("create hosts sucess!")
Exemplo n.º 5
0
def create_remoteusers(argvs):
    """
    create remoteusers
    :param argvs:
    :return:
    """
    if '-f' in argvs:
        remoteusers_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreate_remoteusers -f <the new remoteusers file>", logout=True)
        return
    source = yaml_parser(remoteusers_file)
    if source:
        logger.debug("source:\n%s" % source)
        for key, val in source.items():
            logger.debug("%s:%s" % (key, val))
            obj = models.RemoteUser(username=val.get('username'),
                                    auth_type=val.get('auth_type'),
                                    password=val.get('password'))
            logger.info(obj)
            session.add(obj)
        session.commit()
        logger.info("create remoteusers sucess!")