dagger-wasm/ci/src/main.rs

112 lines
3.5 KiB
Rust
Raw Normal View History

2023-05-05 23:35:12 +02:00
use std::sync::Arc;
use dagger_sdk::HostDirectoryOptsBuilder;
#[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install().unwrap();
let client = dagger_sdk::connect().await?;
let wasm = build_wasm_binary(client.clone()).await?;
run_wasmtime(client.clone(), wasm.clone()).await?;
run_wasmer(client, wasm).await?;
Ok(())
}
async fn build_wasm_binary(client: Arc<dagger_sdk::Query>) -> eyre::Result<dagger_sdk::File> {
let target = "wasm32-wasi";
let binary = "dagger-wasm.wasm";
let cargo_cache = client.cache_volume("cargo_cache");
let src = client.host().directory_opts(
".",
HostDirectoryOptsBuilder::default()
.include(vec!["Cargo.toml", "Cargo.lock", "src/"])
.build()?,
);
let wasm = client
.container()
.from("rustlang/rust:nightly")
.with_mounted_cache("~/.cargo", cargo_cache.id().await?)
.with_exec(vec!["rustup", "target", "add", target])
.with_directory("/app", src.id().await?)
.with_workdir("/app")
.with_exec(vec![
"cargo",
"build",
format!("--target={target}").as_str(),
"--release",
])
.file(format!("target/{target}/release/{binary}"));
Ok(wasm)
}
async fn run_wasmtime(client: Arc<dagger_sdk::Query>, wasm: dagger_sdk::File) -> eyre::Result<()> {
let wasmtime_version = "v8.0.1";
let wasmtime_architecture = "aarch64";
let wasmtime_download_url = format!("https://github.com/bytecodealliance/wasmtime/releases/download/{wasmtime_version}/wasmtime-{wasmtime_version}-{wasmtime_architecture}-linux.tar.xz");
let debian = build_debian(client).await?;
let out = debian
.with_exec(vec![
"wget",
&wasmtime_download_url,
"-O",
"wasmtime.tar.xz",
])
.with_exec(vec!["tar", "-xvf", "wasmtime.tar.xz"])
.with_exec(vec![
"mv",
&format!("wasmtime-{wasmtime_version}-{wasmtime_architecture}-linux/wasmtime"),
"/usr/bin/wasmtime",
])
.with_exec(vec!["wasmtime", "-V"])
.with_file("/usr/lib/dagger-wasm.wasm", wasm.id().await?)
.with_exec(vec!["wasmtime", "/usr/lib/dagger-wasm.wasm"]);
let out = out.stdout().await?;
println!("wasmtime: {}", out);
Ok(())
}
async fn run_wasmer(client: Arc<dagger_sdk::Query>, wasm: dagger_sdk::File) -> eyre::Result<()> {
let wasmer_version = "v3.3.0";
let wasmer_architecture = "aarch64";
let wasmer_download_url =
format!("https://github.com/wasmerio/wasmer/releases/download/{wasmer_version}/wasmer-linux-{wasmer_architecture}.tar.gz");
let debian = build_debian(client).await?;
let out = debian
.with_exec(vec!["wget", &wasmer_download_url, "-O", "wasmer.tar.gz"])
.with_exec(vec!["tar", "-xvf", "wasmer.tar.gz"])
.with_exec(vec!["mv", "bin/wasmer", "/usr/bin/wasmer"])
.with_exec(vec!["wasmer", "-v"])
.with_file("/usr/lib/dagger-wasm.wasm", wasm.id().await?)
.with_exec(vec!["wasmer", "run", "/usr/lib/dagger-wasm.wasm"]);
let out = out.stdout().await?;
println!("wasmer: {}", out);
Ok(())
}
async fn build_debian(client: Arc<dagger_sdk::Query>) -> eyre::Result<dagger_sdk::Container> {
let debian = client
.container()
.from("debian:bookworm")
.with_workdir("/tmp/")
.with_exec(vec!["apt", "update", "-y"])
.with_exec(vec!["apt", "install", "wget", "xz-utils", "-y"]);
Ok(debian)
}