diff --git a/agent/Cargo.lock b/agent/Cargo.lock index f8e415c..2ed1003 100644 --- a/agent/Cargo.lock +++ b/agent/Cargo.lock @@ -592,6 +592,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -633,6 +642,20 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nix" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" +dependencies = [ + "bitflags", + "cfg-if", + "libc", + "memoffset", + "pin-utils", + "static_assertions", +] + [[package]] name = "ntapi" version = "0.4.1" @@ -857,9 +880,10 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", - "libc", + "nix", "prost", "reqwest", + "serde_json", "sysinfo", "tokio", "tokio-stream", @@ -1099,6 +1123,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strsim" version = "0.10.0" diff --git a/agent/Cargo.toml b/agent/Cargo.toml index ebd58aa..6f528ab 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -6,9 +6,10 @@ edition = "2021" [dependencies] anyhow = "1.0.71" clap = { version = "4.3.9" } -libc = "0.2.147" +nix = "0.26.2" prost = "0.11.9" reqwest = { version = "0.11.18", features = ["blocking"] } +serde_json = "1.0.99" sysinfo = { version = "0.29.2", default-features = false } tokio = { version = "1.28.2", features = ["rt-multi-thread", "io-util", "process", "macros", "signal"] } tokio-stream = { version = "0.1.14", features = ["net"] } diff --git a/agent/get_prymn.sh b/agent/get_prymn.sh index f54db09..ca7a480 100755 --- a/agent/get_prymn.sh +++ b/agent/get_prymn.sh @@ -28,7 +28,7 @@ main() { ensure chmod u+x "$file" # Run the installer - "$file" --install + "$file" --install "$@" local ret=$? rm "${file}" diff --git a/agent/src/bin/prymn_agent.rs b/agent/src/bin/prymn_agent.rs index 81276af..97adf77 100644 --- a/agent/src/bin/prymn_agent.rs +++ b/agent/src/bin/prymn_agent.rs @@ -12,25 +12,17 @@ fn main() -> anyhow::Result<()> { let command = clap::Command::new(env!("CARGO_BIN_NAME")) .version(env!("CARGO_PKG_VERSION")) .arg(arg!(-d --daemon "Run agent as daemon").exclusive(true)) - .arg(arg!(--install "Install this agent binary to the system").exclusive(true)) - .arg(arg!(--update "Download updated agent version and install it").exclusive(true)) + .arg(arg!(--install "Install this agent binary to the system").exclusive(true)) .arg_required_else_help(true) .try_get_matches() .unwrap_or_else(|e| e.exit()); - let is_daemon = command.get_flag("daemon"); - let is_install = command.get_flag("install"); - let is_update = command.get_flag("update"); - - if is_daemon { + if command.get_flag("daemon") { tracing::info!("running as daemon"); server::main() - } else if is_install { + } else if let Some(token) = command.get_one::("install") { tracing::info!("running install mode"); - self_update::install() - } else if is_update { - tracing::info!("running update mode"); - self_update::update() + self_update::install(token) } else { unreachable!() } diff --git a/agent/src/self_update.rs b/agent/src/self_update.rs index b39b66a..b39b9a0 100644 --- a/agent/src/self_update.rs +++ b/agent/src/self_update.rs @@ -1,5 +1,6 @@ use std::{os::unix::prelude::PermissionsExt, path::Path}; +use anyhow::{anyhow, Context}; use reqwest::Url; const GET_PRYMN_URL: &str = "todo"; @@ -13,11 +14,15 @@ pub fn update() -> anyhow::Result<()> { todo!(); } -pub fn install() -> anyhow::Result<()> { +pub fn install(token: &str) -> anyhow::Result<()> { let this_exe = std::env::current_exe()?; let prymn_path = Path::new("/usr/local/bin/prymn_agent"); - copy_binary(&this_exe, prymn_path) + copy_binary(&this_exe, prymn_path)?; + register_agent(token).context("while registering the agent")?; + install_service()?; + + Ok(()) } fn copy_binary(src: impl AsRef, dest: impl AsRef) -> anyhow::Result<()> { @@ -38,3 +43,26 @@ fn copy_binary(src: impl AsRef, dest: impl AsRef) -> anyhow::Result< Ok(()) } + +fn register_agent(token: &str) -> anyhow::Result<()> { + let client = reqwest::blocking::Client::new(); + + let res = client + .post("http://localhost:4000/api/v1/servers/register") + .body(serde_json::json!({"token": token}).to_string()) + .send()?; + + if !res.status().is_success() { + // TODO: Make better error message + return Err(anyhow!( + "register request returned an error: {}", + res.text()? + )); + } + + Ok(()) +} + +fn install_service() -> anyhow::Result<()> { + Ok(()) +}