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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ install:

before_script:
- composer validate
- ./vendor/bin/behat
- sudo ./vendor/bin/behat
- ./ci/prepare.sh

jobs:
Expand Down
44 changes: 42 additions & 2 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
<?php
/**
* Behat tests.
*
* @package ee-cli
*/

include_once(__DIR__ . '/../../php/class-ee.php');
include_once(__DIR__ . '/../../php/utils.php');
/* Start: Loading required files to enable EE::launch() in tests. */
define( 'EE_ROOT', __DIR__ . '/../..' );

include_once( EE_ROOT . '/php/class-ee.php' );
include_once( EE_ROOT . '/php/EE/Runner.php' );
include_once( EE_ROOT . '/php/utils.php' );

define( 'EE', true );
define( 'EE_VERSION', trim( file_get_contents( EE_ROOT . '/VERSION' ) ) );
define( 'EE_CONF_ROOT', '/opt/easyengine' );

require_once EE_ROOT . '/php/bootstrap.php';

if ( ! class_exists( 'EE\Runner' ) ) {
require_once EE_ROOT . '/php/EE/Runner.php';
}

if ( ! class_exists( 'EE\Configurator' ) ) {
require_once EE_ROOT . '/php/EE/Configurator.php';
}

$logger_dir = EE_ROOT . '/php/EE/Loggers';
$iterator = new \DirectoryIterator( $logger_dir );

// Make sure the base class is declared first.
include_once "$logger_dir/Base.php";

foreach ( $iterator as $filename ) {
if ( '.php' !== pathinfo( $filename, PATHINFO_EXTENSION ) ) {
continue;
}

include_once "$logger_dir/$filename";
}
$runner = \EE::get_runner();
$runner->init_logger();
/* End. Loading required files to enable EE::launch() in tests. */

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterFeatureScope;
Expand Down
47 changes: 12 additions & 35 deletions php/class-ee-docker.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

use function \EE\Utils\default_debug;
use function \EE\Utils\default_launch;
use function \EE\Utils\mustache_render;

