feat: add initial churn

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-25 21:52:28 +02:00
parent f61d0bbf12
commit 97978df287
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
7 changed files with 258 additions and 21 deletions

164
Cargo.lock generated
View File

@ -224,6 +224,7 @@ dependencies = [
"axum", "axum",
"clap", "clap",
"dotenv", "dotenv",
"reqwest",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -326,6 +327,22 @@ dependencies = [
"unreachable", "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]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.9" version = "0.2.9"
@ -591,6 +608,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 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]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.2.0" version = "1.2.0"
@ -905,6 +937,19 @@ dependencies = [
"tokio-rustls", "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]] [[package]]
name = "ident_case" name = "ident_case"
version = "1.0.1" version = "1.0.1"
@ -1059,6 +1104,24 @@ dependencies = [
"windows-sys", "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]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" version = "0.46.0"
@ -1094,6 +1157,50 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" 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]] [[package]]
name = "overload" name = "overload"
version = "0.1.1" version = "0.1.1"
@ -1161,6 +1268,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]] [[package]]
name = "platform-info" name = "platform-info"
version = "1.0.2" version = "1.0.2"
@ -1234,10 +1347,12 @@ dependencies = [
"http-body", "http-body",
"hyper", "hyper",
"hyper-rustls", "hyper-rustls",
"hyper-tls",
"ipnet", "ipnet",
"js-sys", "js-sys",
"log", "log",
"mime", "mime",
"native-tls",
"once_cell", "once_cell",
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite",
@ -1247,6 +1362,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"tokio", "tokio",
"tokio-native-tls",
"tokio-rustls", "tokio-rustls",
"tokio-util", "tokio-util",
"tower-service", "tower-service",
@ -1350,6 +1466,15 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" 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]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
@ -1366,6 +1491,29 @@ dependencies = [
"untrusted", "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]] [[package]]
name = "serde" name = "serde"
version = "1.0.164" version = "1.0.164"
@ -1621,6 +1769,16 @@ dependencies = [
"syn 2.0.29", "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]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.24.1" version = "0.24.1"
@ -1804,6 +1962,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -14,3 +14,6 @@ tracing-subscriber = { version = "0.3.17" }
clap = { version = "4.3.4", features = ["derive", "env"] } clap = { version = "4.3.4", features = ["derive", "env"] }
dotenv = { version = "0.15.0" } dotenv = { version = "0.15.0" }
axum = { version = "0.6.18" } axum = { version = "0.6.18" }
async-trait = "*"
reqwest = {version = "0.11.20", features = ["json"]}

View File

@ -1,5 +1,6 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{response::IntoResponse, routing::get, Router};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
#[derive(Parser)] #[derive(Parser)]
@ -44,8 +45,16 @@ async fn main() -> anyhow::Result<()> {
async fn handle_command(cmd: Command) -> anyhow::Result<()> { async fn handle_command(cmd: Command) -> anyhow::Result<()> {
match cmd.command { match cmd.command {
Some(Commands::Daemon { host }) => { Some(Commands::Daemon { host }) => {
tracing::info!("starting agent server on {}", host); tracing::info!("Starting churn server");
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
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(()) Ok(())
} }
Some(Commands::Connect { Some(Commands::Connect {
@ -56,3 +65,7 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
None => todo!(), None => todo!(),
} }
} }
async fn ping() -> impl IntoResponse {
"pong!"
}

View File

@ -1,9 +1,12 @@
use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::routing::{get, post}; use axum::routing::{get, post};
use axum::Router; use axum::{async_trait, Router};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use tokio::sync::Mutex;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)] #[command(author, version, about, long_about = None, subcommand_required = true)]
@ -20,6 +23,44 @@ enum Commands {
}, },
} }
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
impl Default for AgentService {
fn default() -> Self {
Self(Arc::new(DefaultAgentService::default()))
}
}
struct DefaultAgentService {
agents: Arc<Mutex<HashMap<String, Agent>>>,
}
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<String> {
todo!()
}
}
#[async_trait]
trait AgentServiceTrait {
async fn enroll(&self, agent: Agent) -> anyhow::Result<String>;
}
struct Agent {
pub name: String,
}
struct AppState {}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
@ -29,17 +70,20 @@ async fn main() -> anyhow::Result<()> {
match cli.command { match cli.command {
Some(Commands::Serve { host }) => { Some(Commands::Serve { host }) => {
tracing::info!("Starting service"); tracing::info!("Starting churn server");
let app = Router::new().nest( let app = Router::new()
"/agent", .route("/ping", get(ping))
Router::new() .nest(
.route("/enroll", post(enroll)) "/agent",
.route("/ping", post(ping)) Router::new()
.route("/events", post(get_tasks)), .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) axum::Server::bind(&host)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
@ -55,10 +99,14 @@ async fn enroll() -> impl IntoResponse {
todo!() todo!()
} }
async fn ping() -> impl IntoResponse { async fn agent_ping() -> impl IntoResponse {
todo!() todo!()
} }
async fn get_tasks() -> impl IntoResponse { async fn get_tasks() -> impl IntoResponse {
todo!() todo!()
} }
async fn ping() -> impl IntoResponse {
"pong!"
}

View File

@ -11,3 +11,4 @@ tracing-subscriber.workspace = true
clap.workspace = true clap.workspace = true
dotenv.workspace = true dotenv.workspace = true
axum.workspace = true axum.workspace = true
reqwest.workspace = true

View File

@ -51,9 +51,11 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
} => todo!(), } => todo!(),
Commands::Health { server, agent } => { Commands::Health { server, agent } => {
tracing::info!("connecting to server: {}", server); 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); 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(()) Ok(())
} }
} }

View File

@ -17,18 +17,24 @@ async fn main() -> eyre::Result<()> {
.with_exec(vec!["churn-server", "serve", "--host", "0.0.0.0:3000"]) .with_exec(vec!["churn-server", "serve", "--host", "0.0.0.0:3000"])
.with_exposed_port(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_service_binding("churn-server", server.id().await?)
.with_exec(vec![ .with_exec(vec![
"churn", "churn",
"health", "health",
"--server", "--server",
"churn-server:3000", "http://churn-server:3000",
"--agent", "--agent",
"churn-agent:3000", "http://churn-agent:3000",
]) ]);
.exit_code()
.await?; let stdout = churning.stdout().await?;
println!("{stdout}");
let stderr = churning.stderr().await?;
println!("{stderr}");
churning.exit_code().await?;
Ok(()) Ok(())
} }