示例#1
0
文件: views.py 项目: chenjinpeng1/S12
def create_users(argvs):
	cfg_file = argv_parser(argvs)
	source = utils.yaml_parser(cfg_file)
	# 如果解析出内容就将配置文件中的数据更新到数据库
	if source:
		session = db_conn.session
		for key in source:
			logger.debug("{}==>key:{},value:{}".format(source, key, source[key]))
			if source[key].get("admin_tag"):
				# 判断配置文件中是否有管理员的项
				obj = db_modles.UserProfile(username=key, password=source[key].get("password"), admin_tag=True)
			else:
				obj = db_modles.UserProfile(username=key, password=source[key].get("password"))
			# 如果在配置文件中有组信息
			if source[key].get("HostGroups"):
				groups = session.query(db_modles.HostGroup).filter(
					db_modles.HostGroup.name.in_(source[key].get("HostGroups"))
				).all()
				# 如果在数据库中没有找到了这个组名
				if not groups:
					logger.debug("Can't find <{}> in the host_group table.".format(source[key].get("HostGroups")))
					raise SystemExit("Invalid hostgroup parameters in the cfg_file.")
				obj.groups = groups
			if source[key].get("bind_hosts"):
				pass
			session.add(obj)
		session.commit()
示例#2
0
def create_bind_host_2_group():
    """创建绑定主机对应分组"""
    bind_host_file = os.path.join(TABLES_DIR, "bind_host.yaml")
    data = utils.yaml_parser(bind_host_file)
    for k, v in data.items():
        # print(k, v)
        host_info = v.get("host")  # 获取主机信息
        host_obj = db_conn.session.query(table_structure.Host).filter(
            host_info.get("ip") == table_structure.Host.ip).first()

        host_user_info = v.get("hostuser")  # 获取主机用户名密码信息
        login_type = host_user_info.get("login_type")  # 获取主机用户登陆类型
        username = host_user_info.get("username")  # 获取主机用户名
        password = host_user_info.get("password") or ""  # 获取主机用户密码,如果为空则输出""
        host_user_obj = db_conn.session.query(table_structure.HostUser).filter(
            login_type == table_structure.HostUser.login_type,
            username == table_structure.HostUser.username,
            password == table_structure.HostUser.password).first()

        bind_host_obj = db_conn.session.query(table_structure.BindHost).filter(
            host_obj.id == table_structure.BindHost.host_id,
            host_user_obj.id == table_structure.BindHost.host_user_id,
        ).first()

        group_info = v.get("groups")  # 获取分组信息
        if group_info:  # 如果存在分组则进行分组操作
            for i in group_info:
                group_obj = db_conn.session.query(
                    table_structure.Group).filter(
                        i == table_structure.Group.name).first()
                bind_host_obj.groups.append(group_obj)
    db_conn.session.commit()
示例#3
0
def create_hostandsysuser(argvs):
    """
	host.id -- sysuser.id 将Host表和Sysuser表通过id关联起来
	eq:
		10.10.10.1 -- root
		10.10.10.1 -- mysql
		10.10.10.1 -- tomcat
		10.10.10.2 -- root
	:param argvs:
	:return:
	"""
    cfg_file = argv_parser(argvs)
    source = utils.yaml_parser(cfg_file)
    if source:
        session = db_conn.session
        for key in source:
            logger.debug("{}==>key:{}, value:{}".format(
                source, key, source[key]))
            # 先根据配置文件中的hostname从Host表中找到host对象
            host_obj = session.query(
                db_modles.Host).filter(db_modles.Host.hostname ==
                                       source[key].get("hostname")).first()
            assert host_obj  # 确保找得到该host对象
            sys_users_list = source[key]["sys_users"]
            for item in sys_users_list:
                assert item["auth_type"]  # 确保item有"auth_type"这个项
                if item.get("auth_type") == "ssh-password":
                    sys_user_obj = session.query(db_modles.Sysuser).filter(
                        db_modles.Sysuser.username == item.get("username"),
                        db_modles.Sysuser.password == item.get("password"),
                    ).first()
                else:
                    sys_user_obj = session.query(db_modles.Sysuser).filter(
                        db_modles.Sysuser.username == item.get("username"),
                        db_modles.Sysuser.auth_type == item.get("auth_type"),
                    ).first()
                if not sys_user_obj:
                    logger.info("Can't find {} in <sys_user> table.".format(
                        item.get("username")))
                    raise SystemExit(
                        "Invalid sys_users parameters in cfg_file.")
                host_and_sysusers_obj = db_modles.HostandSysuser(
                    host_id=host_obj.id, sysuser_id=sys_user_obj.id)
                session.add(host_and_sysusers_obj)
                # 如果配置文件中有groups项
                if source[key].get("groups"):
                    host_group_objs = session.query(db_modles.Group).filter(
                        db_modles.Group.name.in_(
                            source[key].get("groups"))).all()
                    assert host_group_objs
                    host_and_sysusers_obj.groups = host_group_objs
                # 如果配置文件中有user_profiles项
                if source[key].get("user_profiles"):
                    user_profiles_objs = session.query(
                        db_modles.UserProfile).filter(
                            db_modles.UserProfile.username.in_(
                                source[key].get("user_profiles"))).all()
                    assert user_profiles_objs
                    host_and_sysusers_obj.user_profiles = user_profiles_objs
            session.commit()
