Skip to content

Commit 9b847e5

Browse files
committed
支持SSH远程连接(Win->Linux)
Signed-off-by: wuqiongjin <suchinfinity@qq.com>
1 parent b70a793 commit 9b847e5

3 files changed

Lines changed: 83 additions & 8 deletions

File tree

src/extend/auto_linux_plan.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ def create_crontab_entry(task_name, task_id, trigger_type, day=None, time=None):
7777
home_dir = os.path.expanduser("~")
7878
path_env = os.environ.get('PATH', '/usr/local/bin:/usr/bin:/bin')
7979
user = os.environ.get('USER', os.environ.get('LOGNAME', 'user'))
80+
config_data = Utils.read_dict_from_json(AppPath.DataJson) or {}
81+
display = config_data.get(Key.LinuxDisplay, ':0')
8082

8183
# 构建完整的命令,包含环境变量设置
82-
command = f'PATH={path_env} HOME={home_dir} USER={user} DISPLAY=:1 {runner_cmd} --task_id={task_id}'
84+
command = f'PATH={path_env} HOME={home_dir} USER={user} DISPLAY={display} {runner_cmd} --task_id={task_id}'
8385

8486
# 解析时间
8587
try:

src/runner/runner_main.py

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,99 @@
11
import argparse
2-
from pathlib import Path
2+
import json
3+
import subprocess
34
import sys
5+
from pathlib import Path
46

57
if not getattr(sys, 'frozen', False) and __package__ is None:
68
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
79

810
from src.utils.log import Log
11+
from src.utils.const import AppPath, Key
12+
from src.extend.auto_linux_plan import create_crontab_task, delete_crontab_task
913
from src.runner.executor import run_task_by_id
1014

11-
1215
def main(argv: list[str] | None = None) -> int:
1316
parser = argparse.ArgumentParser(description="Auto-Clock Runner")
14-
parser.add_argument("--task_id", required=True, help="Task ID")
15-
parser.add_argument("--headless", action="store_true", help="Run in headless mode")
17+
subparsers = parser.add_subparsers(dest="command")
18+
19+
run_parser = subparsers.add_parser("run", help="Run a task by task_id")
20+
run_parser.add_argument("--task_id", required=True, help="Task ID")
21+
run_parser.add_argument("--headless", action="store_true", help="Run in headless mode")
22+
23+
cron_create_parser = subparsers.add_parser("cron_create", help="Create a Linux crontab entry for a task")
24+
cron_create_parser.add_argument("--task_json_path", required=True, help="Path to task json file on Linux")
25+
26+
cron_delete_parser = subparsers.add_parser("cron_delete", help="Delete a Linux crontab entry by task name")
27+
cron_delete_parser.add_argument("--task_name", required=True, help="System plan name")
28+
29+
set_current_parser = subparsers.add_parser("set_current", help="Set servers/current to a version directory")
30+
set_current_parser.add_argument("--version", required=True, help="Version string")
31+
set_current_parser.add_argument(
32+
"--servers_root",
33+
default=str(Path(AppPath.AppRoot) / "servers"),
34+
help="Servers root directory (default: ~/.local/share/auto-clock/servers)",
35+
)
1636

1737
args = parser.parse_args(argv)
1838

39+
# Backward compatible flags: auto-clock-runner --task_id=xxx [--headless]
40+
if args.command is None:
41+
legacy_parser = argparse.ArgumentParser(add_help=False)
42+
legacy_parser.add_argument("--task_id")
43+
legacy_parser.add_argument("--headless", action="store_true")
44+
legacy_args, _ = legacy_parser.parse_known_args(argv)
45+
if legacy_args.task_id:
46+
args.command = "run"
47+
args.task_id = legacy_args.task_id
48+
args.headless = legacy_args.headless
49+
else:
50+
parser.print_help()
51+
return 1
52+
1953
Log.open()
2054
try:
21-
ok, error = run_task_by_id(task_id=args.task_id, headless=args.headless)
22-
if ok:
55+
if args.command == "run":
56+
ok, error = run_task_by_id(task_id=args.task_id, headless=args.headless)
57+
if ok:
58+
return 0
59+
return 2
60+
61+
if args.command == "cron_create":
62+
task_path = Path(args.task_json_path)
63+
if not task_path.exists():
64+
Log.error(f"Task json not found: {task_path}")
65+
return 1
66+
task = json.loads(task_path.read_text(encoding="utf-8"))
67+
ok, error = create_crontab_task(task)
68+
if ok:
69+
return 0
70+
Log.error(error or "Create crontab task failed")
71+
return 2
72+
73+
if args.command == "cron_delete":
74+
ok, error = delete_crontab_task(args.task_name)
75+
if ok:
76+
return 0
77+
Log.error(error or "Delete crontab task failed")
78+
return 2
79+
80+
if args.command == "set_current":
81+
servers_root = Path(args.servers_root).expanduser().resolve()
82+
target_dir = (servers_root / args.version).resolve()
83+
link_path = servers_root / "current"
84+
servers_root.mkdir(parents=True, exist_ok=True)
85+
if not target_dir.exists():
86+
Log.error(f"Version directory not found: {target_dir}")
87+
return 1
88+
# Use ln -sfn to update atomically
89+
result = subprocess.run(["ln", "-sfn", str(target_dir), str(link_path)], capture_output=True, text=True)
90+
if result.returncode != 0:
91+
Log.error(result.stderr.strip() or f"ln failed with code {result.returncode}")
92+
return 2
2393
return 0
24-
return 2
94+
95+
Log.error(f"Unknown command: {args.command}")
96+
return 1
2597
except Exception as e:
2698
Log.error(str(e))
2799
return 1

src/utils/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Key:
6565
Unknown: str = "Unknown"
6666
Empty: str = ""
6767
LinuxUserName: str = "LinuxUserName"
68+
LinuxDisplay: str = "linux_display"
6869
CheckLinuxCredentialsOnPlanCreate: str = "check_linux_credentials_on_plan_create"
6970

7071
@dataclass

0 commit comments

Comments
 (0)