-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create the RuntimeMetadata and Repository structs. Refactor Run…
…times (#72) * feat: create the RuntimeMetadata and Repository structs. Refactor Runtimes * feat: merge metadata files into RemoteFile. Add tests
- Loading branch information
1 parent
67ac7f0
commit d59d0f9
Showing
14 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::{remote_file::RemoteFile, runtime::RuntimeStatus}; | ||
use anyhow::{anyhow, Result}; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
/// Metadata associated to a Runtime. It contains information | ||
/// about a certain runtime like name, version and all the | ||
/// details to run workers with it. | ||
/// | ||
/// A runtime is a Wasm binary + configuration that can run | ||
/// a source code as a worker. The configuration includes | ||
/// different pieces like polyfills files, templates, | ||
/// arguments, etc. | ||
#[derive(Deserialize)] | ||
pub struct RuntimeMetadata<'a> { | ||
/// Name of the runtime (like ruby, python, etc) | ||
name: &'a str, | ||
/// Specific version of the runtime | ||
version: &'a str, | ||
/// Current status in the repository | ||
status: RuntimeStatus, | ||
/// Associated extensions | ||
extensions: Vec<&'a str>, | ||
/// Arguments to pass to the Wasm module via WASI | ||
args: Vec<&'a str>, | ||
/// A list of environment variables that must be configured | ||
/// for the runtime to work. | ||
envs: Option<HashMap<&'a str, &'a str>>, | ||
/// The reference to a remote binary (url + checksum) | ||
binary: RemoteFile<'a>, | ||
/// The reference to a remote polyfill file (url + checksum) | ||
polyfill: Option<RemoteFile<'a>>, | ||
/// The refernmece to a template file for the worker. It will wrap the | ||
/// source code into a template that can include imports, | ||
/// function calls, etc. | ||
template: Option<RemoteFile<'a>>, | ||
} | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
impl<'a> RuntimeMetadata<'a> { | ||
/// Reads and parses the metadata from a slice of bytes. It will return | ||
/// a result as the deserialization may fail. | ||
pub fn from_slice(data: &'a [u8]) -> Result<Self> { | ||
toml::from_slice::<RuntimeMetadata>(data) | ||
.map_err(|_| anyhow!("wws could not deserialize the runtime metadata")) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::runtimes::remote_file::Checksum; | ||
|
||
use super::*; | ||
use std::{any::Any, fs}; | ||
|
||
#[test] | ||
fn parse_runtime_toml() { | ||
let contents = fs::read("tests/data/metadata/runtime.toml").unwrap(); | ||
let metadata = RuntimeMetadata::from_slice(&contents).unwrap(); | ||
|
||
assert_eq!(metadata.name, "ruby"); | ||
assert_eq!(metadata.version, "3.2.0+20230118-8aec06d"); | ||
assert_eq!(metadata.status.type_id(), RuntimeStatus::Active.type_id()); | ||
assert_eq!(metadata.binary.url, "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/ruby%2F3.2.0%2B20230118-8aec06d/ruby-3.2.0.wasm"); | ||
|
||
let Checksum::Sha256 { value } = metadata.binary.checksum; | ||
assert_eq!( | ||
value, | ||
"e2d91cff05ec59ed9c88aadbd3b477842092054bf24c5d944d5ad6dbafdd3b32" | ||
); | ||
|
||
// Optionals | ||
let polyfill = metadata.polyfill.unwrap(); | ||
assert_eq!( | ||
polyfill.url, | ||
"https://raw.githubusercontent.com/Angelmmiguel/wws-index-test/main/ruby/poly.rb" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod metadata; | ||
mod modules; | ||
mod remote_file; | ||
mod repository; | ||
pub mod runtime; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/workers/runtimes/native.rs → src/runtimes/modules/native.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use serde::Deserialize; | ||
|
||
/// A file represents a combination of both a remote URL and | ||
/// a checksum. It will provide utils to download and validate | ||
/// the file. | ||
/// | ||
/// If the checksum is not present, the validation will return | ||
/// always an Ok result. | ||
/// | ||
#[derive(Deserialize)] | ||
pub struct RemoteFile<'a> { | ||
/// URL pointing to the file | ||
pub url: &'a str, | ||
/// Checksum to validate the given file | ||
pub checksum: Checksum<'a>, | ||
} | ||
|
||
/// A list of available checksums. For now, | ||
/// we will support only sha256 | ||
#[derive(Deserialize)] | ||
#[serde(rename_all = "lowercase", tag = "type")] | ||
pub enum Checksum<'a> { | ||
Sha256 { value: &'a str }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::metadata::RuntimeMetadata; | ||
use anyhow::{anyhow, Result}; | ||
use serde::Deserialize; | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
/// A Repository contains the list of runtimes available on it. | ||
/// This file is used by wws to properly show the list of available | ||
/// repos and install them. | ||
/// | ||
/// By default, this repository class rely on the [WebAssembly Language Runtimes](https://github.com/vmware-labs/webassembly-language-runtimes) | ||
/// repository. It looks for a repository.toml file in the Git repo. | ||
#[derive(Deserialize)] | ||
pub struct Repository<'a> { | ||
/// Version of the repository file | ||
pub version: u32, | ||
/// The list of runtimes available in the repository | ||
#[serde(borrow)] | ||
pub runtimes: Vec<RuntimeMetadata<'a>>, | ||
} | ||
|
||
// TODO: Remove it when implementing the manager | ||
#[allow(dead_code)] | ||
impl<'a> Repository<'a> { | ||
/// Reads and parses the metadata from a slice of bytes. It will return | ||
/// a result as the deserialization may fail. | ||
pub fn from_slice(data: &'a [u8]) -> Result<Self> { | ||
toml::from_slice::<Repository>(data) | ||
.map_err(|_| anyhow!("wws could not deserialize the repository metadata")) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
|
||
#[test] | ||
fn parse_index_toml() { | ||
let contents = fs::read("tests/data/metadata/repository.toml").unwrap(); | ||
let repo = Repository::from_slice(&contents).unwrap(); | ||
|
||
assert_eq!(repo.version, 1); | ||
assert_eq!(repo.runtimes.len(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version = 1 | ||
|
||
[[runtimes]] | ||
name = "ruby" | ||
version = "3.2.0+20230118-8aec06d" | ||
status = "active" | ||
args = [ "--", "/src/index.rb" ] | ||
binary = { url = "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/ruby%2F3.2.0%2B20230118-8aec06d/ruby-3.2.0.wasm", checksum = { type = "sha256", value = "e2d91cff05ec59ed9c88aadbd3b477842092054bf24c5d944d5ad6dbafdd3b32" } } | ||
extensions = [ "rb" ] | ||
polyfill = { url = "https://raw.githubusercontent.com/Angelmmiguel/wws-index-test/main/ruby/poly.rb", checksum = { type = "sha256", value = "2ba09117ed20a05480615b2aaaf6c7cd7f61fa06f4919777d773df2fcbc736cf" } } | ||
template = { url = "https://raw.githubusercontent.com/Angelmmiguel/wws-index-test/main/ruby/template.txt", checksum = { type = "sha256", value = "6d808b4747cf30f82665a38a47e1176513bbdd6ad558c09db03d719e33ad2da0" } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name = "ruby" | ||
version = "3.2.0+20230118-8aec06d" | ||
status = "active" | ||
args = [ "--", "/src/index.rb" ] | ||
binary = { url = "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/ruby%2F3.2.0%2B20230118-8aec06d/ruby-3.2.0.wasm", checksum = { type = "sha256", value = "e2d91cff05ec59ed9c88aadbd3b477842092054bf24c5d944d5ad6dbafdd3b32" } } | ||
extensions = [ "rb" ] | ||
polyfill = { url = "https://raw.githubusercontent.com/Angelmmiguel/wws-index-test/main/ruby/poly.rb", checksum = { type = "sha256", value = "2ba09117ed20a05480615b2aaaf6c7cd7f61fa06f4919777d773df2fcbc736cf" } } | ||
template = { url = "https://raw.githubusercontent.com/Angelmmiguel/wws-index-test/main/ruby/template.txt", checksum = { type = "sha256", value = "6d808b4747cf30f82665a38a47e1176513bbdd6ad558c09db03d719e33ad2da0" } } |