-
Notifications
You must be signed in to change notification settings - Fork 27
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.' ); | ||
} | ||
} | ||
|
||
$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' ); | ||
} | ||
|
@@ -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.' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again something useful can be added here like: |
||
} | ||
} | ||
} 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.' ); | ||
|
@@ -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.' ); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
throw new \Exception( 'There was some error in stopping and removing containers.' ); | ||
} | ||
} | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.