From 7db23b01df8247b8181f3c160d263d5b346aa3f1 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Mon, 22 Dec 2025 12:54:14 +0800 Subject: [PATCH] Add testcontainer for agent image with nginx --- Cargo.lock | 343 ++++++++++++++++---- apps/cli/Cargo.toml | 2 +- apps/cli/src/cmd/db_migrate_and_generate.rs | 1 + apps/container/Cargo.toml | 2 +- apps/container/src/agent.rs | 120 +++++++ apps/container/src/db/config.rs | 6 +- apps/container/src/db/postgresql.rs | 8 +- apps/container/src/env.rs | 4 + apps/container/src/lib.rs | 18 +- apps/container/src/main.rs | 136 +++++++- apps/container/src/types.rs | 4 + apps/container/src/util.rs | 17 +- justfile | 6 +- 13 files changed, 589 insertions(+), 78 deletions(-) create mode 100644 apps/container/src/agent.rs diff --git a/Cargo.lock b/Cargo.lock index d4b7e5b..2aa5314 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,12 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "anyhow" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" + [[package]] name = "argon2" version = "0.5.3" @@ -117,6 +123,22 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "astral-tokio-tar" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec179a06c1769b1e42e1e2cbe74c7dcdb3d6383c838454d063eaac5bbb7ebbe5" +dependencies = [ + "filetime", + "futures-core", + "libc", + "portable-atomic", + "rustc-hash", + "tokio", + "tokio-stream", + "xattr", +] + [[package]] name = "async-stream" version = "0.3.6" @@ -295,12 +317,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.10.0" @@ -342,13 +358,17 @@ dependencies = [ [[package]] name = "bollard" -version = "0.18.1" +version = "0.19.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97ccca1260af6a459d75994ad5acc1651bcabcbdbc41467cc9786519ab854c30" +checksum = "87a52479c9237eb04047ddb94788c41ca0d26eaff8b697ecfbb4c32f7fdc3b1b" dependencies = [ + "async-stream", "base64 0.22.1", + "bitflags", + "bollard-buildkit-proto", "bollard-stubs", "bytes", + "chrono", "futures-core", "futures-util", "hex", @@ -361,7 +381,9 @@ dependencies = [ "hyper-util", "hyperlocal", "log", + "num", "pin-project-lite", + "rand 0.9.2", "rustls", "rustls-native-certs", "rustls-pemfile", @@ -373,19 +395,40 @@ dependencies = [ "serde_urlencoded", "thiserror", "tokio", + "tokio-stream", "tokio-util", + "tonic", "tower-service", "url", "winapi", ] [[package]] -name = "bollard-stubs" -version = "1.47.1-rc.27.3.1" +name = "bollard-buildkit-proto" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f179cfbddb6e77a5472703d4b30436bff32929c0aa8a9008ecf23d1d3cdd0da" +checksum = "85a885520bf6249ab931a764ffdb87b0ceef48e6e7d807cfdb21b751e086e1ad" dependencies = [ + "prost", + "prost-types", + "tonic", + "tonic-prost", + "ureq", +] + +[[package]] +name = "bollard-stubs" +version = "1.49.1-rc.28.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5731fe885755e92beff1950774068e0cae67ea6ec7587381536fca84f1779623" +dependencies = [ + "base64 0.22.1", + "bollard-buildkit-proto", + "bytes", + "chrono", + "prost", "serde", + "serde_json", "serde_repr", "serde_with", ] @@ -1110,13 +1153,12 @@ dependencies = [ [[package]] name = "etcetera" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c7b13d0780cb82722fd59f6f57f925e143427e4a75313a6c77243bf5326ae6" +checksum = "de48cc4d1c1d97a20fd819def54b890cadde72ed3ad0c614822a0a433361be96" dependencies = [ "cfg-if", - "home", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1136,6 +1178,17 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" +[[package]] +name = "ferroid" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce161062fb044bd629c2393590efd47cab8d0241faf15704ffb0d47b7b4e4a35" +dependencies = [ + "portable-atomic", + "rand 0.9.2", + "web-time", +] + [[package]] name = "ff" version = "0.13.1" @@ -1579,6 +1632,19 @@ dependencies = [ "tower-service", ] +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.17" @@ -1901,9 +1967,9 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb" dependencies = [ - "bitflags 2.10.0", + "bitflags", "libc", - "redox_syscall 0.5.18", + "redox_syscall", ] [[package]] @@ -2035,7 +2101,7 @@ version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" dependencies = [ - "bitflags 2.10.0", + "bitflags", "cfg-if", "cfg_aliases", "libc", @@ -2050,6 +2116,20 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -2076,6 +2156,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -2113,6 +2202,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -2141,7 +2241,7 @@ version = "0.10.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328" dependencies = [ - "bitflags 2.10.0", + "bitflags", "cfg-if", "foreign-types", "libc", @@ -2270,7 +2370,7 @@ checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.18", + "redox_syscall", "smallvec", "windows-link", ] @@ -2418,6 +2518,26 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pin-project" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -2467,6 +2587,12 @@ dependencies = [ "regex", ] +[[package]] +name = "portable-atomic" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f59e70c4aef1e55797c2e8fd94a4f2a973fc972cfde0e0b05f683667b0cd39dd" + [[package]] name = "potential_utf" version = "0.1.4" @@ -2563,6 +2689,38 @@ dependencies = [ "yansi", ] +[[package]] +name = "prost" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7231bd9b3d3d33c86b58adbac74b5ec0ad9f496b19d22801d773636feaa95f3d" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-derive" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9120690fafc389a67ba3803df527d0ec9cbbc9cc45e4cc20b332996dfb672425" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "prost-types" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9b4db3d6da204ed77bb26ba83b6122a73aeb2e87e25fbf7ad2e84c4ccbf8f72" +dependencies = [ + "prost", +] + [[package]] name = "ptr_meta" version = "0.1.4" @@ -2663,22 +2821,13 @@ dependencies = [ "getrandom 0.3.4", ] -[[package]] -name = "redox_syscall" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "redox_syscall" version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ - "bitflags 2.10.0", + "bitflags", ] [[package]] @@ -2798,7 +2947,7 @@ version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd490c5b18261893f14449cbd28cb9c0b637aebf161cd77900bfdedaff21ec32" dependencies = [ - "bitflags 2.10.0", + "bitflags", "once_cell", "serde", "serde_derive", @@ -2852,6 +3001,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + [[package]] name = "rustc_version" version = "0.4.1" @@ -2867,7 +3022,7 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" dependencies = [ - "bitflags 2.10.0", + "bitflags", "errno", "libc", "linux-raw-sys", @@ -2880,6 +3035,7 @@ version = "0.23.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "533f54bc6a7d4f647e46ad909549eda97bf5afc1585190ef692b4286b198bd8f" dependencies = [ + "log", "once_cell", "ring", "rustls-pki-types", @@ -3190,7 +3346,7 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 2.10.0", + "bitflags", "core-foundation 0.9.4", "core-foundation-sys", "libc", @@ -3203,7 +3359,7 @@ version = "3.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3297343eaf830f66ede390ea39da1d462b6b0c1b000f420d0a83f898bbbe6ef" dependencies = [ - "bitflags 2.10.0", + "bitflags", "core-foundation 0.10.1", "core-foundation-sys", "libc", @@ -3585,7 +3741,7 @@ dependencies = [ "atoi", "base64 0.22.1", "bigdecimal", - "bitflags 2.10.0", + "bitflags", "byteorder", "bytes", "chrono", @@ -3632,7 +3788,7 @@ dependencies = [ "atoi", "base64 0.22.1", "bigdecimal", - "bitflags 2.10.0", + "bitflags", "byteorder", "chrono", "crc", @@ -3832,18 +3988,20 @@ dependencies = [ [[package]] name = "testcontainers" -version = "0.24.0" +version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23bb7577dca13ad86a78e8271ef5d322f37229ec83b8d98da6d996c588a1ddb1" +checksum = "1483605f58b2fff80d786eb56a0b6b4e8b1e5423fbc9ec2e3e562fa2040d6f27" dependencies = [ + "astral-tokio-tar", "async-trait", "bollard", - "bollard-stubs", "bytes", "docker_credential", "either", - "etcetera 0.10.0", + "etcetera 0.11.0", + "ferroid", "futures", + "itertools", "log", "memchr", "parse-display", @@ -3854,7 +4012,6 @@ dependencies = [ "thiserror", "tokio", "tokio-stream", - "tokio-tar", "tokio-util", "url", ] @@ -4019,21 +4176,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-tar" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5714c010ca3e5c27114c1cdeb9d14641ace49874aa5626d7149e47aedace75" -dependencies = [ - "filetime", - "futures-core", - "libc", - "redox_syscall 0.3.5", - "tokio", - "tokio-stream", - "xattr", -] - [[package]] name = "tokio-util" version = "0.7.17" @@ -4090,6 +4232,46 @@ dependencies = [ "winnow", ] +[[package]] +name = "tonic" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb7613188ce9f7df5bfe185db26c5814347d110db17920415cf2fbcad85e7203" +dependencies = [ + "async-trait", + "axum", + "base64 0.22.1", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "socket2", + "sync_wrapper", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-prost" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66bd50ad6ce1252d87ef024b3d64fe4c3cf54a86fb9ef4c631fdd0ded7aeaa67" +dependencies = [ + "bytes", + "prost", + "tonic", +] + [[package]] name = "tower" version = "0.5.2" @@ -4098,9 +4280,12 @@ checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ "futures-core", "futures-util", + "indexmap 2.12.0", "pin-project-lite", + "slab", "sync_wrapper", "tokio", + "tokio-util", "tower-layer", "tower-service", "tracing", @@ -4112,7 +4297,7 @@ version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8" dependencies = [ - "bitflags 2.10.0", + "bitflags", "bytes", "http", "pin-project-lite", @@ -4284,6 +4469,34 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d39cb1dbab692d82a977c0392ffac19e188bd9186a9f32806f0aaa859d75585a" +dependencies = [ + "base64 0.22.1", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "ureq-proto", + "utf-8", + "webpki-roots 1.0.4", +] + +[[package]] +name = "ureq-proto" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d81f9efa9df032be5934a46a068815a10a042b494b6a58cb0a1a97bb5467ed6f" +dependencies = [ + "base64 0.22.1", + "http", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.7" @@ -4296,6 +4509,12 @@ dependencies = [ "serde", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -4439,6 +4658,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki-roots" version = "0.26.11" diff --git a/apps/cli/Cargo.toml b/apps/cli/Cargo.toml index 8357949..f88fc3d 100644 --- a/apps/cli/Cargo.toml +++ b/apps/cli/Cargo.toml @@ -8,7 +8,7 @@ async-trait = "0.1.89" container-simulate = { path = "../container" } migration = {path = "../../public/migration"} shared = {path = "../../public/shared"} -testcontainers = "0.24.0" +testcontainers = "0.26.0" tokio = { version = "1.47.0", features = ["full"] } url = "2.5.7" clap = { version = "4.5.48", features = ["derive", "env"] } diff --git a/apps/cli/src/cmd/db_migrate_and_generate.rs b/apps/cli/src/cmd/db_migrate_and_generate.rs index 4e7ac08..a563431 100644 --- a/apps/cli/src/cmd/db_migrate_and_generate.rs +++ b/apps/cli/src/cmd/db_migrate_and_generate.rs @@ -54,6 +54,7 @@ fn action( for db_config in database_configs { let config = container::Config { database: db_config, + agent: None, }; let mut detached_handler = container::start_detached(&config).await; match migrate_and_generate_entity(&config, &output_path).await { diff --git a/apps/container/Cargo.toml b/apps/container/Cargo.toml index bc502e1..5c48710 100644 --- a/apps/container/Cargo.toml +++ b/apps/container/Cargo.toml @@ -9,7 +9,7 @@ path = "src/lib.rs" [dependencies] async-trait = "0.1.89" -testcontainers = "0.24.0" +testcontainers = { version = "0.26.0" } shared = { path = "../../public/shared" } tokio = { version = "1.47.0", features = ["full"] } url = "2.5.7" diff --git a/apps/container/src/agent.rs b/apps/container/src/agent.rs new file mode 100644 index 0000000..ee08852 --- /dev/null +++ b/apps/container/src/agent.rs @@ -0,0 +1,120 @@ +use std::{error::Error, sync::Arc}; +use testcontainers::{ + ContainerAsync, GenericBuildableImage, GenericImage, ImageExt, + core::{AccessMode, BuildImageOptions, ContainerPort, Mount, WaitFor}, + runners::{AsyncBuilder, AsyncRunner}, +}; + +use crate::{ + db::UnStartedContainer, + types::{ConfigInfoType, WithContainer}, +}; + +pub const SOCK_NAME: &str = "yanpm-agent.sock"; +const SOCK_FOLDER: &str = "/var/run/yanpm"; +const NGINX_CONFIG_DIR: &str = "/etc/nginx/conf.d"; + +#[derive(Clone)] +pub struct AgentContainerConfig { + pub image: String, + pub tag: String, + pub container_name: String, + pub dockerfile_path: String, + pub force_build: bool, + pub agent_config: AgentConfig, + pub nginx_config: NginxConfig, +} + +pub type AgentConfigInfoType = ConfigInfoType; + +#[derive(Clone)] +pub struct AgentContainerInfo { + pub container: Arc>, + pub config: AgentContainerConfig, +} + +impl WithContainer for AgentContainerInfo { + fn container(&self) -> &Arc> { + &self.container + } +} + +#[derive(Clone)] +pub struct AgentConfig { + pub sock_folder: String, // path to be mounted to host for unix socket + pub nginx_config_dir: String, // path to be mounted to host for nginx config files, only the agent generated folder will be mounted + pub sock_perm: u32, // permissions to set on the unix socket + pub sock_gid: String, // GID to set on the unix socket +} + +#[derive(Clone)] +pub struct NginxConfig { + pub expose_http: bool, + pub expose_https: bool, +} + +impl AgentContainerConfig { + pub fn new( + image: String, + tag: String, + container_name: String, + dockerfile_path: String, + force_build: bool, + // agent configs + agent_config: AgentConfig, + nginx_config: NginxConfig, + ) -> Self { + AgentContainerConfig { + image, + tag, + container_name, + dockerfile_path, + force_build, + // default agent configs + agent_config, + nginx_config, + } + } + + pub async fn get_unstarted_container(&self) -> Result> { + let mut image = GenericBuildableImage::new(&self.image, &self.tag) + .with_dockerfile(&self.dockerfile_path) + .build_image_with(BuildImageOptions::new().with_skip_if_exists(!self.force_build)) + .await?; + + if self.nginx_config.expose_http { + image = image.with_exposed_port(ContainerPort::Tcp(80)); + } + + if self.nginx_config.expose_https { + image = image.with_exposed_port(ContainerPort::Tcp(443)); + } + + image = image.with_wait_for(WaitFor::message_on_either_std("Starting yanpm-daemon on")); + + Ok(image + .with_container_name(self.container_name.clone()) + .with_env_var("YANPM_AGENT_SOCK", format!("{}/{}", SOCK_FOLDER, SOCK_NAME)) + .with_env_var("YANPM_NGINX_CONFIG_DIR", NGINX_CONFIG_DIR.to_string()) + .with_env_var( + "YANPM_AGENT_SOCK_PERM", + self.agent_config.sock_perm.to_string(), + ) + .with_env_var("YANPM_AGENT_SOCK_GID", self.agent_config.sock_gid.clone()) + .with_mount( + Mount::bind_mount( + self.agent_config.sock_folder.clone(), + SOCK_FOLDER.to_string(), + ) + .with_access_mode(AccessMode::ReadWrite), + ) + .with_mount( + Mount::bind_mount( + self.agent_config.nginx_config_dir.clone(), + NGINX_CONFIG_DIR.to_string(), + ) + .with_access_mode(AccessMode::ReadWrite), + ) + .start()) + } +} diff --git a/apps/container/src/db/config.rs b/apps/container/src/db/config.rs index 1c69d06..ff0be61 100644 --- a/apps/container/src/db/config.rs +++ b/apps/container/src/db/config.rs @@ -9,7 +9,7 @@ pub struct OptionalContainerConfig { } #[derive(Clone)] -pub struct ContainerConfig { +pub struct DatabaseContainerConfig { pub image: String, pub tag: String, pub container_name: String, @@ -19,8 +19,8 @@ pub struct ContainerConfig { } impl OptionalContainerConfig { - pub fn fill_with(&self, other: &ContainerConfig) -> ContainerConfig { - ContainerConfig { + pub fn fill_with(&self, other: &DatabaseContainerConfig) -> DatabaseContainerConfig { + DatabaseContainerConfig { image: self.image.clone().unwrap_or_else(|| other.image.clone()), tag: self.tag.clone().unwrap_or_else(|| other.tag.clone()), container_name: self diff --git a/apps/container/src/db/postgresql.rs b/apps/container/src/db/postgresql.rs index f4343d4..5c0cec2 100644 --- a/apps/container/src/db/postgresql.rs +++ b/apps/container/src/db/postgresql.rs @@ -11,12 +11,12 @@ use crate::{ ConfigInfoType, db::{ ContainerizedDBInfo, DBConfigInfoType, DBInfo, UnStartedContainer, - config::{ContainerConfig, OptionalContainerConfig}, + config::{DatabaseContainerConfig, OptionalContainerConfig}, }, }; -pub fn get_default_config() -> ContainerConfig { - ContainerConfig { +pub fn get_default_config() -> DatabaseContainerConfig { + DatabaseContainerConfig { container_name: "yanpm-postgres".to_string(), database_name: "postgres".to_string(), user: "postgres".to_string(), @@ -27,7 +27,7 @@ pub fn get_default_config() -> ContainerConfig { } pub struct PostgreSQLContainer { - pub config: ContainerConfig, + pub config: DatabaseContainerConfig, } #[async_trait] diff --git a/apps/container/src/env.rs b/apps/container/src/env.rs index 162883f..0c2200b 100644 --- a/apps/container/src/env.rs +++ b/apps/container/src/env.rs @@ -32,6 +32,10 @@ impl EnvFile { env_file } + pub fn write_line(&mut self, key: &str, value: &str) { + self._write_line_buffer(key, value); + } + pub fn write(&mut self, stream: &mut dyn Write, with_prefix: bool) { self._write_buffer(stream, with_prefix); } diff --git a/apps/container/src/lib.rs b/apps/container/src/lib.rs index e1b4bc3..bd2fee4 100644 --- a/apps/container/src/lib.rs +++ b/apps/container/src/lib.rs @@ -1,9 +1,11 @@ +pub mod agent; pub mod db; mod env; pub mod types; mod util; use crate::{ + agent::AgentConfigInfoType, db::DBConfigInfoType, types::{ConfigInfoType, WithContainer, WithoutContainer}, util::{ @@ -15,6 +17,7 @@ use crate::{ #[derive(Clone)] pub struct Config { pub database: DBConfigInfoType, + pub agent: Option, } // relative to the pwd @@ -56,26 +59,29 @@ impl<'a> Drop for DetachedHandle<'a> { } async fn start(config: &Config) { - let db_config = &config.database; - // // write the config files for the api server and database client println!("Writing config files..."); - write_env_files(db_config); + write_env_files(&config.database, &config.agent); println!("Config files written to:"); println!(" - {}", to_absolute_path(API_CONFIG_PATH).display()); println!(" - {}", to_absolute_path(DB_CONFIG_PATH).display()); } async fn stop(config: &Config) { - let db_config = &config.database; // stop the container println!("Stopping container..."); - stop_container(db_config, "database".to_string()).await; + println!("Stopping database container..."); + stop_container(&config.database, "database".to_string()).await; + if let Some(agent) = &config.agent { + println!("Stopping agent container..."); + stop_container(agent, "agent".to_string()).await; + } + println!("Container stopped."); // remove the generated config file println!("Removing generated config file..."); remove_file_if_exists(DB_CONFIG_PATH); remove_file_if_exists(API_CONFIG_PATH); - println!("Container stopped."); + println!("Generated config files removed."); } pub async fn start_attached(config: &Config) { diff --git a/apps/container/src/main.rs b/apps/container/src/main.rs index b4e38c3..cffd7c2 100644 --- a/apps/container/src/main.rs +++ b/apps/container/src/main.rs @@ -1,6 +1,10 @@ +use std::sync::Arc; + use clap::Parser; -use container::Config; +use container::agent::{AgentConfig, AgentContainerConfig, AgentContainerInfo, NginxConfig}; use container::start_attached; +use container::types::ConfigInfoType; +use container::{Config, agent}; use container::db::DBInfo; @@ -11,12 +15,52 @@ struct Args { /// Database type to use: 'postgres' or 'sqlite'. Can also be set with DB_TYPE env var. #[arg(long, default_value = "sqlite", env = "DB_TYPE")] db_type: String, + + // agent related + /// force build agent image + #[arg(long, default_value_t = false, env = "AGENT_FORCE_BUILD")] + agent_force_build: bool, + /// dockerfile path for building agent image + #[arg(long, env = "AGENT_DOCKERFILE_PATH", required = false)] + agent_dockerfile_path: Option, + /// host's location to mount nginx config files folder generated by the agent + #[arg(long, env = "AGENT_NGINX_CONFIG_DIR", required = false)] + agent_nginx_config_dir: Option, + /// host's location folder to mount the unix socket files + #[arg(long, env = "AGENT_SOCK_PATH", required = false)] + agent_sock_path: Option, + /// socket permissions to set on the unix socket + #[arg(long, default_value = "660", env = "AGENT_SOCK_PERM", required = false)] + agent_sock_perm: u32, + /// socket GID to set on the unix socket + #[arg(long, default_value = "", env = "AGENT_SOCK_GID", required = false)] + agent_sock_gid: String, + /// nginx expose http port + #[arg( + long, + default_value_t = true, + env = "AGENT_NGINX_EXPOSE_HTTP", + required = false + )] + agent_nginx_expose_http: bool, + /// nginx expose https port + #[arg( + long, + default_value_t = false, + env = "AGENT_NGINX_EXPOSE_HTTPS", + required = false + )] + agent_nginx_expose_https: bool, +} + +struct ParsedArgs { + db_type: String, + agent_container_config: Option, } #[tokio::main] async fn main() { - // Parse command line arguments and environment variables - let args = Args::parse(); + let args = parse_args().await; println!("Starting container with database type: {}", args.db_type); let db_config = match args.db_type.to_lowercase().as_str() { @@ -43,11 +87,97 @@ async fn main() { }; println!("Database configuration obtained."); + let agent_container = if let Some(agent_config) = &args.agent_container_config { + println!( + "Agent container will be used with socket path: {} and nginx config dir: {}", + agent_config.agent_config.sock_folder, agent_config.agent_config.nginx_config_dir + ); + Some(agent_config.get_unstarted_container().await) + } else { + println!("No agent container configuration provided, skipping agent setup."); + None + }; + let config = Config { database: db_config, + agent: match agent_container { + Some(Ok(container)) => Some(ConfigInfoType::Containerized(AgentContainerInfo { + container: Arc::new(container.await.expect("Failed to start agent container")), + config: args.agent_container_config.expect("Invalid config state"), + })), + Some(Err(e)) => { + eprintln!("Failed to set up agent container: {}", e); + std::process::exit(1); + } + None => None, + }, }; println!("Starting container..."); start_attached(&config).await; println!("Container stopped. Exiting..."); } + +async fn parse_args() -> ParsedArgs { + // Parse command line arguments and environment variables + let args = Args::parse(); + + // if any required args are missing, do not start agent + let dockerfile_path = match args.agent_dockerfile_path { + None => { + println!("Agent dockerfile path not provided, skipping agent setup."); + return ParsedArgs { + db_type: args.db_type, + agent_container_config: None, + }; + } + Some(path) => path, + }; + + let time = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + let agent_config = AgentConfig { + sock_folder: match args.agent_sock_path { + None => { + // create a temp dir for the socket path + let temp_dir = std::env::temp_dir().join(format!("yanpm-agent-sock-{}", time)); + std::fs::create_dir_all(&temp_dir) + .expect("Failed to create temp dir for agent socket"); + temp_dir.to_string_lossy().to_string() + } + Some(path) => path, + }, + nginx_config_dir: match args.agent_nginx_config_dir { + None => { + // create a temp dir for the nginx config dir + let temp_dir = + std::env::temp_dir().join(format!("yanpm-agent-nginx-configs-{}", time)); + std::fs::create_dir_all(&temp_dir) + .expect("Failed to create temp dir for agent nginx configs"); + temp_dir.to_string_lossy().to_string() + } + Some(path) => path, + }, + sock_perm: args.agent_sock_perm, + sock_gid: args.agent_sock_gid.clone(), + }; + + ParsedArgs { + db_type: args.db_type, + agent_container_config: Some(AgentContainerConfig { + image: "yanpm-agent".to_string(), + tag: "latest".to_string(), + container_name: format!("yanpm-agent-container-{}", time), + dockerfile_path, + force_build: args.agent_force_build, + agent_config, + nginx_config: NginxConfig { + expose_http: args.agent_nginx_expose_http, + expose_https: args.agent_nginx_expose_https, + }, + }), + } +} diff --git a/apps/container/src/types.rs b/apps/container/src/types.rs index d91a7e6..e43e702 100644 --- a/apps/container/src/types.rs +++ b/apps/container/src/types.rs @@ -10,6 +10,10 @@ pub trait WithoutContainer { fn on_delete(&self); } +impl WithoutContainer for () { + fn on_delete(&self) {} +} + #[derive(Clone)] pub enum ConfigInfoType where diff --git a/apps/container/src/util.rs b/apps/container/src/util.rs index d513904..c80fada 100644 --- a/apps/container/src/util.rs +++ b/apps/container/src/util.rs @@ -4,6 +4,7 @@ use tokio::signal::unix::{SignalKind, signal}; use crate::{ API_CONFIG_PATH, DB_CONFIG_PATH, + agent::{AgentConfigInfoType, AgentContainerInfo, SOCK_NAME}, db::DBConfigInfoType, env::{self, EnvFile}, types::{ConfigInfoType, WithContainer, WithoutContainer}, @@ -20,7 +21,7 @@ pub fn to_absolute_path(path: &str) -> PathBuf { .clean() } -pub fn write_env_files(db_config: &DBConfigInfoType) { +pub fn write_env_files(db_config: &DBConfigInfoType, agent_config: &Option) { let api_config_path_absolute = to_absolute_path(API_CONFIG_PATH); let db_config_path_absolute = to_absolute_path(DB_CONFIG_PATH); @@ -33,6 +34,20 @@ pub fn write_env_files(db_config: &DBConfigInfoType) { let mut db_env = api_env.clone(); db_env.file_type = env::EnvFileType::DotEnv; + // agent related env vars + if let Some(agent) = agent_config + && let ConfigInfoType::Containerized(agent) = agent + { + api_env.write_line( + "AGENT__SOCK__PATH", + format!("{}/{}", &agent.config.agent_config.sock_folder, SOCK_NAME).as_str(), + ); + api_env.write_line( + "AGENT__NGINX__CONFIG__DIR", + &agent.config.agent_config.nginx_config_dir, + ); + } + let mut api_file = std::fs::File::create(&api_config_path_absolute).expect("Failed to create API config file"); diff --git a/justfile b/justfile index 37cf4ba..fede830 100644 --- a/justfile +++ b/justfile @@ -2,6 +2,8 @@ set dotenv-load := true # development environment file set dotenv-filename := "./public/database/.env.generated" +DEFAULT_SIMULATE_ARGS := "--agent-dockerfile-path=../agent/Dockerfile" + cli *args: cd apps/cli && \ if [ -n "{{args}}" ]; then \ @@ -13,9 +15,9 @@ cli *args: simulate *args: cd apps/container && \ if [ -n "{{args}}" ]; then \ - cargo run --bin container-simulate -- --db-type={{args}}; \ + cargo run --bin container-simulate -- {{args}}; \ else \ - cargo run --bin container-simulate; \ + cargo run --bin container-simulate -- {{DEFAULT_SIMULATE_ARGS}}; \ fi # Usage: (following SeaORM migration commands)