feat: add base app

This commit is contained in:
Kasper Juul Hermansen 2023-05-05 23:35:12 +02:00
commit 831ca76eef
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
11 changed files with 2031 additions and 0 deletions

3
.drone.yaml Normal file
View File

@ -0,0 +1,3 @@
kind: template
load: shuttle-template.yaml
name: dagger-wasm

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
target/
.shuttle/

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "dagger-wasm"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "dagger-wasm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

40
actions/ci.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"context"
"log"
"os"
"os/exec"
)
func Ci(ctx context.Context) error {
err := runRustPipeline(ctx)
if err != nil {
log.Fatal(err)
}
return nil
}
func runRustPipeline(ctx context.Context) error {
cmd := exec.Command("cargo", "build")
cmd.Dir = "ci"
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
cmd = exec.Command("ci/target/debug/ci")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
return nil
}

3
actions/go.mod Normal file
View File

@ -0,0 +1,3 @@
module actions
go 1.20

1840
ci/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
ci/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "ci"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
color-eyre = "0.6.2"
dagger-sdk = "0.2.22"
eyre = "0.6.8"
tokio = { version = "1.28.0", features = ["full"] }

111
ci/src/main.rs Normal file
View File

@ -0,0 +1,111 @@
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)
}

1
shuttle.yaml Normal file
View File

@ -0,0 +1 @@
plan: false

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}