@name 测试指定任务 @author hwliang @param args { trigger_id 任务ID } @return dict
(self,args)
| 784 | return page |
| 785 | |
| 786 | def test_trigger(self,args): |
| 787 | ''' |
| 788 | @name 测试指定任务 |
| 789 | @author hwliang |
| 790 | @param args<dict_obj>{ |
| 791 | trigger_id<int> 任务ID |
| 792 | } |
| 793 | @return dict |
| 794 | ''' |
| 795 | if not args or not 'trigger_id' in args: |
| 796 | return public.returnMsg(False,'参数错误') |
| 797 | |
| 798 | |
| 799 | trigger_info = self._sql.table('trigger').where('trigger_id=?',(args.trigger_id,)).find() |
| 800 | if not trigger_info: |
| 801 | return public.returnMsg(False,'任务不存在') |
| 802 | script_body = '' |
| 803 | script_type = 'bash' |
| 804 | return_type = 'string' |
| 805 | |
| 806 | script_exts = {'bash':'sh','python':'py','php':'php','node':'js','ruby':'rb','perl':'pl'} |
| 807 | script_args = '' |
| 808 | #检查脚本是否存在 |
| 809 | if trigger_info['script_id']: |
| 810 | script_info = self._sql.table('scripts').where('script_id=?',(trigger_info['script_id'],)).find() |
| 811 | if not script_info: |
| 812 | return public.returnMsg(False,'脚本不存在') |
| 813 | script_body = script_info['script'] |
| 814 | script_type = script_info['script_type'] |
| 815 | return_type = script_info['return_type'] |
| 816 | if 'args' in trigger_info and trigger_info['args']: script_args = trigger_info['args'] |
| 817 | else: |
| 818 | script_body = trigger_info['script_body'] |
| 819 | |
| 820 | trigger_start_time = int(time.time()) |
| 821 | result_msg = ['正在执行任务...'] |
| 822 | if script_args: script_args = ' {}'.format(script_args) |
| 823 | |
| 824 | tmp_file = '{}/tmp/trigger_{}.{}'.format(public.get_panel_path(),trigger_info['trigger_id'],script_exts[script_type]) |
| 825 | public.writeFile(tmp_file,script_body) |
| 826 | |
| 827 | #执行脚本 |
| 828 | if script_type == 'bash': |
| 829 | result = public.ExecShell("bash {}{}".format(tmp_file,script_args)) |
| 830 | else: |
| 831 | result = public.ExecShell("{} {}{}".format(public.get_python_bin(),tmp_file,script_args)) |
| 832 | |
| 833 | if os.path.exists(tmp_file): |
| 834 | os.remove(tmp_file) |
| 835 | |
| 836 | if not result[0]: |
| 837 | self.add_task_log(trigger_info['script_id'],trigger_info['trigger_id'],0,0,result[0],result[1],trigger_start_time,int(time.time())) |
| 838 | return public.returnMsg(False,'脚本执行失败,错误信息:\n{}'.format(result[1])) |
| 839 | |
| 840 | |
| 841 | result_msg.append("任务脚本已执行完成,返回结果:\n{}".format(result[0])) |
| 842 | #检查任务事件 |
| 843 | operator_where = self._sql.table('operator_where').where('trigger_id=?',(args.trigger_id,)).select() |
nothing calls this directly
no test coverage detected