class EE_DOCKER {

/**
Expand All @@ -23,7 +19,7 @@ public static function boot_container( $container, $command = '' ) {
return true;
}
} else {
return self::create_container( $container, $command );
return EE::exec( $command );
}
}

Expand All @@ -35,8 +31,7 @@ public static function container_status( $container ) {
if ( $ret ) {
EE::error( 'Docker is not installed. Please install Docker to run EasyEngine.' );
}
$status = EE::launch( "docker inspect -f '{{.State.Running}}' $container", false, true );
default_debug( $status );
$status = EE::launch( "docker inspect -f '{{.State.Running}}' $container" );
if ( ! $status->return_code ) {
if ( preg_match( '/true/', $status->stdout ) ) {
return 'running';
Expand All @@ -56,7 +51,7 @@ public static function container_status( $container ) {
* @return bool success.
*/
public static function start_container( $container ) {
return default_launch( "docker start $container" );
return EE::exec( "docker start $container" );
}

/**
Expand All @@ -67,7 +62,7 @@ public static function start_container( $container ) {
* @return bool success.
*/
public static function stop_container( $container ) {
return default_launch( "docker stop $container" );
return EE::exec( "docker stop $container" );
}

/**
Expand All @@ -78,25 +73,7 @@ public static function stop_container( $container ) {
* @return bool success.
*/
public static function restart_container( $container ) {
return default_launch( "docker restart $container" );
}

/**
* Function to create and start the container if it does not exist.
*
* @param String $container Container to be created.
* @param String $command Command to launch the container.
*
* @return bool success.
*/
public static function create_container( $container, $command ) {

$launch = EE::launch( $command, false, true );
default_debug( $launch );
if ( ! $launch->return_code ) {
return true;
}
EE::error( $launch->stderr );
return EE::exec( "docker restart $container" );
}

/**
Expand All @@ -107,7 +84,7 @@ public static function create_container( $container, $command ) {
* @return bool success.
*/
public static function create_network( $name ) {
return default_launch( "docker network create $name" );
return EE::exec( "docker network create $name" );
}

/**
Expand All @@ -119,7 +96,7 @@ public static function create_network( $name ) {
* @return bool success.
*/
public static function connect_network( $name, $connect_to ) {
return default_launch( "docker network connect $name $connect_to" );
return EE::exec( "docker network connect $name $connect_to" );
}

/**
Expand All @@ -130,7 +107,7 @@ public static function connect_network( $name, $connect_to ) {
* @return bool success.
*/
public static function rm_network( $name ) {
return default_launch( "docker network rm $name" );
return EE::exec( "docker network rm $name" );
}

/**
Expand All @@ -142,7 +119,7 @@ public static function rm_network( $name ) {
* @return bool success.
*/
public static function disconnect_network( $name, $connected_to ) {
return default_launch( "docker network disconnect $name $connected_to" );
return EE::exec( "docker network disconnect $name $connected_to" );
}


Expand Down Expand Up @@ -183,11 +160,11 @@ public static function docker_compose_up( $dir, $services = [] ) {
$chdir_return_code = chdir( $dir );
if ( $chdir_return_code ) {
if ( empty( $services ) ) {
return default_launch( 'docker-compose up -d' );
return EE::exec( 'docker-compose up -d' );
} else {
$all_services = implode( ' ', $services );

return default_launch( "docker-compose up -d $all_services" );
return EE::exec( "docker-compose up -d $all_services" );
}
}

Expand All @@ -205,7 +182,7 @@ public static function docker_compose_down( $dir ) {
$chdir_return_code = chdir( $dir );
if ( $chdir_return_code ) {

return default_launch( 'docker-compose down' );
return EE::exec( 'docker-compose down' );
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion php/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function delete_site( $level, $site_name, $site_root ) {
if ( EE::docker()::docker_compose_down( $site_root ) ) {
EE::log( "[$site_name] Docker Containers removed." );
} else {
EE\Utils\default_launch( "docker rm -f $(docker ps -q -f=label=created_by=EasyEngine -f=label=site_name=$site_name)" );
EE::exec( "docker rm -f $(docker ps -q -f=label=created_by=EasyEngine -f=label=site_name=$site_name)" );
if ( $level > 3 ) {
EE::warning( 'Error in removing docker containers.' );
}
Expand Down
68 changes: 67 additions & 1 deletion php/class-ee.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,17 @@ public static function error_to_string( $errors ) {
*
* @return int|ProcessRun The command exit status, or a ProcessRun object for full details.
*/
public static function launch( $command, $exit_on_error = true, $return_detailed = false, $env = null, $cwd = null ) {
public static function launch( $command, $exit_on_error = false, $return_detailed = true, $env = null, $cwd = null ) {
Utils\check_proc_available( 'launch' );

self::debug( '-----------------------' );
self::debug( "COMMAND: $command" );

$proc = Process::create( $command, $cwd, $env );
$results = $proc->run();

self::debug_run_command( $results );

if ( -1 == $results->return_code ) {
self::warning( "Spawned process returned exit code {$results->return_code}, which could be caused by a custom compiled version of PHP that uses the --enable-sigchild option." );
}
Expand All @@ -851,6 +856,51 @@ public static function launch( $command, $exit_on_error = true, $return_detailed
return $results->return_code;
}

/**
* Launch an arbitrary external process that takes over I/O.
*
* @access public
* @category Execution
*
* @param string $command External process to launch.
* @param bool $echo_stdout Print stdout to terminal. Default false.
* @param bool $echo_stderr Print stderr to terminal. Default false.
* @param boolean $exit_on_error Exit if the command returns an elevated return code with stderr.
*
* @return bool True if executed successfully. False if failed.
*/
public static function exec( $command, $echo_stdout = false, $echo_stderr = false, $exit_on_error = false ) {
Utils\check_proc_available( 'exec' );

self::debug( '-----------------------' );
self::debug( "COMMAND: $command" );

$proc = Process::create( $command, null, null );
$results = $proc->run();

self::debug_run_command( $results );

if ( -1 == $results->return_code ) {
self::warning( "Spawned process returned exit code {$results->return_code}, which could be caused by a custom compiled version of PHP that uses the --enable-sigchild option." );
}

if ( $echo_stdout ) {
echo $results->stdout;
}
if ( $echo_stderr && ! $exit_on_error ) {
echo $results->stderr;
}
if ( ! $results->return_code ) {
return true;
}
if ( $exit_on_error ) {
exit( $results->return_code );
}

return false;

}

/**
* Run a EE command in a new process reusing the current runtime arguments.
*
Expand Down Expand Up @@ -906,6 +956,22 @@ public static function launch_self( $command, $args = array(), $assoc_args = arr
return self::launch( $full_command, $exit_on_error, $return_detailed );
}

/**
* Format and print debug messages for external command launch.
*
* @param Object $launch external command object.
*/
private static function debug_run_command( $launch ) {
if ( ! empty( $launch->stdout ) ) {
self::debug( "STDOUT: $launch->stdout" );
}
if ( ! empty( $launch->stderr ) ) {
self::debug( "STDERR: $launch->stderr" );
}
self::debug( "RETURN CODE: $launch->return_code" );
self::debug( '-----------------------' );
}

/**
* Get the path to the PHP binary used when executing EE.
*
Expand Down
8 changes: 4 additions & 4 deletions php/commands/src/CLI_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ public function self_uninstall( $args, $assoc_args ) {

EE::confirm("Are you sure you want to remove EasyEngine and all its sites(along with their data)?\nThis is an irreversible action. No backup will be kept.", $assoc_args);

Utils\default_launch("docker rm -f $(docker ps -aqf label=org.label-schema.vendor=\"EasyEngine\")");
EE::exec("docker rm -f $(docker ps -aqf label=org.label-schema.vendor=\"EasyEngine\")");
$home = Utils\get_home_dir();
Utils\default_launch("rm -rf $home/.ee/");
EE::exec("rm -rf $home/.ee/");

$records = EE::db()->select(['site_path']);

Expand All @@ -514,8 +514,8 @@ public function self_uninstall( $args, $assoc_args ) {
$fs->remove($sites_paths);
}

Utils\default_launch("rm -df $home/ee-sites/");
Utils\default_launch("rm -rf /opt/easyengine/");
EE::exec("rm -df $home/ee-sites/");
EE::exec("rm -rf /opt/easyengine/");

if ( Utils\inside_phar() ) {
unlink( realpath( $_SERVER['argv'][0] ) );
Expand Down
8 changes: 4 additions & 4 deletions php/site-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function create_etc_hosts_entry( $site_name ) {
$host_line = LOCALHOST_IP . "\t$site_name";
$etc_hosts = file_get_contents( '/etc/hosts' );
if ( ! preg_match( "/\s+$site_name\$/m", $etc_hosts ) ) {
if ( EE\Utils\default_launch( "/bin/bash -c 'echo \"$host_line\" >> /etc/hosts'" ) ) {
if ( EE::exec( "/bin/bash -c 'echo \"$host_line\" >> /etc/hosts'" ) ) {
EE::success( 'Host entry successfully added.' );
} else {
EE::warning( "Failed to add $site_name in host entry, Please do it manually!" );
Expand Down Expand Up @@ -281,7 +281,7 @@ function start_site_containers( $site_root ) {

EE::log( 'Pulling latest images. This may take some time.' );
chdir( $site_root );
\EE\Utils\default_launch( 'docker-compose pull' );
EE::exec( 'docker-compose pull' );
EE::log( 'Starting site\'s services.' );
if ( ! EE::docker()::docker_compose_up( $site_root ) ) {
throw new \Exception( 'There was some error in docker-compose up.' );
Expand All @@ -302,6 +302,6 @@ function run_compose_command( $action, $container, $action_to_display = null, $s
$display_action = $action_to_display ? $action_to_display : $action;
$display_service = $service_to_display ? $service_to_display : $container;

\EE::log( ucfirst( $display_action ) . 'ing ' . $display_service );
\EE\Utils\default_launch( "docker-compose $action $container", true, true );
EE::log( ucfirst( $display_action ) . 'ing ' . $display_service );
EE::exec( "docker-compose $action $container", true, true );
}
44 changes: 0 additions & 44 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,50 +1409,6 @@ function delem_log( $log_data ) {
EE::get_file_logger()->info( "======================== $log_data ========================" );
}

/**
* Format and print debug messages for EE::launch
*
* @param Object $launch EE::Launch command object
*/
function default_debug( $launch ) {
EE::debug( '-----------------------' );
EE::debug( "COMMAND: $launch->command" );
if ( ! empty( $launch->stdout ) ) {
EE::debug( "STDOUT: $launch->stdout" );
}
if ( ! empty( $launch->stderr ) ) {
EE::debug( "STDERR: $launch->stderr" );
}
EE::debug( "RETURN CODE: $launch->return_code" );
EE::debug( '-----------------------' );
}

/**
* Default Launch command.
* This takes care of executing the command as well as debugging it to terminal as well as file.
*
* @param string $command The command to be executed via EE::launch();
* @param array $env Environment variables to set when running the command.
* @param string $cwd Directory to execute the command in.
*
* @return bool True if executed successfully. False if failed.
*/
function default_launch( $command, $echo_stdout = false, $echo_stderr = false, $env = null, $cwd = null ) {
$launch = EE::launch( $command, false, true, $env, $cwd );
default_debug( $launch );

if( $echo_stdout ) {
echo $launch->stdout;
}
if( $echo_stderr ) {
echo $launch->stderr;
}
if ( ! $launch->return_code ) {
return true;
}
return false;
}

/**
* Function that takes care of executing the command as well as debugging it to terminal as well as file.
*
Expand Down