agent: register agent to backend
This commit is contained in:
parent
a8699035d8
commit
40be58f9c6
5 changed files with 68 additions and 17 deletions
32
agent/Cargo.lock
generated
32
agent/Cargo.lock
generated
|
@ -592,6 +592,15 @@ version = "2.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memoffset"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mime"
|
name = "mime"
|
||||||
version = "0.3.17"
|
version = "0.3.17"
|
||||||
|
@ -633,6 +642,20 @@ dependencies = [
|
||||||
"tempfile",
|
"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]]
|
[[package]]
|
||||||
name = "ntapi"
|
name = "ntapi"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
@ -857,9 +880,10 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"libc",
|
"nix",
|
||||||
"prost",
|
"prost",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde_json",
|
||||||
"sysinfo",
|
"sysinfo",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
|
@ -1099,6 +1123,12 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "static_assertions"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.10.0"
|
version = "0.10.0"
|
||||||
|
|
|
@ -6,9 +6,10 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.71"
|
anyhow = "1.0.71"
|
||||||
clap = { version = "4.3.9" }
|
clap = { version = "4.3.9" }
|
||||||
libc = "0.2.147"
|
nix = "0.26.2"
|
||||||
prost = "0.11.9"
|
prost = "0.11.9"
|
||||||
reqwest = { version = "0.11.18", features = ["blocking"] }
|
reqwest = { version = "0.11.18", features = ["blocking"] }
|
||||||
|
serde_json = "1.0.99"
|
||||||
sysinfo = { version = "0.29.2", default-features = false }
|
sysinfo = { version = "0.29.2", default-features = false }
|
||||||
tokio = { version = "1.28.2", features = ["rt-multi-thread", "io-util", "process", "macros", "signal"] }
|
tokio = { version = "1.28.2", features = ["rt-multi-thread", "io-util", "process", "macros", "signal"] }
|
||||||
tokio-stream = { version = "0.1.14", features = ["net"] }
|
tokio-stream = { version = "0.1.14", features = ["net"] }
|
||||||
|
|
|
@ -28,7 +28,7 @@ main() {
|
||||||
ensure chmod u+x "$file"
|
ensure chmod u+x "$file"
|
||||||
|
|
||||||
# Run the installer
|
# Run the installer
|
||||||
"$file" --install
|
"$file" --install "$@"
|
||||||
local ret=$?
|
local ret=$?
|
||||||
|
|
||||||
rm "${file}"
|
rm "${file}"
|
||||||
|
|
|
@ -12,25 +12,17 @@ fn main() -> anyhow::Result<()> {
|
||||||
let command = clap::Command::new(env!("CARGO_BIN_NAME"))
|
let command = clap::Command::new(env!("CARGO_BIN_NAME"))
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.arg(arg!(-d --daemon "Run agent as daemon").exclusive(true))
|
.arg(arg!(-d --daemon "Run agent as daemon").exclusive(true))
|
||||||
.arg(arg!(--install "Install this agent binary to the system").exclusive(true))
|
.arg(arg!(--install <TOKEN> "Install this agent binary to the system").exclusive(true))
|
||||||
.arg(arg!(--update "Download updated agent version and install it").exclusive(true))
|
|
||||||
.arg_required_else_help(true)
|
.arg_required_else_help(true)
|
||||||
.try_get_matches()
|
.try_get_matches()
|
||||||
.unwrap_or_else(|e| e.exit());
|
.unwrap_or_else(|e| e.exit());
|
||||||
|
|
||||||
let is_daemon = command.get_flag("daemon");
|
if command.get_flag("daemon") {
|
||||||
let is_install = command.get_flag("install");
|
|
||||||
let is_update = command.get_flag("update");
|
|
||||||
|
|
||||||
if is_daemon {
|
|
||||||
tracing::info!("running as daemon");
|
tracing::info!("running as daemon");
|
||||||
server::main()
|
server::main()
|
||||||
} else if is_install {
|
} else if let Some(token) = command.get_one::<String>("install") {
|
||||||
tracing::info!("running install mode");
|
tracing::info!("running install mode");
|
||||||
self_update::install()
|
self_update::install(token)
|
||||||
} else if is_update {
|
|
||||||
tracing::info!("running update mode");
|
|
||||||
self_update::update()
|
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{os::unix::prelude::PermissionsExt, path::Path};
|
use std::{os::unix::prelude::PermissionsExt, path::Path};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Context};
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
|
|
||||||
const GET_PRYMN_URL: &str = "todo";
|
const GET_PRYMN_URL: &str = "todo";
|
||||||
|
@ -13,11 +14,15 @@ pub fn update() -> anyhow::Result<()> {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn install() -> anyhow::Result<()> {
|
pub fn install(token: &str) -> anyhow::Result<()> {
|
||||||
let this_exe = std::env::current_exe()?;
|
let this_exe = std::env::current_exe()?;
|
||||||
let prymn_path = Path::new("/usr/local/bin/prymn_agent");
|
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<Path>, dest: impl AsRef<Path>) -> anyhow::Result<()> {
|
fn copy_binary(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> anyhow::Result<()> {
|
||||||
|
@ -38,3 +43,26 @@ fn copy_binary(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> anyhow::Result<
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue