Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds nginx test on container restart and makes use of EE_DOCKER methods #315

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 23 additions & 24 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,23 @@ public function restart( $args, $assoc_args, $whitelisted_containers = [] ) {

$this->site_data = get_site_info( $args );

chdir( $this->site_data['site_fs_path'] );

if ( $all || $no_service_specified ) {
$containers = $whitelisted_containers;
} else {
$containers = array_keys( $assoc_args );
}

foreach ( $containers as $container ) {
\EE\Site\Utils\run_compose_command( 'restart', $container );
if ( in_array( 'nginx', $containers ) ) {
$nginx_test_command = "sh -c 'nginx -t'";
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], 'nginx', $nginx_test_command ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better error message on failure would be: Nginx configuration test failed. Please check the site's nginx config.
There was some error in docker-compose exec is a very generic message and does not convey anything helpful to the end user using EasyEngine.

}
}

$all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers;

if ( ! \EE_DOCKER::docker_compose_restart( $this->site_data['site_fs_path'], $all_containers ) ) {
throw new \Exception( 'There was some error in docker-compose restart.' );
}
\EE\Utils\delem_log( 'site restart stop' );
}
Expand Down Expand Up @@ -625,36 +632,28 @@ public function reload( $args, $assoc_args, $whitelisted_containers = [], $reloa
\EE\Utils\delem_log( 'site reload start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$all = \EE\Utils\get_flag_value( $assoc_args, 'all' );
if ( ! array_key_exists( 'nginx', $reload_commands ) ) {
$reload_commands['nginx'] = 'nginx sh -c \'nginx -t && service openresty reload\'';
}
$reload_commands['php'] = "bash -c 'kill -USR2 1'";
$reload_commands['nginx'] = "sh -c 'nginx -t && service openresty reload'";
$no_service_specified = count( $assoc_args ) === 0;

$this->site_data = get_site_info( $args );

chdir( $this->site_data['site_fs_path'] );

if ( $all || $no_service_specified ) {
$this->reload_services( $whitelisted_containers, $reload_commands );
foreach ( $whitelisted_containers as $container ) {
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again something useful can be added here like: Reloading service for $container failed.

}
}
} else {
$this->reload_services( array_keys( $assoc_args ), $reload_commands );
foreach ( array_keys( $assoc_args ) as $container ) {
if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) {
throw new \Exception( 'There was some error in docker-compose exec.' );
}
}
}
\EE\Utils\delem_log( 'site reload stop' );
}

/**
* Executes reload commands. It needs separate handling as commands to reload each service is different.
*
* @param array $services Services to reload.
* @param array $reload_commands Commands to reload the services.
*/
private function reload_services( $services, $reload_commands ) {

foreach ( $services as $service ) {
\EE\Site\Utils\run_compose_command( 'exec', $reload_commands[ $service ], 'reload', $service );
}
}

/**
* Function to verify and check the global services dependent for given site.
* Enables the dependent service if it is down.
Expand Down
33 changes: 10 additions & 23 deletions src/helper/site-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ function site_status_check( $site_url ) {
*/
function start_site_containers( $site_fs_path, $containers = [] ) {

chdir( $site_fs_path );
EE::log( 'Starting site\'s services.' );
if ( ! \EE_DOCKER::docker_compose_up( $site_fs_path, $containers ) ) {
throw new \Exception( 'There was some error in docker-compose up.' );
Expand All @@ -435,9 +434,11 @@ function start_site_containers( $site_fs_path, $containers = [] ) {
*/
function restart_site_containers( $site_fs_path, $containers ) {

chdir( $site_fs_path );
EE::log( 'Restarting site\'s services.' );
$all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers;
EE::exec( "docker-compose restart $all_containers" );
if ( ! \EE_DOCKER::docker_compose_restart( $site_fs_path, $all_containers ) ) {
throw new \Exception( 'There was some error in docker-compose restart.' );
}
}

/**
Expand All @@ -448,27 +449,13 @@ function restart_site_containers( $site_fs_path, $containers ) {
*/
function stop_site_containers( $site_fs_path, $containers ) {

chdir( $site_fs_path );
EE::log( 'Stopping and removing site\'s services.' );
$all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers;
EE::exec( "docker-compose stop $all_containers" );
EE::exec( "docker-compose rm -f $all_containers" );
}

/**
* Generic function to run a docker compose command. Must be ran inside correct directory.
*
* @param string $action docker-compose action to run.
* @param string $container The container on which action has to be run.
* @param string $action_to_display The action message to be displayed.
* @param string $service_to_display The service message to be displayed.
*/
function run_compose_command( $action, $container, $action_to_display = null, $service_to_display = null ) {

$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::exec( "docker-compose $action $container", true, true );
$stopped = \EE_DOCKER::docker_compose_stop( $site_fs_path, $all_containers );
$forcermd = \EE_DOCKER::docker_compose_forcerm( $site_fs_path, $all_containers );
if ( ! (stopped && forcermd) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing $ in variable names. Also, fix phpcs issues.

throw new \Exception( 'There was some error in stopping and removing containers.' );
}
}

/**
Expand Down