From 798baaacb9ca6c9eaa01c8f48c4fc0162e66cafe Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Tue, 26 Sep 2023 13:57:26 +0200 Subject: [PATCH] Add version aliasing --- dozer-cli/src/cli/cloud.rs | 5 +++ dozer-cli/src/simple/cloud/version.rs | 19 +++++++- dozer-cli/src/simple/cloud_orchestrator.rs | 51 ++++++++++++++-------- dozer-types/protos/cloud.proto | 18 ++++++++ 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/dozer-cli/src/cli/cloud.rs b/dozer-cli/src/cli/cloud.rs index 8d0e020cec..705a698af4 100644 --- a/dozer-cli/src/cli/cloud.rs +++ b/dozer-cli/src/cli/cloud.rs @@ -143,6 +143,11 @@ pub enum VersionCommand { /// The version to set as current version: u32, }, + /// Creates or updates an alias to point at the given version + Alias { alias: String, version: u32 }, + /// Remove alias + #[command(name = "rm-alias", visible_alias = "rma")] + RmAlias { alias: String }, } #[derive(Debug, Clone, Subcommand)] diff --git a/dozer-cli/src/simple/cloud/version.rs b/dozer-cli/src/simple/cloud/version.rs index 913380c3aa..c71f30a003 100644 --- a/dozer-cli/src/simple/cloud/version.rs +++ b/dozer-cli/src/simple/cloud/version.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, fmt::Write}; use dozer_api::rest::DOZER_SERVER_NAME_HEADER; use dozer_cache::Phase; @@ -113,6 +113,23 @@ pub fn version_status_table(status: &Result) -> Strin } } +pub fn version_string(version: u32, is_current: bool, aliases: &HashMap) -> String { + let aliases: Vec<&str> = aliases + .iter() + .filter_map(move |(alias, alias_version)| { + (*alias_version == version).then_some(alias.as_str()) + }) + .collect(); + let mut s = format!("v{version}"); + if is_current { + write!(s, " (current)").unwrap(); + }; + if !aliases.is_empty() { + write!(s, " [{}]", aliases.join(", ")).unwrap(); + }; + s +} + pub fn version_is_up_to_date( status: &Result, current_status: &Result, diff --git a/dozer-cli/src/simple/cloud_orchestrator.rs b/dozer-cli/src/simple/cloud_orchestrator.rs index 77b8a5fa0d..72acf02e6d 100644 --- a/dozer-cli/src/simple/cloud_orchestrator.rs +++ b/dozer-cli/src/simple/cloud_orchestrator.rs @@ -12,6 +12,7 @@ use crate::errors::{ use crate::simple::cloud::deployer::deploy_app; use crate::simple::cloud::login::CredentialInfo; use crate::simple::cloud::monitor::monitor_app; +use crate::simple::cloud::version::version_string; use crate::simple::token_layer::TokenLayer; use crate::simple::SimpleOrchestrator; use crate::CloudOrchestrator; @@ -25,7 +26,7 @@ use dozer_types::grpc_types::cloud::{ }; use dozer_types::grpc_types::cloud::{ CreateAppRequest, DeploymentInfo, DeploymentStatusWithHealth, File, ListDeploymentRequest, - SetCurrentVersionRequest, + RmAliasRequest, SetAliasRequest, SetCurrentVersionRequest, }; use dozer_types::log::info; use dozer_types::prettytable::{row, table}; @@ -259,18 +260,22 @@ impl CloudOrchestrator for SimpleOrchestrator { } } - let mut version = "".to_string(); - for (loop_version, loop_deployment) in response.versions.iter() { - if loop_deployment == &deployment.deployment { - if Some(*loop_version) == response.current_version { - version = format!("v{loop_version} (current)"); - } else { - version = format!("v{loop_version}"); - } - break; - } - } - + let found = response + .versions + .iter() + .find_map(|(version, version_deployment)| { + (*version_deployment == deployment.deployment).then_some(*version) + }); + + let version = found + .map(|version| { + version_string( + version, + Some(version) == response.current_version, + &response.aliases, + ) + }) + .unwrap_or_default(); deployment_table.add_row(row![ deployment.deployment, format!("Deployment Status: {:?}", deployment.status), @@ -550,7 +555,7 @@ impl SimpleOrchestrator { ); table.add_row(row![ - format!("v{version}"), + version_string(version, false, &status.aliases), version_status_table(&version_status) ]); @@ -561,7 +566,7 @@ impl SimpleOrchestrator { ) .await; table.add_row(row![ - format!("v{current_version} (current)"), + version_string(current_version, true, &Default::default()), version_status_table(¤t_version_status) ]); @@ -574,20 +579,32 @@ impl SimpleOrchestrator { } } else { table.add_row(row![ - format!("v{version} (current)"), + version_string(version, true, &status.aliases), version_status_table(&version_status) ]); table.printstd(); } } else { table.add_row(row![ - format!("v{version}"), + version_string(version, false, &status.aliases), version_status_table(&version_status) ]); table.printstd(); info!("No current version"); }; } + VersionCommand::Alias { alias, version } => { + client + .set_alias(SetAliasRequest { + app_id, + version, + alias, + }) + .await?; + } + VersionCommand::RmAlias { alias } => { + client.rm_alias(RmAliasRequest { app_id, alias }).await?; + } } Ok::<_, CloudError>(()) diff --git a/dozer-types/protos/cloud.proto b/dozer-types/protos/cloud.proto index f329adcb56..4698430215 100644 --- a/dozer-types/protos/cloud.proto +++ b/dozer-types/protos/cloud.proto @@ -28,6 +28,8 @@ service DozerCloud { rpc get_status(GetStatusRequest) returns (GetStatusResponse); rpc list_deployments(ListDeploymentRequest) returns (ListDeploymentResponse); rpc list_versions(ListVersionsRequest) returns (ListVersionsResponse); + rpc set_alias(SetAliasRequest) returns (SetAliasResponse); + rpc rm_alias(RmAliasRequest) returns (RmAliasRequest); rpc set_current_version(SetCurrentVersionRequest) returns (SetCurrentVersionResponse); rpc list_files(ListFilesRequest) returns (ListFilesResponse); rpc get_configuration(GetConfigurationRequest) returns (GetConfigurationResponse); @@ -139,6 +141,7 @@ message GetStatusResponse { repeated DeploymentStatusWithHealth deployments = 2; map versions = 4; optional uint32 current_version = 5; + map aliases = 6; } message DeploymentStatusWithHealth { DeploymentInfo deployment = 1; @@ -224,6 +227,21 @@ message SetCurrentVersionRequest { message SetCurrentVersionResponse {} +message SetAliasRequest { + string app_id = 1; + uint32 version = 2; + string alias = 3; +} + +message SetAliasResponse {} + +message RmAliasRequest { + string app_id = 1; + string alias = 2; +} + +message RmAliasResponse {} + message ConnectionRequest { string yaml_content = 2; }