def create_users(argvs): 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.UserProfile(username=key, password=val.get('password')) 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()
def create_users(argvs): # 创建堡垒机本机用户 ''' create little_finger access user :param argvs: :return: ''' if '-f' in argvs: #先判断有没有-f参数 user_file = argvs[argvs.index("-f") + 1] #通过index计算出-f的索引值,在通过+1 获得userfile else: print_err( "invalid usage, should be:\ncreateusers -f <the new users file>", quit=True) source = yaml_parser(user_file) #通过yaml_parser将文件序列化为字典赋值给source if source: for key, val in source.items(): print(key, val) obj = models.UserProfile(username=key, password=val.get('password')) 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()
def create_users(argvs): msg = 'the new users file' user_file = parse_argvs(argvs, msg) source = yaml_parser(user_file) if source: for key,val in source.items(): obj = models.UserProfile(username=key,password=val.get('password')) if val.get('groups'):#多对多关系 groups = common_filters.groups_filter(val) obj.groups = groups if val.get('bind_hosts'):#多对多关系 bind_hosts = common_filters.bind_hosts_filter(val) obj.bind_hosts = bind_hosts session.add(obj) session.commit()
def create_users(user_file): source = yaml_parser(user_file) if source: for key, val in source.items(): print(key, val) obj = models.UserProfile(username=key, password=val['password']) if val.get('groups'): '''如果val中含有group,且该group存在,则同时讲该用户加入对应group''' groups = common.bind_group_filter(val) obj.groups = groups if val.get('bind_hosts'): '''如果val有bind_hosts,则同时绑定hosts''' bind_hosts = common.bind_hosts_filter(val) obj.bind_hosts = bind_hosts session.add(obj) session.commit()
def create_users(argvs): ''' create little_finger access user :param argvs: :return: ''' if '-f' in argvs: # 获取userprofile配置文件 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.UserProfile(username=key, password=val.get('password')) # 添加堡垒机用户关联组 if val.get('groups'): # 数据库查询组过滤组名是[xxx] 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()