churn/crates/churning/src/main.rs
kjuulh 7e22a7f3ab
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
feat: update
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-04-06 21:30:49 +02:00

137 lines
4.0 KiB
Rust

use std::{path::PathBuf, sync::Arc};
use dagger_rust::build::{RustVersion, SlimImage};
use dagger_sdk::Query;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> eyre::Result<()> {
// let mut config = Config::default();
// config.logger = None;
println!("Building churning...");
//let client = dagger_sdk::connect_opts(config).await?;
let client = dagger_sdk::connect().await?;
let cli = build_container(client.clone(), "churn").await?;
let server = build_container(client.clone(), "churn-server").await?;
let server = server
.with_env_variable("CHURN_DATABASE", "sled")
.with_env_variable("CHURN_SLED_PATH", "/mnt/sled")
.with_mounted_cache("/mnt/sled", client.cache_volume("sled"))
.with_exec(vec!["churn-server", "serve", "--host", "0.0.0.0:3000"])
.with_exposed_port(3000);
let server_service = server.as_service();
let agent = build_container(client.clone(), "churn-agent").await?;
let agent = agent
.with_service_binding("churn-server", server_service.clone())
.with_exec(vec!["churn-agent", "daemon", "--host", "0.0.0.0:3000"])
.with_exposed_port(3000);
let agent_service = agent.as_service();
let churning = cli
.with_service_binding("churn-agent", agent_service)
.with_service_binding("churn-server", server_service)
.with_env_variable("CHURN_SERVER", "http://churn-server:3000")
.with_env_variable("CHURN_SERVER_TOKEN", "something")
.with_env_variable("CHURN_AGENT", "http://churn-agent:3000")
.with_env_variable("CHURN_AGENT_NAME", "churn-agent")
.with_exec(vec![
"churn",
"health",
"--server",
"http://churn-server:3000",
"--agent",
"http://churn-agent:3000",
]);
let stdout = churning.stdout().await?;
println!("{stdout}");
let stderr = churning.stderr().await?;
println!("{stderr}");
churning.sync().await?;
println!("Finished building churning...");
repl(churning).await?; //.with_entrypoint(vec!["churn"])).await?;
Ok(())
}
async fn repl(container: dagger_sdk::Container) -> eyre::Result<()> {
loop {
let stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
stdout.write_all(b"> ").await?;
stdout.flush().await?;
let mut input = String::new();
let mut stdin = tokio::io::BufReader::new(stdin);
stdin.read_line(&mut input).await?;
let input = input.trim();
if input == "q" {
break;
}
let container = container.with_exec(input.split(' ').collect());
match container.stdout().await {
Ok(stdout) => {
println!("{stdout}");
}
Err(e) => {
eprintln!("{}", e);
}
}
match container.sync().await {
Ok(_) => {}
Err(_e) => {
//eprintln!("encountred error: {}", e);
}
}
}
Ok(())
}
async fn build_container(client: Query, bin_name: &str) -> eyre::Result<dagger_sdk::Container> {
let crates = &["crates/*", "ci"];
let debian_deps = &[
"libssl-dev",
"pkg-config",
"openssl",
"git",
"jq",
"capnproto",
];
let debian_image = "debian:bullseye".to_string();
let images = dagger_rust::build::RustBuild::new(client.clone())
.build_release(
None::<PathBuf>,
RustVersion::Nightly,
crates,
debian_deps,
vec![SlimImage::Debian {
image: debian_image,
deps: debian_deps
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
architecture: dagger_rust::build::BuildArchitecture::Amd64,
}],
bin_name,
)
.await?;
Ok(images.first().take().unwrap().clone())
}