Skip to content

Commit

Permalink
chore: refactor cloud methods (#2045)
Browse files Browse the repository at this point in the history
* chore: remove auth for health service

* chore: refactor commands

* chore: fix clippy

* chore: use deployment status instead

* chore: fix clippy
  • Loading branch information
v3g42 authored Sep 18, 2023
1 parent bfa302e commit e098ce0
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 223 deletions.
2 changes: 1 addition & 1 deletion dozer-api/src/grpc/client_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl ApiServer {
} else {
unauthenticated_reflection_service = Some(reflection_service);
};
let health_service = auth_middleware.layer(health_service);
let health_service = health_service;

let mut auth_service = None;
let security = get_api_security(self.security.to_owned());
Expand Down
15 changes: 3 additions & 12 deletions dozer-cli/src/cli/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ pub enum CloudCommands {
},
/// List all dozer application in Dozer Cloud
List(ListCommandArgs),
/// Dozer API server management
#[command(subcommand)]
Api(ApiCommand),
/// Dozer app secrets management
#[command(subcommand)]
Secrets(SecretsCommand),
Expand All @@ -75,6 +72,9 @@ pub struct DeployCommandArgs {

#[arg(long = "allow-incompatible")]
pub allow_incompatible: bool,

#[arg(long = "follow")]
pub follow: bool,
}

pub fn default_num_api_instances() -> i32 {
Expand Down Expand Up @@ -144,15 +144,6 @@ pub enum VersionCommand {
},
}

#[derive(Debug, Clone, Subcommand)]
pub enum ApiCommand {
/// Sets the number of replicas to serve Dozer APIs
SetNumApiInstances {
/// The number of replicas to set
num_api_instances: i32,
},
}

#[derive(Debug, Clone, Subcommand)]
pub enum SecretsCommand {
/// Creates new secret
Expand Down
2 changes: 0 additions & 2 deletions dozer-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ pub mod cloud_app_context;
mod cloud_helper;
pub mod config_helper;
pub mod console_helper;
#[cfg(feature = "cloud")]
mod progress_printer;
#[cfg(test)]
mod tests;
mod utils;
Expand Down
1 change: 0 additions & 1 deletion dozer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ fn run() -> Result<(), OrchestrationError> {

match cloud.command.clone() {
CloudCommands::Deploy(deploy) => dozer.deploy(cloud, deploy, cli.config_paths),
CloudCommands::Api(api) => dozer.api(cloud, api),
CloudCommands::Login {
organisation_slug,
profile_name,
Expand Down
115 changes: 61 additions & 54 deletions dozer-cli/src/simple/cloud/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
use crate::errors::CloudError;

use crate::simple::cloud::progress_printer::ProgressPrinter;
use crate::simple::token_layer::TokenLayer;
use dozer_types::grpc_types::cloud::dozer_cloud_client::DozerCloudClient;
use dozer_types::grpc_types::cloud::DeploymentStatus;
use dozer_types::grpc_types::cloud::GetDeploymentStatusRequest;

use crate::errors::CloudError::GRPCCallError;
use dozer_types::grpc_types::cloud::{Secret, StopRequest, StopResponse};
use dozer_types::log::{error, info};

use crate::cloud_app_context::CloudAppContext;
use crate::progress_printer::ProgressPrinter;
use dozer_types::grpc_types::cloud::AppResponse;
use dozer_types::grpc_types::cloud::DeployAppRequest;
use dozer_types::grpc_types::cloud::DeployAppResponse;
use dozer_types::grpc_types::cloud::DeployStep;
use dozer_types::grpc_types::cloud::File;
use dozer_types::grpc_types::cloud::{Secret, StopRequest, StopResponse};
use dozer_types::log::info;

pub async fn deploy_app(
client: &mut DozerCloudClient<TokenLayer>,
app_id: &Option<String>,
secrets: Vec<Secret>,
allow_incompatible: bool,
files: Vec<File>,
follow: bool,
) -> Result<(), CloudError> {
let mut response = client
let response = client
.deploy_application(DeployAppRequest {
app_id: app_id.clone(),
secrets,
Expand All @@ -32,55 +29,65 @@ pub async fn deploy_app(
.await?
.into_inner();

let app_id = response.app_id;
let deployment_id = response.deployment_id;
let url = response.deployment_url;
info!("Deploying new application with App Id: {app_id}, Deployment Id: {deployment_id}");
info!("Follow the deployment progress at {url}");

if follow {
print_progress(client, app_id, deployment_id).await?;
}

Ok::<(), CloudError>(())
}

async fn print_progress(
client: &mut DozerCloudClient<TokenLayer>,
app_id: String,
deployment_id: String,
) -> Result<(), CloudError> {
let mut current_step = 0;
let mut printer = ProgressPrinter::new();
while let Some(DeployAppResponse {
result,
completed: _,
error: error_message,
}) = response.message().await.map_err(GRPCCallError)?
{
if let Some(message) = error_message {
error!("{}", message);
let request = GetDeploymentStatusRequest {
app_id,
deployment_id,
};
loop {
let response = client
.get_deployment_status(request.clone())
.await?
.into_inner();

if response.status == DeploymentStatus::Success as i32 {
info!("Deployment completed successfully");
break;
} else if response.status == DeploymentStatus::Failed as i32 {
info!("Deployment failed!");
break;
} else {
match result {
Some(dozer_types::grpc_types::cloud::deploy_app_response::Result::AppCreated(
AppResponse {
app_id: new_app_id, ..
},
)) => {
if let Some(app_id) = app_id {
info!("Updating {app_id} application");
} else {
info!("Deploying new application {new_app_id}");
CloudAppContext::save_app_id(new_app_id)?;
}
}
Some(dozer_types::grpc_types::cloud::deploy_app_response::Result::Step(
DeployStep {
text,
is_completed,
current_step,
..
},
)) => {
if is_completed {
printer.complete_step(current_step, &text);
} else {
printer.start_step(current_step, &text);
}
}
Some(dozer_types::grpc_types::cloud::deploy_app_response::Result::LogMessage(
message,
)) => {
message.split('\n').for_each(|line| {
info!("{}", line);
});
}
None => {}
let steps = response.steps.clone();
let completed_steps = response
.steps
.into_iter()
.filter(|s| {
s.status == DeploymentStatus::Success as i32 && s.step_index >= current_step
})
.map(|s| (s.step_index, s.step_text))
.collect::<Vec<(u32, String)>>();

for (step_no, text) in completed_steps.iter() {
printer.complete_step(*step_no, text)
}

if !completed_steps.is_empty() {
current_step = completed_steps.last().unwrap().0 + 1;
let text = steps[current_step as usize].step_text.clone();
printer.start_step(current_step, &text);
}
}
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}

Ok::<(), CloudError>(())
}

Expand Down
1 change: 1 addition & 0 deletions dozer-cli/src/simple/cloud/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod deployer;
pub mod login;
pub mod monitor;
pub mod progress_printer;
pub mod version;
File renamed without changes.
Loading

0 comments on commit e098ce0

Please sign in to comment.