From cfdf62511882a5353f9e371405b2fca6699c69b3 Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Fri, 10 Nov 2023 21:04:54 +0000 Subject: [PATCH] Add support for pulling Spin OCI images Signed-off-by: James Sturtevant --- Cargo.lock | 234 ++++++++++++++++++++--------- Makefile | 7 + containerd-shim-spin/Cargo.toml | 2 +- containerd-shim-spin/src/engine.rs | 128 ++++++++++++---- 4 files changed, 267 insertions(+), 104 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43ec825..bdfc2c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ahash" version = "0.8.3" @@ -31,15 +46,15 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.69" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "async-trait" -version = "0.1.68" +version = "0.1.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" +checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", @@ -63,6 +78,21 @@ dependencies = [ "rand", ] +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.13.1" @@ -77,9 +107,9 @@ checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5" [[package]] name = "base64" -version = "0.21.2" +version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" +checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" [[package]] name = "bitflags" @@ -107,9 +137,12 @@ checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" [[package]] name = "cc" -version = "1.0.79" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] [[package]] name = "cfg-if" @@ -142,7 +175,7 @@ dependencies = [ [[package]] name = "containerd-wasm-shims-tests" -version = "0.9.2" +version = "0.9.3" dependencies = [ "anyhow", "curl", @@ -172,7 +205,7 @@ dependencies = [ "openssl-probe", "openssl-sys", "schannel", - "socket2", + "socket2 0.4.9", "winapi", ] @@ -293,6 +326,12 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "fnv" version = "1.0.7" @@ -316,18 +355,18 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] name = "form_urlencoded" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" dependencies = [ "percent-encoding", ] [[package]] name = "futures" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "531ac96c6ff5fd7c62263c5e3c67a603af4fcaee2e1a0ae5565ba3a11e69e549" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" dependencies = [ "futures-channel", "futures-core", @@ -340,9 +379,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" dependencies = [ "futures-core", "futures-sink", @@ -350,15 +389,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" [[package]] name = "futures-executor" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" dependencies = [ "futures-core", "futures-task", @@ -367,38 +406,38 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" [[package]] name = "futures-macro" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.29", ] [[package]] name = "futures-sink" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" [[package]] name = "futures-task" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" [[package]] name = "futures-util" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ "futures-channel", "futures-core", @@ -414,15 +453,21 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.8" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" dependencies = [ "cfg-if", "libc", "wasi", ] +[[package]] +name = "gimli" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" + [[package]] name = "hashbrown" version = "0.12.3" @@ -504,7 +549,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.4.9", "tokio", "tower-service", "tracing", @@ -573,9 +618,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "idna" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" dependencies = [ "unicode-bidi", "unicode-normalization", @@ -591,6 +636,16 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "indexmap" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad227c3af19d4914570ad36d30409928b75967c298feb9ea1969db3a610bb14e" +dependencies = [ + "equivalent", + "hashbrown 0.14.0", +] + [[package]] name = "instant" version = "0.1.12" @@ -644,7 +699,7 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95578de7d6eac4fba42114bc751e38c59a739968769df1be56feba6f17fd148e" dependencies = [ - "base64 0.21.2", + "base64 0.21.5", "bytes", "chrono", "http", @@ -823,9 +878,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "mime" @@ -833,16 +888,24 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "mio" -version = "0.8.6" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" +checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" dependencies = [ "libc", - "log", "wasi", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -857,13 +920,22 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.15" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -957,9 +1029,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "pin-project" @@ -983,9 +1055,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -1062,6 +1134,12 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "ryu" version = "1.0.13" @@ -1125,9 +1203,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.188" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" dependencies = [ "serde_derive", ] @@ -1144,9 +1222,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.188" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" dependencies = [ "proc-macro2", "quote", @@ -1166,11 +1244,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.96" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ - "indexmap", + "indexmap 2.0.1", "itoa", "ryu", "serde", @@ -1182,7 +1260,7 @@ version = "0.9.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c" dependencies = [ - "indexmap", + "indexmap 1.9.2", "itoa", "ryu", "serde", @@ -1223,6 +1301,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "socket2" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1262,18 +1350,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.47" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.47" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", @@ -1297,18 +1385,18 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.26.0" +version = "1.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" +checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" dependencies = [ - "autocfg", + "backtrace", "libc", "mio", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.5", "tokio-macros", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -1323,13 +1411,13 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "1.8.2" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.29", ] [[package]] @@ -1346,9 +1434,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.7" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" dependencies = [ "bytes", "futures-core", @@ -1492,9 +1580,9 @@ checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" [[package]] name = "url" -version = "2.3.1" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" dependencies = [ "form_urlencoded", "idna", diff --git a/Makefile b/Makefile index 5b389bc..565a056 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,13 @@ fmt: cargo fmt --all -- --check cargo clippy --all-targets --all-features --workspace -- --deny=warnings +.PHONY: fix +fix: + $(foreach shim,$(SHIMS),cargo fmt --all --manifest-path=containerd-shim-$(shim)/Cargo.toml;) + $(foreach shim,$(SHIMS),cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-$(shim)/Cargo.toml --fix -- -D warnings;) + cargo fmt --all + cargo clippy --all-targets --all-features --workspace --fix -- --deny=warnings + .PHONY: build build: $(foreach shim,$(SHIMS),build-$(shim)-cross-$(TARGET)) echo "Build complete" diff --git a/containerd-shim-spin/Cargo.toml b/containerd-shim-spin/Cargo.toml index 0793262..c34fbf1 100644 --- a/containerd-shim-spin/Cargo.toml +++ b/containerd-shim-spin/Cargo.toml @@ -29,6 +29,6 @@ serde = "1.0" serde_json = "1.0" url = "2.3" anyhow = "1.0" - +oci-spec = { version = "0.6.3" } [workspace] diff --git a/containerd-shim-spin/src/engine.rs b/containerd-shim-spin/src/engine.rs index 65ff799..f2e9bfc 100644 --- a/containerd-shim-spin/src/engine.rs +++ b/containerd-shim-spin/src/engine.rs @@ -1,7 +1,9 @@ use anyhow::{anyhow, ensure, Context, Result}; use containerd_shim_wasm::container::{Engine, RuntimeContext, Stdio}; use log::info; +use oci_spec::image::MediaType; use spin_app::locked::LockedApp; +use spin_loader::cache::Cache; use spin_loader::FilesMountStrategy; use spin_manifest::schema::v2::AppManifest; use spin_redis_engine::RedisTrigger; @@ -9,7 +11,8 @@ use spin_trigger::TriggerHooks; use spin_trigger::{loader, RuntimeConfig, TriggerExecutor, TriggerExecutorBuilder}; use spin_trigger_http::HttpTrigger; use std::collections::HashSet; -use std::env; +use std::fs::File; +use std::io::Write; use std::net::SocketAddr; use std::net::ToSocketAddrs; use std::path::{Path, PathBuf}; @@ -38,21 +41,98 @@ impl TriggerHooks for StdioTriggerHook { } } +#[derive(Clone)] +enum AppSource { + File(PathBuf), + Oci, +} + +impl std::fmt::Debug for AppSource { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AppSource::File(path) => write!(f, "File({})", path.display()), + AppSource::Oci => write!(f, "Oci"), + } + } +} + impl SpinEngine { - fn app_source(&self) -> Result { - spin_common::paths::resolve_manifest_file_path("/spin.toml") + async fn app_source(&self, ctx: &impl RuntimeContext, cache: &Cache) -> Result { + match ctx.wasm_layers() { + [] => Ok(AppSource::File( + spin_common::paths::resolve_manifest_file_path("/spin.toml")?, + )), + layers => { + info!( + " >>> configuring spin oci application {}", + ctx.wasm_layers().len() + ); + + for artifact in layers { + match artifact.config.media_type() { + MediaType::Other(name) + if name == spin_oci::client::SPIN_APPLICATION_MEDIA_TYPE => + { + let path = PathBuf::from("/spin.json"); + log::info!("writing spin oci config to {:?}", path); + File::create(&path) + .context("failed to create spin.json")? + .write_all(&artifact.layer) + .context("failed to write spin.json")?; + } + MediaType::Other(name) + if name == "application/vnd.wasm.content.layer.v1+wasm" => + { + log::info!( + "writing artifact config to cache, near {:?}", + cache.manifests_dir() + ); + cache + .write_wasm(&artifact.layer, &artifact.config.digest()) + .await?; + } + _ => {} + } + } + Ok(AppSource::Oci) + } + } } - fn resolve_app_source(&self, app_source: PathBuf) -> Result { - Ok(ResolvedAppSource::File { - manifest_path: app_source.clone(), - manifest: spin_manifest::manifest_from_file(app_source)?, - }) + async fn resolve_app_source( + &self, + app_source: AppSource, + cache: &Cache, + ) -> Result { + let resolve_app_source = match app_source { + AppSource::File(source) => ResolvedAppSource::File { + manifest_path: source.clone(), + manifest: spin_manifest::manifest_from_file(source.clone())?, + }, + AppSource::Oci => { + let working_dir = PathBuf::from("/"); + let loader = spin_oci::OciLoader::new(working_dir); + + //todo + let reference = "docker.io/library/wasmtest_spin:latest"; + + let locked_app = loader + .load_from_cache(PathBuf::from("/spin.json"), reference, cache) + .await?; + ResolvedAppSource::OciRegistry { locked_app } + } + }; + Ok(resolve_app_source) } - async fn wasm_exec_async(&self) -> Result<()> { - let app_source = self.app_source()?; - let resolved_app_source = self.resolve_app_source(app_source.clone())?; + async fn wasm_exec_async(&self, ctx: &impl RuntimeContext) -> Result<()> { + // create a cache directory at /.cache + // this is needed for the spin Loaders to work + let cache = Cache::new(Some(PathBuf::from("/.cache"))) + .await + .context("failed to create cache")?; + let app_source = self.app_source(ctx, &cache).await?; + let resolved_app_source = self.resolve_app_source(app_source.clone(), &cache).await?; let trigger_cmd = trigger_command_for_resolved_app_source(&resolved_app_source) .with_context(|| format!("Couldn't find trigger executor for {app_source:?}"))?; let locked_app = self.load_resolved_app_source(resolved_app_source).await?; @@ -99,20 +179,6 @@ impl SpinEngine { ResolvedAppSource::File { manifest_path, .. } => { // TODO: This should be configurable, see https://github.com/deislabs/containerd-wasm-shims/issues/166 let files_mount_strategy = FilesMountStrategy::Direct; - - // create a cache directory at /.cache - // this is needed for the spin LocalLoader to work - // TODO: spin should provide a more flexible `loader::from_file` that - // does not assume the existance of a cache directory - let cache_dir = PathBuf::from("/.cache"); - env::set_var("XDG_CACHE_HOME", &cache_dir); - - if !cache_dir.exists() { - tokio::fs::create_dir_all(&cache_dir) - .await - .with_context(|| format!("failed to create {:?}", cache_dir))?; - } - spin_loader::from_file(&manifest_path, files_mount_strategy).await } ResolvedAppSource::OciRegistry { locked_app } => Ok(locked_app), @@ -163,12 +229,12 @@ impl Engine for SpinEngine { "spin" } - fn run_wasi(&self, _ctx: &impl RuntimeContext, stdio: Stdio) -> Result { + fn run_wasi(&self, ctx: &impl RuntimeContext, stdio: Stdio) -> Result { info!("setting up wasi"); stdio.redirect()?; let rt = Runtime::new().context("failed to create runtime")?; - rt.block_on(self.wasm_exec_async())?; + rt.block_on(self.wasm_exec_async(ctx))?; Ok(0) } @@ -202,9 +268,11 @@ impl ResolvedAppSource { ResolvedAppSource::File { manifest, .. } => { manifest.triggers.keys().collect::>() } - ResolvedAppSource::OciRegistry { locked_app: _ } => { - todo!("OCI not yet supported") - } + ResolvedAppSource::OciRegistry { locked_app } => locked_app + .triggers + .iter() + .map(|t| &t.trigger_type) + .collect::>(), }; ensure!(!types.is_empty(), "no triggers in app");