def __init__(self, *args, **kwargs): self.config_items = Config_read() self.basepath = GET().get_db_dir() self.create_dir = CREATE() '''检查配置文件中的配置项属于绝对路径还是相对路径。并且如果目录不存在自行创建''' self.local_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='DATABASE', key='DATABASE_NAME')) self.schema_dir = os.path.join(self.basepath, 'schema.sql')
def __initalizeData(self): ''' 初始化ansible ''' Options = namedtuple('Options', [ 'connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff', 'tags', ]) self.options = Options( connection='smart', remote_user='******', ack_pass=None, forks=int(self.forks), ask_sudo_pass=False, verbosity=5, module_path=None, become=True, become_method='su', become_user='******', check=None, listhosts=False, listtasks=False, listtags=False, diff=False, syntax=None, tags=self.tags, ) self.loader = DataLoader() self.variable_manager = VariableManager(loader=self.loader) self.config_items = Config_read() self.log_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='LOGGER_DIR', key='ANSIBLE_LOG_FILE')) self.inventory = InventoryManager( loader=self.loader, sources=self.config_items.get_value( sections='ANSIBLE', key='INVENTORY_CONFIG').split(",")) self.variable_manager.set_inventory(self.inventory) self.passwords = dict(conn_pass='******', become_pass='******')
def __initalizeData(self): self.basepath = GET().get_log_dir() self.create_dir = CREATE() self.config_items = Config_read() self.local_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='LOGGER_DIR', key='GLOBAL_LOG_FILE')) self.external_path = self.create_dir.create_dir(basepath=self.basepath, extra_path=self.path) self.logger = logging.getLogger(self.logger_name)
def play_adhoc(self,hosts,module_name,module_args,register='result',forks=int(Config_read().get_value(sections='ANSIBLE',key='FORKS')),tags=[]): self.play = Runner(forks=forks,tags=tags) self.host_lists = hosts self.module_name = module_name self.module_args = module_args self.register = register self.play.run(self.host_lists,module_name,module_args,register) result = self.play.get_adhoc_result()
def play_playbook(self,extra_vars,playbooks,forks=int(Config_read().get_value(sections='ANSIBLE',key='FORKS')),ssh_user = '******',project_name = 'ansible',tags = []): self.playbooks = [playbooks] self.forks = forks self.tags = tags self.ssh_user = ssh_user self.project_name = project_name self.tags = tags self.extra_vars = extra_vars from flaskr_bigapp.lib.db.sqlite3_db import Playbooks_Record self.playbooks_record = Playbooks_Record() self.play = Runner(forks=self.forks,tags=self.tags) self.play.run_playbook(playbooks=self.playbooks, ssh_user=self.ssh_user, project_name=self.project_name, extra_vars=self.extra_vars ) result = self.play.get_playbook_result() dict = {'playbooks':self.playbooks,'ssh_user':self.ssh_user,'project_name':self.project_name,'extra_vars':self.extra_vars,'forks':self.forks,'tags':self.tags} test=json.dumps(dict,sort_keys=True,indent = 4,separators=(',', ': '),encoding="utf8",ensure_ascii=True ) print test self.playbooks_record.insert_tables(playbooks=self.playbooks,ssh_user=self.ssh_user,project_name=self.project_name,extra_vars=self.extra_vars,forks=self.forks,tags=self.tags,time=datetime(2015,1,12))
from flaskr_bigapp.lib.ops.runner import Runner from flaskr_bigapp.lib.sys.configuration import Config_read from flaskr_bigapp.lib.sys.configuration import Config_read from flaskr_bigapp.lib.sys.make import GET,CREATE from peewee import SqliteDatabase from datetime import datetime import json config_items = Config_read() basepath = GET().get_db_dir() create_dir = CREATE() db = SqliteDatabase(create_dir.create_dir(basepath=basepath, extra_path=config_items.get_value(sections='DATABASE',key='DATABASE_NAME'))) class Play(object): def __init__(self,*args,**kwargs): pass; def play_adhoc(self,hosts,module_name,module_args,register='result',forks=int(Config_read().get_value(sections='ANSIBLE',key='FORKS')),tags=[]): self.play = Runner(forks=forks,tags=tags) self.host_lists = hosts self.module_name = module_name self.module_args = module_args self.register = register self.play.run(self.host_lists,module_name,module_args,register) result = self.play.get_adhoc_result() def play_playbook(self,extra_vars,playbooks,forks=int(Config_read().get_value(sections='ANSIBLE',key='FORKS')),ssh_user = '******',project_name = 'ansible',tags = []): self.playbooks = [playbooks] self.forks = forks self.tags = tags self.ssh_user = ssh_user
class Database(object): def __init__(self, *args, **kwargs): self.config_items = Config_read() self.basepath = GET().get_db_dir() self.create_dir = CREATE() '''检查配置文件中的配置项属于绝对路径还是相对路径。并且如果目录不存在自行创建''' self.local_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='DATABASE', key='DATABASE_NAME')) self.schema_dir = os.path.join(self.basepath, 'schema.sql') def connect_db(self): return sqlite3_db.connect(self.local_path) def create_db(self): '''创建sql数据并并当执行完成后自动关闭数据库链接''' with closing(self.connect_db()) as db: with open(self.schema_dir) as f: # cursor(游标),用来执行数据库命令。可以excute数据命令、exeutescript执行脚本。 db.cursor().executescript(f.read()) db.commit() db.close() def insert_db(self, sql): db = self.connect_db() cur = db.cursor() cur.execute(sql) db.commit() def select_db(self): db = self.connect_db() cur = db.cursor() cur.execute('SELECT * from playbooks_record') print cur.fetchall() db.close() def insert_db_temp(self): db = self.connect_db() tables = 'playbooks_record' dicts = { 'playbooks': 'test', 'ssh_user': '******', 'project_name': 'test', 'extra_vars': "{'hostlists':'127.0.0.12'}", 'forks': "20", 'tags': "['f**k']" } key_lists = [keys.strip() for keys in dicts.keys()] value_lists = [value for value in dicts.values()] key_len = len(key_lists) value_len = len(value_lists) if key_len == value_len: return_key = ', '.join(list(key_lists)) return_value = ', '.join(list(value_lists)) else: print 'f**k' for i in range(0, key_len): print '?' print return_key print return_value #g.db.execute('INSERT INTO %s (%s) VALUES (?, ?)' %(tables,return_key), [return_value]) #g.db.commit() def query_db(query, args=(), one=False): cur = g.db.execute(query, args) rv = [ dict((cur.description[idx][0], value) for idx, value in enumerate(row)) for row in cur.fetchall() ] return (rv[0] if rv else None) if one else rv
class Runner(object): def __init__(self, tags, forks, *args, **kwargs): self._inventory = None self._variable_manager = None self._loader = None self._options = None self.passwords = None self.callback = None self.log_file_name = 'ansible.log' self.forks = forks self.tags = tags self._unreachable_hosts = dict() self.basepath = GET().get_log_dir() self.create_dir = CREATE() self.__initalizeData() def __initalizeData(self): ''' 初始化ansible ''' Options = namedtuple('Options', [ 'connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff', 'tags', ]) self.options = Options( connection='smart', remote_user='******', ack_pass=None, forks=int(self.forks), ask_sudo_pass=False, verbosity=5, module_path=None, become=True, become_method='su', become_user='******', check=None, listhosts=False, listtasks=False, listtags=False, diff=False, syntax=None, tags=self.tags, ) self.loader = DataLoader() self.variable_manager = VariableManager(loader=self.loader) self.config_items = Config_read() self.log_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='LOGGER_DIR', key='ANSIBLE_LOG_FILE')) self.inventory = InventoryManager( loader=self.loader, sources=self.config_items.get_value( sections='ANSIBLE', key='INVENTORY_CONFIG').split(",")) self.variable_manager.set_inventory(self.inventory) self.passwords = dict(conn_pass='******', become_pass='******') ''' 运行ansible adhoc ''' def run(self, host_lists, module_name, module_args, register): self.logger = Logger(logger_name='ansible_adhoc', external_path=self.log_path) self.callback = ResultsCollector() play_source = dict(name='Ansible Play', hosts=host_lists, gather_facts='no', tasks=[ dict(action=dict(module=module_name, args=module_args), register=register) ]) play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, stdout_callback=self.callback, ) result = tqm.run(play) except Exception as e: print e self.logger.error(message='Ansible adhoc error: %s' % e) finally: if tqm is not None: tqm.cleanup() ''' 运行ansible playbook ''' def run_playbook(self, playbooks, ssh_user, project_name, extra_vars={}): try: self.playbookcallback = PlaybookResultsCollector() self.logger = Logger(logger_name='ansible_playbook', external_path=self.log_path) self.playbooks = playbooks if not os.path.exists(self.playbooks[0]): self.logger.error(message="Playbooks '%s' is not exist" % self.playbooks[0]) sys.exit(1) else: self.logger.debug(message="Playbooks '%s' is exist" % self.playbooks[0]) self.ssh_user = ssh_user self.project_name = project_name self.ack_pass = False self.connection = 'smart' self.extra_vars = extra_vars self.variable_manager.extra_vars = extra_vars pb = None pb = PlaybookExecutor( playbooks=self.playbooks, inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, #tag = 'repo' ) pb._tqm._stdout_callback = self.playbookcallback result = pb.run() except Exception as e: print e self.logger.critical(message="Ansilbe playbook error:%s" % e) def get_adhoc_message(self, status, host, result): self.status = 'Status:[%s]' % status self.host = ',Remote Host:[%s]' % host if result.get('start', None): self.start_time = result['start'] else: self.start_time = None if result.get('end', None): self.end_time = result['end'] else: self.end_time = None if self.start_time is not None and self.end_time is not None: self.period = ',Period:[%s - %s]' % (self.start_time, self.end_time) else: self.period = '' if result.get('cmd', None): self.command = 'Command:[%s]' % result['cmd'] else: self.command = '' if result.get('msg', None): return_data = result.get('msg').replace("\r\n", ",") elif result.get('stdout', None): return_data = result.get('stdout').replace("\n", ",") elif result.get('stderr', None): return_data = result.get('stderr').replace("\n", ",") else: return_data = result.get('stdout').replace("\n", ",") self.return_data = ',Result:[%s]' % return_data self.message = self.status + self.host + self.period + self.command + self.return_data return self.message def get_playbook_message(self, status, host, result): self.status = 'Status:[%s]' % status self.host = ',Remote Host:[%s]' % host if result.get('msg', None): self.msg = ',Msg:[%s]' % result['msg'].replace("\r\n", ",") else: self.msg = ',Msg:[%s]' % result self.playbooks = ',Playbook:[%s]' % self.playbooks[0] self.message = self.status + self.host + self.playbooks + self.msg return self.message ''' 将callback返回的ansible执行结果,打印成日志条目 ''' def get_adhoc_result(self): self.result_raw = {'Ok': {}, 'Failures': {}, 'Unreachable': {}} for host, result in self.callback.host_ok.items(): status = 'Ok' self.get_adhoc_message(status, host, result) self.result_raw['Ok'][host] = result self.logger.info(message=self.message) for host, result in self.callback.host_failed.items(): status = 'Failures' self.get_adhoc_message(status, host, result) self.result_raw['Failures'][host] = result self.logger.error(message=self.message) for host, result in self.callback.host_unreachable.items(): print self.callback.host_unreachable status = 'Unreachable' self.get_adhoc_message(status, host, result) self.result_raw['Unreachable'][host] = result self.logger.error(message=self.message) def get_playbook_result(self): self.result_raw = { 'Unreachable': {}, 'Skipped': {}, 'Ok': {}, 'Changed': {}, 'Failures': {} } for host, result in self.playbookcallback.task_ok.items(): status = 'Ok' self.get_playbook_message(status, host, result) self.result_raw['Ok'][host] = result self.logger.info(message=self.message) for host, result in self.playbookcallback.task_skip.items(): status = 'Skip' self.get_playbook_message(status, host, result) self.result_raw['Skip'][host] = result self.logger.info(message=self.message) for host, result in self.playbookcallback.task_failed.items(): status = 'Failures' self.get_playbook_message(status, host, result) self.result_raw['Failures'][host] = result self.logger.error(message=self.message) for host, result in self.playbookcallback.task_unreachable.items(): status = 'Unreachable' self.get_playbook_message(status, host, result) self.result_raw['Unreachable'][host] = result self.logger.error(message=self.message)
class Logger(object): def __init__(self, external_path, logger_name='root'): self.logger_name = logger_name self.path = external_path self.__initalizeData() self.__timeroating_kind() self.__file_kind() self.__stream_kind() def __initalizeData(self): self.basepath = GET().get_log_dir() self.create_dir = CREATE() self.config_items = Config_read() self.local_path = self.create_dir.create_dir( basepath=self.basepath, extra_path=self.config_items.get_value(sections='LOGGER_DIR', key='GLOBAL_LOG_FILE')) self.external_path = self.create_dir.create_dir(basepath=self.basepath, extra_path=self.path) self.logger = logging.getLogger(self.logger_name) def level(self, level_name): if level_name == 'DEBUG': self.level_return = logging.DEBUG elif level_name == 'INFO': self.level_return = logging.INFO elif level_name == 'WARNING': self.level_return = logging.WARNING elif level_name == 'ERROR': self.level_return = logging.ERROR elif level_name == 'CRITICAL': self.level_return = logging.CRITICAL else: self.level_return = logging.INFO return self.level_return def __self_logger(self, message): self.logger = logging.getLogger() st_handler = logging.StreamHandler() st_handler.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - [%(levelname)s] %(filename)s[line:%(lineno)d] - %(message)s' )) rf_handler = logging.handlers.TimedRotatingFileHandler( self.local_path, when='midnight', interval=1, backupCount=7, ) rf_handler.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - [%(levelname)s] %(filename)s[line:%(lineno)d] - %(message)s' )) self.logger.addHandler(rf_handler) self.logger.addHandler(st_handler) self.logger.critical(message) sys.exit(1) def __timeroating_kind(self): try: self.logger.setLevel( self.level(level_name=self.config_items.get_value( sections='LOGGER', key='GLOBAL_LOG_LEVEL'))) rf_handler = logging.handlers.TimedRotatingFileHandler( self.local_path, when='midnight', interval=1, backupCount=7, ) rf_handler.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - [%(levelname)s] %(filename)s[line:%(lineno)d] - %(message)s' )) self.logger.addHandler(rf_handler) except Exception as e: self.__self_logger(message=e) def __file_kind(self): try: f_handler = logging.FileHandler(self.external_path) f_handler.setLevel( self.level(level_name=self.config_items.get_value( sections='LOGGER', key='FILE_LOG_LEVEL'))) f_handler.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - [%(levelname)s] %(filename)s[line:%(lineno)d] - %(message)s' )) self.logger.addHandler(f_handler) except Exception as e: self.__self_logger(message=e) def __stream_kind(self): try: st_handler = logging.StreamHandler() st_handler.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - [%(levelname)s] %(filename)s[line:%(lineno)d] - %(message)s' )) st_handler.setLevel( self.level(level_name=self.config_items.get_value( sections='LOGGER', key='STREAM_LOG_LEVEL'))) self.logger.addHandler(st_handler) except Exception as e: self.__self_logger(message='System crash: %s!' % e) def debug(self, message): self.logger.debug(message) def info(self, message): self.logger.info(message) def warning(self, message): self.logger.warning(message) def error(self, message): self.logger.error(message) def critical(self, message): self.logger.critical(message)