Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 42 additions & 47 deletions src/Site_WP_Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class Site_WP_Docker {
* @param array $filters Array of flags to determine the docker-compose.yml generation.
* Empty/Default -> Generates default WordPress docker-compose.yml
* ['le'] -> Enables letsencrypt in the generation.
* @param array $volumes Array containing volume info passable to \EE_DOCKER::get_mounting_volume_array().
*
* @return String docker-compose.yml content string.
*/
public function generate_docker_compose_yml( array $filters = [] ) {
public function generate_docker_compose_yml( array $filters = [], $volumes ) {
$img_versions = \EE\Utils\get_image_versions();
$base = [];

Expand All @@ -26,31 +27,31 @@ public function generate_docker_compose_yml( array $filters = [] ) {
],
];

// db configuration.
$db['service_name'] = [ 'name' => 'db' ];
$db['image'] = [ 'name' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'] ];
$db['restart'] = $restart_default;
$db['labels'] = [
'label' => [
'name' => 'io.easyengine.site=${VIRTUAL_HOST}',
],
];
$db['volumes'] = [
[
'vol' => [
'name' => 'data_db:/var/lib/mysql',
if ( in_array( 'db', $filters, true ) ) {
// db configuration.
$db['service_name'] = [ 'name' => 'db' ];
$db['image'] = [ 'name' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'] ];
$db['restart'] = $restart_default;
$db['labels'] = [
'label' => [
'name' => 'io.easyengine.site=${VIRTUAL_HOST}',
],
],
];
$db['environment'] = [
'env' => [
[ 'name' => 'MYSQL_ROOT_PASSWORD' ],
[ 'name' => 'MYSQL_DATABASE' ],
[ 'name' => 'MYSQL_USER' ],
[ 'name' => 'MYSQL_PASSWORD' ],
],
];
$db['networks'] = $network_default;
];
$db['volumes'] = [
[
'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['db'] ),
],
];
$db['environment'] = [
'env' => [
[ 'name' => 'MYSQL_ROOT_PASSWORD' ],
[ 'name' => 'MYSQL_DATABASE' ],
[ 'name' => 'MYSQL_USER' ],
[ 'name' => 'MYSQL_PASSWORD' ],
],
];
$db['networks'] = $network_default;
}
// PHP configuration.
$php['service_name'] = [ 'name' => 'php' ];
$php['image'] = [ 'name' => 'easyengine/php:' . $img_versions['easyengine/php'] ];
Expand All @@ -71,11 +72,7 @@ public function generate_docker_compose_yml( array $filters = [] ) {
];
$php['volumes'] = [
[
'vol' => [
[ 'name' => 'htdocs:/var/www' ],
[ 'name' => 'config_php:/usr/local/etc' ],
[ 'name' => 'log_php:/var/log/php' ],
],
'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['php'] ),
],
];
$php['environment'] = [
Expand Down Expand Up @@ -126,11 +123,7 @@ public function generate_docker_compose_yml( array $filters = [] ) {
$nginx['environment']['env'][] = [ 'name' => 'HTTPS_METHOD=nohttps' ];
}
$nginx['volumes'] = [
'vol' => [
[ 'name' => 'htdocs:/var/www' ],
[ 'name' => 'config_nginx:/usr/local/openresty/nginx/conf' ],
[ 'name' => 'log_nginx:/var/log/nginx' ],
],
'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['nginx'] ),
];
$nginx['labels'] = [
'label' => [
Expand Down Expand Up @@ -195,12 +188,7 @@ public function generate_docker_compose_yml( array $filters = [] ) {
],
];
$postfix['volumes'] = [
'vol' => [
[ 'name' => '/dev/log:/dev/log' ],
[ 'name' => 'data_postfix:/var/spool/postfix' ],
[ 'name' => 'ssl_postfix:/etc/ssl/postfix' ],
[ 'name' => 'config_postfix:/etc/postfix' ],
],
'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['postfix'] ),
];
$postfix['networks'] = $network_default;

Expand All @@ -223,7 +211,7 @@ public function generate_docker_compose_yml( array $filters = [] ) {
$base[] = $redis;
}

$volumes = [
$external_volumes = [
'external_vols' => [
[ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'htdocs' ],
[ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'config_nginx' ],
Expand All @@ -246,18 +234,25 @@ public function generate_docker_compose_yml( array $filters = [] ) {
];

if ( in_array( 'db', $filters, true ) ) {
$base[] = $db;
$volumes['external_vols'][] = [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'data_db' ];
$base[] = $db;
$external_volumes['external_vols'] = array_merge( $external_volumes['external_vols'], [
[ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_data' ],
[ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_conf' ],
[ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_logs' ],
] );
} else {
$network['enable_backend_network'] = true;
}

$binding = [
'services' => $base,
'network' => $network,
'created_volumes' => $volumes,
'services' => $base,
'network' => $network,
];

if ( ! IS_DARWIN ) {
$binding['created_volumes'] = $external_volumes;
}

$docker_compose_yml = mustache_render( SITE_WP_TEMPLATE_ROOT . '/docker-compose.mustache', $binding );

return $docker_compose_yml;
Expand Down
163 changes: 131 additions & 32 deletions src/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,38 +428,9 @@ private function configure_site_files() {
$custom_conf_source = SITE_WP_TEMPLATE_ROOT . '/config/nginx/user.conf.mustache';
$process_user = posix_getpwuid( posix_geteuid() );

$volumes = [
[ 'name' => 'htdocs', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app' ],
[ 'name' => 'config_nginx', 'path_to_symlink' => dirname( dirname( $site_nginx_default_conf ) ) ],
[ 'name' => 'config_php', 'path_to_symlink' => dirname( dirname( $site_php_ini ) ) ],
[ 'name' => 'log_php', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/php' ],
[ 'name' => 'log_nginx', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx' ],
[
'name' => 'data_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/spool'
],
[
'name' => 'ssl_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/ssl'
],
[
'name' => 'config_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/config/postfix'
],
];

if ( 'db' === $this->site_data['db_host'] ) {
$volumes[] = [
'name' => 'data_db',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/db'
];
}

\EE::log( 'Creating WordPress site ' . $this->site_data['site_url'] );
\EE::log( 'Copying configuration files.' );

$this->docker->create_volumes( $this->site_data['site_url'], $volumes );

$default_conf_content = $this->generate_default_conf( $this->site_data['app_sub_type'], $this->cache_type, $server_name );
$local = ( 'db' === $this->site_data['db_host'] ) ? true : false;

Expand Down Expand Up @@ -489,13 +460,23 @@ private function configure_site_files() {
try {
$this->dump_docker_compose_yml( [ 'nohttps' => true ] );
$this->fs->dumpFile( $site_conf_env, $env_content );
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'postfix' ] );
if ( ! IS_DARWIN ) {
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'postfix' ] );
}
\EE\Site\Utils\set_postfix_files( $this->site_data['site_url'], $this->site_data['site_fs_path'] . '/services' );
$this->fs->dumpFile( $site_nginx_default_conf, $default_conf_content );
$this->fs->copy( $custom_conf_source, $custom_conf_dest );
$this->fs->remove( $this->site_data['site_fs_path'] . '/app/html' );
$this->fs->dumpFile( $site_php_ini, $php_ini_content );
\EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] );
if ( IS_DARWIN ) {
if ( 'db' === $this->site_data['db_host'] ) {
$db_conf_file = $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf';
$this->fs->copy( SITE_WP_TEMPLATE_ROOT . '/my.cnf.mustache', $db_conf_file );
}
\EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php', 'postfix' ] );
} else {
\EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] );
}
} catch ( \Exception $e ) {
$this->catch_clean( $e );
}
Expand All @@ -505,10 +486,128 @@ private function configure_site_files() {
* Generate and place docker-compose.yml file.
*
* @param array $additional_filters Filters to alter docker-compose file.
*
* @ignorecommand
*/
public function dump_docker_compose_yml( $additional_filters = [] ) {

$site_conf_dir = $this->site_data['site_fs_path'] . '/config';
$site_nginx_default_conf = $site_conf_dir . '/nginx/conf.d/main.conf';
$site_php_ini = $site_conf_dir . '/php/php/php.ini';

$volumes = [
'nginx' => [
[
'name' => 'htdocs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/app',
'container_path' => '/var/www',
],
[
'name' => 'config_nginx',
'path_to_symlink' => dirname( dirname( $site_nginx_default_conf ) ),
'container_path' => '/usr/local/openresty/nginx/conf',
'skip_darwin' => true,
],
[
'name' => 'config_nginx',
'path_to_symlink' => $site_nginx_default_conf,
'container_path' => '/usr/local/openresty/nginx/conf/conf.d/main.conf',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'log_nginx',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx',
'container_path' => '/var/log/nginx',
],
],
'php' => [
[
'name' => 'htdocs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/app',
'container_path' => '/var/www',
],
[
'name' => 'config_php',
'path_to_symlink' => dirname( dirname( $site_php_ini ) ),
'container_path' => '/usr/local/etc',
'skip_darwin' => true,
],
[
'name' => 'config_php',
'path_to_symlink' => $site_php_ini,
'container_path' => '/usr/local/etc/php/php/php.ini',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'log_php',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/php',
'container_path' => '/var/log/php',
],

],
'postfix' => [
[
'name' => '/dev/log',
'path_to_symlink' => '/dev/log',
'container_path' => '/dev/log',
'skip_volume' => true,
'skip_darwin' => true,
],
[
'name' => 'data_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/spool',
'container_path' => '/var/spool/postfix',
],
[
'name' => 'ssl_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/ssl',
'container_path' => '/etc/ssl/postfix',
],
[
'name' => 'config_postfix',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/config/postfix',
'container_path' => '/etc/postfix',
'skip_darwin' => true,
],
],
];

if ( 'db' === $this->site_data['db_host'] ) {
$volumes['db'] = [
[
'name' => 'db_data',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/data',
'container_path' => '/var/lib/mysql',
],
[
'name' => 'db_conf',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf',
'container_path' => '/etc/mysql',
'skip_darwin' => true,
],
[
'name' => 'db_conf',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf',
'container_path' => '/etc/mysql/my.cnf',
'skip_linux' => true,
'skip_volume' => true,
],
[
'name' => 'db_logs',
'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/logs',
'container_path' => '/var/log/mysql',
],
];
}

if ( ! IS_DARWIN && empty( $this->docker->get_volumes_by_label( $this->site_data['site_url'] ) ) ) {
foreach ( $volumes as $volume ) {
$this->docker->create_volumes( $this->site_data['site_url'], $volume );
}
}

$site_docker_yml = $this->site_data['site_fs_path'] . '/docker-compose.yml';

$filter = [];
Expand All @@ -523,7 +622,7 @@ public function dump_docker_compose_yml( $additional_filters = [] ) {
$filter[ $key ] = $addon_filter;
}

$docker_compose_content = $site_docker->generate_docker_compose_yml( $filter );
$docker_compose_content = $site_docker->generate_docker_compose_yml( $filter, $volumes );
$this->fs->dumpFile( $site_docker_yml, $docker_compose_content );
}

Expand Down
2 changes: 1 addition & 1 deletion templates/docker-compose.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ services:
{{#volumes}}
volumes:
{{#vol}}
- "{{name}}"
- "{{.}}"
{{/vol}}
{{/volumes}}
{{#environment}}
Expand Down
Loading