Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
feat: add integration with drawbridge
Browse files Browse the repository at this point in the history
- Rewrite much of the frontend scripting code.
- Change some messaging around file limits.
- Create a more interactive experience preparing workloads.
- Add handling code for drawbridge, including pulling a Enarx.toml preview.
- Add server code for pulling Enarx.toml if the frontend fails to do it.
- Add some initial examples (some of which are broken at the moment).
- Change the "Deploy workload" positioning, size, and color.

Signed-off-by: Nicholas Farshidmehr <[email protected]>
  • Loading branch information
definitelynobody committed Aug 2, 2022
1 parent aecf0c8 commit ae678ea
Show file tree
Hide file tree
Showing 10 changed files with 571 additions and 107 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ in
axum = rustPackages."registry+https://github.com/rust-lang/crates.io-index".axum."0.5.13" { inherit profileName; };
clap = rustPackages."registry+https://github.com/rust-lang/crates.io-index".clap."3.2.14" { inherit profileName; };
enarx_config = rustPackages."registry+https://github.com/rust-lang/crates.io-index".enarx-config."0.6.1" { inherit profileName; };
humansize = rustPackages."registry+https://github.com/rust-lang/crates.io-index".humansize."1.1.1" { inherit profileName; };
lazy_static = rustPackages."registry+https://github.com/rust-lang/crates.io-index".lazy_static."1.4.0" { inherit profileName; };
num_cpus = rustPackages."registry+https://github.com/rust-lang/crates.io-index".num_cpus."1.13.1" { inherit profileName; };
once_cell = rustPackages."registry+https://github.com/rust-lang/crates.io-index".once_cell."1.13.0" { inherit profileName; };
openidconnect = rustPackages."registry+https://github.com/rust-lang/crates.io-index".openidconnect."2.3.2" { inherit profileName; };
Expand Down Expand Up @@ -741,6 +743,13 @@ in
src = fetchCratesIo { inherit name version; sha256 = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"; };
});

"registry+https://github.com/rust-lang/crates.io-index".humansize."1.1.1" = overridableMkRustCrate (profileName: rec {
name = "humansize";
version = "1.1.1";
registry = "registry+https://github.com/rust-lang/crates.io-index";
src = fetchCratesIo { inherit name version; sha256 = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026"; };
});

"registry+https://github.com/rust-lang/crates.io-index".hyper."0.14.20" = overridableMkRustCrate (profileName: rec {
name = "hyper";
version = "0.14.20";
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ num_cpus = "1.13.1"
serde = { version = "1.0.136", default-features = false }
serde_json = { version = "1.0.82", default-features = false }
enarx-config = { version = "0.6.1", default-features = false }

humansize = { version = "1.1.1", default-features = false }
lazy_static = { version = "1.4.0", default-features = false }
10 changes: 10 additions & 0 deletions examples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
examples/cryptle-rust:0.1.0
examples/echo-tcp-rust-mio:0.1.0
examples/echo-tcp-rust-tokio:0.1.0
examples/fibonacci-c:0.2.0
examples/fibonacci-cpp:0.2.0
examples/fibonacci-go:0.2.0
examples/fibonacci-rust:0.2.0
examples/fibonacci-zig:0.3.0
examples/greenhouse-monitor-csharp:0.1.0
examples/http-rust-tokio:0.1.0
7 changes: 1 addition & 6 deletions src/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ use axum::async_trait;
use axum::extract::{FromRequest, RequestParts};
use axum::response::Response;
use once_cell::sync::Lazy;
use reqwest::{Client, ClientBuilder};
use serde::Deserialize;
use tokio::sync::RwLock;
use tokio::time::sleep;
use tracing::error;

use crate::reference::Ref;
use crate::HTTP;

use super::Session;

static HTTP: Lazy<Client> = Lazy::new(|| {
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
ClientBuilder::new().user_agent(USER_AGENT).build().unwrap()
});

const STAR_TIMEOUT: Duration = Duration::from_secs(6 * 60 * 60);
static STAR: Lazy<RwLock<HashMap<(usize, &'static str), bool>>> =
Lazy::new(|| HashMap::new().into());
Expand Down
98 changes: 83 additions & 15 deletions src/jobs.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
use std::process::Stdio;
use std::str::FromStr;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};

use anyhow::anyhow;
use tempfile::NamedTempFile;
use tokio::io::AsyncReadExt;
use tokio::process::{Child, Command};
use tracing::{debug, error};
use uuid::Uuid;

static COUNT: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug)]
pub enum WorkloadType {
Drawbridge,
Browser,
}

impl FromStr for WorkloadType {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"drawbridge" => WorkloadType::Drawbridge,
"browser" => WorkloadType::Browser,
_ => return Err(anyhow!("Unknown workload type {s}")),
})
}
}

pub enum Standard {
Output,
Error,
Expand All @@ -19,8 +43,10 @@ pub enum Standard {
pub struct Job {
pub uuid: Uuid,
exec: Child,
wasm: NamedTempFile,
toml: NamedTempFile,
workload_type: WorkloadType,
slug: Option<String>,
wasm: Option<NamedTempFile>,
toml: Option<NamedTempFile>,
}

impl Drop for Job {
Expand All @@ -34,24 +60,66 @@ impl Job {
COUNT.load(Ordering::SeqCst)
}

pub fn new(cmd: String, wasm: NamedTempFile, toml: NamedTempFile) -> std::io::Result<Self> {
let uuid = Uuid::new_v4();
let exec = Command::new(cmd)
.arg("run")
.arg("--wasmcfgfile")
.arg(toml.path())
.arg(wasm.path())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()?;
pub fn new(
cmd: String,
workload_type: String,
slug: Option<String>,
wasm: Option<NamedTempFile>,
toml: Option<NamedTempFile>,
) -> Result<Self, Response> {
let workload_type = WorkloadType::from_str(&workload_type).map_err(|e| {
debug!("Failed to parse workload type: {e}");
StatusCode::BAD_REQUEST.into_response()
})?;
let exec = match workload_type {
WorkloadType::Drawbridge => {
let slug = slug
.as_ref()
.ok_or_else(|| StatusCode::BAD_REQUEST.into_response())?;
Command::new(cmd)
.arg("deploy")
.arg(slug)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.map_err(|e| {
error!("failed to spawn process: {e}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
})?
}
WorkloadType::Browser => {
let wasm = wasm
.as_ref()
.ok_or_else(|| StatusCode::BAD_REQUEST.into_response())?;
let toml = toml
.as_ref()
.ok_or_else(|| StatusCode::BAD_REQUEST.into_response())?;
Command::new(cmd)
.arg("run")
.arg("--wasmcfgfile")
.arg(toml.path())
.arg(wasm.path())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.map_err(|e| {
error!("failed to spawn process: {e}");
StatusCode::INTERNAL_SERVER_ERROR.into_response()
})?
}
};

COUNT.fetch_add(1, Ordering::SeqCst);

Ok(Self {
uuid,
uuid: Uuid::new_v4(),
exec,
workload_type,
slug,
wasm,
toml,
})
Expand Down
Loading

0 comments on commit ae678ea

Please sign in to comment.