示例#4
0
def create_users(argvs):
    cfg_file = argv_parser(argvs)
    source = utils.yaml_parser(cfg_file)
    # 如果解析出内容就将配置文件中的数据更新到数据库
    if source:
        session = db_conn.session
        for key in source:
            logger.debug("{}==>key:{},value:{}".format(source, key,
                                                       source[key]))
            if source[key].get("admin_tag"):
                # 判断配置文件中是否有管理员的项
                obj = db_modles.UserProfile(
                    username=key,
                    password=source[key].get("password"),
                    admin_tag=True)
            else:
                obj = db_modles.UserProfile(
                    username=key, password=source[key].get("password"))
            # 如果在配置文件中有组信息
            if source[key].get("HostGroups"):
                groups = session.query(db_modles.HostGroup).filter(
                    db_modles.HostGroup.name.in_(
                        source[key].get("HostGroups"))).all()
                # 如果在数据库中没有找到了这个组名
                if not groups:
                    logger.debug(
                        "Can't find <{}> in the host_group table.".format(
                            source[key].get("HostGroups")))
                    raise SystemExit(
                        "Invalid hostgroup parameters in the cfg_file.")
                obj.groups = groups
            if source[key].get("bind_hosts"):
                pass
            session.add(obj)
        session.commit()
示例#5
0
def create_group():
    """创建分组"""
    group_file = os.path.join(TABLES_DIR, "group.yaml")
    data = utils.yaml_parser(group_file)
    for k, v in data.items():
        group_obj = table_structure.Group(name=v[0])
        db_conn.session.add(group_obj)
    db_conn.session.commit()
示例#6
0
def create_user():
    """创建堡垒机用户"""
    user_file = os.path.join(TABLES_DIR, "user.yaml")
    data = utils.yaml_parser(user_file)
    for k, v in data.items():
        user_obj = table_structure.User(username=v.get("username"),
                                        password=v.get("password"))
        db_conn.session.add(user_obj)
    db_conn.session.commit()
示例#7
0
def create_host():
    """创建主机信息"""
    host_file = os.path.join(TABLES_DIR, "host.yaml")
    data = utils.yaml_parser(host_file)
    for k, v in data.items():
        host_obj = table_structure.Host(hostname=k,
                                        ip=v.get("ip"),
                                        port=v.get("port") or 22)
        db_conn.session.add(host_obj)
    db_conn.session.commit()
