From eb3f8334ea61bbda5466ae56a5539b2a7e997851 Mon Sep 17 00:00:00 2001 From: dhsathiya Date: Mon, 3 Aug 2020 11:40:07 +0530 Subject: [PATCH 1/8] Add variable for custom docker-compose file for site Signed-off-by: dhsathiya --- php/EE/Runner.php | 1 + 1 file changed, 1 insertion(+) diff --git a/php/EE/Runner.php b/php/EE/Runner.php index e45a46ac6..026306339 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -56,6 +56,7 @@ private function init_ee() { define( 'DB', EE_ROOT_DIR . '/db/ee.sqlite' ); define( 'LOCALHOST_IP', '127.0.0.1' ); + define( 'SITE_CUSTOM_DOCKER_COMPOSE', 'docker-compose-custom.yml' ); $db_dir = dirname( DB ); if ( ! is_dir( $db_dir ) ) { From 79fabf232989345cdda703ae52966230159ffc4c Mon Sep 17 00:00:00 2001 From: dhsathiya Date: Mon, 3 Aug 2020 11:46:11 +0530 Subject: [PATCH 2/8] Update docker_compose_up for custom docker-compose file Signed-off-by: dhsathiya --- php/class-ee-docker.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index 8c0be97b2..eb90ff6d4 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -159,14 +159,18 @@ public static function disconnect_site_network_from( $site_name, $from_container * @return bool success. */ public static function docker_compose_up( $dir, $services = [] ) { + $fs = new Filesystem(); $chdir_return_code = chdir( $dir ); if ( $chdir_return_code ) { if ( empty( $services ) ) { return EE::exec( 'docker-compose up -d' ); } else { $all_services = implode( ' ', $services ); - - return EE::exec( "docker-compose up -d $all_services" ); + if ( $fs->exists( SITE_CUSTOM_DOCKER_COMPOSE ) ) { + return EE::exec( 'docker-compose -f docker-compose.yml -f ' . SITE_CUSTOM_DOCKER_COMPOSE . " up -d $all_services" ); + } else { + return EE::exec( "docker-compose up -d $all_services" ); + } } } From 9096530c2aaeab0b56735501f948596c11febb6f Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Wed, 30 Sep 2020 12:07:09 +0530 Subject: [PATCH 3/8] Add custom docker-compose in class-ee-docker.php --- php/class-ee-docker.php | 12 +++++------- php/utils.php | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index bbf3bd200..dfbd9639b 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -1,6 +1,7 @@ exists( SITE_CUSTOM_DOCKER_COMPOSE ) ) { - return EE::exec( 'docker-compose -f docker-compose.yml -f ' . SITE_CUSTOM_DOCKER_COMPOSE . " up -d $all_services" ); - } else { - return EE::exec( "docker-compose up -d $all_services" ); - } + + return EE::exec( docker_compose_with_custom() . ' up -d '. $all_services ); } } @@ -199,7 +197,7 @@ public static function docker_compose_down( $dir ) { $chdir_return_code = chdir( $dir ); if ( $chdir_return_code ) { - return EE::exec( 'docker-compose down' ); + return EE::exec( docker_compose_with_custom() . ' down' ); } return false; diff --git a/php/utils.php b/php/utils.php index 6a70e0fff..f349c2605 100644 --- a/php/utils.php +++ b/php/utils.php @@ -9,6 +9,7 @@ use EE; use EE\Iterators\Transform; use Mustangostang\Spyc; +use Symfony\Component\Filesystem\Filesystem; const PHAR_STREAM_PREFIX = 'phar://'; @@ -1794,3 +1795,27 @@ function sanitize_file_folder_name( $input_name, $strict = true, $remove_forward // Remove starting and ending hyphens as a starting hyphen in string might be considered as parameter in bash file/folder creation. return trim( $output, '-' ); } + +/** + * Function to return docker-compose command with custom docker-compose files + * + * @param array $files_before_custom Files to be included before custom compose file is included + * @return string + */ +function docker_compose_with_custom( array $files_before_custom = [] ) : string { + $fs = new Filesystem(); + + $command = 'docker-compose -f docker-compose.yml'; + + foreach ( $files_before_custom as $file ) { + if ( $fs->exists( $file ) ) { + $command .= ' -f ' . $file ; + } + } + + if ( $fs->exists( SITE_CUSTOM_DOCKER_COMPOSE ) ) { + $command .= ' -f ' . SITE_CUSTOM_DOCKER_COMPOSE ; + } + + return $command; +} From 3c126d2937f3bc56052e2e5a322336072c5c3bce Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Wed, 30 Sep 2020 17:03:23 +0530 Subject: [PATCH 4/8] Add custom docker-compose support in GlobalContainers.php --- php/EE/Migration/GlobalContainers.php | 11 ++++++----- php/class-ee-docker.php | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/php/EE/Migration/GlobalContainers.php b/php/EE/Migration/GlobalContainers.php index a2f77dc2d..1b81f8cd9 100644 --- a/php/EE/Migration/GlobalContainers.php +++ b/php/EE/Migration/GlobalContainers.php @@ -4,6 +4,7 @@ use EE; use Symfony\Component\Filesystem\Filesystem; +use function EE\Utils\docker_compose_with_custom; /** * Upgrade existing global containers to new docker-image @@ -55,7 +56,7 @@ public static function revert_global_containers( $source_path, $dest_path, $upda chdir( EE_ROOT_DIR . '/services' ); - if ( ! EE::exec( 'docker-compose up -d ' . $services_to_regenerate ) ) { + if ( ! EE::exec( docker_compose_with_custom() . ' up -d ' . $services_to_regenerate ) ) { throw new \Exception( 'Unable to downgrade global containers. Please check logs for more details.' ); } EE::debug( 'Complete restoring global docker-compose.yml file from backup' ); @@ -81,7 +82,7 @@ public static function down_global_containers( $updated_images ) { EE::debug( "Removing $global_container_name" ); if ( false !== \EE_DOCKER::container_status( $global_container_name ) ) { - if ( ! EE::exec( "docker-compose stop $global_service_name && docker-compose rm -f $global_service_name" ) ) { + if ( ! EE::exec( docker_compose_with_custom() . " stop $global_service_name && ". docker_compose_with_custom() . " rm -f $global_service_name" ) ) { throw new \Exception( "Unable to stop $global_container_name container" ); } } @@ -115,7 +116,7 @@ public static function global_service_down( $service_name ) { EE::debug( 'Start ' . $service_name . ' container removing' ); chdir( EE_ROOT_DIR . '/services' ); - if ( ! EE::exec( "docker-compose stop $service_name && docker-compose rm -f $service_name" ) ) { + if ( ! EE::exec( docker_compose_with_custom() . " stop $service_name && ". docker_compose_with_custom() . " rm -f $service_name" ) ) { throw new \Exception( sprintf( 'Unable to remove %1$s container', $service_name ) ); } EE::debug( 'Complete ' . $service_name . ' container removing' ); @@ -149,7 +150,7 @@ public static function enable_support_containers() { throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) ); } - $command = 'docker-compose --project-name=ee up -d global-db global-redis'; + $command = docker_compose_with_custom() . ' --project-name=ee up -d global-db global-redis'; if ( ! EE::exec( $command ) ) { throw new \Exception( 'Unable to create support container.' ); } @@ -163,7 +164,7 @@ public static function disable_support_containers() { throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) ); } - $command = 'docker-compose --project-name=ee down'; + $command = docker_compose_with_custom() . ' --project-name=ee down'; if ( ! EE::exec( $command ) ) { throw new \Exception( 'Unable to remove support container.' ); } diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index dfbd9639b..4bfba6699 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -213,7 +213,7 @@ public static function docker_compose_down( $dir ) { */ public static function service_exists( $service, $site_fs_path ) { chdir( $site_fs_path ); - $launch = EE::launch( 'docker-compose config --services' ); + $launch = EE::launch( docker_compose_with_custom() . ' config --services' ); $services = explode( PHP_EOL, trim( $launch->stdout ) ); return in_array( $service, $services, true ); From 81994713279e9b423636b4b6ebb8d6b8b81e1b9d Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Mon, 5 Oct 2020 15:24:58 +0530 Subject: [PATCH 5/8] Remove additional space --- php/utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/utils.php b/php/utils.php index f349c2605..d67e984bf 100644 --- a/php/utils.php +++ b/php/utils.php @@ -1817,5 +1817,5 @@ function docker_compose_with_custom( array $files_before_custom = [] ) : string $command .= ' -f ' . SITE_CUSTOM_DOCKER_COMPOSE ; } - return $command; + return $command; } From 0a0fc150a7091e0f01fa9e140a6997755655b46e Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Mon, 5 Oct 2020 15:39:25 +0530 Subject: [PATCH 6/8] Add custom docker-compose support --- php/EE/Runner.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php/EE/Runner.php b/php/EE/Runner.php index 026306339..87aa41f7b 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -10,6 +10,7 @@ use EE\Utils; use Monolog\Logger; use Mustangostang\Spyc; +use function EE\Utils\docker_compose_with_custom; /** * Performs the execution of a command. @@ -473,7 +474,7 @@ private function generate_ssh_command( $bits, $ee_command ) { } if ( 'docker-compose' === $bits['scheme'] ) { - $command = 'docker-compose exec %s%s%s sh -c %s'; + $command = docker_compose_with_custom() . ' exec %s%s%s sh -c %s'; $escaped_command = sprintf( $command, From 4eb4857ebf1e491b3c6a8dcdd84637efeca54b46 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 27 Oct 2020 19:28:14 +0530 Subject: [PATCH 7/8] Move docker_compose_with_custom to class EE_DOCKER --- php/EE/Migration/GlobalContainers.php | 11 ++++----- php/EE/Runner.php | 5 ++-- php/class-ee-docker.php | 33 +++++++++++++++++++++++---- php/utils.php | 23 ------------------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/php/EE/Migration/GlobalContainers.php b/php/EE/Migration/GlobalContainers.php index 1b81f8cd9..b802cb544 100644 --- a/php/EE/Migration/GlobalContainers.php +++ b/php/EE/Migration/GlobalContainers.php @@ -4,7 +4,6 @@ use EE; use Symfony\Component\Filesystem\Filesystem; -use function EE\Utils\docker_compose_with_custom; /** * Upgrade existing global containers to new docker-image @@ -56,7 +55,7 @@ public static function revert_global_containers( $source_path, $dest_path, $upda chdir( EE_ROOT_DIR . '/services' ); - if ( ! EE::exec( docker_compose_with_custom() . ' up -d ' . $services_to_regenerate ) ) { + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' up -d ' . $services_to_regenerate ) ) { throw new \Exception( 'Unable to downgrade global containers. Please check logs for more details.' ); } EE::debug( 'Complete restoring global docker-compose.yml file from backup' ); @@ -82,7 +81,7 @@ public static function down_global_containers( $updated_images ) { EE::debug( "Removing $global_container_name" ); if ( false !== \EE_DOCKER::container_status( $global_container_name ) ) { - if ( ! EE::exec( docker_compose_with_custom() . " stop $global_service_name && ". docker_compose_with_custom() . " rm -f $global_service_name" ) ) { + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . " stop $global_service_name && ". \EE_DOCKER::docker_compose_with_custom() . " rm -f $global_service_name" ) ) { throw new \Exception( "Unable to stop $global_container_name container" ); } } @@ -116,7 +115,7 @@ public static function global_service_down( $service_name ) { EE::debug( 'Start ' . $service_name . ' container removing' ); chdir( EE_ROOT_DIR . '/services' ); - if ( ! EE::exec( docker_compose_with_custom() . " stop $service_name && ". docker_compose_with_custom() . " rm -f $service_name" ) ) { + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . " stop $service_name && ". \EE_DOCKER::docker_compose_with_custom() . " rm -f $service_name" ) ) { throw new \Exception( sprintf( 'Unable to remove %1$s container', $service_name ) ); } EE::debug( 'Complete ' . $service_name . ' container removing' ); @@ -150,7 +149,7 @@ public static function enable_support_containers() { throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) ); } - $command = docker_compose_with_custom() . ' --project-name=ee up -d global-db global-redis'; + $command = \EE_DOCKER::docker_compose_with_custom() . ' --project-name=ee up -d global-db global-redis'; if ( ! EE::exec( $command ) ) { throw new \Exception( 'Unable to create support container.' ); } @@ -164,7 +163,7 @@ public static function disable_support_containers() { throw new \Exception( sprintf( '%s path does not exist', EE_SERVICE_DIR ) ); } - $command = docker_compose_with_custom() . ' --project-name=ee down'; + $command = \EE_DOCKER::docker_compose_with_custom() . ' --project-name=ee down'; if ( ! EE::exec( $command ) ) { throw new \Exception( 'Unable to remove support container.' ); } diff --git a/php/EE/Runner.php b/php/EE/Runner.php index 87aa41f7b..11e8b125f 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -10,7 +10,6 @@ use EE\Utils; use Monolog\Logger; use Mustangostang\Spyc; -use function EE\Utils\docker_compose_with_custom; /** * Performs the execution of a command. @@ -57,7 +56,7 @@ private function init_ee() { define( 'DB', EE_ROOT_DIR . '/db/ee.sqlite' ); define( 'LOCALHOST_IP', '127.0.0.1' ); - define( 'SITE_CUSTOM_DOCKER_COMPOSE', 'docker-compose-custom.yml' ); + define( 'SITE_CUSTOM_DOCKER_COMPOSE', 'docker-compose.custom.yml' ); $db_dir = dirname( DB ); if ( ! is_dir( $db_dir ) ) { @@ -474,7 +473,7 @@ private function generate_ssh_command( $bits, $ee_command ) { } if ( 'docker-compose' === $bits['scheme'] ) { - $command = docker_compose_with_custom() . ' exec %s%s%s sh -c %s'; + $command = \EE_DOCKER::docker_compose_with_custom() . ' exec %s%s%s sh -c %s'; $escaped_command = sprintf( $command, diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index 4bfba6699..ae17a7a65 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -1,10 +1,33 @@ exists( $file ) ) { + $command .= ' -f ' . $file ; + } + } + + if ( $fs->exists( SITE_CUSTOM_DOCKER_COMPOSE ) ) { + $command .= ' -f ' . SITE_CUSTOM_DOCKER_COMPOSE ; + } + + return $command; + } + /** * Check and Start or create container if not running. * @@ -164,11 +187,11 @@ public static function docker_compose_up( $dir, $services = [] ) { $chdir_return_code = chdir( $dir ); if ( $chdir_return_code ) { if ( empty( $services ) ) { - return EE::exec( docker_compose_with_custom() . ' up -d' ); + return EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' up -d' ); } else { $all_services = implode( ' ', $services ); - return EE::exec( docker_compose_with_custom() . ' up -d '. $all_services ); + return EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' up -d '. $all_services ); } } @@ -197,7 +220,7 @@ public static function docker_compose_down( $dir ) { $chdir_return_code = chdir( $dir ); if ( $chdir_return_code ) { - return EE::exec( docker_compose_with_custom() . ' down' ); + return EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' down' ); } return false; @@ -213,7 +236,7 @@ public static function docker_compose_down( $dir ) { */ public static function service_exists( $service, $site_fs_path ) { chdir( $site_fs_path ); - $launch = EE::launch( docker_compose_with_custom() . ' config --services' ); + $launch = EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' config --services' ); $services = explode( PHP_EOL, trim( $launch->stdout ) ); return in_array( $service, $services, true ); diff --git a/php/utils.php b/php/utils.php index d67e984bf..8aaf06be5 100644 --- a/php/utils.php +++ b/php/utils.php @@ -1796,26 +1796,3 @@ function sanitize_file_folder_name( $input_name, $strict = true, $remove_forward return trim( $output, '-' ); } -/** - * Function to return docker-compose command with custom docker-compose files - * - * @param array $files_before_custom Files to be included before custom compose file is included - * @return string - */ -function docker_compose_with_custom( array $files_before_custom = [] ) : string { - $fs = new Filesystem(); - - $command = 'docker-compose -f docker-compose.yml'; - - foreach ( $files_before_custom as $file ) { - if ( $fs->exists( $file ) ) { - $command .= ' -f ' . $file ; - } - } - - if ( $fs->exists( SITE_CUSTOM_DOCKER_COMPOSE ) ) { - $command .= ' -f ' . SITE_CUSTOM_DOCKER_COMPOSE ; - } - - return $command; -} From e25beed29c66b8c946a4f4e852eea69b645f350d Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 30 Oct 2020 11:18:39 +0530 Subject: [PATCH 8/8] Correct custom file format --- php/EE/Runner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/EE/Runner.php b/php/EE/Runner.php index 11e8b125f..933892c99 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -56,7 +56,7 @@ private function init_ee() { define( 'DB', EE_ROOT_DIR . '/db/ee.sqlite' ); define( 'LOCALHOST_IP', '127.0.0.1' ); - define( 'SITE_CUSTOM_DOCKER_COMPOSE', 'docker-compose.custom.yml' ); + define( 'SITE_CUSTOM_DOCKER_COMPOSE', 'docker-compose-custom.yml' ); $db_dir = dirname( DB ); if ( ! is_dir( $db_dir ) ) {