diff --git a/php/EE/Migration/GlobalContainers.php b/php/EE/Migration/GlobalContainers.php index a2f77dc2d..b802cb544 100644 --- a/php/EE/Migration/GlobalContainers.php +++ b/php/EE/Migration/GlobalContainers.php @@ -55,7 +55,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( \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' ); @@ -81,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 stop $global_service_name && docker-compose 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" ); } } @@ -115,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 stop $service_name && docker-compose 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' ); @@ -149,7 +149,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 = \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.' ); } @@ -163,7 +163,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 = \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 e45a46ac6..933892c99 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 ) ) { @@ -472,7 +473,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 = \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 306769521..ae17a7a65 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -4,6 +4,30 @@ class EE_DOCKER { + /** + * 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 + */ + public static 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; + } + /** * Check and Start or create container if not running. * @@ -159,14 +183,15 @@ 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' ); + return EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' up -d' ); } else { $all_services = implode( ' ', $services ); - return EE::exec( "docker-compose up -d $all_services" ); + return EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' up -d '. $all_services ); } } @@ -195,7 +220,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( \EE_DOCKER::docker_compose_with_custom() . ' down' ); } return false; @@ -211,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 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 6a70e0fff..8aaf06be5 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,4 @@ 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, '-' ); } +