예제 #1
0
    def apply(self):
        """
        解析任务
        执行任务
        放到任务队列
        返回任务队列id
        :return:
        """
        if '-f' in self.sys_args:
            # yam 配置文件
            yaml_file_index = self.sys_args.index('-f') + 1

            try:
                # 保存配置文件
                yaml_file_name = self.sys_args[yaml_file_index]
                state_data = self.load_state_files(yaml_file_name)

                # 按照不同的操作系统生成不同的配置文件
                for os_type, os_type_data in self.config_data_dic.items():

                    for section_name, section_data in state_data.items():
                        # print(section_name)
                        for mod_name, mod_data in section_data.items():
                            # print('   ', mod_name)  # user.present

                            # 根据配置文件的名字,在plugins中判断是否存在此模块
                            base_mode_name = mod_name.split('.')[0]  # user

                            module_obj = self.get_module_instance(
                                base_mode_name=base_mode_name, os_type=os_type)
                            moudle_parse_result = module_obj.syntax_parser(
                                section_name, mod_name, mod_data, os_type)
                            self.config_data_dic[os_type].append(
                                moudle_parse_result)

                print('config_data_dic'.center(60, '*'))
                print(self.config_data_dic)

                # 生成任务mq消息
                new_task_obj = tasks.TaskHandle(self.db_models,
                                                self.config_data_dic,
                                                self.settings, self)
                new_task_obj.dispatch_task()  # 分发任务

            except IndexError as e:
                exit('state file must -f after')

        else:
            exit('state file must -f after')
예제 #2
0
    def apply(self):
        '''
        1. load the configurations file
        2. parse it
        3. create a task and sent it to the MQ
        4. collect the result with task-callback id
        :return:
        '''

        if '-f' in self.sys_argvs:
            yaml_file_index = self.sys_argvs.index('-f') + 1
            try:
                yaml_filename = self.sys_argvs[yaml_file_index]
                state_data = self.load_state_files(yaml_filename)
                #print('state data:',state_data)

                for os_type, os_type_data in self.config_data_dic.items(
                ):  #按照不同的操作系统单独生成一份配置文件
                    for section_name, section_data in state_data.items(
                    ):  # section_name == apache
                        print('Section:', section_name)
                        for mod_name, mod_data in section_data.items(
                        ):  # mod_name == user,group(模板.方法),
                            base_mod_name = mod_name.split(".")[0]
                            module_obj = self.get_module_instance(
                                base_mod_name=base_mod_name,
                                os_type=os_type)  #生成不同操作系统的类
                            module_parse_result = module_obj.syntax_parser(
                                section_name, mod_name, mod_data,
                                os_type)  # 语法解析
                            self.config_data_dic[os_type].append(
                                module_parse_result)

                #代表上面的所有数据解析已完成 ,接下来生成一个任务,并把任务 放入队列
                print('config_data_dic'.center(60, '*'))
                print(self.config_data_dic)
                #生成新任务
                new_task_obj = tasks.TaskHandle(self.db_models,
                                                self.config_data_dic,
                                                self.settings, self)
                new_task_obj.dispatch_task()
            except IndexError as e:
                exit("state file must be provided after -f")

        else:
            exit("statefile must be specified.")