示例#8
0
文件: views.py 项目: chenjinpeng1/S12
def create_hostandsysuser(argvs):
	"""
	host.id -- sysuser.id 将Host表和Sysuser表通过id关联起来
	eq:
		10.10.10.1 -- root
		10.10.10.1 -- mysql
		10.10.10.1 -- tomcat
		10.10.10.2 -- root
	:param argvs:
	:return:
	"""
	cfg_file = argv_parser(argvs)
	source = utils.yaml_parser(cfg_file)
	if source:
		session = db_conn.session
		for key in source:
			logger.debug("{}==>key:{}, value:{}".format(source, key, source[key]))
			# 先根据配置文件中的hostname从Host表中找到host对象
			host_obj = session.query(db_modles.Host).filter(
				db_modles.Host.hostname == source[key].get("hostname")
			).first()
			assert host_obj  # 确保找得到该host对象
			sys_users_list = source[key]["sys_users"]
			for item in sys_users_list:
				assert item["auth_type"]  # 确保item有"auth_type"这个项
				if item.get("auth_type") == "ssh-password":
					sys_user_obj = session.query(db_modles.Sysuser).filter(
						db_modles.Sysuser.username == item.get("username"),
						db_modles.Sysuser.password == item.get("password"),
					).first()
				else:
					sys_user_obj = session.query(db_modles.Sysuser).filter(
						db_modles.Sysuser.username == item.get("username"),
						db_modles.Sysuser.auth_type == item.get("auth_type"),
					).first()
				if not sys_user_obj:
					logger.info("Can't find {} in <sys_user> table.".format(item.get("username")))
					raise SystemExit("Invalid sys_users parameters in cfg_file.")
				host_and_sysusers_obj = db_modles.HostandSysuser(host_id=host_obj.id, sysuser_id=sys_user_obj.id)
				session.add(host_and_sysusers_obj)
				# 如果配置文件中有groups项
				if source[key].get("groups"):
					host_group_objs = session.query(db_modles.Group).filter(
						db_modles.Group.name.in_(source[key].get("groups"))
					).all()
					assert host_group_objs
					host_and_sysusers_obj.groups = host_group_objs
				# 如果配置文件中有user_profiles项
				if source[key].get("user_profiles"):
					user_profiles_objs = session.query(db_modles.UserProfile).filter(
						db_modles.UserProfile.username.in_(source[key].get("user_profiles"))
					).all()
					assert user_profiles_objs
					host_and_sysusers_obj.user_profiles = user_profiles_objs
			session.commit()
示例#9
0
def create_host_user():
    """创建主机用户名密码"""
    host_user_file = os.path.join(TABLES_DIR, "host_user.yaml")
    data = utils.yaml_parser(host_user_file)
    for k, v in data.items():
        host_user_obj = table_structure.HostUser(
            login_type=v.get("login_type"),
            username=v.get("username"),
            password=v.get("password") or "")
        db_conn.session.add(host_user_obj)
    db_conn.session.commit()
示例#10
0
def create_sysusers(argvs):
    cfg_file = argv_parser(argvs)
    source = utils.yaml_parser(cfg_file)
    if source:
        session = db_conn.session
        for key in source:
            obj = db_modles.Sysuser(
                username=source[key].get("username"),
                auth_type=source[key].get("auth_type"),
                password=source[key].get("password"),
            )
            session.add(obj)
        session.commit()
示例#11
0
文件: views.py 项目: chenjinpeng1/S12
def create_sysusers(argvs):
	cfg_file = argv_parser(argvs)
	source = utils.yaml_parser(cfg_file)
	if source:
		session = db_conn.session
		for key in source:
			obj = db_modles.Sysuser(
				username=source[key].get("username"),
				auth_type=source[key].get("auth_type"),
				password=source[key].get("password"),
			)
			session.add(obj)
		session.commit()
示例#12
0
文件: views.py 项目: chenjinpeng1/S12
def create_hosts(argvs):
	cfg_file = argv_parser(argvs)
	source = utils.yaml_parser(cfg_file)
	if source:
		session = db_conn.session
		for key in source:
			logger.debug("{}==>key:{}, value:{}".format(source, key, source[key]))
			obj = db_modles.Host(
					hostname=key,
					ip_addr=source[key].get("ip_addr"),
					port=source[key].get("port") or 22,
			)
			session.add(obj)
		session.commit()
示例#13
0
def create_hosts(argvs):
    cfg_file = argv_parser(argvs)
    source = utils.yaml_parser(cfg_file)
    if source:
        session = db_conn.session
        for key in source:
            logger.debug("{}==>key:{}, value:{}".format(
                source, key, source[key]))
            obj = db_modles.Host(
                hostname=key,
                ip_addr=source[key].get("ip_addr"),
                port=source[key].get("port") or 22,
            )
            session.add(obj)
        session.commit()
示例#14
0
文件: views.py 项目: chenjinpeng1/S12
def create_groups(argvs):
	cfg_file = argv_parser(argvs)
	source = utils.yaml_parser(cfg_file)
	if source:
		session = db_conn.session
		for key in source:
			logger.debug("{}==>key:{}, value:{}".format(source, key, source[key]))
			obj = db_modles.HostGroup(name=key)
			# 如果配置文件中有host_list选项
			if source[key].get("host_list"):
				host_list = info_filter.get_host_list(source[key])
				obj.host_and_sysusers = host_list
			# 如果配置文件中有user_profiles选项
			if source[key].get("user_profiles"):
				user_profiles = info_filter.get_user_profiles(source[key])
				obj.user_profiles = user_profiles
			session.add(obj)
		session.commit()
