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

Factors trigger #2693

Merged
merged 8 commits into from
Aug 1, 2024
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
76 changes: 75 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl App {
}

/// Returns the trigger metadata for a specific trigger type.
pub fn get_trigger_metadata<'this, T: Deserialize<'this> + Default>(
pub fn get_trigger_metadata<'this, T: Deserialize<'this>>(
&'this self,
trigger_type: &str,
) -> Result<Option<T>> {
Expand Down Expand Up @@ -140,6 +140,20 @@ impl App {
.filter(move |trigger| trigger.locked.trigger_type == trigger_type)
}

/// Returns an iterator of trigger IDs and deserialized trigger configs for
/// the given `trigger_type`.
pub fn trigger_configs<'a, T: Deserialize<'a>>(
&'a self,
trigger_type: &'a str,
) -> Result<impl IntoIterator<Item = (&'a str, T)>> {
self.triggers_with_type(trigger_type)
.map(|trigger| {
let config = trigger.typed_config::<T>()?;
Ok((trigger.id(), config))
})
.collect::<Result<Vec<_>>>()
}

/// Checks that the application does not have any host requirements
/// outside the supported set. The error case returns a comma-separated
/// list of unmet requirements.
Expand Down Expand Up @@ -215,12 +229,12 @@ pub struct AppTrigger<'a> {

impl<'a> AppTrigger<'a> {
/// Returns this trigger's app-unique ID.
pub fn id(&self) -> &str {
pub fn id(&self) -> &'a str {
&self.locked.id
}

/// Returns the Trigger's type.
pub fn trigger_type(&self) -> &str {
pub fn trigger_type(&self) -> &'a str {
&self.locked.trigger_type
}

Expand Down
10 changes: 10 additions & 0 deletions crates/core/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ impl<T> Store<T> {
};
self.inner.set_epoch_deadline(ticks);
}

/// Provides access to the inner [`wasmtime::Store`]'s data.
pub fn data(&self) -> &T {
self.inner.data()
}

/// Provides access to the inner [`wasmtime::Store`]'s data.
pub fn data_mut(&mut self) -> &mut T {
self.inner.data_mut()
}
}

impl<T> AsRef<wasmtime::Store<T>> for Store<T> {
Expand Down
3 changes: 1 addition & 2 deletions crates/core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ async fn run_test(
let app = App::new("test-app", locked);
let configured_app = factors.configure_app(app, Default::default())?;
let mut builders = factors.prepare(&configured_app, "test-component")?;
// FIXME: it is unfortunate that we have to unwrap here...
builders.wasi.as_mut().unwrap().args(args);
builders.wasi().args(args);
let instance_state = factors.build_instance_state(builders)?;
let state = TestState {
core: State::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/factor-outbound-http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod spin;
mod wasi;
mod wasi_2023_10_18;
mod wasi_2023_11_10;
pub mod wasi_2023_10_18;
pub mod wasi_2023_11_10;

use spin_factor_outbound_networking::{OutboundAllowedHosts, OutboundNetworkingFactor};
use spin_factors::{
Expand Down
13 changes: 12 additions & 1 deletion crates/factor-outbound-http/src/wasi_2023_10_18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ mod bindings {
interfaces: r#"
include wasi:http/proxy@0.2.0-rc-2023-10-18;
"#,
async: {
// Only need async exports
only_imports: [],
},
with: {
"wasi:io/poll/pollable": latest::io::poll::Pollable,
"wasi:io/streams/input-stream": latest::io::streams::InputStream,
Expand All @@ -41,6 +45,12 @@ mod wasi {
pub use super::bindings::wasi::{http0_2_0_rc_2023_10_18 as http, io0_2_0_rc_2023_10_18 as io};
}

pub mod exports {
pub mod wasi {
pub use super::super::bindings::exports::wasi::http0_2_0_rc_2023_10_18 as http;
}
}

use wasi::http::types::{
Error as HttpError, Fields, FutureIncomingResponse, FutureTrailers, Headers, IncomingBody,
IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest, OutgoingResponse,
Expand All @@ -51,8 +61,9 @@ use wasi::io::streams::{InputStream, OutputStream};

use crate::wasi::WasiHttpImplInner;

pub fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
pub(crate) fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
where
T: Send,
F: Fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner> + Send + Sync + Copy + 'static,
{
wasi::http::types::add_to_linker_get_host(linker, closure)?;
Expand Down
13 changes: 12 additions & 1 deletion crates/factor-outbound-http/src/wasi_2023_11_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ mod bindings {
interfaces: r#"
include wasi:http/proxy@0.2.0-rc-2023-11-10;
"#,
async: {
// Only need async exports
only_imports: [],
},
with: {
"wasi:io/poll/pollable": latest::io::poll::Pollable,
"wasi:io/streams/input-stream": latest::io::streams::InputStream,
Expand All @@ -45,6 +49,12 @@ mod wasi {
pub use super::bindings::wasi::{http0_2_0_rc_2023_11_10 as http, io0_2_0_rc_2023_11_10 as io};
}

pub mod exports {
pub mod wasi {
pub use super::super::bindings::exports::wasi::http0_2_0_rc_2023_11_10 as http;
}
}

use wasi::http::types::{
DnsErrorPayload, ErrorCode as HttpErrorCode, FieldSizePayload, Fields, FutureIncomingResponse,
FutureTrailers, HeaderError, Headers, IncomingBody, IncomingRequest, IncomingResponse, Method,
Expand All @@ -56,8 +66,9 @@ use wasi::io::streams::{Error as IoError, InputStream, OutputStream};

use crate::wasi::WasiHttpImplInner;

pub fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
pub(crate) fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
where
T: Send,
F: Fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner> + Send + Sync + Copy + 'static,
{
wasi::http::types::add_to_linker_get_host(linker, closure)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/factor-variables/src/spin_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::runtime_config::RuntimeConfig;
/// Resolves a runtime configuration for the variables factor from a TOML table.
pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result<RuntimeConfig> {
// Always include the environment variable provider.
let mut providers = vec![Box::new(EnvVariablesProvider::default()) as _];
let mut providers = vec![Box::<EnvVariablesProvider>::default() as _];
let Some(array) = table.get("variable_provider") else {
return Ok(RuntimeConfig { providers });
};
Expand Down
1 change: 1 addition & 0 deletions crates/factor-wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = { workspace = true }
[dependencies]
async-trait = "0.1"
cap-primitives = "3.0.0"
spin-common = { path = "../common" }
spin-factors = { path = "../factors" }
tokio = { version = "1" }
wasmtime = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/factor-wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod spin;
mod wasi_2023_10_18;
mod wasi_2023_11_10;

Expand Down Expand Up @@ -122,7 +123,7 @@ impl Factor for WasiFactor {
}
}

pub trait FilesMounter {
pub trait FilesMounter: Send + Sync {
fn mount_files(
&self,
app_component: &AppComponent,
Expand Down
48 changes: 48 additions & 0 deletions crates/factor-wasi/src/spin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::path::PathBuf;

use spin_common::{ui::quoted_path, url::parse_file_url};
use spin_factors::anyhow::{ensure, Context};

use crate::FilesMounter;

pub struct SpinFilesMounter {
working_dir: PathBuf,
allow_transient_writes: bool,
}

impl SpinFilesMounter {
pub fn new(working_dir: impl Into<PathBuf>, allow_transient_writes: bool) -> Self {
Self {
working_dir: working_dir.into(),
allow_transient_writes,
}
}
}

impl FilesMounter for SpinFilesMounter {
fn mount_files(
&self,
app_component: &spin_factors::AppComponent,
mut ctx: crate::MountFilesContext,
) -> spin_factors::anyhow::Result<()> {
for content_dir in app_component.files() {
let source_uri = content_dir
.content
.source
.as_deref()
.with_context(|| format!("Missing 'source' on files mount {content_dir:?}"))?;
let source_path = self.working_dir.join(parse_file_url(source_uri)?);
ensure!(
source_path.is_dir(),
"SpinFilesMounter only supports directory mounts; {} is not a directory",
quoted_path(&source_path),
);
let guest_path = &content_dir.path;
let guest_path = guest_path
.to_str()
.with_context(|| format!("guest path {guest_path:?} not valid UTF-8"))?;
ctx.preopened_dir(source_path, guest_path, self.allow_transient_writes)?;
}
Ok(())
}
}
11 changes: 10 additions & 1 deletion crates/factors-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {

#vis struct #builders_name {
#(
pub #factor_names: Option<<#factor_types as #Factor>::InstanceBuilder>,
#factor_names: Option<<#factor_types as #Factor>::InstanceBuilder>,
)*
}

#[allow(dead_code)]
impl #builders_name {
#(
pub fn #factor_names(&mut self) -> &mut <#factor_types as #Factor>::InstanceBuilder {
self.#factor_names.as_mut().unwrap()
}
)*
}

Expand Down
Loading
Loading