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

Implement wasmer run {url} #3295

Merged
merged 22 commits into from
Nov 20, 2022
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
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
#[cfg(not(test))]
use dialoguer::Input;

/// Subcommand for listing packages
Expand Down
35 changes: 35 additions & 0 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
use url::Url;
use wasmer::FunctionEnv;
use wasmer::*;
#[cfg(feature = "cache")]
Expand Down Expand Up @@ -827,6 +828,11 @@ pub(crate) fn try_run_package_or_file(
) -> Result<(), anyhow::Error> {
let debug_msgs_allowed = isatty::stdout_isatty();

if let Ok(url) = url::Url::parse(&format!("{}", r.path.display())) {
let result = try_run_url(&url, args, r, debug);
return result;
}

// Check "r.path" is a file or a package / command name
if r.path.exists() {
if r.path.is_dir() && r.path.join("wapm.toml").exists() {
Expand Down Expand Up @@ -908,3 +914,32 @@ pub(crate) fn try_run_package_or_file(
// else: local package not found - try to download and install package
try_autoinstall_package(args, &sv, package_download_info, r.force_install)
}

fn try_run_url(url: &Url, _args: &[String], r: &Run, _debug: bool) -> Result<(), anyhow::Error> {
let checksum = wasmer_registry::get_remote_webc_checksum(url)
.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;

let packages = wasmer_registry::get_all_installed_webc_packages();

if !packages.iter().any(|p| p.checksum == checksum) {
let sp = start_spinner(format!("Installing {}", url));

let result = wasmer_registry::install_webc_package(url, &checksum);

result.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;

if let Some(sp) = sp {
sp.close();
}
}

let webc_dir = wasmer_registry::get_webc_dir();

let webc_install_path = webc_dir
.context("Error installing package: no webc dir")?
.join(checksum);

let mut r = r.clone();
r.path = webc_install_path;
r.execute()
}
6 changes: 5 additions & 1 deletion lib/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dirs = "4.0.0"
graphql_client = "0.11.0"
serde = { version = "1.0.145", features = ["derive"] }
anyhow = "1.0.65"
reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "blocking", "multipart", "json"] }
reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "blocking", "multipart", "json", "stream"] }
futures-util = "0.3.25"
whoami = "1.2.3"
serde_json = "1.0.85"
url = "2.3.1"
Expand All @@ -24,5 +25,8 @@ tar = "0.4.38"
flate2 = "1.0.24"
semver = "1.0.14"
lzma-rs = "0.2.0"
webc = { version ="3.0.1", features = ["mmap"] }
hex = "0.4.3"
tokio = "1.21.2"
tempdir = "0.3.7"
log = "0.4.17"
42 changes: 33 additions & 9 deletions lib/registry/src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::Duration;
#[cfg(target_os = "wasi")]
use {wasm_bus_reqwest::prelude::header::*, wasm_bus_reqwest::prelude::*};

mod proxy {
pub(crate) mod proxy {
//! Code for dealing with setting things up to proxy network requests
use thiserror::Error;

Expand All @@ -25,6 +25,36 @@ mod proxy {
ConnectionError(String),
}

pub fn maybe_set_up_proxy_blocking(
builder: reqwest::blocking::ClientBuilder,
) -> anyhow::Result<reqwest::blocking::ClientBuilder> {
#[cfg(not(target_os = "wasi"))]
use anyhow::Context;
#[cfg(not(target_os = "wasi"))]
if let Some(proxy) = maybe_set_up_proxy_inner()
.map_err(|e| anyhow::anyhow!("{e}"))
.context("install_webc_package: failed to setup proxy for reqwest Client")?
{
return Ok(builder.proxy(proxy));
}
Ok(builder)
}

pub fn maybe_set_up_proxy(
builder: reqwest::ClientBuilder,
) -> anyhow::Result<reqwest::ClientBuilder> {
#[cfg(not(target_os = "wasi"))]
use anyhow::Context;
#[cfg(not(target_os = "wasi"))]
if let Some(proxy) = maybe_set_up_proxy_inner()
.map_err(|e| anyhow::anyhow!("{e}"))
.context("install_webc_package: failed to setup proxy for reqwest Client")?
{
return Ok(builder.proxy(proxy));
}
Ok(builder)
}

/// Tries to set up a proxy
///
/// This function reads from wapm config's `proxy.url` first, then checks
Expand All @@ -37,7 +67,7 @@ mod proxy {
/// A return value of `Ok(None)` means that there was no attempt to set up a proxy,
/// `Ok(Some(proxy))` means that the proxy was set up successfully, and `Err(e)` that
/// there was a failure while attempting to set up the proxy.
pub fn maybe_set_up_proxy() -> anyhow::Result<Option<reqwest::Proxy>> {
fn maybe_set_up_proxy_inner() -> anyhow::Result<Option<reqwest::Proxy>> {
use std::env;
let proxy = if let Ok(proxy_url) = env::var("ALL_PROXY").or_else(|_| env::var("all_proxy"))
{
Expand Down Expand Up @@ -120,13 +150,7 @@ pub fn whoami_distro() -> String {

fn setup_client() -> Result<Client, anyhow::Error> {
let builder = Client::builder();

let builder = if let Some(proxy) = proxy::maybe_set_up_proxy()? {
builder.proxy(proxy)
} else {
builder
};

let builder = proxy::maybe_set_up_proxy_blocking(builder)?;
builder.build().map_err(|e| e.into())
}

Expand Down
Loading