Skip to content

Commit

Permalink
Merge pull request #115 from WeareJH/wp-fixes
Browse files Browse the repository at this point in the history
fix: prevent name collision when re-using the php service for WP
  • Loading branch information
shakyShane authored Jun 23, 2020
2 parents e05b31b + 56694b6 commit adcd268
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 156 deletions.
10 changes: 5 additions & 5 deletions wf2_core/src/recipes/wp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn CliCommand<'a, 'b>>> {
wp_recipe_subcommands()
Expand Down Expand Up @@ -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;
150 changes: 0 additions & 150 deletions wf2_core/src/recipes/wp/services.rs

This file was deleted.

40 changes: 40 additions & 0 deletions wf2_core/src/recipes/wp/services/mod.rs
Original file line number Diff line number Diff line change
@@ -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<DcService>,
}

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<DcService> {
self.services.clone()
}
}
24 changes: 24 additions & 0 deletions wf2_core/src/recipes/wp/services/wp_cli.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
24 changes: 24 additions & 0 deletions wf2_core/src/recipes/wp/services/wp_db.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
37 changes: 37 additions & 0 deletions wf2_core/src/recipes/wp/services/wp_nginx.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
93 changes: 93 additions & 0 deletions wf2_core/src/recipes/wp/services/wp_php.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit adcd268

Please sign in to comment.