zoukankan      html  css  js  c++  java
  • saltstack主机管理项目:动态调用插件解析-模块解析(五)

    一、动态调用插件解析

     1、目录结构

    1、base_module代码解析:

        def syntax_parser(self,section_name,mod_name,mod_data):
            print("-going to parser state data:",section_name,mod_name)
            for state_item in mod_data:
                print("	",state_item)
                for key,val in state_item.items():
                    if hasattr(self,key):
                        state_func = getattr(self,key)
                        state_func(val)
                    else:
                        exit("Error:module [%s] has no argument [%s]" %( mod_name,key ))
    

    2、sate.py代码

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:luoahong
    from Arya.backends.base_module import BaseSaltModule
    import os
    
    class State(BaseSaltModule):
    
        def load_state_files(self,state_filename):
            from yaml import load, dump
            try:
                from yaml import CLoader as Loader, CDumper as Dumper
            except ImportError:
                from yaml import Loader, Dumper
            state_file_path = "%s/%s" %(self.settings.SALT_CONFIG_FILES_DIR,state_filename)
            if os.path.isfile(state_file_path):
                with open(state_file_path) as f:
                    data = load(f.read(), Loader=Loader)
                    return data
            else:
                exit("%s is not a valid yaml config file" % state_filename)
    
        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():
                            print('Section:',section_name)

    3、运行截图

    二、模块参数解析

    1、目录结构

    二、代码

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:luoahong
    
    
    class BaseSaltModule(object):
        def __init__(self,sys_argvs,db_models,settings):
            self.db_models = db_models
            self.settings = settings
            self.sys_argvs = sys_argvs
    
        def get_selected_os_types(self):
            data = {}
            for host in self.host_list:
                data[host.os_type] = []
            print('--->data',data)
            return data
        def process(self):
            self.fetch_hosts()
            self.config_data_dic = self.get_selected_os_types()
        def require(self,*args,**kwargs):
            pass
        def fetch_hosts(self):
            print('--fetching hosts---')
    
            if '-h' in self.sys_argvs or '-g' in self.sys_argvs:
                host_list = []
                if '-h' in self.sys_argvs:
                    host_str_index = self.sys_argvs.index('-h') +1
                    if len(self.sys_argvs) <= host_str_index:
                        exit("host argument must be provided after -h")
                    else: #get the host str
                        host_str = self.sys_argvs[host_str_index]
                        host_str_list = host_str.split(',')
                        host_list += self.db_models.Host.objects.filter(hostname__in=host_str_list)
                if '-g' in self.sys_argvs:
                    group_str_index = self.sys_argvs.index('-g') +1
                    if len(self.sys_argvs) <= group_str_index:
                        exit("group argument must be provided after -g")
                    else: #get the group str
                        group_str = self.sys_argvs[group_str_index]
                        group_str_list = group_str.split(',')
                        group_list = self.db_models.HostGroup.objects.filter(name__in=group_str_list)
                        for group in group_list:
                            host_list += group.hosts.select_related()
                self.host_list = set(host_list)
                return True
                print('----host list:', host_list)
            else:
                exit("host [-h] or group[-g] argument must be provided")
    
    
        def syntax_parser(self,section_name,mod_name,mod_data):
            print("-going to parser state data:",section_name,mod_name)
            for state_item in mod_data:
                print("	",state_item)
                for key,val in state_item.items():
                    if hasattr(self,key):
                        state_func = getattr(self,key)
                        state_func(val)
                    else:
                        exit("Error:module [%s] has no argument [%s]" %( mod_name,key ))

    sates.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:luoahong
    from Arya.backends.base_module import BaseSaltModule
    import os
    
    class State(BaseSaltModule):
    
        def load_state_files(self,state_filename):
            from yaml import load, dump
            try:
                from yaml import CLoader as Loader, CDumper as Dumper
            except ImportError:
                from yaml import Loader, Dumper
            state_file_path = "%s/%s" %(self.settings.SALT_CONFIG_FILES_DIR,state_filename)
            if os.path.isfile(state_file_path):
                with open(state_file_path) as f:
                    data = load(f.read(), Loader=Loader)
                    return data
            else:
                exit("%s is not a valid yaml config file" % state_filename)
    
        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():
                            print('Section:',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)
                                    module_obj.syntax_parser(section_name,mod_name,mod_data )
                                else:
                                    exit("module [%s] is not exist" % base_mod_name)
                                #print("  ",mod_name)
                                #for state_item in mod_data:
                                #    print("	",state_item)
    
                except IndexError as e:
                    exit("state file must be provided after -f")
    
            else:
                exit("statefile must be specified.")
  • 相关阅读:
    第一个JAVA程序
    python小项目(python实现鉴黄)源码
    整蛊小病毒,自己拿来快乐
    JavaScript(这里主要侧重于 JavaScript HTML DOM)杂项
    图片与文本基础(html和css)
    CSS的基础学习
    Javascript中this作用域以及bind方法的重写
    斐波那契数列算法求解及速度
    javascript中的描述对象(Descriptor)获取和定义随笔
    vue-cli3生产环境和开发环境路径的替换
  • 原文地址:https://www.cnblogs.com/luoahong/p/9426776.html
Copyright © 2011-2022 走看看