@name 测试运行脚本 @author hwliang @param args { script_id 脚本ID } @return dict
(self,args)
| 366 | return page |
| 367 | |
| 368 | def test_script(self,args): |
| 369 | ''' |
| 370 | @name 测试运行脚本 |
| 371 | @author hwliang |
| 372 | @param args<dict_obj>{ |
| 373 | script_id<int> 脚本ID |
| 374 | } |
| 375 | @return dict |
| 376 | ''' |
| 377 | script_start_time = int(time.time()) |
| 378 | script_id = args.get('script_id',0) |
| 379 | if not script_id: return public.returnMsg(False,'脚本ID不能为空') |
| 380 | script_info = self._sql.table('scripts').where('script_id=?',(script_id,)).field('script,script_type,return_type').find() |
| 381 | script_args = args.get('args/s','') |
| 382 | if script_args: script_args = ' ' + script_args |
| 383 | if 'is_args' in script_info and script_info['is_args'] and not script_args: |
| 384 | return public.returnMsg(False,'该脚本需要参数') |
| 385 | if not script_info: return public.returnMsg(False,'指定脚本不存在') |
| 386 | tmp_file= '{}/tmp/{}.tmp'.format(public.get_panel_path(),public.md5(script_info['script'])) |
| 387 | try: |
| 388 | public.writeFile(tmp_file,script_info['script']) |
| 389 | if script_info['script_type'] == 'bash': |
| 390 | result = public.ExecShell("bash {}{}".format(tmp_file,script_args)) |
| 391 | elif script_info['script_type'] == 'python': |
| 392 | python_bin = public.get_python_bin() |
| 393 | result = public.ExecShell('{} -u {}{}'.format(python_bin,tmp_file,script_args)) |
| 394 | res = result[0].strip() |
| 395 | if result[1]: |
| 396 | self.add_task_log(script_id,0,0,0,result[0],result[1],script_start_time,int(time.time())) |
| 397 | return public.returnMsg(False,'脚本运行错误,请检查脚本代码是否有误: \n{}'.format(result[0] + "\n" + result[1].split('.tmp:')[-1])) |
| 398 | except Exception as ex: |
| 399 | error_msg = public.get_error_info() |
| 400 | self.add_task_log(script_id,0,0,0,'',error_msg,script_start_time,int(time.time())) |
| 401 | return public.returnMsg(False,'执行失败,错误信息:\n{}'.format(error_msg)) |
| 402 | finally: |
| 403 | if os.path.exists(tmp_file): os.remove(tmp_file) |
| 404 | self.add_task_log(script_id,0,0,1,result[0],result[1],script_start_time,int(time.time())) |
| 405 | return public.returnMsg(True,"执行成功: \n{}".format(public.xssencode2(res))) |
| 406 | |
| 407 | |
| 408 |
nothing calls this directly
no test coverage detected