This commit is contained in:
2026-01-23 03:10:40 -08:00
parent 1e306d7d78
commit af313557dd

View File

@@ -375,9 +375,9 @@ def install_service_systemd(
if os.name != "nt" and hasattr(os, "geteuid") and os.geteuid() == 0:
print(
"Running as root; systemd user services arent available. Falling back to cron @reboot."
"Running as root; installing system-wide systemd service instead."
)
return install_service_cron(
return install_service_systemd_system(
service_name,
repo_root,
venv_py,
@@ -456,6 +456,81 @@ def install_service_systemd(
return False
def install_service_systemd_system(
service_name: str,
repo_root: Path,
venv_py: Path,
headless: bool = True,
detached: bool = True,
pull: bool = False,
workspace_root: Optional[Path] = None,
) -> bool:
try:
systemctl = shutil.which("systemctl")
if not systemctl:
print(
"systemctl not available; falling back to crontab @reboot (if present)."
)
return install_service_cron(
service_name,
repo_root,
venv_py,
headless,
detached,
pull=pull,
workspace_root=workspace_root,
)
unit_dir = Path("/etc/systemd/system")
service_file = unit_dir / f"{service_name}.service"
unit_dir.mkdir(parents=True, exist_ok=True)
local_helper = repo_root / "run_client.py"
if not local_helper.exists():
local_helper = repo_root / "scripts" / "run_client.py"
target_script = local_helper if local_helper.exists() else Path(__file__).resolve()
exec_args = f'"{venv_py}" "{target_script}" --detached '
if headless:
exec_args += "--headless "
if pull:
exec_args += "--pull "
exec_args += f'--repo-root "{repo_root}" '
content = (
"[Unit]\n"
"Description=Medios-Macina Client (system service)\n"
"After=network.target\n\n"
"[Service]\n"
"Type=simple\n"
f"ExecStart={exec_args}\n"
f"WorkingDirectory={repo_root}\n"
"Restart=on-failure\n"
"Environment=PYTHONUNBUFFERED=1\n"
"[Install]\n"
"WantedBy=multi-user.target\n"
)
service_file.write_text(content, encoding="utf-8")
for cmd in [
[systemctl, "daemon-reload"],
[systemctl, "enable", "--now", f"{service_name}.service"],
]:
subprocess.run(cmd, check=True)
print(f"system-wide systemd service '{service_name}' enabled and started.")
return True
except subprocess.CalledProcessError as exc:
print("Failed to install system-wide service:", exc)
return False
except PermissionError as exc:
print("Permission denied while writing systemd unit:", exc)
return False
except Exception as exc:
print("system-wide install error:", exc)
return False
def uninstall_service_systemd(service_name: str) -> bool:
try:
systemctl = shutil.which("systemctl")