from Arya.backends.base_module import BaseSaltModule
import os
class State(BaseSaltModule):
def load_state_files(self,state_filename): #加载yumfeile
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> :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):
#导入 模块