Skip to content

Commit

Permalink
feat(cargo-shuttle): beta archive deployment separation (#1814)
Browse files Browse the repository at this point in the history
* feat: deployment version id

* feat: archive response

* nits

* nit: plural uri
  • Loading branch information
jonaro00 authored Jul 1, 2024
1 parent 90c51de commit d60c310
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
23 changes: 18 additions & 5 deletions cargo-shuttle/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use reqwest::{RequestBuilder, Response};
use serde::{Deserialize, Serialize};
use shuttle_common::constants::headers::X_CARGO_SHUTTLE_VERSION;
use shuttle_common::log::{LogsRange, LogsResponseBeta};
use shuttle_common::models::deployment::{DeploymentRequest, DeploymentRequestBeta};
use shuttle_common::models::deployment::{
DeploymentRequest, DeploymentRequestBeta, UploadArchiveResponseBeta,
};
use shuttle_common::models::team;
use shuttle_common::models::{deployment, project, service, ToJson};
use shuttle_common::{resource, ApiKey, LogItem, VersionInfo};
Expand Down Expand Up @@ -111,18 +113,29 @@ impl ShuttleApiClient {
deployment_req: DeploymentRequestBeta,
) -> Result<deployment::ResponseBeta> {
let path = format!("/projects/{project}/deployments");
let deployment_req = rmp_serde::to_vec(&deployment_req)
.context("serialize DeploymentRequest as a MessagePack byte vector")?;
self.post(path, Some(deployment_req))
.await
.context("failed to start deployment")?
.to_json()
.await
}

pub async fn upload_archive_beta(
&self,
project: &str,
data: Vec<u8>,
) -> Result<UploadArchiveResponseBeta> {
let path = format!("/projects/{project}/deployments/archives");

let url = format!("{}{}", self.api_url, path);
let mut builder = self.client.post(url);
builder = self.set_auth_bearer(builder);

builder
.body(deployment_req)
.body(data)
.send()
.await
.context("failed to send deployment to the Shuttle server")?
.context("failed to upload archive")?
.to_json()
.await
}
Expand Down
10 changes: 7 additions & 3 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ impl Shuttle {
let package_name = package.name.to_owned();
build_args.package_name = Some(package_name);

// activate shuttle feature is present
// activate shuttle feature if present
let (no_default_features, features) = if package.features.contains_key("shuttle") {
(true, Some(vec!["shuttle".to_owned()]))
} else {
Expand Down Expand Up @@ -1893,7 +1893,8 @@ impl Shuttle {
}

deployment_req.data = self.make_archive(args.secret_args.secrets.clone(), self.beta)?;
if deployment_req.data.len() > CREATE_SERVICE_BODY_LIMIT {
// TODO: Make size warning server-side on beta.
if !self.beta && deployment_req.data.len() > CREATE_SERVICE_BODY_LIMIT {
bail!(
r#"The project is too large - the limit is {} MB. \
Your project archive is {:.1} MB. \
Expand All @@ -1905,7 +1906,10 @@ impl Shuttle {

// End early for beta
if self.beta {
deployment_req_buildarch_beta.data = deployment_req.data;
let arch = client
.upload_archive_beta(self.ctx.project_name(), deployment_req.data)
.await?;
deployment_req_buildarch_beta.archive_version_id = arch.archive_version_id;
deployment_req_buildarch_beta.build_meta = Some(BuildMetaBeta {
git_commit_id: deployment_req.git_commit_id,
git_commit_msg: deployment_req.git_commit_msg,
Expand Down
27 changes: 8 additions & 19 deletions common/src/models/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ pub fn get_deployments_table(
}
}

#[derive(Deserialize, Serialize)]
pub struct UploadArchiveResponseBeta {
/// The S3 object version ID of the uploaded object
pub archive_version_id: String,
}

#[derive(Default, Deserialize, Serialize)]
pub struct DeploymentRequest {
/// Tar archive
Expand All @@ -296,8 +302,8 @@ pub enum DeploymentRequestBeta {

#[derive(Default, Deserialize, Serialize)]
pub struct DeploymentRequestBuildArchiveBeta {
/// Zip archive
pub data: Vec<u8>,
/// The S3 object version ID of the archive to use
pub archive_version_id: String,
pub build_args: Option<BuildArgsBeta>,
/// Secrets to add before this deployment.
/// TODO: Remove this in favour of a separate secrets uploading action.
Expand Down Expand Up @@ -337,23 +343,6 @@ impl Default for BuildArgsBeta {
}
}

impl BuildArgsBeta {
pub fn into_vars(&self) -> [(&str, &str); 7] {
[
("CARGO_CHEF", if self.cargo_chef { "true" } else { "" }),
("CARGO_BUILD", if self.cargo_build { "true" } else { "" }),
("PACKAGE", self.package_name.as_deref().unwrap_or_default()),
("BIN", self.binary_name.as_deref().unwrap_or_default()),
("FEATURES", self.features.as_deref().unwrap_or_default()),
(
"NO_DEFAULT_FEATURES",
if self.no_default_features { "true" } else { "" },
),
("MOLD", if self.mold { "true" } else { "" }),
]
}
}

#[derive(Default, Deserialize, Serialize)]
pub struct BuildMetaBeta {
pub git_commit_id: Option<String>,
Expand Down
10 changes: 4 additions & 6 deletions common/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ impl ToJson for reqwest::Response {
trace!("parsing response to common error");
let res: error::ApiError = match serde_json::from_slice(&full) {
Ok(res) => res,
_ => {
error::ApiError {
message: "Did not understand the response from the server. Please log a ticket for this".to_string(),
status_code: status_code.as_u16(),
}
}
_ => error::ApiError {
message: "Failed to parse response from the server.".to_string(),
status_code: status_code.as_u16(),
},
};

Err(res.into())
Expand Down

0 comments on commit d60c310

Please sign in to comment.