From 2eb624b7719e9a1603f1133315a5d3440a67d814 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:36:28 +0530 Subject: [PATCH 01/15] Add volume cleanup Signed-off-by: Riddhesh Sanghvi --- src/helper/class-ee-site.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 25a6833b..457344ec 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -5,6 +5,7 @@ use EE; use EE\Model\Site; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; use function EE\Site\Utils\auto_site_name; use function EE\Site\Utils\get_site_info; use function EE\Site\Utils\reload_global_nginx_proxy; @@ -197,6 +198,15 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = [] } } + $volumes_dir = '/var/lib/docker/volumes'; + $volume_prefix = \EE\Site\Utils\get_site_prefix( $site_url ); + $finder = new Finder(); + $finder->in( $volumes_dir ); + $volumes_to_delete = $finder->directories()->name( $volume_prefix . '*' ); + foreach ( iterator_to_array( $volumes_to_delete, true ) as $volume ) { + \EE::exec( 'docker volume rm ' . basename( $volume ) ); + } + if ( ! empty( $db_data['db_host'] ) ) { \EE\Site\Utils\cleanup_db( $db_data['db_host'], $db_data['db_name'] ); \EE\Site\Utils\cleanup_db_user( $db_data['db_host'], $db_data['db_user'] ); From b15db269fb98cba2d8ac7f15642cfdf3624a4490 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:44:58 +0530 Subject: [PATCH 02/15] Add utility function to get prefix and create volumes Signed-off-by: Riddhesh Sanghvi --- src/helper/site-utils.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 4fc4723d..d4d0bd26 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -476,3 +476,31 @@ function get_global_auth() { ]; } + +/** + * Gets a dockerized prefix created for site. + * + * @param string $site_url Name of the site. + * + * @return string prefix derived from the name. + */ +function get_site_prefix( $site_url ) { + return str_replace( [ '.', '-' ], '', $site_url ); +} + +/** + * Function to create external docker volumes and related symlinks. + * + * @param string $volume_prefix Prefix with which external volume needs to be created. + * @param array $volumes The volumes to be created. + * $volumes[$key]['name'] => specifies the name of volume to be created + * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. + */ +function create_volumes( $volume_prefix, $volumes ) { + $fs = new Filesystem(); + foreach ( $volumes as $volume ) { + $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); + \EE::exec( sprintf( 'docker volume create %s_%s', $volume_prefix, $volume['name'] ) ); + $fs->symlink( sprintf( '/var/lib/docker/volumes/%s_%s/_data', $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); + } +} From 85cd76dbe90ea0dede4fd02f3ceef9600c251df5 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:48:12 +0530 Subject: [PATCH 03/15] Update volume mountings Signed-off-by: Riddhesh Sanghvi --- src/site-type/Site_HTML_Docker.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/site-type/Site_HTML_Docker.php b/src/site-type/Site_HTML_Docker.php index 150508e6..5d86c1db 100644 --- a/src/site-type/Site_HTML_Docker.php +++ b/src/site-type/Site_HTML_Docker.php @@ -38,10 +38,9 @@ public function generate_docker_compose_yml( array $filters = [] ) { } $nginx['volumes'] = [ 'vol' => [ - [ 'name' => './app:/var/www' ], - [ 'name' => './config/nginx/main.conf:/etc/nginx/conf.d/default.conf' ], - [ 'name' => './config/nginx/custom:/etc/nginx/custom' ], - [ 'name' => './logs/nginx:/var/log/nginx' ], + [ 'name' => 'htdocs:/var/www' ], + [ 'name' => 'config_nginx:/etc/nginx' ], + [ 'name' => 'log_nginx:/var/log/nginx' ], ], ]; $nginx['labels'] = [ @@ -66,8 +65,8 @@ public function generate_docker_compose_yml( array $filters = [] ) { $base[] = $nginx; $binding = [ - 'services' => $base, - 'network' => [ + 'services' => $base, + 'network' => [ 'networks_labels' => [ 'label' => [ [ 'name' => 'org.label-schema.vendor=EasyEngine' ], @@ -75,6 +74,9 @@ public function generate_docker_compose_yml( array $filters = [] ) { ], ], ], + 'created_volumes' => [ + 'prefix' => $filters['site_prefix'], + ], ]; $docker_compose_yml = mustache_render( SITE_TEMPLATE_ROOT . '/docker-compose.mustache', $binding ); From 7366d42a111525f730bfcf04cda28b810c540fe7 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:52:20 +0530 Subject: [PATCH 04/15] Update configuration of files and starting of containers for new mountings Signed-off-by: Riddhesh Sanghvi --- src/site-type/html.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/site-type/html.php b/src/site-type/html.php index 33d0fa80..a46ffceb 100644 --- a/src/site-type/html.php +++ b/src/site-type/html.php @@ -155,15 +155,24 @@ private function configure_site_files() { $site_conf_dir = $this->site_data['site_fs_path'] . '/config'; $site_conf_env = $this->site_data['site_fs_path'] . '/.env'; - $site_nginx_default_conf = $site_conf_dir . '/nginx/main.conf'; + $site_nginx_default_conf = $site_conf_dir . '/nginx/default.conf'; $site_src_dir = $this->site_data['site_fs_path'] . '/app/htdocs'; $process_user = posix_getpwuid( posix_geteuid() ); $custom_conf_dest = $site_conf_dir . '/nginx/custom/user.conf'; $custom_conf_source = SITE_TEMPLATE_ROOT . '/config/nginx/user.conf.mustache'; + $volume_prefix = \EE\Site\Utils\get_site_prefix( $this->site_data['site_url'] ); + $volumes = [ + [ 'name' => 'htdocs', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app' ], + [ 'name' => 'config_nginx', 'path_to_symlink' => dirname( $site_nginx_default_conf ) ], + [ 'name' => 'log_nginx', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx' ], + ]; + \EE::log( sprintf( 'Creating site %s.', $this->site_data['site_url'] ) ); \EE::log( 'Copying configuration files.' ); + \EE\Site\Utils\create_volumes( $volume_prefix, $volumes ); + $default_conf_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx/main.conf.mustache', [ 'server_name' => $this->site_data['site_url'] ] ); $env_data = [ @@ -176,9 +185,12 @@ 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'] ); $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->remove( $this->site_data['site_fs_path'] . '/config/nginx/conf.d' ); + \EE::exec( 'docker-compose restart nginx' ); $index_data = [ 'version' => 'v' . EE_VERSION, 'site_src_root' => $this->site_data['site_fs_path'] . '/app/htdocs', @@ -201,8 +213,9 @@ protected function dump_docker_compose_yml( $additional_filters = [] ) { $site_docker_yml = $this->site_data['site_fs_path'] . '/docker-compose.yml'; - $filter = []; - $filter[] = $this->site_data['site_type']; + $filter = []; + $filter[] = $this->site_data['site_type']; + $filter['site_prefix'] = \EE\Site\Utils\get_site_prefix( $this->site_data['site_url'] ); foreach ( $additional_filters as $key => $addon_filter ) { $filter[ $key ] = $addon_filter; @@ -225,8 +238,6 @@ private function create_site() { $this->level = 3; $this->configure_site_files(); - \EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'] ); - \EE\Site\Utils\create_etc_hosts_entry( $this->site_data['site_url'] ); if ( ! $this->skip_status_check ) { $this->level = 4; From 7668ff9671efd18eb449957adb142effea50c59a Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:53:06 +0530 Subject: [PATCH 05/15] Remove unnecessary inclusion post new nginx image update Signed-off-by: Riddhesh Sanghvi --- templates/config/nginx/main.conf.mustache | 2 -- 1 file changed, 2 deletions(-) diff --git a/templates/config/nginx/main.conf.mustache b/templates/config/nginx/main.conf.mustache index 37d7a6fc..a44627c4 100644 --- a/templates/config/nginx/main.conf.mustache +++ b/templates/config/nginx/main.conf.mustache @@ -65,6 +65,4 @@ server { {{! /locations.conf }} client_max_body_size 100m; - - include /etc/nginx/custom/*.conf; } From 782d5cf47d5a7ab141cc3388cd60cc716fc0eb2f Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 13:53:40 +0530 Subject: [PATCH 06/15] Update volume block in docker-compose template Signed-off-by: Riddhesh Sanghvi --- templates/docker-compose.mustache | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/templates/docker-compose.mustache b/templates/docker-compose.mustache index 1683d3f5..15e0a71b 100644 --- a/templates/docker-compose.mustache +++ b/templates/docker-compose.mustache @@ -51,6 +51,19 @@ services: {{/networks}} {{/services}} +{{#created_volumes}} +volumes: + htdocs: + external: + name: {{prefix}}_htdocs + config_nginx: + external: + name: {{prefix}}_config_nginx + log_nginx: + external: + name: {{prefix}}_log_nginx +{{/created_volumes}} + {{#network}} networks: site-network: From ce33dc51a2388bcffec405f3733a619ee835ccd7 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 14:50:23 +0530 Subject: [PATCH 07/15] Add labels in volume creation Signed-off-by: Riddhesh Sanghvi --- src/helper/site-utils.php | 19 +++++++++++++++---- src/site-type/html.php | 5 ++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index d4d0bd26..9187c0d8 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -491,16 +491,27 @@ function get_site_prefix( $site_url ) { /** * Function to create external docker volumes and related symlinks. * - * @param string $volume_prefix Prefix with which external volume needs to be created. + * @param string $site_url Site for which volumes have to be created. * @param array $volumes The volumes to be created. * $volumes[$key]['name'] => specifies the name of volume to be created * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. */ -function create_volumes( $volume_prefix, $volumes ) { - $fs = new Filesystem(); +function create_volumes( $site_url, $volumes ) { + $volume_prefix = get_site_prefix( $site_url ); + $fs = new Filesystem(); foreach ( $volumes as $volume ) { $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); - \EE::exec( sprintf( 'docker volume create %s_%s', $volume_prefix, $volume['name'] ) ); + \EE::exec( + sprintf( + 'docker volume create \ + --label "org.label-schema.vendor=EasyEngine" \ + --label "io.easyengine.site=%s" \ + %s_%s', + $site_url, + $volume_prefix, + $volume['name'] + ) + ); $fs->symlink( sprintf( '/var/lib/docker/volumes/%s_%s/_data', $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); } } diff --git a/src/site-type/html.php b/src/site-type/html.php index a46ffceb..c83e1d75 100644 --- a/src/site-type/html.php +++ b/src/site-type/html.php @@ -161,8 +161,7 @@ private function configure_site_files() { $custom_conf_dest = $site_conf_dir . '/nginx/custom/user.conf'; $custom_conf_source = SITE_TEMPLATE_ROOT . '/config/nginx/user.conf.mustache'; - $volume_prefix = \EE\Site\Utils\get_site_prefix( $this->site_data['site_url'] ); - $volumes = [ + $volumes = [ [ 'name' => 'htdocs', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app' ], [ 'name' => 'config_nginx', 'path_to_symlink' => dirname( $site_nginx_default_conf ) ], [ 'name' => 'log_nginx', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx' ], @@ -171,7 +170,7 @@ private function configure_site_files() { \EE::log( sprintf( 'Creating site %s.', $this->site_data['site_url'] ) ); \EE::log( 'Copying configuration files.' ); - \EE\Site\Utils\create_volumes( $volume_prefix, $volumes ); + \EE\Site\Utils\create_volumes( $this->site_data['site_url'], $volumes ); $default_conf_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx/main.conf.mustache', [ 'server_name' => $this->site_data['site_url'] ] ); From 471902bc6bd69395e4cdf0a02008f5445ad0a61f Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 14:58:36 +0530 Subject: [PATCH 08/15] Use labels to delete volumes Signed-off-by: Riddhesh Sanghvi --- src/helper/class-ee-site.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 457344ec..d433f034 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -198,13 +198,10 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = [] } } - $volumes_dir = '/var/lib/docker/volumes'; - $volume_prefix = \EE\Site\Utils\get_site_prefix( $site_url ); - $finder = new Finder(); - $finder->in( $volumes_dir ); - $volumes_to_delete = $finder->directories()->name( $volume_prefix . '*' ); - foreach ( iterator_to_array( $volumes_to_delete, true ) as $volume ) { - \EE::exec( 'docker volume rm ' . basename( $volume ) ); + $launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $site_url ) ); + $volumes = explode( PHP_EOL, trim( $launch->stdout ) ); + foreach ( $volumes as $volume ) { + \EE::exec( 'docker volume rm ' . $volume ); } if ( ! empty( $db_data['db_host'] ) ) { From c0c08c3c765ea34719021bc23a57c85665e1d25a Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 15:34:14 +0530 Subject: [PATCH 09/15] Update fetching root docker directory Signed-off-by: Riddhesh Sanghvi --- src/helper/site-utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 9187c0d8..9c534899 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -512,6 +512,8 @@ function create_volumes( $site_url, $volumes ) { $volume['name'] ) ); - $fs->symlink( sprintf( '/var/lib/docker/volumes/%s_%s/_data', $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); + $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); + $docker_root_dir = trim( $launch->stdout ); + $fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); } } From 7f21d0744f2c188b07520b1c348a492024b06097 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 16:14:50 +0530 Subject: [PATCH 10/15] Update mustache templating Signed-off-by: Riddhesh Sanghvi --- src/site-type/Site_HTML_Docker.php | 12 +++++++++--- templates/docker-compose.mustache | 12 ++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/site-type/Site_HTML_Docker.php b/src/site-type/Site_HTML_Docker.php index 5d86c1db..0a2d6397 100644 --- a/src/site-type/Site_HTML_Docker.php +++ b/src/site-type/Site_HTML_Docker.php @@ -62,6 +62,14 @@ public function generate_docker_compose_yml( array $filters = [] ) { ] ]; + $volumes = [ + 'external_vols' => [ + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'htdocs' ], + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'config_nginx' ], + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'log_nginx' ], + ], + ]; + $base[] = $nginx; $binding = [ @@ -74,9 +82,7 @@ public function generate_docker_compose_yml( array $filters = [] ) { ], ], ], - 'created_volumes' => [ - 'prefix' => $filters['site_prefix'], - ], + 'created_volumes' => $volumes, ]; $docker_compose_yml = mustache_render( SITE_TEMPLATE_ROOT . '/docker-compose.mustache', $binding ); diff --git a/templates/docker-compose.mustache b/templates/docker-compose.mustache index 15e0a71b..a03e5fa1 100644 --- a/templates/docker-compose.mustache +++ b/templates/docker-compose.mustache @@ -53,15 +53,11 @@ services: {{#created_volumes}} volumes: - htdocs: + {{#external_vols}} + {{ext_vol_name}}: external: - name: {{prefix}}_htdocs - config_nginx: - external: - name: {{prefix}}_config_nginx - log_nginx: - external: - name: {{prefix}}_log_nginx + name: {{prefix}}_{{ext_vol_name}} + {{/external_vols}} {{/created_volumes}} {{#network}} From 8d5b2c756e6ce163a7bac0ab7113cae320dc7d9c Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 23 Oct 2018 17:29:25 +0530 Subject: [PATCH 11/15] Remove same call out of loop Signed-off-by: Riddhesh Sanghvi --- src/helper/site-utils.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 9c534899..22113ca6 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -497,8 +497,12 @@ function get_site_prefix( $site_url ) { * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. */ function create_volumes( $site_url, $volumes ) { - $volume_prefix = get_site_prefix( $site_url ); - $fs = new Filesystem(); + + $volume_prefix = get_site_prefix( $site_url ); + $fs = new Filesystem(); + $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); + $docker_root_dir = trim( $launch->stdout ); + foreach ( $volumes as $volume ) { $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); \EE::exec( @@ -512,8 +516,6 @@ function create_volumes( $site_url, $volumes ) { $volume['name'] ) ); - $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); - $docker_root_dir = trim( $launch->stdout ); $fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); } } From dd6cb05cb57d637d4b3379229a6d11a558b2cea5 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Wed, 24 Oct 2018 09:47:08 +0530 Subject: [PATCH 12/15] Remove extra use statement Co-Authored-By: mrrobot47 --- src/helper/class-ee-site.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index d433f034..411f1929 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -5,7 +5,6 @@ use EE; use EE\Model\Site; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Finder\Finder; use function EE\Site\Utils\auto_site_name; use function EE\Site\Utils\get_site_info; use function EE\Site\Utils\reload_global_nginx_proxy; From f5626226411470255d5dcd33ef6573153b4711a2 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Wed, 24 Oct 2018 09:47:30 +0530 Subject: [PATCH 13/15] Update nginx conf filename Co-Authored-By: mrrobot47 --- src/site-type/html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/site-type/html.php b/src/site-type/html.php index c83e1d75..49781cb5 100644 --- a/src/site-type/html.php +++ b/src/site-type/html.php @@ -155,7 +155,7 @@ private function configure_site_files() { $site_conf_dir = $this->site_data['site_fs_path'] . '/config'; $site_conf_env = $this->site_data['site_fs_path'] . '/.env'; - $site_nginx_default_conf = $site_conf_dir . '/nginx/default.conf'; + $site_nginx_default_conf = $site_conf_dir . '/nginx/main.conf'; $site_src_dir = $this->site_data['site_fs_path'] . '/app/htdocs'; $process_user = posix_getpwuid( posix_geteuid() ); $custom_conf_dest = $site_conf_dir . '/nginx/custom/user.conf'; From d68ba9b087978659ebf7e047abd7da178ea07655 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 24 Oct 2018 09:52:52 +0530 Subject: [PATCH 14/15] Add comment for getting docker root dir Signed-off-by: Riddhesh Sanghvi --- src/helper/site-utils.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 22113ca6..c2c211a0 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -498,8 +498,10 @@ function get_site_prefix( $site_url ) { */ function create_volumes( $site_url, $volumes ) { - $volume_prefix = get_site_prefix( $site_url ); - $fs = new Filesystem(); + $volume_prefix = get_site_prefix( $site_url ); + $fs = new Filesystem(); + + // This command will get the root directory for Docker, generally `/var/lib/docker`. $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); $docker_root_dir = trim( $launch->stdout ); From db30b68e2fe2aec8cfbc608cbca78007c2fb51be Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 24 Oct 2018 12:40:18 +0530 Subject: [PATCH 15/15] Move functions to core and update usage Signed-off-by: Riddhesh Sanghvi --- src/helper/class-ee-site.php | 3 +-- src/helper/site-utils.php | 45 ------------------------------------ src/site-type/html.php | 4 ++-- 3 files changed, 3 insertions(+), 49 deletions(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 411f1929..0d67396b 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -197,8 +197,7 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = [] } } - $launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $site_url ) ); - $volumes = explode( PHP_EOL, trim( $launch->stdout ) ); + $volumes = \EE::docker()::get_volumes_by_label( $site_url ); foreach ( $volumes as $volume ) { \EE::exec( 'docker volume rm ' . $volume ); } diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index c2c211a0..4fc4723d 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -476,48 +476,3 @@ function get_global_auth() { ]; } - -/** - * Gets a dockerized prefix created for site. - * - * @param string $site_url Name of the site. - * - * @return string prefix derived from the name. - */ -function get_site_prefix( $site_url ) { - return str_replace( [ '.', '-' ], '', $site_url ); -} - -/** - * Function to create external docker volumes and related symlinks. - * - * @param string $site_url Site for which volumes have to be created. - * @param array $volumes The volumes to be created. - * $volumes[$key]['name'] => specifies the name of volume to be created - * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. - */ -function create_volumes( $site_url, $volumes ) { - - $volume_prefix = get_site_prefix( $site_url ); - $fs = new Filesystem(); - - // This command will get the root directory for Docker, generally `/var/lib/docker`. - $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); - $docker_root_dir = trim( $launch->stdout ); - - foreach ( $volumes as $volume ) { - $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); - \EE::exec( - sprintf( - 'docker volume create \ - --label "org.label-schema.vendor=EasyEngine" \ - --label "io.easyengine.site=%s" \ - %s_%s', - $site_url, - $volume_prefix, - $volume['name'] - ) - ); - $fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); - } -} diff --git a/src/site-type/html.php b/src/site-type/html.php index 49781cb5..e023bed3 100644 --- a/src/site-type/html.php +++ b/src/site-type/html.php @@ -170,7 +170,7 @@ private function configure_site_files() { \EE::log( sprintf( 'Creating site %s.', $this->site_data['site_url'] ) ); \EE::log( 'Copying configuration files.' ); - \EE\Site\Utils\create_volumes( $this->site_data['site_url'], $volumes ); + $this->docker->create_volumes( $this->site_data['site_url'], $volumes ); $default_conf_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx/main.conf.mustache', [ 'server_name' => $this->site_data['site_url'] ] ); @@ -214,7 +214,7 @@ protected function dump_docker_compose_yml( $additional_filters = [] ) { $filter = []; $filter[] = $this->site_data['site_type']; - $filter['site_prefix'] = \EE\Site\Utils\get_site_prefix( $this->site_data['site_url'] ); + $filter['site_prefix'] = $this->docker->get_docker_style_prefix( $this->site_data['site_url'] ); foreach ( $additional_filters as $key => $addon_filter ) { $filter[ $key ] = $addon_filter;