Skip to content

Commit

Permalink
feat(runtime): beta runtime as client (#1844)
Browse files Browse the repository at this point in the history
* feat(runtime): beta runtime code skeleton

* feat(runtime): call provision endpoint

* feat: api-client tracing feature

* wip: runtime health checks and arg passing

* feat: runtime parse env vars

* fix: lock

* nit

* feat: runtime poll resources

* feat: skip provision request for secrets

* feat: beta runtime args struct
  • Loading branch information
jonaro00 authored Aug 2, 2024
1 parent 91c189a commit f8170be
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 294 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repository = "https://github.com/shuttle-hq/shuttle"

# https://doc.rust-lang.org/cargo/reference/workspaces.html#the-workspacedependencies-table
[workspace.dependencies]
shuttle-api-client = { path = "api-client", version = "0.47.0" }
shuttle-api-client = { path = "api-client", version = "0.47.0", default-features = false }
shuttle-backends = { path = "backends", version = "0.47.0" }
shuttle-codegen = { path = "codegen", version = "0.47.0" }
shuttle-common = { path = "common", version = "0.47.0" }
Expand Down
2 changes: 1 addition & 1 deletion admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
shuttle-api-client = { workspace = true }
shuttle-api-client = { workspace = true, default-features = true }
shuttle-common = { workspace = true, features = ["models"] }
shuttle-backends = { workspace = true }

Expand Down
9 changes: 7 additions & 2 deletions api-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ reqwest-middleware = "0.2.5"
rmp-serde = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
task-local-extensions = "0.1.4"
tokio = { workspace = true, features = ["macros", "signal", "rt-multi-thread"] }
tokio-tungstenite = { workspace = true }
tracing = { workspace = true, features = ["default"] }
url = { workspace = true }
uuid = { workspace = true, features = ["v4"] }

task-local-extensions = { version = "0.1.4", optional = true }
tracing = { workspace = true, features = ["default"], optional = true }

[features]
default = ["tracing"]
tracing = ["dep:tracing", "dep:task-local-extensions"]
34 changes: 28 additions & 6 deletions api-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod middleware;

use std::time::Duration;

use anyhow::{Context, Result};
Expand All @@ -9,19 +7,25 @@ use reqwest::header::HeaderMap;
use reqwest::Response;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use shuttle_common::log::{LogsRange, LogsResponseBeta};
use shuttle_common::models::deployment::{
DeploymentRequest, DeploymentRequestBeta, UploadArchiveResponseBeta,
};
use shuttle_common::models::{deployment, project, service, team, user, ToJson};
use shuttle_common::resource::{ProvisionResourceRequest, ShuttleResourceOutput};
use shuttle_common::{resource, LogItem, VersionInfo};
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
use tracing::{debug, error};
use uuid::Uuid;

#[cfg(feature = "tracing")]
mod middleware;
#[cfg(feature = "tracing")]
use crate::middleware::LoggingMiddleware;
#[cfg(feature = "tracing")]
use tracing::{debug, error};

#[derive(Clone)]
pub struct ShuttleApiClient {
Expand All @@ -37,9 +41,12 @@ impl ShuttleApiClient {
builder = builder.default_headers(h);
}
let client = builder.timeout(Duration::from_secs(60)).build().unwrap();
let client = reqwest_middleware::ClientBuilder::new(client)
.with(LoggingMiddleware)
.build();

let builder = reqwest_middleware::ClientBuilder::new(client);
#[cfg(feature = "tracing")]
let builder = builder.with(LoggingMiddleware);
let client = builder.build();

Self {
client,
api_url,
Expand Down Expand Up @@ -208,6 +215,18 @@ impl ShuttleApiClient {
self.delete_json(format!("/projects/{project}/resources/{}", r#type))
.await
}
pub async fn provision_resource_beta(
&self,
project: &str,
req: ProvisionResourceRequest,
) -> Result<ShuttleResourceOutput<Value>> {
self.post_json(format!("/projects/{project}/resources"), Some(req))
.await
}
pub async fn get_secrets_beta(&self, project: &str) -> Result<resource::Response> {
self.get_json(format!("/projects/{project}/resources/secrets"))
.await
}

pub async fn create_project(
&self,
Expand Down Expand Up @@ -411,6 +430,7 @@ impl ShuttleApiClient {
}

let (stream, _) = connect_async(request).await.with_context(|| {
#[cfg(feature = "tracing")]
error!("failed to connect to websocket");
"could not connect to websocket"
})?;
Expand Down Expand Up @@ -446,6 +466,7 @@ impl ShuttleApiClient {

if let Some(body) = body {
let body = serde_json::to_string(&body)?;
#[cfg(feature = "tracing")]
debug!("Outgoing body: {}", body);
builder = builder.body(body);
builder = builder.header("Content-Type", "application/json");
Expand Down Expand Up @@ -477,6 +498,7 @@ impl ShuttleApiClient {

if let Some(body) = body {
let body = serde_json::to_string(&body)?;
#[cfg(feature = "tracing")]
debug!("Outgoing body: {}", body);
builder = builder.body(body);
builder = builder.header("Content-Type", "application/json");
Expand Down
1 change: 0 additions & 1 deletion backends/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod headers;
pub mod metrics;
mod otlp_tracing_bridge;
pub mod project_name;
pub mod resource;
pub mod trace;

#[cfg(any(test, feature = "test-utils"))]
Expand Down
8 changes: 0 additions & 8 deletions backends/src/resource.rs

This file was deleted.

2 changes: 1 addition & 1 deletion cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "A cargo command for the Shuttle platform (https://www.shuttle.rs/
homepage = "https://www.shuttle.rs"

[dependencies]
shuttle-api-client = { workspace = true }
shuttle-api-client = { workspace = true, default-features = true }
shuttle-common = { workspace = true, features = ["models"] }
shuttle-proto = { workspace = true, features = ["provisioner", "runtime-client"] }
shuttle-service = { workspace = true, features = ["builder", "runner"] }
Expand Down
1 change: 0 additions & 1 deletion cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,6 @@ impl Shuttle {
let (mut runtime, mut runtime_client) = runner::start(
beta,
port,
SocketAddr::new(Ipv4Addr::LOCALHOST.into(), port),
runtime_executable,
service.workspace_path.as_path(),
)
Expand Down
14 changes: 4 additions & 10 deletions deployer/src/runtime_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::HashMap,
net::{Ipv4Addr, SocketAddr},
path::{Path, PathBuf},
sync::Arc,
};
Expand Down Expand Up @@ -58,15 +57,10 @@ impl RuntimeManager {
.unwrap_or_default()
);

let (mut process, runtime_client) = runner::start(
false,
port,
SocketAddr::new(Ipv4Addr::LOCALHOST.into(), port),
runtime_executable,
project_path,
)
.await
.context("failed to start shuttle runtime")?;
let (mut process, runtime_client) =
runner::start(false, port, runtime_executable, project_path)
.await
.context("failed to start shuttle runtime")?;

let stdout = process
.stdout
Expand Down
5 changes: 4 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Runtime to start and manage any service that runs on shuttle"
doctest = false

[dependencies]
shuttle-api-client = { workspace = true }
shuttle-codegen = { workspace = true }
shuttle-common = { workspace = true, features = ["extract_propagation"] }
shuttle-proto = { workspace = true, features = ["runtime"] }
Expand All @@ -18,6 +19,7 @@ shuttle-service = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
colored = { workspace = true, optional = true }
hyper = { workspace = true, features = ["http1", "server", "tcp"] }
serde = { workspace = true }
serde_json = { workspace = true }
strfmt = { workspace = true }
Expand All @@ -35,9 +37,10 @@ uuid = { workspace = true }

[features]
default = ["setup-tracing"]
api-client-tracing = ["shuttle-api-client/tracing"]

setup-tracing = [
"tracing-subscriber/default",
"tracing-subscriber/env-filter",
"colored",
"dep:colored",
]
Loading

0 comments on commit f8170be

Please sign in to comment.