From 924bcf8c8c09419b49fd816230dfa6bcabb9cc18 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 5 Apr 2024 22:45:25 +0200 Subject: [PATCH] feat: handle for cuddle_file Signed-off-by: kjuulh --- Cargo.lock | 23 +++++ crates/cuddle-ci/Cargo.toml | 6 ++ crates/cuddle-ci/src/cuddle_file.rs | 89 ++++++++++++++++--- crates/cuddle-ci/src/rust_service.rs | 3 + crates/cuddle-ci/src/rust_service/assets.rs | 2 +- .../cuddle-ci/src/rust_service/cuddle_file.rs | 61 +++++++++++++ 6 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 crates/cuddle-ci/src/rust_service/cuddle_file.rs diff --git a/Cargo.lock b/Cargo.lock index 02d725e..367cc18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -358,6 +358,7 @@ dependencies = [ "dagger-sdk", "eyre", "futures", + "pretty_assertions", "serde", "serde_json", "serde_yaml", @@ -498,6 +499,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.7" @@ -1247,6 +1254,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.79" @@ -2253,3 +2270,9 @@ dependencies = [ "linux-raw-sys", "rustix", ] + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/crates/cuddle-ci/Cargo.toml b/crates/cuddle-ci/Cargo.toml index b490308..cc10040 100644 --- a/crates/cuddle-ci/Cargo.toml +++ b/crates/cuddle-ci/Cargo.toml @@ -27,4 +27,10 @@ chrono = {version = "0.4.37"} toml = "0.8.12" [dev-dependencies] +pretty_assertions = "1.4.0" tokio.workspace = true + +[features] +default = [] +dagger = [] +integration = [] diff --git a/crates/cuddle-ci/src/cuddle_file.rs b/crates/cuddle-ci/src/cuddle_file.rs index 5978b86..6d67c7d 100644 --- a/crates/cuddle-ci/src/cuddle_file.rs +++ b/crates/cuddle-ci/src/cuddle_file.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use std::{collections::BTreeMap, path::PathBuf}; use serde::{Deserialize, Serialize}; @@ -6,6 +6,33 @@ use serde::{Deserialize, Serialize}; pub struct CuddleFile { pub vars: CuddleVars, pub deployment: Option, + pub components: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub struct CuddleComponents { + pub database: Option, + pub assets: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub struct CuddleAssets { + pub volumes: Option>, + pub clean: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub struct CuddleAssetInclude { + pub from: PathBuf, + pub to: PathBuf, +} + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +#[serde(untagged)] +pub enum CuddleDatabase { + Enabled(bool), + Default {}, + Values { migrations: PathBuf }, } #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] @@ -54,13 +81,7 @@ impl CuddleFile { #[cfg(test)] mod test { - use std::collections::BTreeMap; - - use crate::cuddle_file::{ - CuddleCluster, CuddleClusters, CuddleDeploymentCluster, CuddleDeploymentEnv, CuddleVars, - }; - - use super::CuddleFile; + use super::*; #[test] fn parse_file() { @@ -82,6 +103,13 @@ deployment: prod: clusters: - clank_prod +components: + database: true + + assets: + volumes: + - from: somewhere + to: somewhere-else scripts: render: @@ -119,11 +147,52 @@ scripts: clusters: Some(CuddleClusters(clusters)), }, deployment: Some(crate::cuddle_file::CuddleDeployment { - registry: "git@git.front.kjuulh.io:kjuulh/clank-cluster".into(), + registry: "git@git.front.kjuulh.io:kjuulh/clank-clusters".into(), env: CuddleDeploymentEnv(deployment), }), + components: Some(CuddleComponents { + database: Some(CuddleDatabase::Enabled(true)), + assets: Some(CuddleAssets { + volumes: Some(vec![CuddleAssetInclude { + from: "somewhere".into(), + to: "somewhere-else".into(), + }]), + clean: None, + }), + }), }; - assert_eq!(expected, res) + pretty_assertions::assert_eq!(expected, res) + } + + #[test] + fn cuddle_database_default() { + let cuddle_file = r#" +base: "git@git.front.kjuulh.io:kjuulh/cuddle-base.git" + +vars: + service: "infrastructure-example" + registry: kasperhermansen + +components: + database: {} +"#; + + let res = CuddleFile::parse_cuddle_file(cuddle_file).expect("to parse file"); + + let expected = CuddleFile { + vars: CuddleVars { + service: "infrastructure-example".into(), + registry: "kasperhermansen".into(), + clusters: None, + }, + deployment: None, + components: Some(CuddleComponents { + database: Some(CuddleDatabase::Default {}), + assets: None, + }), + }; + + pretty_assertions::assert_eq!(expected, res) } } diff --git a/crates/cuddle-ci/src/rust_service.rs b/crates/cuddle-ci/src/rust_service.rs index 277be63..3eb5e29 100644 --- a/crates/cuddle-ci/src/rust_service.rs +++ b/crates/cuddle-ci/src/rust_service.rs @@ -442,6 +442,7 @@ mod cargo_binstall; mod cargo_clean; mod clap_sanity_test; mod cuddle_cli; +mod cuddle_file; mod docker_cache; mod docker_cli; mod kubectl; @@ -458,6 +459,7 @@ pub mod extensions { pub use super::cargo_clean::*; pub use super::clap_sanity_test::*; pub use super::cuddle_cli::*; + pub use super::cuddle_file::*; pub use super::docker_cache::*; pub use super::docker_cli::*; pub use super::kubectl::*; @@ -480,6 +482,7 @@ mod test { }; #[tokio::test] + #[cfg(any(feature = "dagger", feature = "integration"))] async fn test_can_build_rust() -> eyre::Result<()> { let client = dagger_sdk::connect().await?; diff --git a/crates/cuddle-ci/src/rust_service/assets.rs b/crates/cuddle-ci/src/rust_service/assets.rs index 6d3cbce..98757a3 100644 --- a/crates/cuddle-ci/src/rust_service/assets.rs +++ b/crates/cuddle-ci/src/rust_service/assets.rs @@ -1,7 +1,7 @@ use std::{path::PathBuf, sync::Arc}; use async_trait::async_trait; -use dagger_sdk::{Container}; +use dagger_sdk::Container; use crate::{dagger_middleware::DaggerMiddleware, leptos_service::LeptosService}; diff --git a/crates/cuddle-ci/src/rust_service/cuddle_file.rs b/crates/cuddle-ci/src/rust_service/cuddle_file.rs new file mode 100644 index 0000000..a48cbe5 --- /dev/null +++ b/crates/cuddle-ci/src/rust_service/cuddle_file.rs @@ -0,0 +1,61 @@ +use std::path::PathBuf; + +use crate::cuddle_file::{CuddleDatabase, CuddleFile}; + +use super::{ + extensions::{AssetsExt, CargoCleanExt, SqlxExt}, + RustService, +}; + +#[derive(Default)] +pub struct CuddleFileAction {} + +impl CuddleFileAction { + pub fn new() -> Self { + Self {} + } +} + +pub trait CuddleFileExt { + fn with_cuddle_file(&mut self, cuddle_file: &CuddleFile) -> &mut Self; +} + +impl CuddleFileExt for RustService { + fn with_cuddle_file(&mut self, cuddle_file: &CuddleFile) -> &mut Self { + let mut s = self + .with_bin_name(&cuddle_file.vars.service) + .with_deployment(false); + + if let Some(components) = &cuddle_file.components { + s = if let Some(database) = &components.database { + match database { + CuddleDatabase::Enabled(true) => s.with_sqlx().with_sqlx_migrations( + PathBuf::from("crates") + .join(&cuddle_file.vars.service) + .join("migrations/crdb"), + ), + CuddleDatabase::Values { migrations } => { + s.with_sqlx().with_sqlx_migrations(migrations) + } + CuddleDatabase::Enabled(false) | CuddleDatabase::Default {} => s, + } + } else { + s + }; + + if let Some(assets) = &components.assets { + if let Some(true) = assets.clean { + s = s.with_cargo_clean() + } + + if let Some(volumes) = &assets.volumes { + let mappings = volumes.iter().cloned().map(|val| (val.from, val.to)); + + s = s.with_assets(mappings); + } + } + } + + s + } +}