示例#15
0
def create_groups(argvs):
    cfg_file = argv_parser(argvs)
    source = utils.yaml_parser(cfg_file)
    if source:
        session = db_conn.session
        for key in source:
            logger.debug("{}==>key:{}, value:{}".format(
                source, key, source[key]))
            obj = db_modles.HostGroup(name=key)
            # 如果配置文件中有host_list选项
            if source[key].get("host_list"):
                host_list = info_filter.get_host_list(source[key])
                obj.host_and_sysusers = host_list
            # 如果配置文件中有user_profiles选项
            if source[key].get("user_profiles"):
                user_profiles = info_filter.get_user_profiles(source[key])
                obj.user_profiles = user_profiles
            session.add(obj)
        session.commit()
示例#16
0
def create_user_2_group():
    """创建堡垒机用户对应分组"""
    user_file = os.path.join(TABLES_DIR, "user.yaml")
    data = utils.yaml_parser(user_file)
    for k, v in data.items():
        user_username = v.get("username")
        user_password = v.get("password")
        user_obj = db_conn.session.query(table_structure.User).filter(
            user_username == table_structure.User.username,
            user_password == table_structure.User.password).first()
        group_info = v.get("groups")
        if group_info:
            for group in group_info:
                group_name = group
                group_obj = db_conn.session.query(
                    table_structure.Group).filter(
                        group_name == table_structure.Group.name).first()
                user_obj.groups.append(group_obj)
    db_conn.session.commit()
示例#17
0
def create_bind_host():
    """创建绑定主机"""
    bind_host_file = os.path.join(TABLES_DIR, "bind_host.yaml")
    data = utils.yaml_parser(bind_host_file)
    for k, v in data.items():
        # print(k, v)
        host_info = v.get("host")  # 获取主机信息
        host_obj = db_conn.session.query(table_structure.Host).filter(
            host_info.get("ip") == table_structure.Host.ip).first()

        host_user_info = v.get("hostuser")  # 获取主机用户名密码信息
        login_type = host_user_info.get("login_type")  # 获取主机用户登陆类型
        username = host_user_info.get("username")  # 获取主机用户名
        password = host_user_info.get("password") or ""  # 获取主机用户密码,如果为空则输出""
        host_user_obj = db_conn.session.query(table_structure.HostUser).filter(
            login_type == table_structure.HostUser.login_type,
            username == table_structure.HostUser.username,
            password == table_structure.HostUser.password).first()

        bind_host_obj = table_structure.BindHost(host_id=host_obj.id,
                                                 host_user_id=host_user_obj.id)
        db_conn.session.add(bind_host_obj)
    db_conn.session.commit()
示例#18
0
def create_user_2_bindhost():
    """创建堡垒机用户对应绑定主机"""
    user_file = os.path.join(TABLES_DIR, "user.yaml")
    data = utils.yaml_parser(user_file)
    for k, v in data.items():
        user_username = v.get("username")
        user_password = v.get("password")
        user_obj = db_conn.session.query(table_structure.User).filter(
            user_username == table_structure.User.username,
            user_password == table_structure.User.password).first()
        bind_host_info = v.get("bindhosts")
        if bind_host_info:
            for bind_host in bind_host_info:
                host_info = bind_host.get("host")  # 获取主机信息
                host_obj = db_conn.session.query(table_structure.Host).filter(
                    host_info.get("ip") == table_structure.Host.ip).first()

                host_user_info = bind_host.get("hostuser")  # 获取主机用户名密码信息
                login_type = host_user_info.get("login_type")  # 获取主机用户登陆类型
                username = host_user_info.get("username")  # 获取主机用户名
                password = host_user_info.get(
                    "password") or ""  # 获取主机用户密码,如果为空则输出""
                host_user_obj = db_conn.session.query(
                    table_structure.HostUser).filter(
                        login_type == table_structure.HostUser.login_type,
                        username == table_structure.HostUser.username,
                        password == table_structure.HostUser.password).first()

                bind_host_obj = db_conn.session.query(
                    table_structure.BindHost).filter(
                        host_obj.id == table_structure.BindHost.host_id,
                        host_user_obj.id ==
                        table_structure.BindHost.host_user_id,
                    ).first()
                user_obj.bind_hosts.append(bind_host_obj)
    db_conn.session.commit()