Skip to content

Commit

Permalink
generate infra map on build (#1813)
Browse files Browse the repository at this point in the history
  • Loading branch information
phiSgr authored Oct 8, 2024
1 parent 79db935 commit 49133b0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/framework-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ async fn top_command_handler(
// TODO get rid of the routines and use functions instead

create_dockerfile(&project_arc)?.show();
let _ = build_dockerfile(&project_arc, *amd64, *arm64)?;
let _: RoutineSuccess = build_dockerfile(&project_arc, *amd64, *arm64).await?;

wait_for_usage_capture(capture_handle).await;

Expand Down
39 changes: 37 additions & 2 deletions apps/framework-cli/src/cli/routines/docker_packager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::{RoutineFailure, RoutineSuccess};
use crate::cli::display::with_spinner;
use crate::cli::routines::util::ensure_docker_running;
use crate::framework::core::infrastructure_map::InfrastructureMap;
use crate::framework::core::primitive_map::PrimitiveMap;
use crate::framework::languages::SupportedLanguages;
use crate::utilities::constants::{
APP_DIR, CLI_INTERNAL_VERSIONS_DIR, OLD_PROJECT_CONFIG_FILE, PACKAGE_JSON, PROJECT_CONFIG_FILE,
Expand Down Expand Up @@ -73,6 +75,8 @@ COPY --chown=moose:moose ./project.tom[l] ./project.toml
COPY --chown=moose:moose ./moose.config.tom[l] ./moose.config.toml
COPY --chown=moose:moose ./versions .moose/versions
COPY --chown=moose:moose ./infrastructure_map.json .moose/infrastructure_map.json
# Placeholder for the language specific install command
INSTALL_COMMAND
Expand Down Expand Up @@ -174,7 +178,7 @@ pub fn create_dockerfile(project: &Project) -> Result<RoutineSuccess, RoutineFai
)))
}

pub fn build_dockerfile(
pub async fn build_dockerfile(
project: &Project,
is_amd64: bool,
is_arm64: bool,
Expand Down Expand Up @@ -267,12 +271,43 @@ pub fn build_dockerfile(
}
}

// Generate and save InfrastructureMap as JSON
let primitive_map = PrimitiveMap::load(project).await.map_err(|e| {
RoutineFailure::new(
Message::new("Failed".to_string(), "to load PrimitiveMap".to_string()),
e,
)
})?;

let infra_map = InfrastructureMap::new(primitive_map);

let json_path = internal_dir.join("packager/infrastructure_map.json");
fs::create_dir_all(json_path.parent().unwrap()).map_err(|e| {
RoutineFailure::new(
Message::new(
"Failed".to_string(),
"to create .moose directory".to_string(),
),
e,
)
})?;

infra_map.save_to_json(&json_path).map_err(|e| {
RoutineFailure::new(
Message::new(
"Failed".to_string(),
"to save InfrastructureMap as JSON".to_string(),
),
e,
)
})?;

// consts::CLI_VERSION is set from an environment variable during the CI/CD process
// however, it's set to 0.0.1 in development,
// so we set it to a recent version for the purpose of local dev testing.
let mut cli_version = constants::CLI_VERSION;
if cli_version == "0.0.1" {
cli_version = "0.3.626";
cli_version = "0.3.655";
}

let build_all = is_amd64 == is_arm64;
Expand Down
13 changes: 13 additions & 0 deletions apps/framework-cli/src/framework/core/infrastructure_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use super::primitive_map::PrimitiveMap;
use crate::framework::controller::{InitialDataLoad, InitialDataLoadStatus};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PrimitiveTypes {
Expand Down Expand Up @@ -675,6 +677,17 @@ impl InfrastructureMap {
})
.collect()
}

pub fn save_to_json(&self, path: &Path) -> Result<(), std::io::Error> {
let json = serde_json::to_string(self)?;
fs::write(path, json)
}

pub fn load_from_json(path: &Path) -> Result<Self, std::io::Error> {
let json = fs::read_to_string(path)?;
let infra_map = serde_json::from_str(&json)?;
Ok(infra_map)
}
}

#[cfg(test)]
Expand Down
10 changes: 8 additions & 2 deletions apps/framework-cli/src/framework/core/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
use clickhouse_rs::ClientHandle;
use rdkafka::error::KafkaError;
use std::collections::HashMap;
use std::path::Path;

#[derive(Debug, thiserror::Error)]
pub enum PlanningError {
Expand Down Expand Up @@ -43,8 +44,13 @@ pub async fn plan_changes(
client: &mut ClientHandle,
project: &Project,
) -> Result<InfraPlan, PlanningError> {
let primitive_map = PrimitiveMap::load(project).await?;
let target_infra_map = InfrastructureMap::new(primitive_map);
let target_infra_map = if project.is_production {
let json_path = Path::new(".moose/infrastructure_map.json");
InfrastructureMap::load_from_json(json_path).map_err(|e| PlanningError::Other(e.into()))?
} else {
let primitive_map = PrimitiveMap::load(project).await?;
InfrastructureMap::new(primitive_map)
};

let current_infra_map = {
// in the rest of this block of code,
Expand Down

0 comments on commit 49133b0

Please sign in to comment.