MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / create_task

Method create_task

mod/project/node/executorMod.py:113–188  ·  view source on GitHub ↗
(get)

Source from the content-addressed store, hash-verified

111
112 @staticmethod
113 def create_task(get):
114 node_ids = list_args(get, 'node_ids')
115 if not node_ids:
116 return json_response(status=False, msg="节点ID不能为空")
117 try:
118 node_ids = [int(i) for i in node_ids]
119 except:
120 return json_response(status=False, msg="节点ID格式错误")
121
122 e_db = TaskFlowsDB()
123 script_id = get.get('script_id/d', 0)
124 if script_id:
125 s = e_db.Script.find("id = ?", (script_id,))
126 if not s:
127 return json_response(status=False, msg="脚本不存在")
128
129 elif get.get("script_content/s", "").strip():
130 if not (get.get("script_type", "").strip() in ("python", "shell")):
131 return json_response(status=False, msg="脚本类型错误")
132 s = Script("", get.get("script_type", "").strip(), content=get.get("script_content", "").strip())
133 s.id = 0
134 else:
135 return json_response(status=False, msg="请选择脚本")
136
137 nodes_db = ServerNodeDB()
138 nodes = []
139 timestamp = int(datetime.now().timestamp())
140 for i in node_ids:
141 n = nodes_db.get_node_by_id(i)
142 if not n:
143 return json_response(status=False, msg="节点id为【{}】的节点不存在".format(i))
144 n["ssh_conf"] = json.loads(n["ssh_conf"])
145 if not n["ssh_conf"]:
146 return json_response(status=False, msg="节点id为【{}】的节点未配置ssh信息,无法进行指令分发".format(i))
147 n["log_name"] = "{}_{}_{}.log".format(public.md5(s.content)[::2], timestamp, n['remarks'])
148 nodes.append(n)
149
150 e_task = CommandTask(
151 script_id=s.id,
152 script_content=s.content,
153 script_type=s.script_type,
154 flow_id=0,
155 step_index=0,
156 )
157 command_task_id = e_db.CommandTask.create(e_task)
158 e_task.id = command_task_id
159 if not isinstance(command_task_id, int) or command_task_id <= 0:
160 return json_response(status=False, msg="创建任务失败:" + command_task_id)
161
162 log_list = []
163 for i in nodes:
164 elog = CommandLog(
165 command_task_id=command_task_id,
166 server_id=i["id"],
167 ssh_host=i["ssh_conf"]["host"],
168 status=0,
169 log_name=i["log_name"],
170 )

Callers 3

compose_backupMethod · 0.45
compose_restoreMethod · 0.45

Calls 15

get_node_by_idMethod · 0.95
create_logMethod · 0.95
to_dictMethod · 0.95
list_argsFunction · 0.90
json_responseFunction · 0.90
TaskFlowsDBClass · 0.90
ScriptClass · 0.90
ServerNodeDBClass · 0.90
CommandTaskClass · 0.90
CommandLogClass · 0.90
nowMethod · 0.80
ExecShellMethod · 0.80

Tested by

no test coverage detected