예제 #3
0
    def apply(self):
        '''1. load yml文件  2. parse it 3.创建任务发送到MQ, 4.收集结果with task-callback id'''

        if '-f' in self.sys_argvs:
            yaml_file_index = self.sys_argvs.index('-f') + 1
            try:
                yaml_filename = self.sys_argvs[yaml_file_index]
                state_data = self.load_state_files(yaml_filename)
                # print('state data:', state_data)

                for os_type, os_type_data in self.config_data_dic.items():
                    # 按照不同的操作系统生成不同配置文件,操作系统相同,执行的命令也就相同
                    # print("\t系统类型",os_type)
                    for section_name, section_data in state_data.items():
                        # print('Section(部分名):', section_name)
                        #mod_name类似于user.present、group.present,下面的两个print可以不用
                        for mod_name, mod_data in section_data.items():
                            # print("模块名", mod_name)
                            # for state_item in mod_data:
                            #     print("\t", state_item)
                            base_mod_name = mod_name.split(".")[0]
                            #在这里根据字符串,拿到具体的模块,诸如user.User、group.Group的类
                            module_obj = self.get_module_instance(
                                base_mod_name=base_mod_name, os_type=os_type)
                            module_parse_result = module_obj.syntax_parser(
                                section_name, mod_name, mod_data,
                                os_type)  #用mod_data映射具体的uid、gid方法
                            self.config_data_dic[os_type].append(
                                module_parse_result)
                # print("config_data_dic".center(60,'*'))
                print("config_data_dic是什么", self.config_data_dic)
                print("self是state.State()...", self)
                #这里把self实例传进去,是为了获得host_list信息,self在那个模块里,self就是谁
                new_task_obj = tasks.TaskHandle(self.db_models,
                                                self.config_data_dic,
                                                self.settings, self)
                new_task_obj.dispatch_task()
            except IndexError as e:  #超出边界了
                exit("state file must be provided after -f")
        else:
            exit("statefile必须提供")
예제 #4
0
    def apply(self):
        '''
        1. load the configurations file
        2. parse it
        3. create a task and sent it to the MQ
        4. collect the result with task-callback id
        :return:
        '''

        if '-f' in self.sys_argvs:
            yaml_file_index = self.sys_argvs.index('-f') + 1
            try:
                yaml_filename = self.sys_argvs[yaml_file_index]
                state_data = self.load_state_files(yaml_filename)
                #print('state data:',state_data)
                print(self.config_data_dic)
                for os_type, os_type_data in self.config_data_dic.items(
                ):  #按照不同的操作系统单独生成一份配置文件
                    for section_name, section_data in state_data.items():
                        print('\033[32;1mSection:\033[0m', section_name)

                        for mod_name, mod_data in section_data.items():
                            base_mod_name = mod_name.split(".")[0]
                            plugin_file_path = "%s/%s.py" % (
                                self.settings.SALT_PLUGINS_DIR, base_mod_name)
                            if os.path.isfile(plugin_file_path):
                                #导入 模块

                                module_plugin = __import__('plugins.%s' %
                                                           base_mod_name)
                                special_os_module_name = "%s%s" % (
                                    os_type.capitalize(),
                                    base_mod_name.capitalize())
                                #print('dir module plugin:',module_plugin,base_mod_name)
                                #getattr(module_plugin,base_mod_name)
                                module_file = getattr(
                                    module_plugin, base_mod_name)  # 这里才是真正导入模块
                                if hasattr(module_file, special_os_module_name
                                           ):  #判断有没有根据操作系统的类型进行特殊解析 的类,在这个文件里
                                    module_instance = getattr(
                                        module_file, special_os_module_name)
                                else:
                                    module_instance = getattr(
                                        module_file,
                                        base_mod_name.capitalize())

                                #开始调用 此module 进行配置解析
                                module_obj = module_instance(self.sys_argvs,
                                                             self.db_models,
                                                             self.settings,
                                                             os_type=os_type)
                                parser_result = module_obj.syntax_parser(
                                    section_name, mod_name, mod_data)
                                self.config_data_dic[os_type].append(
                                    parser_result)

                            else:
                                exit("module [%s] is not exist" %
                                     base_mod_name)
                            #print("  ",mod_name)
                            #for state_item in mod_data:
                            #    print("\t",state_item)

                print('\033[31;1mfinal parse config\033[0m'.center(50, '-'))
                for os_type, os_type_data in self.config_data_dic.items():
                    print('-----------os:%s------' % os_type)
                    for section in os_type_data:
                        print(section)
                        print('===================================')
                #生成新任务
                new_task_obj = tasks.TaskHandle(self.db_models,
                                                self.config_data_dic,
                                                self.settings, self)
                new_task_obj.dispatch_task()
            except IndexError as e:
                exit("state file must be provided after -f")

        else:
            exit("statefile must be specified.")