|
14 | 14 | from ui.custom.custom_widget import TaskListWidget |
15 | 15 | from ui.template.ui_page import PageContent, Container |
16 | 16 | from src.ui.ui_system_plan import SystemPlanDialog |
17 | | -from src.extend.ssh_client import SshClient, SshConfig |
18 | | -from src.extend.remote_linux_runner import RemoteLinuxRunner |
19 | 17 | from src.utils.const import WebPath |
20 | 18 |
|
21 | 19 | # 根据操作系统导入相应的模块 |
@@ -100,6 +98,15 @@ def _write_tasks(self, tasks) -> bool: |
100 | 98 | except Exception: |
101 | 99 | return False |
102 | 100 |
|
| 101 | + def _get_remote_plan_service(self): |
| 102 | + try: |
| 103 | + w = self.window() |
| 104 | + if hasattr(w, "remote_plan_service"): |
| 105 | + return w.remote_plan_service |
| 106 | + except Exception: |
| 107 | + pass |
| 108 | + return None |
| 109 | + |
103 | 110 | def _read_config(self) -> dict: |
104 | 111 | try: |
105 | 112 | w = self.window() |
@@ -250,33 +257,10 @@ def do_create_plan(self): |
250 | 257 | MessageBox("请先在 Settings -> SSH Settings 点击“连接”,连接成功后再创建远端计划任务") |
251 | 258 | return |
252 | 259 |
|
253 | | - host = str(self._get_cfg(Key.SshHost, "") or "").strip() |
254 | | - username = str(self._get_cfg(Key.SshUsername, "") or "").strip() |
255 | | - password = str(self._get_cfg(Key.SshPassword, "") or "") |
256 | | - use_pkey = bool(self._get_cfg(Key.SshUsePrivateKey, False)) |
257 | | - pkey_path = str(self._get_cfg(Key.SshPrivateKeyPath, "") or "").strip() |
258 | | - version = Utils.get_app_version_from_config_json(default="") |
259 | | - if not host or not username or not version: |
260 | | - raise Exception("SSH配置不完整或无法获取版本号") |
261 | | - url = WebPath.LinuxRunnerDownloadUrlTemplate.format(version=version) |
262 | | - ssh_cfg = SshConfig( |
263 | | - host=host, |
264 | | - username=username, |
265 | | - password=None if use_pkey else password, |
266 | | - pkey_path=pkey_path if use_pkey else None, |
267 | | - ) |
268 | | - with SshClient(ssh_cfg) as ssh: |
269 | | - remote = RemoteLinuxRunner(ssh) |
270 | | - ok2, err = remote.ensure_installed_from_url(version=version, url=url) |
271 | | - if not ok2: |
272 | | - raise Exception(err or "远端安装 linux-runner 失败") |
273 | | - code, out, err2 = remote.set_current(version) |
274 | | - if code != 0: |
275 | | - raise Exception((err2 or out or "").strip() or "远端 set_current 失败") |
276 | | - code, out, err2 = remote.cron_create(task) |
277 | | - if code != 0: |
278 | | - raise Exception((err2 or out or "").strip() or "远端创建 crontab 失败") |
279 | | - ok, error = True, None |
| 260 | + svc = self._get_remote_plan_service() |
| 261 | + if svc is None: |
| 262 | + raise Exception("远端服务未初始化,请重新连接SSH") |
| 263 | + ok, error = svc.cron_create(task) |
280 | 264 | else: |
281 | 265 | ok, error = create_task(task) |
282 | 266 | elif platform.system() == "Linux": |
@@ -331,39 +315,12 @@ def delete_system_plan(self): |
331 | 315 | MessageBox("请先在 Settings -> SSH Settings 点击“连接”,连接成功后再删除远端计划任务") |
332 | 316 | return |
333 | 317 |
|
334 | | - host = str(self._get_cfg(Key.SshHost, "") or "").strip() |
335 | | - username = str(self._get_cfg(Key.SshUsername, "") or "").strip() |
336 | | - password = str(self._get_cfg(Key.SshPassword, "") or "") |
337 | | - use_pkey = bool(self._get_cfg(Key.SshUsePrivateKey, False)) |
338 | | - pkey_path = str(self._get_cfg(Key.SshPrivateKeyPath, "") or "").strip() |
339 | | - version = Utils.get_app_version_from_config_json(default="") |
340 | | - if not host or not username or not version: |
341 | | - raise Exception("SSH配置不完整或无法获取版本号") |
342 | | - url = WebPath.LinuxRunnerDownloadUrlTemplate.format(version=version) |
343 | | - ssh_cfg = SshConfig( |
344 | | - host=host, |
345 | | - username=username, |
346 | | - password=None if use_pkey else password, |
347 | | - pkey_path=pkey_path if use_pkey else None, |
348 | | - ) |
349 | | - with SshClient(ssh_cfg) as ssh: |
350 | | - remote = RemoteLinuxRunner(ssh) |
351 | | - ok2, err = remote.ensure_installed_from_url(version=version, url=url) |
352 | | - if not ok2: |
353 | | - raise Exception(err or "远端安装 linux-runner 失败") |
354 | | - code, out, err2 = remote.set_current(version) |
355 | | - if code != 0: |
356 | | - raise Exception((err2 or out or "").strip() or "远端 set_current 失败") |
357 | | - |
358 | | - if delete_task[Key.TriggerType] == Key.Multiple and isinstance(plan_name, dict): |
359 | | - for task_name in plan_name: |
360 | | - code, out, err2 = remote.cron_delete(plan_name.get(task_name)) |
361 | | - if code != 0: |
362 | | - raise Exception((err2 or out or "").strip() or "远端删除 crontab 失败") |
363 | | - else: |
364 | | - code, out, err2 = remote.cron_delete(plan_name) |
365 | | - if code != 0: |
366 | | - raise Exception((err2 or out or "").strip() or "远端删除 crontab 失败") |
| 318 | + svc = self._get_remote_plan_service() |
| 319 | + if svc is None: |
| 320 | + raise Exception("远端服务未初始化,请重新连接SSH") |
| 321 | + |
| 322 | + names = svc.task_names_from_plan(delete_task) |
| 323 | + ok, error = svc.cron_delete(names) |
367 | 324 | else: |
368 | 325 | if delete_task[Key.TriggerType] == Key.Multiple and isinstance(plan_name, dict): |
369 | 326 | for task_name in plan_name: |
|
0 commit comments