Skip to content

Commit

Permalink
refactor: find code to wrap legacy runtime (#675)
Browse files Browse the repository at this point in the history
* refactor: find code to wrap legacy runtime

* misc: update README with test instructions
  • Loading branch information
chesedo authored Mar 3, 2023
1 parent 3dc3ac7 commit 91a9fdd
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 178 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

9 changes: 1 addition & 8 deletions proto/runtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ message LoadResponse {
message StartRequest {
// Id to associate with the deployment being started
bytes deployment_id = 1;
// Name of service to start
string service_name = 2;
// Address and port to start the service on
string ip = 3;
}
Expand All @@ -47,12 +45,7 @@ message StartResponse {
bool success = 1;
}

message StopRequest {
// Id to associate with the deployment being stopped
bytes deployment_id = 1;
// Name of service to stop
string service_name = 2;
}
message StopRequest {}

message StopResponse {
// Was the stop successful
Expand Down
1 change: 0 additions & 1 deletion resources/secrets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ keywords = ["shuttle-service", "secrets"]
[dependencies]
async-trait = "0.1.56"
shuttle-service = { path = "../../service", version = "0.8.0", default-features = false }
tokio = { version = "1.19.2", features = ["rt"] }
7 changes: 1 addition & 6 deletions resources/secrets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::BTreeMap;
use async_trait::async_trait;

use shuttle_service::{Error, Factory, ResourceBuilder};
use tokio::runtime::Runtime;

pub struct Secrets;

Expand All @@ -14,11 +13,7 @@ impl ResourceBuilder<SecretStore> for Secrets {
Self {}
}

async fn build(
self,
factory: &mut dyn Factory,
_runtime: &Runtime,
) -> Result<SecretStore, Error> {
async fn build(self, factory: &mut dyn Factory) -> Result<SecretStore, Error> {
let secrets = factory.get_secrets().await?;

Ok(SecretStore { secrets })
Expand Down
10 changes: 9 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ license.workspace = true
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "rocket"

[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
Expand All @@ -25,6 +28,11 @@ wasmtime = "4.0.0"
wasmtime-wasi = "4.0.0"
futures = "0.3.25"

# For rocket.rs
# TODO: remove
shuttle-secrets = { path = "../resources/secrets" }
rocket = "0.5.0-rc.2"

[dependencies.shuttle-common]
workspace = true
features = ["wasm"]
Expand All @@ -34,4 +42,4 @@ workspace = true

[dependencies.shuttle-service]
workspace = true
features = ["loader"]
features = ["loader", "web-rocket"] # TODO: remove web-rocket
19 changes: 9 additions & 10 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,43 @@ curl localhost:7002/goodbye
```

## shuttle-legacy

Load and run an .so library that implements `shuttle_service::Service`.
This will no loger load a `.so` will the code to start the runtime will be codegened for all services.
An example can be found in `src/bin/rocket.rs` which contains the secrets rocket example at the bottom and the codegen at the top.

To test, first start a provisioner from the root directory using:

```bash
docker-compose -f docker-compose.rendered.yml up provisioner
```

Then in another shell, start the runtime using the clap CLI:
Then in another shell, start the wrapped runtime using the clap CLI:

```bash
cargo run -- --legacy --provisioner-address http://localhost:5000
cargo run -- --port 6001 --storage-manager-type working-dir --storage-manager-path ./
```

Or directly (this is the path hardcoded in `deployer::start`):
```bash
# first, make sure the shuttle-runtime binary is built
cargo build
# then
/home/<path to shuttle repo>/target/debug/shuttle-runtime --legacy --provisioner-address http://localhost:5000
/home/<path to shuttle repo>/target/debug/shuttle-runtime --port 6001 --storage-manager-type working-dir --storage-manager-path ./
```

Pass the path to `deployer::start`
Then in another shell, load a `.so` file and start it up:
Then in another shell, load the service and start it up:

``` bash
# load
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "path": "/home/<path to shuttle>/examples/rocket/hello-world/target/debug/libhello_world.so"}' localhost:6001 runtime.Runtime/Load
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "path": "/home/<path to shuttle>/examples/rocket/hello-world/target/debug/libhello_world.so", "secrets": {"MY_API_KEY": "test"}}' localhost:6001 runtime.Runtime/Load

# run (this deployment id is default uuid encoded as base64)
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "deployment_id": "MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw"}' localhost:6001 runtime.Runtime/Start
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic", "deployment_id": "MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw", "ip": "127.0.0.1:8000"}' localhost:6001 runtime.Runtime/Start

# subscribe to logs
grpcurl -plaintext -import-path ../proto -proto runtime.proto localhost:6001 runtime.Runtime/SubscribeLogs

# stop (the service started in the legacy runtime can't currently be stopped)
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{"service_name": "Tonic"}' localhost:6001 runtime.Runtime/Stop
grpcurl -plaintext -import-path ../proto -proto runtime.proto -d '{}' localhost:6001 runtime.Runtime/Stop
```

## Running the tests
Expand Down
11 changes: 2 additions & 9 deletions runtime/src/axum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use shuttle_proto::runtime::{
self, LoadRequest, LoadResponse, StartRequest, StartResponse, StopRequest, StopResponse,
SubscribeLogsRequest,
};
use shuttle_service::ServiceName;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::{mpsc, oneshot};
use tokio_stream::wrappers::ReceiverStream;
Expand Down Expand Up @@ -149,18 +148,12 @@ impl Runtime for AxumWasm {
&self,
request: tonic::Request<StopRequest>,
) -> Result<tonic::Response<StopResponse>, Status> {
let request = request.into_inner();

let service_name = ServiceName::from_str(request.service_name.as_str())
.map_err(|err| Status::from_error(Box::new(err)))?;
let _request = request.into_inner();

let kill_tx = self.kill_tx.lock().unwrap().deref_mut().take();

if let Some(kill_tx) = kill_tx {
if kill_tx
.send(format!("stopping deployment: {}", &service_name))
.is_err()
{
if kill_tx.send(format!("stopping deployment")).is_err() {
error!("the receiver dropped");
return Err(Status::internal("failed to stop deployment"));
}
Expand Down
50 changes: 50 additions & 0 deletions runtime/src/bin/rocket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// The few line below is what we should now codegen for legacy
#[tokio::main]
async fn main() {
shuttle_runtime::start(loader).await;
}

async fn loader<S: shuttle_common::storage_manager::StorageManager>(
mut factory: shuttle_runtime::ProvisionerFactory<S>,
) -> shuttle_service::ShuttleRocket {
use shuttle_service::ResourceBuilder;

let secrets = shuttle_secrets::Secrets::new().build(&mut factory).await?;

rocket(secrets).await
}

// Everything below this is the usual code a user will write
use anyhow::anyhow;
use rocket::response::status::BadRequest;
use rocket::State;
use shuttle_secrets::SecretStore;

#[rocket::get("/secret")]
async fn secret(state: &State<MyState>) -> Result<String, BadRequest<String>> {
Ok(state.secret.clone())
}

struct MyState {
secret: String,
}

// #[shuttle_service::main]
pub async fn rocket(
// #[shuttle_secrets::Secrets] secret_store: SecretStore,
secret_store: SecretStore,
) -> shuttle_service::ShuttleRocket {
// get secret defined in `Secrets.toml` file.
let secret = if let Some(secret) = secret_store.get("MY_API_KEY") {
secret
} else {
return Err(anyhow!("secret was not found").into());
};

let state = MyState { secret };
let rocket = rocket::build()
.mount("/", rocket::routes![secret])
.manage(state);

Ok(rocket)
}
14 changes: 0 additions & 14 deletions runtime/src/legacy/error.rs

This file was deleted.

Loading

0 comments on commit 91a9fdd

Please sign in to comment.