diff --git a/spug_api/apps/schedule/executors.py b/spug_api/apps/schedule/executors.py index ff8b7a7..c247a9a 100644 --- a/spug_api/apps/schedule/executors.py +++ b/spug_api/apps/schedule/executors.py @@ -38,7 +38,10 @@ def host_executor(host, command): def schedule_worker_handler(job): - history_id, host_id, command = json.loads(job) + history_id, host_id, interpreter, command = json.loads(job) + if interpreter == 'python': + attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3' + command = f'{attach}\n$INTERPRETER << EOF\n# -*- coding: UTF-8 -*-\n{command}\nEOF' if host_id == 'local': code, duration, out = local_executor(command) else: diff --git a/spug_api/apps/schedule/models.py b/spug_api/apps/schedule/models.py index b7ba64c..31f1221 100644 --- a/spug_api/apps/schedule/models.py +++ b/spug_api/apps/schedule/models.py @@ -37,6 +37,7 @@ class Task(models.Model, ModelMixin): ) name = models.CharField(max_length=50) type = models.CharField(max_length=50) + interpreter = models.CharField(max_length=20, default='sh') command = models.TextField() targets = models.TextField() trigger = models.CharField(max_length=20, choices=TRIGGERS) diff --git a/spug_api/apps/schedule/scheduler.py b/spug_api/apps/schedule/scheduler.py index 506c3b5..d723c66 100644 --- a/spug_api/apps/schedule/scheduler.py +++ b/spug_api/apps/schedule/scheduler.py @@ -62,7 +62,7 @@ class Scheduler: self.scheduler.add_job(auto_run_by_day, 'cron', hour=1, minute=20) self.scheduler.add_job(auto_run_by_minute, 'interval', minutes=1) - def _dispatch(self, task_id, command, targets): + def _dispatch(self, task_id, interpreter, command, targets): output = {x: None for x in targets} history = History.objects.create( task_id=task_id, @@ -73,7 +73,7 @@ class Scheduler: Task.objects.filter(pk=task_id).update(latest_id=history.id) rds_cli = get_redis_connection() for t in targets: - rds_cli.rpush(SCHEDULE_WORKER_KEY, json.dumps([history.id, t, command])) + rds_cli.rpush(SCHEDULE_WORKER_KEY, json.dumps([history.id, t, interpreter, command])) connections.close_all() def _init(self): @@ -86,7 +86,7 @@ class Scheduler: self._dispatch, trigger, id=str(task.id), - args=(task.id, task.command, json.loads(task.targets)), + args=(task.id, task.interpreter, task.command, json.loads(task.targets)), ) connections.close_all() except DatabaseError: @@ -106,7 +106,7 @@ class Scheduler: self._dispatch, trigger, id=str(task.id), - args=(task.id, task.command, task.targets), + args=(task.id, task.interpreter, task.command, task.targets), replace_existing=True ) elif task.action == 'remove': diff --git a/spug_api/apps/schedule/views.py b/spug_api/apps/schedule/views.py index d49e004..8a648bd 100644 --- a/spug_api/apps/schedule/views.py +++ b/spug_api/apps/schedule/views.py @@ -27,6 +27,7 @@ class Schedule(View): Argument('id', type=int, required=False), Argument('type', help='请输入任务类型'), Argument('name', help='请输入任务名称'), + Argument('interpreter', help='请选择执行解释器'), Argument('command', help='请输入任务内容'), Argument('rst_notify', type=dict, help='请选择执行失败通知方式'), Argument('targets', type=list, filter=lambda x: len(x), help='请选择执行对象'), diff --git a/spug_web/src/pages/schedule/Step1.js b/spug_web/src/pages/schedule/Step1.js index c5863f1..368a18f 100644 --- a/spug_web/src/pages/schedule/Step1.js +++ b/spug_web/src/pages/schedule/Step1.js @@ -1,6 +1,6 @@ import React, { useState } from 'react'; import { observer } from 'mobx-react'; -import { Form, Input, Select, Modal, Button } from 'antd'; +import { Form, Input, Select, Modal, Button, Radio } from 'antd'; import { ExclamationCircleOutlined } from '@ant-design/icons'; import { LinkButton, ACEditor } from 'components'; import TemplateSelector from '../exec/task/TemplateSelector'; @@ -11,6 +11,7 @@ export default observer(function () { const [form] = Form.useForm(); const [showTmp, setShowTmp] = useState(false); const [command, setCommand] = useState(store.record.command || ''); + const [interpreter, setInterpreter] = useState(store.record.interpreter || 'sh'); function handleAddZone() { let type; @@ -79,10 +80,16 @@ export default observer(function () {