diff --git a/wf2_core/src/recipes/wp/mod.rs b/wf2_core/src/recipes/wp/mod.rs index 5f4e4fca..9129c3f8 100644 --- a/wf2_core/src/recipes/wp/mod.rs +++ b/wf2_core/src/recipes/wp/mod.rs @@ -56,11 +56,6 @@ impl DcTasksTrait for WpRecipe { } } -pub mod pass_thru; -pub mod services; -pub mod subcommands; -pub mod volumes; - impl<'a, 'b> Commands<'a, 'b> for WpRecipe { fn subcommands(&self, _ctx: &Context) -> Vec>> { wp_recipe_subcommands() @@ -96,3 +91,8 @@ impl PassThru for WpRecipe { impl OutputFiles for WpRecipe {} impl ResolveScript for WpRecipe {} + +pub mod pass_thru; +pub mod services; +pub mod subcommands; +pub mod volumes; diff --git a/wf2_core/src/recipes/wp/services.rs b/wf2_core/src/recipes/wp/services.rs deleted file mode 100644 index 0ea792f0..00000000 --- a/wf2_core/src/recipes/wp/services.rs +++ /dev/null @@ -1,150 +0,0 @@ -use crate::context::Context; -use crate::dc_service::DcService; - -use crate::recipes::m2::services::php::PhpService; -use crate::recipes::wp::volumes::WpVolumes; -use crate::recipes::wp::WpRecipe; -use crate::services::{Service, Services}; - -pub struct WpServices { - pub services: Vec, -} -pub struct WpVolumeMounts; - -impl WpServices { - pub const ROOT: &'static str = "/var/www"; - - pub fn from_ctx(ctx: &Context) -> Self { - let services = vec![ - (WpNginxService).dc_service(ctx, &()), - (WpPhpService).dc_service(ctx, &()), - (WpPhpDebugService).dc_service(ctx, &()), - (WpCliService).dc_service(ctx, &()), - (WpDbService).dc_service(ctx, &()), - ]; - - Self { services } - } -} - -impl WpVolumeMounts { - pub const NGINX_CONF: &'static str = "nginx/nginx.conf"; - pub const NGINX_CONF_REMOTE: &'static str = "/etc/nginx/nginx.conf"; - - pub const NGINX_DEFAULT_HOST: &'static str = "nginx/host.conf"; - pub const NGINX_DEFAULT_REMOTE: &'static str = "/etc/nginx/conf.d/default.conf"; -} - -impl Services for WpServices { - fn dc_services(&self) -> Vec { - self.services.clone() - } -} - -struct WpNginxService; - -impl Service for WpNginxService { - const NAME: &'static str = "nginx"; - const IMAGE: &'static str = "wearejh/nginx:stable-m2"; - fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { - let host_port = WpRecipe::ctx_port(&ctx); - DcService::new(ctx.name(), Self::NAME, Self::IMAGE) - .set_depends_on(vec![WpPhpService::NAME]) - .set_volumes(vec![ - format!("{}:{}", ctx.cwd.display(), WpServices::ROOT), - format!( - "{}:{}", - ctx.output_file_path(WpVolumeMounts::NGINX_CONF).display(), - WpVolumeMounts::NGINX_CONF_REMOTE - ), - format!( - "{}:{}", - ctx.output_file_path(WpVolumeMounts::NGINX_DEFAULT_HOST) - .display(), - WpVolumeMounts::NGINX_DEFAULT_REMOTE - ), - ]) - .set_working_dir(WpServices::ROOT) - .set_ports(vec![format!("{}:80", host_port)]) - .finish() - } -} - -struct WpPhpService; - -impl Service for WpPhpService { - const NAME: &'static str = "php"; - const IMAGE: &'static str = PhpService::IMAGE_7_3; - - fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { - let domain = WpRecipe::ctx_domain(&ctx); - DcService::new(ctx.name(), Self::NAME, PhpService::IMAGE_7_3) - .set_volumes(vec![format!("{}:{}", ctx.cwd.display(), WpServices::ROOT)]) - .set_depends_on(vec![WpDbService::NAME]) - .set_working_dir(WpServices::ROOT) - .set_environment(vec![ - "XDEBUG_CONFIG=remote_host=host.docker.internal", - &format!("PHP_IDE_CONFIG=serverName={}", domain), - &format!("PHP_MEMORY_LIMIT=\"{}\"", "2G"), - // - // this one is here to prevent needing to modify/change the - // default bedrock setup. - // - &format!("DB_HOST={}", WpDbService::NAME), - ]) - .finish() - } -} - -struct WpPhpDebugService; - -impl Service for WpPhpDebugService { - const NAME: &'static str = "php-debug"; - const IMAGE: &'static str = PhpService::IMAGE_7_3; - - fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { - let mut php_cnt = (WpPhpService).dc_service(ctx, &()); - { - php_cnt.set_environment(vec!["XDEBUG_ENABLE=true"]); - } - php_cnt - } -} - -struct WpCliService; - -impl Service for WpCliService { - const NAME: &'static str = "wp-cli"; - const IMAGE: &'static str = "wordpress:cli"; - - fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { - DcService::new(ctx.name(), Self::NAME, Self::IMAGE) - .set_working_dir(WpServices::ROOT) - .set_init(true) - .set_depends_on(vec![WpPhpService::NAME]) - .set_volumes(vec![format!("{}:{}", ctx.cwd.display(), WpServices::ROOT)]) - // .set_volumes(vec![WpServices::PHP]) - .set_environment(vec![&format!("DB_HOST={}", WpDbService::NAME)]) - .finish() - } -} - -struct WpDbService; - -impl Service for WpDbService { - const NAME: &'static str = "db"; - const IMAGE: &'static str = "mysql:5.7"; - - fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { - DcService::new(ctx.name(), Self::NAME, Self::IMAGE) - .set_volumes(vec![format!("{}:/var/lib/mysql", WpVolumes::DB)]) - .set_environment(vec![ - "MYSQL_DATABASE=docker", - "MYSQL_USER=docker", - "MYSQL_PASSWORD=docker", - "MYSQL_ROOT_PASSWORD=docker", - ]) - .set_ports(vec!["3307:3306"]) - .finish() - } -} diff --git a/wf2_core/src/recipes/wp/services/mod.rs b/wf2_core/src/recipes/wp/services/mod.rs new file mode 100644 index 00000000..ec08916a --- /dev/null +++ b/wf2_core/src/recipes/wp/services/mod.rs @@ -0,0 +1,40 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::services::{Service, Services}; + +use wp_cli::WpCliService; +use wp_db::WpDbService; +use wp_nginx::WpNginxService; +use wp_php::WpPhpService; +use wp_php_debug::WpPhpDebugService; + +pub mod wp_cli; +pub mod wp_db; +pub mod wp_nginx; +pub mod wp_php; +pub mod wp_php_debug; + +pub struct WpServices { + pub services: Vec, +} + +impl WpServices { + pub const ROOT: &'static str = "/var/www"; + + pub fn from_ctx(ctx: &Context) -> Self { + let services = vec![ + (WpNginxService).dc_service(ctx, &()), + (WpPhpService).dc_service(ctx, &()), + (WpPhpDebugService).dc_service(ctx, &()), + (WpCliService).dc_service(ctx, &()), + (WpDbService).dc_service(ctx, &()), + ]; + + Self { services } + } +} +impl Services for WpServices { + fn dc_services(&self) -> Vec { + self.services.clone() + } +} diff --git a/wf2_core/src/recipes/wp/services/wp_cli.rs b/wf2_core/src/recipes/wp/services/wp_cli.rs new file mode 100644 index 00000000..8a696d21 --- /dev/null +++ b/wf2_core/src/recipes/wp/services/wp_cli.rs @@ -0,0 +1,24 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::recipes::wp::services::wp_db::WpDbService; +use crate::recipes::wp::services::wp_php::WpPhpService; +use crate::recipes::wp::services::WpServices; +use crate::services::Service; + +pub struct WpCliService; + +impl Service for WpCliService { + const NAME: &'static str = "wp-cli"; + const IMAGE: &'static str = "wordpress:cli"; + + fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { + DcService::new(ctx.name(), Self::NAME, Self::IMAGE) + .set_working_dir(WpServices::ROOT) + .set_init(true) + .set_depends_on(vec![WpPhpService::NAME]) + .set_volumes(vec![format!("{}:{}", ctx.cwd.display(), WpServices::ROOT)]) + // .set_volumes(vec![WpServices::PHP]) + .set_environment(vec![&format!("DB_HOST={}", WpDbService::NAME)]) + .finish() + } +} diff --git a/wf2_core/src/recipes/wp/services/wp_db.rs b/wf2_core/src/recipes/wp/services/wp_db.rs new file mode 100644 index 00000000..ca01e6d5 --- /dev/null +++ b/wf2_core/src/recipes/wp/services/wp_db.rs @@ -0,0 +1,24 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::recipes::wp::volumes::WpVolumes; +use crate::services::Service; + +pub struct WpDbService; + +impl Service for WpDbService { + const NAME: &'static str = "db"; + const IMAGE: &'static str = "mysql:5.7"; + + fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { + DcService::new(ctx.name(), Self::NAME, Self::IMAGE) + .set_volumes(vec![format!("{}:/var/lib/mysql", WpVolumes::DB)]) + .set_environment(vec![ + "MYSQL_DATABASE=docker", + "MYSQL_USER=docker", + "MYSQL_PASSWORD=docker", + "MYSQL_ROOT_PASSWORD=docker", + ]) + .set_ports(vec!["3307:3306"]) + .finish() + } +} diff --git a/wf2_core/src/recipes/wp/services/wp_nginx.rs b/wf2_core/src/recipes/wp/services/wp_nginx.rs new file mode 100644 index 00000000..09d8c874 --- /dev/null +++ b/wf2_core/src/recipes/wp/services/wp_nginx.rs @@ -0,0 +1,37 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::recipes::wp::services::wp_php::WpPhpService; +use crate::recipes::wp::services::WpServices; +use crate::recipes::wp::volumes::WpVolumeMounts; +use crate::recipes::wp::WpRecipe; +use crate::services::Service; + +pub struct WpNginxService; + +impl Service for WpNginxService { + const NAME: &'static str = "nginx"; + const IMAGE: &'static str = "wearejh/nginx:stable-m2"; + + fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { + let host_port = WpRecipe::ctx_port(&ctx); + DcService::new(ctx.name(), Self::NAME, Self::IMAGE) + .set_depends_on(vec![WpPhpService::NAME]) + .set_volumes(vec![ + format!("{}:{}", ctx.cwd.display(), WpServices::ROOT), + format!( + "{}:{}", + ctx.output_file_path(WpVolumeMounts::NGINX_CONF).display(), + WpVolumeMounts::NGINX_CONF_REMOTE + ), + format!( + "{}:{}", + ctx.output_file_path(WpVolumeMounts::NGINX_DEFAULT_HOST) + .display(), + WpVolumeMounts::NGINX_DEFAULT_REMOTE + ), + ]) + .set_working_dir(WpServices::ROOT) + .set_ports(vec![format!("{}:80", host_port)]) + .finish() + } +} diff --git a/wf2_core/src/recipes/wp/services/wp_php.rs b/wf2_core/src/recipes/wp/services/wp_php.rs new file mode 100644 index 00000000..9643f102 --- /dev/null +++ b/wf2_core/src/recipes/wp/services/wp_php.rs @@ -0,0 +1,93 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::recipes::m2::services::php::PhpService; +use crate::recipes::wp::services::wp_db::WpDbService; +use crate::recipes::wp::services::WpServices; +use crate::recipes::wp::WpRecipe; +use crate::services::Service; + +pub struct WpPhpService; + +impl Service for WpPhpService { + const NAME: &'static str = "php"; + const IMAGE: &'static str = PhpService::IMAGE_7_3; + + fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { + let domain = WpRecipe::ctx_domain(&ctx); + let php_image = (PhpService).select_image(&ctx); + DcService::new(ctx.name(), Self::NAME, php_image) + .set_volumes(vec![format!("{}:{}", ctx.cwd.display(), WpServices::ROOT)]) + .set_depends_on(vec![WpDbService::NAME]) + .set_working_dir(WpServices::ROOT) + .set_environment(vec![ + "XDEBUG_CONFIG=remote_host=host.docker.internal", + &format!("PHP_IDE_CONFIG=serverName={}", domain), + &format!("PHP_MEMORY_LIMIT=\"{}\"", "2G"), + // + // this one is here to prevent needing to modify/change the + // default bedrock setup. + // + &format!("DB_HOST={}", WpDbService::NAME), + ]) + .finish() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::dc_service::DcService; + + use crate::php::PHP; + + #[test] + fn test_wp_php_service() { + let ctx = Context::default(); + let actual_dc = (WpPhpService).dc_service(&ctx, &()); + let expected = r#" + + name: php + container_name: wf2__wf2_default__php + image: "wearejh/php:7.3-m2" + volumes: + - ".:/var/www" + depends_on: + - db + working_dir: /var/www + environment: + - XDEBUG_CONFIG=remote_host=host.docker.internal + - "PHP_IDE_CONFIG=serverName=localhost:8080" + - "PHP_MEMORY_LIMIT=\"2G\"" + - DB_HOST=db + "#; + let expected_dc: DcService = serde_yaml::from_str(expected).expect("test yaml"); + assert_eq!(actual_dc, expected_dc); + } + + #[test] + fn test_wp_php_with_version_service() { + let ctx = Context { + php_version: PHP::SevenTwo, + ..Context::default() + }; + let actual_dc = (WpPhpService).dc_service(&ctx, &()); + let expected = r#" + + name: php + container_name: wf2__wf2_default__php + image: "wearejh/php:7.2-m2" + volumes: + - ".:/var/www" + depends_on: + - db + working_dir: /var/www + environment: + - XDEBUG_CONFIG=remote_host=host.docker.internal + - "PHP_IDE_CONFIG=serverName=localhost:8080" + - "PHP_MEMORY_LIMIT=\"2G\"" + - DB_HOST=db + "#; + let expected_dc: DcService = serde_yaml::from_str(expected).expect("test yaml"); + assert_eq!(actual_dc, expected_dc); + } +} diff --git a/wf2_core/src/recipes/wp/services/wp_php_debug.rs b/wf2_core/src/recipes/wp/services/wp_php_debug.rs new file mode 100644 index 00000000..87ddeba8 --- /dev/null +++ b/wf2_core/src/recipes/wp/services/wp_php_debug.rs @@ -0,0 +1,49 @@ +use crate::context::Context; +use crate::dc_service::DcService; +use crate::recipes::m2::services::php::PhpService; +use crate::recipes::wp::services::wp_php::WpPhpService; +use crate::services::Service; + +pub struct WpPhpDebugService; + +impl Service for WpPhpDebugService { + const NAME: &'static str = "php-debug"; + const IMAGE: &'static str = PhpService::IMAGE_7_3; + + fn dc_service(&self, ctx: &Context, _vars: &()) -> DcService { + let mut php_cnt = (WpPhpService).dc_service(ctx, &()); + { + php_cnt.set_environment(vec!["XDEBUG_ENABLE=true"]); + php_cnt.set_name(WpPhpDebugService::NAME); + php_cnt.set_container_name(ctx.name(), WpPhpDebugService::NAME); + } + php_cnt + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::dc_service::DcService; + + #[test] + fn test_wp_php_debug_service() { + let ctx = Context::default(); + let actual_dc = (WpPhpDebugService).dc_service(&ctx, &()); + let expected = r#" + + name: php-debug + container_name: wf2__wf2_default__php-debug + image: "wearejh/php:7.3-m2" + volumes: + - ".:/var/www" + depends_on: + - db + working_dir: /var/www + environment: + - XDEBUG_ENABLE=true + "#; + let expected_dc: DcService = serde_yaml::from_str(expected).expect("test yaml"); + assert_eq!(actual_dc, expected_dc); + } +} diff --git a/wf2_core/src/recipes/wp/subcommands/wp_up.rs b/wf2_core/src/recipes/wp/subcommands/wp_up.rs index 63a41e02..cb4587eb 100644 --- a/wf2_core/src/recipes/wp/subcommands/wp_up.rs +++ b/wf2_core/src/recipes/wp/subcommands/wp_up.rs @@ -2,7 +2,7 @@ use crate::commands::CliCommand; use crate::context::Context; use crate::dc_tasks::DcTasksTrait; -use crate::recipes::wp::services::WpVolumeMounts; +use crate::recipes::wp::volumes::WpVolumeMounts; use crate::recipes::wp::WpRecipe; use crate::task::Task; use crate::tasks::docker_clean::docker_clean; diff --git a/wf2_core/src/recipes/wp/volumes.rs b/wf2_core/src/recipes/wp/volumes.rs index 8c4677a2..4a1ae5ca 100644 --- a/wf2_core/src/recipes/wp/volumes.rs +++ b/wf2_core/src/recipes/wp/volumes.rs @@ -10,3 +10,13 @@ impl WpVolumes { pub fn get_volumes(ctx: &Context) -> Vec { vec![DcVolume::new(ctx.name(), WpVolumes::DB)] } + +pub struct WpVolumeMounts; + +impl WpVolumeMounts { + pub const NGINX_CONF: &'static str = "nginx/nginx.conf"; + pub const NGINX_CONF_REMOTE: &'static str = "/etc/nginx/nginx.conf"; + + pub const NGINX_DEFAULT_HOST: &'static str = "nginx/host.conf"; + pub const NGINX_DEFAULT_REMOTE: &'static str = "/etc/nginx/conf.d/default.conf"; +}