-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdeploy.py
More file actions
163 lines (135 loc) · 5.49 KB
/
deploy.py
File metadata and controls
163 lines (135 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Cross-platform service installer for james_library OpenClaw supervisor."""
from __future__ import annotations
import argparse
import getpass
import platform
import subprocess
from pathlib import Path
from openclaw_service import pick_headless_python
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Install james_library as a persistent service")
parser.add_argument("--service-name", default="james-library")
parser.add_argument("--target", default="rain_lab.py", help="Python script managed by OpenClaw")
parser.add_argument("--target-args", nargs=argparse.REMAINDER, default=[])
parser.add_argument("--dry-run", action="store_true", help="Only print generated files/commands")
return parser.parse_args(argv)
def _run(cmd: list[str], dry_run: bool, *, allow_failure: bool = False) -> None:
print("$", " ".join(cmd))
if not dry_run:
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
if allow_failure:
print(f"Ignoring non-zero exit status for optional command: {' '.join(cmd)}")
return
raise
def _windows_install(repo_root: Path, args: argparse.Namespace, dry_run: bool) -> None:
wrapper = repo_root / "openclaw_service.py"
headless_python = pick_headless_python()
clean_target_args = args.target_args[1:] if args.target_args[:1] == ["--"] else args.target_args
target_args = ["--", *clean_target_args] if clean_target_args else []
nssm_install_cmd = [
"nssm",
"install",
args.service_name,
headless_python,
str(wrapper),
"--service-name",
args.service_name,
"--target",
args.target,
*target_args,
]
nssm_dir_cmd = ["nssm", "set", args.service_name, "AppDirectory", str(repo_root)]
nssm_start_cmd = ["nssm", "start", args.service_name]
_run(nssm_install_cmd, dry_run)
_run(nssm_dir_cmd, dry_run)
_run(nssm_start_cmd, dry_run)
def _macos_install(repo_root: Path, args: argparse.Namespace, dry_run: bool) -> None:
launch_agents = Path.home() / "Library" / "LaunchAgents"
launch_agents.mkdir(parents=True, exist_ok=True)
label = f"com.james_library.{args.service_name}"
plist_path = launch_agents / f"{label}.plist"
wrapper = repo_root / "openclaw_service.py"
program_args = [
pick_headless_python(),
str(wrapper),
"--service-name",
args.service_name,
"--target",
args.target,
]
clean_target_args = args.target_args[1:] if args.target_args[:1] == ["--"] else args.target_args
if clean_target_args:
program_args.extend(["--", *clean_target_args])
args_xml = "\n".join(f" <string>{arg}</string>" for arg in program_args)
plist = f"""<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Label</key>
<string>{label}</string>
<key>ProgramArguments</key>
<array>
{args_xml}
</array>
<key>WorkingDirectory</key>
<string>{repo_root}</string>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>{repo_root / 'logs' / 'openclaw.out.log'}</string>
<key>StandardErrorPath</key>
<string>{repo_root / 'logs' / 'openclaw.err.log'}</string>
</dict>
</plist>
"""
print(f"Writing plist: {plist_path}")
if not dry_run:
(repo_root / "logs").mkdir(exist_ok=True)
plist_path.write_text(plist, encoding="utf-8")
# First install has nothing loaded yet; unload may fail and is safe to ignore.
_run(["launchctl", "unload", str(plist_path)], dry_run, allow_failure=True)
_run(["launchctl", "load", str(plist_path)], dry_run)
def _linux_install(repo_root: Path, args: argparse.Namespace, dry_run: bool) -> None:
unit_path = Path("/etc/systemd/system") / f"{args.service_name}.service"
wrapper = repo_root / "openclaw_service.py"
clean_target_args = args.target_args[1:] if args.target_args[:1] == ["--"] else args.target_args
target_tail = " -- " + " ".join(clean_target_args) if clean_target_args else ""
service_text = f"""[Unit]
Description=james_library OpenClaw background service
After=network.target
[Service]
Type=simple
User={getpass.getuser()}
WorkingDirectory={repo_root}
ExecStart={pick_headless_python()} {wrapper} --service-name {args.service_name} --target {args.target}{target_tail}
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
"""
print(f"Writing systemd unit: {unit_path}")
if not dry_run:
(repo_root / "logs").mkdir(exist_ok=True)
unit_path.write_text(service_text, encoding="utf-8")
_run(["systemctl", "daemon-reload"], dry_run)
_run(["systemctl", "enable", "--now", args.service_name], dry_run)
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
repo_root = Path(__file__).resolve().parent
os_name = platform.system().lower()
print(f"Detected platform: {os_name}")
if os_name == "windows":
_windows_install(repo_root, args, args.dry_run)
elif os_name == "darwin":
_macos_install(repo_root, args, args.dry_run)
elif os_name == "linux":
_linux_install(repo_root, args, args.dry_run)
else:
raise RuntimeError(f"Unsupported operating system: {platform.system()}")
return 0
if __name__ == "__main__":
raise SystemExit(main())