okey/cli/commands/
service.rs1use std::env;
2
3use anyhow::{Result, anyhow};
4use nix::unistd;
5
6use crate::{cli::utils::systemctl, fs::service as fs};
7
8pub use systemctl::{restart, status, stop};
9
10pub fn start() -> Result<()> {
11 let dir_path = fs::get_systemd_dir_path()?;
12 let file_path = fs::resolve_service_file_path(&dir_path);
13
14 if !file_path.exists() {
15 return Err(anyhow!(
16 "The systemd service is not installed, run 'okey service install'",
17 ));
18 }
19
20 systemctl::reload_daemon()?;
21 systemctl::start()?;
22
23 Ok(())
24}
25
26pub fn install() -> Result<()> {
27 let exe_path = env::current_exe()?;
28 let exe_path_str = exe_path.to_string_lossy();
29
30 let config = format!(
31 r#"[Unit]
32Description=Okey Service
33
34[Service]
35ExecStart={exe_path_str} start --systemd
36Restart=on-failure
37StandardOutput=journal
38StandardError=journal
39Nice=-20{}
40
41[Install]
42WantedBy=multi-user.target"#,
43 if unistd::geteuid().is_root() {
44 r#"
45CPUSchedulingPolicy=rr
46CPUSchedulingPriority=99
47IOSchedulingClass=realtime
48IOSchedulingPriority=0"#
49 } else {
50 ""
51 }
52 );
53
54 fs::write_systemd_service(&config)?;
55
56 println!("The systemd service has been installed, run 'okey service start' to start it");
57
58 Ok(())
59}
60
61pub fn uninstall() -> Result<()> {
62 let dir_path = fs::get_systemd_dir_path()?;
63 let file_path = fs::resolve_service_file_path(&dir_path);
64
65 if file_path.exists() {
66 systemctl::stop()?;
67 fs::remove_systemd_service()?;
68 systemctl::reload_daemon()?;
69
70 println!("The systemd service has been removed");
71 } else {
72 println!("The systemd service does not exist");
73 }
74
75 Ok(())
76}