예제 #1
0
def create_bindhosts(argvs):
    msg = 'the new bind_hosts file'
    bindhost_file = parse_argvs(argvs, msg)
    source = yaml_parser(bindhost_file)
    if source:
        for key,val in source.items():
            host_obj = session.query(models.Host).filter(models.Host.hostname==val.get('hostname')).first()
            assert host_obj
            for item in val['remote_users']:
                assert item.get('auth_type')
                if item.get('auth_type') == 'ssh-passwd':
                    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()
                # print('>>>>',host_obj,remoteuser_obj)
                bindhost_obj = models.BindHost(host_id=host_obj.id,remote_user_id=remoteuser_obj.id)
                session.add(bindhost_obj)

                if source[key].get('groups'):
                    groups = common_filters.groups_filter(source[key])
                    bindhost_obj.groups = groups
                if source[key].get('user_profiles'):
                    user_profiles = common_filters.user_profiles_filter(source[key])
                    bindhost_obj.user_profiles = user_profiles

        session.commit()
예제 #2
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.Host).filter(
                models.Host.hostname == val.get('hostname')).first()
            assert host_obj  #断言assert必须存在
            for item in val['remote_users']:
                print(item)
                assert item.get('auth_type')
                if item.get('auth_type') == 'ssh-passwd':
                    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,
                              quit=True)
                bindhost_obj = models.BindHost(host_id=host_obj.id,
                                               remoteuser_id=remoteuser_obj.id)
                session.add(bindhost_obj)
                #for groups this host binds to
                if source[key].get('groups'):
                    group_objs = session.query(models.Group).filter(
                        models.Group.name.in_(
                            source[key].get('groups'))).all()
                    assert group_objs
                    '''此处有bug,bindhost导入依赖group,而group导入依赖bindhost'''
                    print('groups:', group_objs)
                    bindhost_obj.groups = group_objs
                #for user_profiles this host binds to
                if source[key].get('user_profiles'):
                    userprofile_objs = session.query(
                        models.UserProfile).filter(
                            models.UserProfile.username.in_(
                                source[key].get('user_profiles'))).all()
                    assert userprofile_objs
                    print("userprofiles:", userprofile_objs)
                    bindhost_obj.user_profiles = userprofile_objs
                #print(bindhost_obj)
        session.commit()
예제 #3
0
    def create_bindhosts(bindhost_file):
        source = yaml_parser(bindhost_file)
        if source:
            for key, val in source.items():
                host_obj = session.query(models.Host).filter(
                    models.Host.hostname == val['hostname']).first()
                #print(host_obj.id)
                for item in val['remote_users']:
                    print(item)
                    if item['auth_type'] == 'ssh-key':
                        remoteuser_obj = session.query(
                            models.RemoteUser).filter(
                                models.RemoteUser.username == item['username'],
                                models.RemoteUser.auth_type ==
                                'ssh-key').first()
                    elif item['auth_type'] == 'ssh-passwd':
                        remoteuser_obj = session.query(
                            models.RemoteUser).filter(
                                models.RemoteUser.username == item['username'],
                                models.RemoteUser.password ==
                                item['password']).first()
                    print(remoteuser_obj.id)
                    if (not host_obj) or (not remoteuser_obj):
                        print(
                            'There is something error between hostname or remote_user.'
                        )
                        continue
                    bindhost_obj = models.BindHost(
                        host_id=host_obj.id, remoteuser_id=remoteuser_obj.id)
                    session.add(bindhost_obj)
                if val['groups']:
                    for item in val['groups']:
                        group_obj = session.query(models.Group).filter(
                            models.Group.name.in_(item)).all()
                        bindhost_obj.groups = group_obj
                if val['user_profiles']:
                    for item in val['user_profiles']:
                        userprofile_obj = session.query(
                            models.UserProfile).filter(
                                models.UserProfile.username.in_(item)).all()
                        bindhost_obj.user_profiles = userprofile_obj

            session.commit()