def create_execute_thread(self): execution_log.info('[thread_executor] task_list left: {}'.format( len(self.__temp_task_list))) if not self.__temp_task_list: self.__fresh_temp_task_list() execute_task = self.__temp_task_list[0] self.__temp_task_list.remove(execute_task) if not self.__temp_host_list: self.__fresh_temp_host_list() deploy_host = self.__temp_host_list[0] self.__temp_host_list.remove(deploy_host) while 1: # loop when thread queue is full try: if self.current_thread_count >= self.max_thread_count: execution_log.info( 'current thread queue is full, waiting task finished') time.sleep(self.loop_interval) else: execute_task.set_state('Executing') deploy_host.state = 'Busy' self.current_thread_count += 1 new_thread = threading.Thread(target=self.__execute, args=(execute_task, deploy_host)) new_thread.setDaemon(True) new_thread.start() new_thread.join(2) break except Exception as e: execution_log.error( 'New thread Error, Exception:\n{}'.format(e)) self.current_thread_count -= 1 deploy_host.state = 'Idle' execute_task.set_state('Assemble Finished')
def __get_src_files(self): static_path = os.path.join(os.getcwd(), self.__static_src) if os.path.exists(self.__test_report_root + '\\' + 'static'): shutil.rmtree(self.__test_report_root + '\\' + 'static') execution_log.info('Target static folder exist, remove the old folder') shutil.copytree(static_path, self.__test_report_root + '\\' + 'static') execution_log.info('Copy static folder to report folder finished') return True
def validate_ftp(ftp_settings): try: ftp = FTPUtils(ftp_settings['server_address'], ftp_settings['username'], ftp_settings['password']) ftp.close() execution_log.info('validate_ftp ' + ftp_settings['server_address'] + ' success') return True except Exception as e: execution_log.error(e) return False
def __fresh_temp_task_list(self): while 1: execution_log.info( '=======================Begin to fresh temp task list==========================' ) for _task in self.execution_queue.get_task_list(): if _task.get_state() == 'Assemble Finished': self.__temp_task_list.append(_task) if not self.__temp_task_list: execution_log.info( '---No valid task, waiting for new task-----') time.sleep(self.loop_interval) else: return
def __fresh_temp_host_list(self): while 1: execution_log.info( '=======================Begin to fresh temp host list==========================' ) for _host in self.__deploy_list: print('========', _host.status) if _host.state.upper() == 'IDLE': self.__temp_host_list.append(_host) if not self.__temp_host_list: execution_log.info( '---No valid Deploy host, waiting for new host-----') time.sleep(self.loop_interval) else: return
def generate(self): env = Environment(loader=FileSystemLoader( os.path.join(os.getcwd(), self.__template_folder), encoding='utf-8')) template = env.get_template(self.__template_name) html = template.render(task_name=self.__name, framework_version=self.framework_version, script_version=self.script_version, start=self.__start_time, end=self.__end_time, final_data=self.__data_by_uut['final_data'], final_data_2=self.__data_by_case['final_data'], data=self.pie_chart_data, total=self.total, encoding='utf-8') # unicode string with open(self.__test_report_root + '\\' + self.__name + '.html', 'w', encoding='utf-8') as f: f.write(html) # copy static folder if self.__get_src_files(): execution_log.info('generate {}.html finished'.format(self.__name)) return self.__test_report_root
def __execute(self, task, host): try: self.deploy(host, task) task.end_time = datetime.datetime.now() self.download_result() self.send_report(task) execution_log.info( '[thread_executor] task left in execute queue: {}'.format( len(self.execution_queue.get_task_list()))) time.sleep(1) # can be removed execution_log.info( '[thread_executor] task_list now is : {}'.format( list( map(lambda i: i.get_name(), self.execution_queue.get_task_list())))) self.current_thread_count -= 1 task.set_state('Execute Finished') host.state = 'Idle' except Exception as e: execution_log.info( 'Unexpect error happen, roll back the task state and host state. Details reason:\n {}' .format(e)) self.current_thread_count -= 1 task.set_state('Assemble Finished') host.state = 'Idle'
def send_report(self, task): report = Report(task) # Send Email email_subject, email_to, html, att_zip, task_report_path = self.email_parameter( report, task) if html is not False: email_handler = Email() email_handler.send_email(email_subject, email_to, html.encode('utf-8'), 'html', attachment=att_zip) email_handler.disconnect() report.remove_report_folder(task_report_path) os.remove(att_zip) self.execution_queue.remove_task(task) execution_log.info( "[thread_executor] remove {} from task_list".format( task.get_name())) execution_log.info( '[thread_executor] remove {} from execute queue'.format( task.get_name())) else: execution_log.info( "Failed to find the email template, please double check")
def insert_task_to_queue(self): receive = self.__pipe.recv() execution_log.info('[Execution] received: {}'.format( receive.get_name())) self.__pipe.send(receive.get_name()) execution_log.info( "[thread_execution_queue_monitor] receive: {}".format( receive.get_name())) self.execution_queue.insert_task(task=receive) execution_log.info( '[thread_execution_queue_monitor] append {} to task_list'.format( receive.get_name())) execution_log.info( '[thread_execution_queue_monitor] task_list now is {}'.format( list( map(lambda i: i.get_name(), self.execution_queue.get_task_list())))) time.sleep(1) # can be removed
def deploy(deploy_server_host, task): # deploy task -> execute task -> collect result execution_log.info('execute_engine deploy {} to {} with {}'.format( task.get_name(), task.get_uut_list()[0].get_hostname(), deploy_server_host.get_hostname())) task.deploy(deploy_server_host) execution_log.info('execute_engine execute {}'.format(task.get_name())) task.execute(deploy_server_host) execution_log.info('execute_engine collect result {}'.format( task.get_name())) task.collect_result(deploy_server_host)