diff --git a/php/EE/Migration/Containers.php b/php/EE/Migration/Containers.php index b00b94918..8c078e60d 100644 --- a/php/EE/Migration/Containers.php +++ b/php/EE/Migration/Containers.php @@ -312,8 +312,22 @@ public static function migrate_site_containers( $updated_images ) { $ee_site_object = SiteContainers::get_site_object( $site['site_type'] ); + // The support project's php shares the site's php alias, and nginx resolves it only at (re)load. + $reload_nginx = in_array( $site['site_type'], [ 'wp', 'php' ], true ); + if ( $site['site_enabled'] ) { + if ( $reload_nginx ) { + // Undone last for this site: after the old containers are back and the support project is gone. + self::$rsp->add_step( + sprintf( 'reload-site-nginx-on-rollback-%s', $site['site_url'] ), + function () {}, + 'EE\Migration\SiteContainers::reload_site_nginx', + null, + [ $site['site_url'], $site['site_fs_path'] ] + ); + } + /** * Enable support containers. */ @@ -332,6 +346,16 @@ public static function migrate_site_containers( $updated_images ) { [ $site ], [ $site, $ee_site_object ] ); + + if ( $reload_nginx ) { + self::$rsp->add_step( + sprintf( 'reload-support-nginx-%s', $site['site_url'] ), + 'EE\Migration\SiteContainers::reload_support_nginx', + null, + [ $site['site_url'], $site['site_fs_path'] ], + null + ); + } } self::$rsp->add_step( @@ -369,6 +393,16 @@ public static function migrate_site_containers( $updated_images ) { [ $site['site_url'], $site['site_fs_path'] ], [ $site['site_url'], $site['site_fs_path'] ] ); + + if ( $reload_nginx ) { + self::$rsp->add_step( + sprintf( 'reload-site-nginx-%s', $site['site_url'] ), + 'EE\Migration\SiteContainers::reload_site_nginx', + null, + [ $site['site_url'], $site['site_fs_path'] ], + null + ); + } } } } diff --git a/php/EE/Migration/SiteContainers.php b/php/EE/Migration/SiteContainers.php index f2b7e0e31..e3a9d3bea 100644 --- a/php/EE/Migration/SiteContainers.php +++ b/php/EE/Migration/SiteContainers.php @@ -223,6 +223,42 @@ public static function reload_nginx( $site_fs_path ) { } } + /** + * Reload site's nginx after its support containers are removed, so it drops the support php's IP. + * A failure only warns: the site works, nginx just keeps retrying the dead peer. + * + * @param string $site_url Site URL. + * @param string $site_fs_path Directory containing site's docker-compose.yml. + */ + public static function reload_site_nginx( $site_url, $site_fs_path ) { + EE::debug( sprintf( 'Start reloading nginx of %s', $site_url ) ); + + try { + self::reload_nginx( $site_fs_path ); + } catch ( \Exception $e ) { + EE::warning( sprintf( 'Could not reload nginx of %1$s after recreating its containers. Some requests may take ~3 s until you run `ee site reload %1$s --nginx`.', $site_url ) ); + + return; + } + + EE::debug( sprintf( 'Complete reloading nginx of %s', $site_url ) ); + } + + /** + * Reload the support nginx after the site's containers are removed, so it drops the old php's IP. + * Best effort: the support nginx only serves until the site's containers are back. + * + * @param string $site_url Site URL. + * @param string $site_fs_path Directory containing site's docker-compose.yml. + */ + public static function reload_support_nginx( $site_url, $site_fs_path ) { + $command = sprintf( "docker-compose --project-name=%s exec nginx sh -c 'nginx -t && nginx -s reload'", self::get_support_project_name( $site_url ) ); + + if ( ! chdir( $site_fs_path ) || ! EE::exec( $command ) ) { + EE::debug( sprintf( 'Could not reload support nginx of %s', $site_url ) ); + } + } + /** * Function to reload site's php. * @@ -262,8 +298,7 @@ public static function docker_compose_pull( $site_fs_path ) { public static function enable_support_containers( $site_url, $site_fs_path ) { EE::debug( sprintf( 'Start enabling containers for %s', $site_url ) ); - $site_name = str_replace( '.', '', $site_url ); - $project_name = sprintf( 'update-ee-%s', $site_name ); + $project_name = self::get_support_project_name( $site_url ); if ( ! chdir( $site_fs_path ) ) { throw new \Exception( sprintf( '%s does not exist.', $site_fs_path ) ); @@ -288,8 +323,7 @@ public static function enable_support_containers( $site_url, $site_fs_path ) { public static function disable_support_containers( $site_url, $site_fs_path ) { EE::debug( sprintf( 'Start disabling support containers for %s', $site_url ) ); - $site_name = str_replace( '.', '', $site_url ); - $project_name = sprintf( 'update-ee-%s', $site_name ); + $project_name = self::get_support_project_name( $site_url ); if ( ! chdir( $site_fs_path ) ) { throw new \Exception( sprintf( '%s does not exist.', $site_fs_path ) ); @@ -302,4 +336,15 @@ public static function disable_support_containers( $site_url, $site_fs_path ) { EE::debug( sprintf( 'Complete disabling support containers for %s', $site_url ) ); } + + /** + * Compose project name of a site's support containers. + * + * @param string $site_url Site URL. + * + * @return string + */ + private static function get_support_project_name( $site_url ) { + return sprintf( 'update-ee-%s', str_replace( '.', '', $site_url ) ); + } }