Skip to content

Commit

Permalink
cargo-shuttle: update deploy cmd for beta platform (#1734)
Browse files Browse the repository at this point in the history
* common: updated deployment models

* cargo-shuttle: update deploy cmd

* common: add more information in a deployment response

* cargo-shuttle: add project uri in deploy output

* fix: Jocelyn's comments on fronted output

* cargo-shuttle: when deploying use the beta field on the Shuttle type

* cargo-shuttle: split beta/alpha deploy functions in client

* cargo-shuttle: addressed left over of alpha deployments table change

* cargo-shuttle: remove pub for beta flag on client

* cargo-shuttle: remove leftover comment

* address Jocelyn review comments
iulianbarbu authored Apr 16, 2024
1 parent cd7b71c commit d3f6c6b
Showing 5 changed files with 452 additions and 388 deletions.
711 changes: 336 additions & 375 deletions Cargo.lock

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions cargo-shuttle/src/client.rs
Original file line number Diff line number Diff line change
@@ -5,8 +5,7 @@ use anyhow::{Context, Result};
use headers::{Authorization, HeaderMapExt};
use percent_encoding::utf8_percent_encode;
use reqwest::header::HeaderMap;
use reqwest::RequestBuilder;
use reqwest::Response;
use reqwest::{RequestBuilder, Response};
use serde::{Deserialize, Serialize};
use shuttle_common::constants::headers::X_CARGO_SHUTTLE_VERSION;
use shuttle_common::models::deployment::DeploymentRequest;
@@ -89,20 +88,35 @@ impl ShuttleApiClient {
project: &str,
deployment_req: DeploymentRequest,
) -> Result<deployment::Response> {
let path = if self.beta {
format!("/projects/{project}")
} else {
format!("/projects/{project}/services/{project}")
};
let path = format!("/projects/{project}/services/{project}");
let deployment_req = rmp_serde::to_vec(&deployment_req)
.context("serialize DeploymentRequest as a MessagePack byte vector")?;

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

builder
.header("Transfer-Encoding", "chunked")
.body(deployment_req)
.send()
.await
.context("failed to send deployment to the Shuttle server")?
.to_json()
.await
}

pub async fn deploy_beta(
&self,
project: &str,
deployment_req: DeploymentRequest,
) -> Result<deployment::EcsResponse> {
let path = format!("/projects/{project}");
let deployment_req = rmp_serde::to_vec(&deployment_req)
.context("serialize DeploymentRequest as a MessagePack byte vector")?;

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

builder
11 changes: 11 additions & 0 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1701,6 +1701,17 @@ impl Shuttle {
);
}

// End this early for beta platform.
if self.beta {
let deployment = client
.deploy_beta(self.ctx.project_name(), deployment_req)
.await
.map_err(suggestions::deploy::deploy_request_failure)?;

deployment.colored_println();
return Ok(CommandOutcome::Ok);
}

let deployment = client
.deploy(self.ctx.project_name(), deployment_req)
.await
11 changes: 11 additions & 0 deletions common/src/deployment.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,17 @@ pub enum State {
Unknown,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Display, Serialize, EnumString)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
#[strum(ascii_case_insensitive)]
pub enum EcsState {
Running,
#[strum(serialize = "in progress")]
InProgress,
Stopped,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentMetadata {
pub env: Environment,
69 changes: 68 additions & 1 deletion common/src/models/deployment.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
use uuid::Uuid;

use crate::deployment::State;
use crate::deployment::{EcsState, State};

/// Max length of strings in the git metadata
pub const GIT_STRINGS_MAX_LENGTH: usize = 80;
@@ -29,6 +29,19 @@ pub struct Response {
pub git_dirty: Option<bool>,
}

#[derive(Deserialize, Serialize)]
pub struct EcsResponse {
pub id: String,
pub latest_deployment_state: EcsState,
pub running_id: Option<String>,
pub updated_at: DateTime<Utc>,
pub uri: String,
pub git_commit_id: Option<String>,
pub git_commit_msg: Option<String>,
pub git_branch: Option<String>,
pub git_dirty: Option<bool>,
}

impl Display for Response {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
@@ -47,6 +60,47 @@ impl Display for Response {
}
}

impl EcsResponse {
pub fn colored_println(&self) {
let running_deployment = self
.running_id
.as_ref()
.map(|id| {
format!(
"\nRunning deployment: '{}' - {} ({})",
id,
"running".to_string().with(
crossterm::style::Color::from_str(EcsState::Running.get_color()).unwrap()
),
self.uri
)
})
.unwrap_or(String::new());

// Stringify the state.
let latest_state = format!(
"{}",
self.latest_deployment_state
.to_string()
// Unwrap is safe because Color::from_str returns the color white if the argument is not a Color.
.with(
crossterm::style::Color::from_str(self.latest_deployment_state.get_color())
.unwrap()
)
);

let state_with_uri = match self.running_id {
None => format!("{latest_state} ({})", self.uri),
Some(_) => latest_state,
};

println!(
"Current deployment: '{}' - {}{running_deployment}",
self.id, state_with_uri
)
}
}

impl State {
/// We return a &str rather than a Color here, since `comfy-table` re-exports
/// crossterm::style::Color and we depend on both `comfy-table` and `crossterm`
@@ -62,6 +116,19 @@ impl State {
}
}

impl EcsState {
/// We return a &str rather than a Color here, since `comfy-table` re-exports
/// crossterm::style::Color and we depend on both `comfy-table` and `crossterm`
/// we may end up with two different versions of Color.
pub fn get_color(&self) -> &str {
match self {
EcsState::InProgress => "cyan",
EcsState::Running => "green",
EcsState::Stopped => "blue",
}
}
}

pub fn get_deployments_table(
deployments: &Vec<Response>,
service_name: &str,

0 comments on commit d3f6c6b

Please sign in to comment.