Skip to content

Commit

Permalink
Add version aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse-Bakker committed Sep 26, 2023
1 parent 03672c2 commit 798baaa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
5 changes: 5 additions & 0 deletions dozer-cli/src/cli/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 18 additions & 1 deletion dozer-cli/src/simple/cloud/version.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -113,6 +113,23 @@ pub fn version_status_table(status: &Result<VersionStatus, CloudError>) -> Strin
}
}

pub fn version_string(version: u32, is_current: bool, aliases: &HashMap<String, u32>) -> 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<VersionStatus, CloudError>,
current_status: &Result<VersionStatus, CloudError>,
Expand Down
51 changes: 34 additions & 17 deletions dozer-cli/src/simple/cloud_orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -550,7 +555,7 @@ impl SimpleOrchestrator {
);

table.add_row(row![
format!("v{version}"),
version_string(version, false, &status.aliases),
version_status_table(&version_status)
]);

Expand All @@ -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(&current_version_status)
]);

Expand All @@ -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>(())
Expand Down
18 changes: 18 additions & 0 deletions dozer-types/protos/cloud.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -139,6 +141,7 @@ message GetStatusResponse {
repeated DeploymentStatusWithHealth deployments = 2;
map<uint32, uint32> versions = 4;
optional uint32 current_version = 5;
map<string, uint32> aliases = 6;
}
message DeploymentStatusWithHealth {
DeploymentInfo deployment = 1;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 798baaa

Please sign in to comment.