1+ #!/usr/bin/env python3
2+ """
3+ HRM Service Installer
4+ Cross-platform service installation for HRM system
5+ Supports Windows (NSSM), Linux (systemd), and macOS (launchd)
6+ """
7+
8+ import subprocess
9+ import sys
10+ import os
11+ import platform
12+ import argparse
13+ from pathlib import Path
14+
15+ def run_command (cmd , check = True , shell = True ):
16+ """Run a command and return success status."""
17+ try :
18+ result = subprocess .run (cmd , shell = shell , check = check , capture_output = True , text = True )
19+ return True , result .stdout , result .stderr
20+ except subprocess .CalledProcessError as e :
21+ return False , e .stdout , e .stderr
22+
23+ def detect_os ():
24+ """Detect the operating system."""
25+ system = platform .system ().lower ()
26+ if system == "windows" :
27+ return "windows"
28+ elif system == "linux" :
29+ return "linux"
30+ elif system == "darwin" :
31+ return "macos"
32+ else :
33+ return "unknown"
34+
35+ def check_admin ():
36+ """Check if running with administrator privileges."""
37+ try :
38+ if detect_os () == "windows" :
39+ import ctypes
40+ return ctypes .windll .shell32 .IsUserAnAdmin ()
41+ else :
42+ return os .geteuid () == 0
43+ except AttributeError :
44+ return False
45+
46+ def install_windows_service (install_dir , service_name ):
47+ """Install HRM as Windows service using NSSM or native commands."""
48+ print ("🔧 Installing HRM as Windows service..." )
49+
50+ # Check for NSSM first
51+ nssm_paths = [
52+ "C:\\ nssm\\ nssm.exe" ,
53+ "C:\\ Program Files\\ nssm\\ nssm.exe" ,
54+ "C:\\ Program Files (x86)\\ nssm\\ nssm.exe"
55+ ]
56+
57+ nssm_exe = None
58+ for path in nssm_paths :
59+ if os .path .exists (path ):
60+ nssm_exe = path
61+ break
62+
63+ exe_path = os .path .join (install_dir , "hrm_system.exe" )
64+
65+ if nssm_exe :
66+ print (f"📦 Using NSSM: { nssm_exe } " )
67+ # Install with NSSM
68+ cmds = [
69+ f'"{ nssm_exe } " install { service_name } "{ exe_path } " --daemon' ,
70+ f'"{ nssm_exe } " set { service_name } DisplayName "HRM AI System"' ,
71+ f'"{ nssm_exe } " set { service_name } Description "Hierarchical Reasoning Model AI System"' ,
72+ f'"{ nssm_exe } " set { service_name } Start SERVICE_AUTO_START' ,
73+ f'"{ nssm_exe } " start { service_name } '
74+ ]
75+ else :
76+ print ("⚠️ NSSM not found, using native sc.exe (limited functionality)" )
77+ # Fallback to native Windows service (requires pre-compiled service)
78+ cmds = [
79+ f'sc.exe create { service_name } binPath= "{ exe_path } --daemon" start= auto' ,
80+ f'sc.exe description { service_name } "Hierarchical Reasoning Model AI System"' ,
81+ f'sc.exe start { service_name } '
82+ ]
83+
84+ for cmd in cmds :
85+ success , stdout , stderr = run_command (cmd )
86+ if not success :
87+ print (f"❌ Command failed: { cmd } " )
88+ if stderr :
89+ print (f"Error: { stderr } " )
90+ return False
91+
92+ print (f"✅ HRM service '{ service_name } ' installed and started on Windows" )
93+ return True
94+
95+ def install_linux_service (install_dir , service_name ):
96+ """Install HRM as systemd service on Linux."""
97+ print ("🔧 Installing HRM as systemd service..." )
98+
99+ service_content = f"""[Unit]
100+ Description=HRM AI System
101+ After=network.target
102+
103+ [Service]
104+ Type=simple
105+ User={ os .getenv ('USER' , 'root' )}
106+ ExecStart={ install_dir } /hrm_system --daemon
107+ Restart=always
108+ RestartSec=5
109+ StandardOutput=journal
110+ StandardError=journal
111+
112+ [Install]
113+ WantedBy=multi-user.target
114+ """
115+
116+ service_path = f"/etc/systemd/system/{ service_name } .service"
117+
118+ # Write service file
119+ try :
120+ with open (service_path , 'w' ) as f :
121+ f .write (service_content )
122+ except PermissionError :
123+ print ("❌ Permission denied. Run with sudo." )
124+ return False
125+
126+ # Reload systemd and enable/start service
127+ cmds = [
128+ "systemctl daemon-reload" ,
129+ f"systemctl enable { service_name } " ,
130+ f"systemctl start { service_name } "
131+ ]
132+
133+ for cmd in cmds :
134+ success , stdout , stderr = run_command (cmd )
135+ if not success :
136+ print (f"❌ Command failed: { cmd } " )
137+ if stderr :
138+ print (f"Error: { stderr } " )
139+ return False
140+
141+ print (f"✅ HRM service '{ service_name } ' installed and started on Linux" )
142+ return True
143+
144+ def install_macos_service (install_dir , service_name ):
145+ """Install HRM as launchd service on macOS."""
146+ print ("🔧 Installing HRM as launchd service..." )
147+
148+ plist_content = f"""<?xml version="1.0" encoding="UTF-8"?>
149+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
150+ <plist version="1.0">
151+ <dict>
152+ <key>Label</key>
153+ <string>{ service_name } </string>
154+ <key>ProgramArguments</key>
155+ <array>
156+ <string>{ install_dir } /hrm_system</string>
157+ <string>--daemon</string>
158+ </array>
159+ <key>RunAtLoad</key>
160+ <true/>
161+ <key>KeepAlive</key>
162+ <true/>
163+ <key>StandardOutPath</key>
164+ <string>/var/log/hrm.log</string>
165+ <key>StandardErrorPath</key>
166+ <string>/var/log/hrm.error.log</string>
167+ </dict>
168+ </plist>
169+ """
170+
171+ plist_path = f"/Library/LaunchDaemons/{ service_name } .plist"
172+
173+ # Write plist file
174+ try :
175+ with open (plist_path , 'w' ) as f :
176+ f .write (plist_content )
177+ except PermissionError :
178+ print ("❌ Permission denied. Run with sudo." )
179+ return False
180+
181+ # Load and start service
182+ cmds = [
183+ f"launchctl load { plist_path } " ,
184+ f"launchctl start { service_name } "
185+ ]
186+
187+ for cmd in cmds :
188+ success , stdout , stderr = run_command (cmd )
189+ if not success :
190+ print (f"❌ Command failed: { cmd } " )
191+ if stderr :
192+ print (f"Error: { stderr } " )
193+ return False
194+
195+ print (f"✅ HRM service '{ service_name } ' installed and started on macOS" )
196+ return True
197+
198+ def main ():
199+ parser = argparse .ArgumentParser (description = "HRM Service Installer" )
200+ parser .add_argument ("--install-dir" , default = "C:\\ Program Files\\ HRM" if detect_os () == "windows" else "/usr/local/hrm" ,
201+ help = "Installation directory" )
202+ parser .add_argument ("--service-name" , default = "HRMSystem" , help = "Service name" )
203+ parser .add_argument ("--uninstall" , action = "store_true" , help = "Uninstall the service instead" )
204+
205+ args = parser .parse_args ()
206+
207+ os_type = detect_os ()
208+ print (f"🖥️ HRM Service Installer - { os_type } " )
209+ print (f"📁 Install Directory: { args .install_dir } " )
210+ print (f"🏷️ Service Name: { args .service_name } " )
211+ print ()
212+
213+ if args .uninstall :
214+ print ("🗑️ Uninstall functionality not yet implemented" )
215+ return
216+
217+ # Check privileges
218+ if not check_admin ():
219+ print ("❌ Administrator/root privileges required!" )
220+ if os_type == "windows" :
221+ print ("Right-click and 'Run as administrator'" )
222+ else :
223+ print ("Run with sudo" )
224+ sys .exit (1 )
225+
226+ # Check if HRM is built
227+ exe_name = "hrm_system.exe" if os_type == "windows" else "hrm_system"
228+ exe_path = os .path .join (args .install_dir , exe_name )
229+ if not os .path .exists (exe_path ):
230+ print (f"❌ HRM executable not found: { exe_path } " )
231+ print ("Please build HRM first and ensure it's in the install directory" )
232+ sys .exit (1 )
233+
234+ # Install based on OS
235+ success = False
236+ if os_type == "windows" :
237+ success = install_windows_service (args .install_dir , args .service_name )
238+ elif os_type == "linux" :
239+ success = install_linux_service (args .install_dir , args .service_name )
240+ elif os_type == "macos" :
241+ success = install_macos_service (args .install_dir , args .service_name )
242+ else :
243+ print (f"❌ Unsupported OS: { os_type } " )
244+ sys .exit (1 )
245+
246+ if success :
247+ print ()
248+ print ("🎉 HRM service installation complete!" )
249+ print ("The system will automatically start on boot." )
250+ else :
251+ print ()
252+ print ("💥 Service installation failed!" )
253+ sys .exit (1 )
254+
255+ if __name__ == "__main__" :
256+ main ()
0 commit comments