Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wasi for v1 wasmrs components & helper macros. #384

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 99 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

40 changes: 15 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,9 @@ members = [
"crates/components/*",
"crates/bins/*",
"crates/wick/*",
"tests/*"
]
exclude = [
"crates/wick/wick-rpc/codegen",
"crates/integration/test-baseline-component",
"crates/integration/test-cli-trigger-component",
"crates/integration/test-cli-with-db",
"crates/integration/test-http-trigger-component",
"tests/template",
"tests/testdata",
"examples/http/middleware/request",
"examples/http/wasm-http-call/wasm-component",
"examples/cli/cli-maker",
"tests/*",
]
exclude = ["crates/wick/wick-rpc/codegen", "tests/template", "tests/testdata"]

[profile.release-with-debug]
inherits = "release"
Expand All @@ -45,6 +34,7 @@ wick-host = { path = "./crates/wick/wick-host", version = "0.4.0" }
wick-interface-types = { path = "./crates/wick/wick-interface-types", version = "0.15.0" }
wick-invocation-server = { path = "./crates/wick/wick-invocation-server", version = "0.1.0" }
wick-oci-utils = { path = "./crates/wick/wick-oci-utils", version = "0.3.0", default-features = false }
wick-operation = { path = "./crates/wick/wick-operation", version = "0.1.0", default-features = false }
wick-package = { path = "./crates/wick/wick-package", version = "0.2.0" }
wick-packet = { path = "./crates/wick/wick-packet", version = "0.15.0", default-features = false }
wick-settings = { path = "./crates/wick/wick-settings", version = "0.1.0" }
Expand Down Expand Up @@ -74,24 +64,24 @@ wick-interface-cli = { path = "./crates/interfaces/wick-interface-cli", version
wick-sql = { path = "./crates/components/wick-sql", version = "0.1.0" }
wick-http-client = { path = "./crates/components/wick-http-client", version = "0.1.0" }
# wasmtime (must align with version in wasmrs)
wasmtime = { version = "10.0.1", default-features = false, features = [
wasmtime = { version = "11.0", default-features = false, features = [
"cache",
"jitdump",
"parallel-compilation",
"cranelift",
"vtune"
"vtune",
] }
wasmtime-wasi = { version = "10.0.1", features = ["sync"] }
wasi-common = { version = "10.0.1" }
wasmtime-wasi = { version = "11.0", features = ["sync"] }
wasi-common = { version = "11.0" }
# wasmrs
wasmrs = { version = "0.13.0" }
wasmrs-codec = { version = "0.13.0" }
wasmrs-frames = { version = "0.13.0" }
wasmrs-guest = { version = "0.13.0" }
wasmrs-host = { version = "0.13.0" }
wasmrs-runtime = { version = "0.13.0" }
wasmrs-rx = { version = "0.13.0" }
wasmrs-wasmtime = { version = "0.13.0" }
wasmrs = { version = "0.14.0" }
wasmrs-codec = { version = "0.14.0" }
wasmrs-frames = { version = "0.14.0" }
wasmrs-guest = { version = "0.14.0" }
wasmrs-host = { version = "0.14.0" }
wasmrs-runtime = { version = "0.14.0" }
wasmrs-rx = { version = "0.14.0" }
wasmrs-wasmtime = { version = "0.14.0" }
#
# Other
#
Expand Down
22 changes: 21 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ rustup := `command -v rustup >/dev/null && echo true || echo false`
# use #!{{python}} to ensure that the python path is correct across platforms.
python := if os() == 'windows' { 'python' } else { '/usr/bin/env python3' }

# The wick repository
repository := "https://github.com/candlecorp/wick"

# The root directory of this project
wick_root := justfile_directory()

set dotenv-load
set positional-arguments
set export
Expand Down Expand Up @@ -70,7 +76,7 @@ wick-tests:
just _wick-db-tests

# Run tests suitable for CI
ci-tests: wasm
ci-tests: install-dev wasm
just unit-tests

# Run unit, integration, and slow tests
Expand Down Expand Up @@ -145,6 +151,18 @@ rustdoc:
mkdir -p docs/static/rustdoc/
rsync -av --delete --exclude=".*" target/doc/ docs/static/rustdoc/

# Quickly generate a new rust WebAssembly example
new-rust-example name path="examples/components":
echo "Generating new rust example: {{name}}"
echo "Note: This is optimized for macOS, if it breaks on your platform, please open an issue at {{repository}}"
cd {{path}} && cargo generate -p {{wick_root}}/templates/rust --name {{name}}

RELATIVE=$(realpath --relative-to={{wick_root}}/{{path}}/{{name}} {{wick_root}}); \
sed -E -i '' "s,([A-Za-z0-9_-]*) = { git = \\\"{{repository}}.git\\\",\\1 = { path = \\\"$RELATIVE/crates/wick/\\1\\\",g" "{{wick_root}}/{{path}}/{{name}}/Cargo.toml"; \
sed -E -i '' "s,wick := \\\"wick\\\",wick := \\\"cargo run --manifest-path=$RELATIVE/Cargo.toml -p wick-cli --\\\",g" "{{wick_root}}/{{path}}/{{name}}/justfile";

echo "New rust example generated at {{path}}/{{name}}"

##################################
### Private, dependency tasks. ###
##################################
Expand Down Expand Up @@ -191,6 +209,7 @@ _run-wasm-task task:
"crates/integration/test-cli-trigger-component",
"crates/integration/test-cli-with-db",
"examples/http/middleware/request",
"examples/components/wasi-fs",
"examples/http/wasm-http-call/wasm-component",
]
for dir in wasm:
Expand All @@ -209,6 +228,7 @@ _wick-http-tests:
_wick-component-tests:
cargo run -p wick-cli -- test ./examples/components/hello-world.wick
cargo run -p wick-cli -- test ./examples/components/composite-db-import.wick
DIR=./examples/components/wasi-fs/ cargo run -p wick-cli -- test ./examples/components/wasi-fs/component.wick

# Run component-codegen unit tests
_codegen-tests:
Expand Down
3 changes: 0 additions & 3 deletions crates/bins/wick/src/commands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ use crate::wick_host::build_component_host;
#[clap(rename_all = "kebab-case")]
#[group(skip)]
pub(crate) struct Options {
#[clap(flatten)]
wasi: crate::wasm::WasiOptions,

#[clap(flatten)]
pub(crate) oci: crate::oci::Options,

Expand Down
3 changes: 0 additions & 3 deletions crates/bins/wick/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ pub(crate) struct Options {
#[clap(flatten)]
pub(crate) oci: crate::oci::Options,

#[clap(flatten)]
wasi: crate::wasm::WasiOptions,

/// The path or OCI URL to a wick manifest or wasm file.
#[clap(action)]
pub(crate) location: String,
Expand Down
3 changes: 0 additions & 3 deletions crates/bins/wick/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub(crate) struct Options {
#[clap(flatten)]
pub(crate) oci: crate::oci::Options,

#[clap(flatten)]
wasi: crate::wasm::WasiOptions,

/// Turn on info logging.
#[clap(long = "info", action)]
pub(crate) info: bool,
Expand Down
2 changes: 1 addition & 1 deletion crates/bins/wick/src/commands/wasm/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) async fn handle(
source_file.read_to_end(&mut buf).unwrap();

let (account, subject) = get_module_keys(
Some(opts.source.clone()),
interface.name().map(|s| s.as_str()),
opts.common.directory,
opts.common.signer,
opts.common.subject,
Expand Down
16 changes: 8 additions & 8 deletions crates/bins/wick/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,39 @@ fn get_key_home() -> PathBuf {
}

pub(crate) async fn get_module_keys(
subject_file: Option<String>,
name: Option<&str>,
directory: Option<PathBuf>,
signer_key: Option<String>,
subject_key: Option<String>,
) -> Result<(KeyPair, KeyPair)> {
let account_keys = match signer_key {
Some(seed) => KeyPair::from_seed(&seed)?,
None => get_or_create(subject_file.clone(), directory.clone(), KeyPairType::Account).await?,
None => get_or_create(name, directory.clone(), KeyPairType::Account).await?,
};

let subject_keys = match subject_key {
Some(seed) => KeyPair::from_seed(&seed)?,
None => get_or_create(subject_file, directory, KeyPairType::Module).await?,
None => get_or_create(name, directory, KeyPairType::Module).await?,
};

Ok((account_keys, subject_keys))
}

pub(crate) async fn get_or_create(
subject: Option<String>,
name: Option<&str>,
directory: Option<PathBuf>,
kp_type: KeyPairType,
) -> Result<KeyPair> {
if subject.is_none() {
return Err(anyhow!("Keypair path or string not supplied."));
if name.is_none() {
return Err(anyhow!("Component name must be supplied to generate signing keys."));
}
let module = subject.unwrap();
let name = name.unwrap();

let dir = directory.map_or_else(get_key_home, |dir| dir);

let module_name = match kp_type {
KeyPairType::Account => std::env::var("USER").unwrap_or_else(|_| "user".to_owned()),
_ => PathBuf::from(module).file_stem().unwrap().to_str().unwrap().to_owned(),
_ => PathBuf::from(name).file_stem().unwrap().to_str().unwrap().to_owned(),
};

let path = format!(
Expand Down
17 changes: 0 additions & 17 deletions crates/bins/wick/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::str::FromStr;

use clap::Args;
use wick_config::config::components::Permissions;

#[derive(Clone, Debug)]
struct StringPair(String, String);

Expand All @@ -15,17 +12,3 @@ impl FromStr for StringPair {
.ok_or_else(|| anyhow!("WASI directories need to be string pairs split by a colon, e.g. /to/dir:/from/dir"))
}
}

#[derive(Debug, Clone, Args)]
pub(crate) struct WasiOptions {
/// Directories to expose to the WASM module via WASI. Ignored if loading a manifest.
#[clap(long = "dirs", value_parser)]
wasi_dir: Vec<StringPair>,
}

impl From<WasiOptions> for Permissions {
fn from(opts: WasiOptions) -> Self {
let dirs = opts.wasi_dir.into_iter().map(|v| (v.0, v.1)).collect();
Self::new(dirs)
}
}
1 change: 0 additions & 1 deletion crates/components/wick-http-client/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl HttpClientComponent {
) -> Result<Self, ComponentError> {
validate(&config, resolver)?;
let addr: UrlResource = resolver(config.resource())
.ok_or_else(|| ComponentError::message(&format!("Could not resolve resource ID {}", config.resource())))?
.and_then(|r| r.try_resource())
.map_err(ComponentError::new)?
.into();
Expand Down
5 changes: 1 addition & 4 deletions crates/components/wick-sql/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ pub(crate) fn gen_signature(
}

pub(crate) fn convert_url_resource(resolver: &Resolver, id: &str) -> Result<Url> {
let addr = resolver(id)
.ok_or_else(|| Error::ResourceNotFound(id.to_owned()))?
.and_then(|r| r.try_resource())
.map_err(Error::InvalidResource)?;
let addr = resolver(id).and_then(|r| r.try_resource())?;

let resource: UrlResource = addr.into();
resource.url().value().cloned().ok_or(Error::InvalidResourceConfig)
Expand Down
7 changes: 2 additions & 5 deletions crates/components/wick-sql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ pub enum Error {
#[error(transparent)]
ComponentError(wick_packet::Error),

#[error("Could not resolve resource: {0}")]
ResourceNotFound(String),

#[error("Database connection not initialized")]
Uninitialized,

#[error("Invalid resource passed to component: {0}")]
InvalidResource(ManifestError),
#[error(transparent)]
Configuration(#[from] ManifestError),

#[error("Resource valid but its value could not be retrieved")]
InvalidResourceConfig,
Expand Down
5 changes: 4 additions & 1 deletion crates/integration/test-baseline-component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ wick-component = { path = "../../wick/wick-component", features = ["datetime"] }
serde = { version = "1", features = ["derive"] }
async-trait = "0.1"
seeded-random = { path = "../../misc/seeded-random", default-features = false, features = [
"uuid"
"uuid",
] }

[build-dependencies]
wick-component-codegen = { path = "../../wick/wick-component-codegen" }

# Including this section keeps this crate out of a workspace without needing to alter the workspace.
[workspace]
18 changes: 9 additions & 9 deletions crates/integration/test-baseline-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wick::*;

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl AddOperation for Component {
impl add::Operation for Component {
type Error = String;
type Outputs = add::Outputs;
type Config = add::Config;
Expand All @@ -34,7 +34,7 @@ impl AddOperation for Component {
}
(Err(err), _) | (_, Err(err)) => {
println!("op:add: received error, propagating forward");
outputs.output.error(format!("Error adding numbers: {}", err));
outputs.output.error(&format!("Error adding numbers: {}", err));
}
}
}
Expand All @@ -46,7 +46,7 @@ impl AddOperation for Component {

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl PowerOperation for Component {
impl power::Operation for Component {
type Error = String;
type Outputs = power::Outputs;
type Config = power::Config;
Expand All @@ -68,7 +68,7 @@ impl PowerOperation for Component {

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl ErrorOperation for Component {
impl error::Operation for Component {
type Error = String;
type Outputs = error::Outputs;
type Config = error::Config;
Expand All @@ -92,7 +92,7 @@ impl ErrorOperation for Component {

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl UuidOperation for Component {
impl uuid::Operation for Component {
type Error = String;
type Outputs = uuid::Outputs;
type Config = uuid::Config;
Expand All @@ -108,7 +108,7 @@ impl UuidOperation for Component {

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl ValidateOperation for Component {
impl validate::Operation for Component {
type Error = String;
type Outputs = validate::Outputs;
type Config = validate::Config;
Expand All @@ -123,10 +123,10 @@ impl ValidateOperation for Component {

if password.len() < MINIMUM_LENGTH {
println!("Too short!");
outputs.output.error(LengthError::TooShort.to_string());
outputs.output.error(&LengthError::TooShort.to_string());
} else if password.len() > MAXIMUM_LENGTH {
println!("Too long!!");
outputs.output.error(LengthError::TooLong.to_string());
outputs.output.error(&LengthError::TooLong.to_string());
} else {
println!("Just right!");
outputs.output.send(&password);
Expand Down Expand Up @@ -167,7 +167,7 @@ static MAXIMUM_LENGTH: usize = 512;

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait(Send))]
impl StrftimeOperation for Component {
impl strftime::Operation for Component {
type Error = String;
type Outputs = strftime::Outputs;
type Config = strftime::Config;
Expand Down
3 changes: 3 additions & 0 deletions crates/integration/test-cli-trigger-component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ anyhow = "1.0"

[build-dependencies]
wick-component-codegen = { path = "../../wick/wick-component-codegen" }

# Including this section keeps this crate out of a workspace without needing to alter the workspace.
[workspace]
2 changes: 1 addition & 1 deletion crates/integration/test-cli-trigger-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wick::*;

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl MainOperation for Component {
impl main::Operation for Component {
type Error = anyhow::Error;
type Outputs = main::Outputs;
type Config = main::Config;
Expand Down
3 changes: 3 additions & 0 deletions crates/integration/test-cli-with-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ console = "0.15"

[build-dependencies]
wick-component-codegen = { path = "../../wick/wick-component-codegen" }

# Including this section keeps this crate out of a workspace without needing to alter the workspace.
[workspace]
2 changes: 1 addition & 1 deletion crates/integration/test-cli-with-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wick::*;

#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl MainOperation for Component {
impl main::Operation for Component {
type Error = Box<dyn std::error::Error>;
type Outputs = main::Outputs;
type Config = main::Config;
Expand Down
Loading