Skip to content

Commit

Permalink
refactor: combine runtimes into one (#438)
Browse files Browse the repository at this point in the history
* feat: add --legacy flag

* refactor: merge next and legacy runtimes

* refactor: merge proto crates

* refactor: rename to runtime

* refactor: promote runtime

* refactor: update README.md

* refactor: cargo sort
  • Loading branch information
chesedo authored Oct 27, 2022
1 parent 37ade4c commit da46e60
Show file tree
Hide file tree
Showing 29 changed files with 985 additions and 185 deletions.
896 changes: 886 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"gateway",
"proto",
"provisioner",
"runtime",
"service"
]
exclude = [
Expand All @@ -16,5 +17,5 @@ exclude = [
"resources/persist",
"resources/secrets",
"resources/shared-db",
"runtimes"
"tmp"
]
2 changes: 1 addition & 1 deletion deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tar = "0.4.38"
thiserror = "1.0.24"
tokio = { version = "1.19.2", features = ["fs"] }
toml = "0.5.9"
tonic = "0.8.0"
tonic = "0.8.2"
tower = { version = "0.4.12", features = ["make"] }
tower-http = { version = "0.3.4", features = ["auth", "trace"] }
tracing = "0.1.35"
Expand Down
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
prost = "0.11.0"
tonic = "0.8.0"
tonic = "0.8.2"

[dependencies.shuttle-common]
version = "0.7.0"
Expand Down
5 changes: 4 additions & 1 deletion proto/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("../proto/provisioner.proto")?;
tonic_build::configure().compile(
&["../proto/provisioner.proto", "../proto/runtime.proto"],
&["../proto"],
)?;

Ok(())
}
File renamed without changes.
4 changes: 4 additions & 0 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ pub mod provisioner {
}
}
}

pub mod runtime {
tonic::include_proto!("runtime");
}
2 changes: 1 addition & 1 deletion provisioner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rand = "0.8.5"
sqlx = { version = "0.6.1", features = ["postgres", "runtime-tokio-native-tls"] }
thiserror = "1.0.32"
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
tonic = "0.8.0"
tonic = "0.8.2"
tracing = "0.1.36"
tracing-subscriber = "0.3.15"

