From 97978df287ee42f523f509ac686a13fa0400a026 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 25 Aug 2023 21:52:28 +0200 Subject: [PATCH] feat: add initial churn Signed-off-by: kjuulh --- Cargo.lock | 164 ++++++++++++++++++++++++++++++++ Cargo.toml | 3 + crates/churn-agent/src/main.rs | 17 +++- crates/churn-server/src/main.rs | 70 +++++++++++--- crates/churn/Cargo.toml | 1 + crates/churn/src/main.rs | 6 +- crates/churning/src/main.rs | 18 ++-- 7 files changed, 258 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88ad7de..362bb9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,6 +224,7 @@ dependencies = [ "axum", "clap", "dotenv", + "reqwest", "tokio", "tracing", "tracing-subscriber", @@ -326,6 +327,22 @@ dependencies = [ "unreachable", ] +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + [[package]] name = "cpufeatures" version = "0.2.9" @@ -591,6 +608,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.0" @@ -905,6 +937,19 @@ dependencies = [ "tokio-rustls", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -1059,6 +1104,24 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -1094,6 +1157,50 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "openssl" +version = "0.10.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "729b745ad4a5575dd06a3e1af1414bd330ee561c01b3899eb584baeaa8def17e" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "866b5f16f90776b9bb8dc1e1802ac6f0513de3a7a7465867bfbc563dc737faac" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "overload" version = "0.1.1" @@ -1161,6 +1268,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + [[package]] name = "platform-info" version = "1.0.2" @@ -1234,10 +1347,12 @@ dependencies = [ "http-body", "hyper", "hyper-rustls", + "hyper-tls", "ipnet", "js-sys", "log", "mime", + "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -1247,6 +1362,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "tokio", + "tokio-native-tls", "tokio-rustls", "tokio-util", "tower-service", @@ -1350,6 +1466,15 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +[[package]] +name = "schannel" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" +dependencies = [ + "windows-sys", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -1366,6 +1491,29 @@ dependencies = [ "untrusted", ] +[[package]] +name = "security-framework" +version = "2.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.164" @@ -1621,6 +1769,16 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.24.1" @@ -1804,6 +1962,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 1f1982a..8b489aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,6 @@ tracing-subscriber = { version = "0.3.17" } clap = { version = "4.3.4", features = ["derive", "env"] } dotenv = { version = "0.15.0" } axum = { version = "0.6.18" } +async-trait = "*" + +reqwest = {version = "0.11.20", features = ["json"]} diff --git a/crates/churn-agent/src/main.rs b/crates/churn-agent/src/main.rs index 3bdc91e..0304f1d 100644 --- a/crates/churn-agent/src/main.rs +++ b/crates/churn-agent/src/main.rs @@ -1,5 +1,6 @@ use std::net::SocketAddr; +use axum::{response::IntoResponse, routing::get, Router}; use clap::{Parser, Subcommand}; #[derive(Parser)] @@ -44,8 +45,16 @@ async fn main() -> anyhow::Result<()> { async fn handle_command(cmd: Command) -> anyhow::Result<()> { match cmd.command { Some(Commands::Daemon { host }) => { - tracing::info!("starting agent server on {}", host); - tokio::time::sleep(std::time::Duration::from_secs(60)).await; + tracing::info!("Starting churn server"); + + let app = Router::new().route("/ping", get(ping)); + + tracing::info!("churn server listening on {}", host); + axum::Server::bind(&host) + .serve(app.into_make_service()) + .await + .unwrap(); + Ok(()) } Some(Commands::Connect { @@ -56,3 +65,7 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> { None => todo!(), } } + +async fn ping() -> impl IntoResponse { + "pong!" +} diff --git a/crates/churn-server/src/main.rs b/crates/churn-server/src/main.rs index c48f8ac..b95bf55 100644 --- a/crates/churn-server/src/main.rs +++ b/crates/churn-server/src/main.rs @@ -1,9 +1,12 @@ +use std::collections::HashMap; use std::net::SocketAddr; +use std::sync::Arc; use axum::response::IntoResponse; use axum::routing::{get, post}; -use axum::Router; +use axum::{async_trait, Router}; use clap::{Parser, Subcommand}; +use tokio::sync::Mutex; #[derive(Parser)] #[command(author, version, about, long_about = None, subcommand_required = true)] @@ -20,6 +23,44 @@ enum Commands { }, } +struct AgentService(Arc); + +impl Default for AgentService { + fn default() -> Self { + Self(Arc::new(DefaultAgentService::default())) + } +} + +struct DefaultAgentService { + agents: Arc>>, +} + +impl Default for DefaultAgentService { + fn default() -> Self { + Self { + agents: Arc::default(), + } + } +} + +#[async_trait] +impl AgentServiceTrait for DefaultAgentService { + async fn enroll(&self, agent: Agent) -> anyhow::Result { + todo!() + } +} + +#[async_trait] +trait AgentServiceTrait { + async fn enroll(&self, agent: Agent) -> anyhow::Result; +} + +struct Agent { + pub name: String, +} + +struct AppState {} + #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv::dotenv().ok(); @@ -29,17 +70,20 @@ async fn main() -> anyhow::Result<()> { match cli.command { Some(Commands::Serve { host }) => { - tracing::info!("Starting service"); + tracing::info!("Starting churn server"); - let app = Router::new().nest( - "/agent", - Router::new() - .route("/enroll", post(enroll)) - .route("/ping", post(ping)) - .route("/events", post(get_tasks)), - ); + let app = Router::new() + .route("/ping", get(ping)) + .nest( + "/agent", + Router::new() + .route("/enroll", post(enroll)) + .route("/ping", post(agent_ping)) + .route("/events", post(get_tasks)), + ) + .with_state(); - tracing::info!("listening on {}", host); + tracing::info!("churn server listening on {}", host); axum::Server::bind(&host) .serve(app.into_make_service()) .await @@ -55,10 +99,14 @@ async fn enroll() -> impl IntoResponse { todo!() } -async fn ping() -> impl IntoResponse { +async fn agent_ping() -> impl IntoResponse { todo!() } async fn get_tasks() -> impl IntoResponse { todo!() } + +async fn ping() -> impl IntoResponse { + "pong!" +} diff --git a/crates/churn/Cargo.toml b/crates/churn/Cargo.toml index cf44a76..7c32059 100644 --- a/crates/churn/Cargo.toml +++ b/crates/churn/Cargo.toml @@ -11,3 +11,4 @@ tracing-subscriber.workspace = true clap.workspace = true dotenv.workspace = true axum.workspace = true +reqwest.workspace = true diff --git a/crates/churn/src/main.rs b/crates/churn/src/main.rs index 5cfe92a..89ac740 100644 --- a/crates/churn/src/main.rs +++ b/crates/churn/src/main.rs @@ -51,9 +51,11 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> { } => todo!(), Commands::Health { server, agent } => { tracing::info!("connecting to server: {}", server); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + reqwest::get(format!("{server}/ping")).await?; + tracing::info!("connected to server successfully"); tracing::info!("connecting to agent: {}", agent); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + reqwest::get(format!("{agent}/ping")).await?; + tracing::info!("connected to agent successfully"); Ok(()) } } diff --git a/crates/churning/src/main.rs b/crates/churning/src/main.rs index e41991c..2c824f9 100644 --- a/crates/churning/src/main.rs +++ b/crates/churning/src/main.rs @@ -17,18 +17,24 @@ async fn main() -> eyre::Result<()> { .with_exec(vec!["churn-server", "serve", "--host", "0.0.0.0:3000"]) .with_exposed_port(3000); - cli.with_service_binding("churn-agent", agent.id().await?) + let churning = cli + .with_service_binding("churn-agent", agent.id().await?) .with_service_binding("churn-server", server.id().await?) .with_exec(vec![ "churn", "health", "--server", - "churn-server:3000", + "http://churn-server:3000", "--agent", - "churn-agent:3000", - ]) - .exit_code() - .await?; + "http://churn-agent:3000", + ]); + + let stdout = churning.stdout().await?; + println!("{stdout}"); + let stderr = churning.stderr().await?; + println!("{stderr}"); + + churning.exit_code().await?; Ok(()) }