Beispiel #1
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>",
            quit=True)
    source = yaml_parser(group_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = models_db.Host_Group(name=key)
            if val.get('bind_hosts'):
                bind_hosts = common_filters.bind_hosts_filter(val)
                obj.bind_hosts = bind_hosts

            if val.get('jump_users'):
                user_profiles = common_filters.user_profiles_filter(val)
                obj.user_profiles = user_profiles
            session.add(obj)
        session.commit()
Beispiel #2
0
def create_users(argvs):
    '''
    create little_finger access 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>",
            quit=True)

    source = yaml_parser(user_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = models_db.Jump_User(username=key,
                                      password=val.get('password'))
            # if val.get('groups'):
            #     groups = session.query(models_db.Host_Group).filter(models_db.Host_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()
Beispiel #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_hosts -f <the new bindhosts file>",
            quit=True)
    source = yaml_parser(bindhosts_file)
    if source:
        for key, val in source.items():
            #print(key,val)
            host_obj = session.query(models_db.Host).filter(
                models_db.Host.hostname == val.get('hostname')).first()
            assert host_obj
            for item in val['ssh_users']:
                # print("******",item )
                assert item.get('auth_type')
                if item.get('auth_type') == 'ssh-password':
                    sshuser_obj = session.query(models_db.Ssh_User).filter(
                        models_db.Ssh_User.username == item.get('username'),
                        models_db.Ssh_User.password == item.get(
                            'password')).first()
                else:
                    sshuser_obj = session.query(models_db.Ssh_User).filter(
                        models_db.Ssh_User.username == item.get('username'),
                        models_db.Ssh_User.auth_type == item.get('auth_type'),
                    ).first()
                if not sshuser_obj:
                    print_err("RemoteUser obj %s does not exist." % item,
                              quit=True)
                bindhost_obj = models_db.Bind_Host(host_id=host_obj.id,
                                                   sshuser_id=sshuser_obj.id)
                session.add(bindhost_obj)

                #for groups this host binds to
                if source[key].get('groups'):
                    group_objs = session.query(models_db.Host_Group).filter(
                        models_db.Host_Group.name.in_(
                            source[key].get('groups'))).all()
                    assert group_objs
                    print('groups:', group_objs)
                    bindhost_obj.hostgroups = group_objs
                #for jump_users this host binds to
                if source[key].get('jump_users'):
                    jumpuser_objs = session.query(models_db.Jump_User).filter(
                        models_db.Jump_User.username.in_(
                            source[key].get('jump_users'))).all()
                    assert jumpuser_objs
                    print("jump_users:", jumpuser_objs)
                    bindhost_obj.jumpusers = jumpuser_objs
                #print(bindhost_obj)
        session.commit()
Beispiel #4
0
def create_sshusers(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>",
            quit=True)
    source = yaml_parser(hosts_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = models_db.Ssh_User(username=val.get("username"),
                                     password=val.get('password'),
                                     auth_type=val.get('auth_type') or 22)
            session.add(obj)
        session.commit()
Beispiel #5
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>",
            quit=True)
    source = yaml_parser(hosts_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = models_db.Host(hostname=key,
                                 ip=val.get('ip_addr'),
                                 port=val.get('port') or 22)
            session.add(obj)
        session.commit()