diff --git a/Cargo.lock b/Cargo.lock index 2ff1dff65..51bbfcb9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -392,8 +392,8 @@ dependencies = [ "hex", "hmac", "jwt", - "monitor_helpers 0.1.1", - "monitor_types 0.1.2", + "monitor_helpers 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "monitor_types 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "mungos", "periphery_client", "serde", @@ -587,7 +587,7 @@ name = "db_client" version = "0.1.0" dependencies = [ "anyhow", - "monitor_types 0.1.2", + "monitor_types 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "mungos", ] @@ -1334,7 +1334,7 @@ dependencies = [ [[package]] name = "monitor_cli" -version = "0.1.0" +version = "0.1.1" dependencies = [ "async_timing_util", "clap", @@ -1354,7 +1354,7 @@ version = "0.1.1" dependencies = [ "anyhow", "futures-util", - "monitor_types 0.1.2", + "monitor_types 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest", "serde", "serde_derive", @@ -1714,8 +1714,8 @@ version = "0.1.0" dependencies = [ "anyhow", "futures-util", - "monitor_helpers 0.1.1", - "monitor_types 0.1.2", + "monitor_helpers 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "monitor_types 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest", "serde", "serde_json", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index c2073d489..25885a211 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monitor_cli" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["MoghTech"] description = "monitor cli | tools to setup monitor system" diff --git a/cli/src/helpers.rs b/cli/src/helpers.rs index 3af5ec072..cb549ed92 100644 --- a/cli/src/helpers.rs +++ b/cli/src/helpers.rs @@ -449,6 +449,23 @@ pub fn start_periphery_container(sub_matches: &ArgMatches) { } } +pub fn gen_periphery_service_file(sub_matches: &ArgMatches) { + let mut file = File::create("/etc/systemd/system/periphery.service") + .expect("failed to create config file. user must be root for this operation."); + file.write_all(generate_periphery_unit_file().as_bytes()) + .expect("failed to write config file. user must be root for this operation"); + + let output = run_command_pipe_to_terminal("systemctl daemon-reload"); + + if output.success() { + println!( + "\n✅ successfully added service to systemd ✅\n" + ) + } else { + eprintln!("\n❌ there was some {} adding service to systemd ❌\n", "error".red()) + } +} + fn write_to_toml(path: &str, toml: impl Serialize) { let path = PathBuf::from_str(&path.replace("~", &std::env::var("HOME").unwrap())) .expect("not a valid path"); @@ -473,3 +490,21 @@ fn generate_secret(length: usize) -> String { .map(char::from) .collect() } + +fn generate_periphery_unit_file() -> String { + let home = env::var("HOME").expect("failed to find $HOME env var"); + let user = env::var("USER").expect("failed to find $USER env var"); + format!("[Unit] +Description=agent to connect with monitor core +After=network.target + +[Service] +Type=simple +User={user} +WorkingDirectory={home} +ExecStart=/bin/bash --login -c 'source {home}/.bashrc; $HOME/.cargo/bin/periphery --config-path ~/.monitor/periphery.config.toml --home-dir $HOME' +TimeoutStartSec=0 + +[Install] +WantedBy=default.target") +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 371229e00..182daa790 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -135,6 +135,10 @@ fn cli() -> Command { arg!(--repo_dir "if running in container, this should be '/repos'. default is ~/.monitor/repos").required(false) ) ) + .subcommand( + Command::new("gen_service") + .about("generate a periphery systemd service file") + ) .subcommand( Command::new("start") .about("tools to start periphery as daemon or container") @@ -202,6 +206,7 @@ fn main() { ); match periphery_command { ("gen_config", sub_matches) => gen_periphery_config(sub_matches), + ("gen_service", sub_matches) => gen_periphery_service_file(sub_matches), ("start", sub_matches) => { let periphery_start_command = sub_matches.subcommand().expect("\n❌ invalid call, should be 'monitor_cli periphery start ' ❌\n"); match periphery_start_command { diff --git a/core/Cargo.toml b/core/Cargo.toml index 5d1336492..d450b1cfe 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,10 +6,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -helpers = { package = "monitor_helpers", path = "../lib/helpers" } -types = { package = "monitor_types", path = "../lib/types" } -# helpers = { package = "monitor_helpers", version = "0.1.0" } -# types = { package = "monitor_types", version = "0.1.1" } +# helpers = { package = "monitor_helpers", path = "../lib/helpers" } +# types = { package = "monitor_types", path = "../lib/types" } +helpers = { package = "monitor_helpers", version = "0.1.1" } +types = { package = "monitor_types", version = "0.1.2" } db = { package = "db_client", path = "../lib/db_client" } periphery = { package = "periphery_client", path = "../lib/periphery_client" } axum_oauth2 = { path = "../lib/axum_oauth2" } diff --git a/lib/db_client/Cargo.toml b/lib/db_client/Cargo.toml index 079dab99b..ec78a67fd 100644 --- a/lib/db_client/Cargo.toml +++ b/lib/db_client/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -types = { package = "monitor_types", path = "../types" } +types = { package = "monitor_types", version = "0.1.2" } +# types = { package = "monitor_types", path = "../types" } mungos = "0.3.0" anyhow = "1.0" \ No newline at end of file diff --git a/lib/monitor_client/Cargo.toml b/lib/monitor_client/Cargo.toml index 473e3881b..68d6ef237 100644 --- a/lib/monitor_client/Cargo.toml +++ b/lib/monitor_client/Cargo.toml @@ -9,8 +9,8 @@ license = "GPL-3.0-or-later" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# monitor_types = "0.1.1" -monitor_types = { path = "../types" } +monitor_types = "0.1.2" +# monitor_types = { path = "../types" } reqwest = { version = "0.11", features = ["json"] } tokio-tungstenite = { version = "0.18", features=["native-tls"] } tokio = "1.23" diff --git a/lib/periphery_client/Cargo.toml b/lib/periphery_client/Cargo.toml index 375b18779..f015db49f 100644 --- a/lib/periphery_client/Cargo.toml +++ b/lib/periphery_client/Cargo.toml @@ -6,8 +6,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -types = { package = "monitor_types", path = "../types" } -helpers = { package = "monitor_helpers", path = "../helpers" } +helpers = { package = "monitor_helpers", version = "0.1.1" } +types = { package = "monitor_types", version = "0.1.2" } +# types = { package = "monitor_types", path = "../types" } +# helpers = { package = "monitor_helpers", path = "../helpers" } tokio-tungstenite = { version = "0.18", features=["native-tls"] } tokio = "1.23" reqwest = { version = "0.11", features = ["json"] }