Expand Down
17 changes: 11 additions & 6 deletions runtimes/legacy/Cargo.toml → runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "shuttle-legacy"
name = "shuttle-runtime"
version = "0.1.0"
edition = "2021"
publish = false
Expand All @@ -8,23 +8,28 @@ publish = false
[dependencies]
anyhow = "1.0.62"
async-trait = "0.1.58"
cap-std = "0.26.0"
clap ={ version = "4.0.18", features = ["derive"] }
serenity = { version = "0.11.5", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
thiserror = "1.0.37"
tokio = { version = "=1.20.1", features = ["full"] }
tonic = "0.8.0"
tonic = "0.8.2"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
wasi-common = "2.0.0"
wasmtime = "2.0.0"
wasmtime-wasi = "2.0.0"

[dependencies.shuttle-common]
version = "0.7.0"
path = "../../common"
path = "../common"

[dependencies.shuttle-runtime-proto]
version = "0.1.0"
[dependencies.shuttle-proto]
version = "0.7.0"
path = "../proto"

[dependencies.shuttle-service]
version = "0.7.0"
default-features = false
features = ["loader"]
path = "../../service"
path = "../service"
13 changes: 13 additions & 0 deletions runtime/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: wasm next

all: wasm next

wasm:
cd ../tmp/wasm; cargo build --target wasm32-wasi
cp ../tmp/wasm/target/wasm32-wasi/debug/shuttle_serenity.wasm bot.wasm

test: wasm
cargo test -- --nocapture

runtime:
cargo build
34 changes: 34 additions & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## How to run

```bash
$ make wasm
$ DISCORD_TOKEN=xxx cargo run
```

In another terminal:

``` bash
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "path": "runtime/bot.wasm"}' localhost:8000 runtime.Runtime/load
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic"}' localhost:8000 runtime.Runtime/start
```
## shuttle-legacy

Load and run an .so library that implements `shuttle_service::Service`.

To test, first start this binary using:

```bash
cargo run -- --legacy
```

Then in another shell, load a `.so` file and start it up:

``` bash
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "path": "examples/rocket/hello-world/target/debug/libhello_world.so"}' localhost:8000 runtime.Runtime/load
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic"}' localhost:8000 runtime.Runtime/start
```

## Running the tests
```bash
$ cd ..; make test
```
4 changes: 4 additions & 0 deletions runtimes/legacy/src/args.rs → runtime/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ pub struct Args {
/// Address to reach provisioner at
#[clap(long, default_value = "localhost:5000")]
pub provisioner_address: Endpoint,

/// Is this runtime for a legacy service
#[clap(long)]
pub legacy: bool,
}
File renamed without changes.
5 changes: 2 additions & 3 deletions runtimes/legacy/src/lib.rs → runtime/src/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use anyhow::anyhow;
use async_trait::async_trait;
use shuttle_common::{database, LogItem};
use shuttle_runtime_proto::runtime::{
use shuttle_proto::runtime::{
runtime_server::Runtime, LoadRequest, LoadResponse, StartRequest, StartResponse,
};
use shuttle_service::{
Expand All @@ -20,8 +20,7 @@ use tokio::sync::mpsc::{self, UnboundedReceiver};
use tonic::{Request, Response, Status};
use tracing::{info, instrument, trace};

pub mod args;
pub mod error;
mod error;

pub struct Legacy {
// Mutexes are for interior mutability
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod args;
mod legacy;
mod next;

pub use args::Args;
pub use legacy::Legacy;
pub use next::Next;
21 changes: 13 additions & 8 deletions runtimes/legacy/src/main.rs → runtime/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::net::{Ipv4Addr, SocketAddr};

use clap::Parser;
use shuttle_legacy::{args::Args, Legacy};
use shuttle_runtime_proto::runtime::runtime_server::RuntimeServer;
use shuttle_proto::runtime::runtime_server::RuntimeServer;
use shuttle_runtime::{Args, Legacy, Next};
use tonic::transport::Server;
use tracing::trace;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
Expand All @@ -24,10 +24,15 @@ async fn main() {
trace!(args = ?args, "parsed args");

let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8000);
let legacy = Legacy::new();
Server::builder()
.add_service(RuntimeServer::new(legacy))
.serve(addr)
.await
.unwrap();
let router = if args.legacy {
let legacy = Legacy::new();
let svc = RuntimeServer::new(legacy);
Server::builder().add_service(svc)
} else {
let next = Next::new();
let svc = RuntimeServer::new(next);
Server::builder().add_service(svc)
};

router.serve(addr).await.unwrap();
}
6 changes: 2 additions & 4 deletions runtimes/next/src/lib.rs → runtime/src/next/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod args;

use std::env;
use std::fs::File;
use std::io::{Read, Write};
Expand All @@ -10,8 +8,8 @@ use std::sync::Arc;
use async_trait::async_trait;
use cap_std::os::unix::net::UnixStream;
use serenity::{model::prelude::*, prelude::*};
use shuttle_runtime_proto::runtime::runtime_server::Runtime;
use shuttle_runtime_proto::runtime::{LoadRequest, LoadResponse, StartRequest, StartResponse};
use shuttle_proto::runtime::runtime_server::Runtime;
use shuttle_proto::runtime::{LoadRequest, LoadResponse, StartRequest, StartResponse};
use tonic::{Request, Response, Status};
use tracing::trace;
use wasi_common::file::FileCaps;
Expand Down
7 changes: 0 additions & 7 deletions runtimes/Cargo.toml

This file was deleted.

16 changes: 0 additions & 16 deletions runtimes/Makefile

This file was deleted.

16 changes: 0 additions & 16 deletions runtimes/legacy/README.md

This file was deleted.

28 changes: 0 additions & 28 deletions runtimes/next/Cargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions runtimes/next/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions runtimes/next/src/args.rs

This file was deleted.

33 changes: 0 additions & 33 deletions runtimes/next/src/main.rs

This file was deleted.

12 changes: 0 additions & 12 deletions runtimes/proto/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions runtimes/proto/build.rs

This file was deleted.

3 changes: 0 additions & 3 deletions runtimes/proto/src/lib.rs

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit da46e60

Please sign in to comment.