diff --git a/migrations/container/20181105125012_site-command_update_php_config.php b/migrations/container/20181105125012_site-command_update_php_config.php index 311f7d9a..8c8456ce 100644 --- a/migrations/container/20181105125012_site-command_update_php_config.php +++ b/migrations/container/20181105125012_site-command_update_php_config.php @@ -131,11 +131,7 @@ public function up() { $old_img_backup = EE_BACKUP_DIR . '/img-version-old.json'; $this->fs->dumpFile( $old_img_backup, json_encode( $current_versions ) ); - foreach ( $img_versions as $img => $version ) { - if ( $current_versions[ $img ] !== $version ) { - $updated_images[] = $img; - } - } + $updated_images = self::get_updated_images( $img_versions, $current_versions ); $first_execution = false; } @@ -235,6 +231,24 @@ public function up() { } + /** + * @param array $img_versions Image => tag the release pins. + * @param array $current_versions Image => tag in the options table. + * + * @return string[] Images whose tag changes. + */ + private static function get_updated_images( array $img_versions, array $current_versions ) { + $updated_images = []; + foreach ( $img_versions as $img => $version ) { + // An image with no options row counts as changed, as before; without isset() PHP reports the missing key. + if ( ! isset( $current_versions[ $img ] ) || $current_versions[ $img ] !== $version ) { + $updated_images[] = $img; + } + } + + return $updated_images; + } + /** * Bring back the existing old config and path. * diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index b000c3ed..7cd7edff 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -2092,6 +2092,8 @@ private function rollback_restore_transaction() { $ok = true; $config_dir = EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] . '/config'; + $this->sid_settings_notice = null; + // First: the php config and DB steps below reach the containers through these files. if ( ! empty( $txn['compose'] ) ) { $ok = $this->rollback_custom_compose() && $ok; @@ -2158,6 +2160,8 @@ private function rollback_restore_transaction() { } if ( $ok ) { + // Before the journal goes: a kill in between leaves it to retry. Kept on a failed rollback, as php.ini may still be the stripped one. + $this->remove_sid_settings_copy( $txn ); $this->restore_txn = null; $this->remove_scratch_dir(); $this->fs->remove( [ $this->get_restore_journal_file(), $txn['stage'], $this->get_compose_snapshot_dir() ] ); @@ -2166,6 +2170,36 @@ private function rollback_restore_transaction() { return $ok; } + /** + * Remove the copy of the backup's php.ini that drop_deprecated_session_sid_settings() saved for a restore that was rolled back. + * + * @param array $txn Restore transaction. + */ + private function remove_sid_settings_copy( array $txn ) { + $file = $txn['sid_copy'] ?? null; + // The journal may be the stage's copy, which the site can write when the stage is inside app/: only the exact path the restore writes. + $shape = '#^' . preg_quote( EE_BACKUP_DIR . '/', '#' ) . 'session-sid-settings-[0-9]+-[0-9]+-restore-[0-9]+/' . preg_quote( (string) $this->site_data['site_url'], '#' ) . '/php\.ini\z#'; + if ( ! is_string( $file ) || false !== strpos( $file, '..' ) || ! preg_match( $shape, $file ) ) { + return; + } + + $site_dir = dirname( $file ); + $root = dirname( $site_dir ); + clearstatcache( true ); + if ( is_link( $root ) || is_link( $site_dir ) || is_link( $file ) ) { + return; + } + // The rollback itself is done, so a copy that can't be removed is reported instead of keeping the journal for a retry. + if ( is_file( $file ) && ! @unlink( $file ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- reported below. + EE::warning( sprintf( 'Could not remove %s, the php.ini copy saved by the restore that was rolled back; remove it by hand.', $file ) ); + + return; + } + // rmdir() refuses a dir that holds other sites' copies. + @rmdir( $site_dir ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. + @rmdir( $root ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. + } + /** * Shutdown handler: a restore that exits before its commit is rolled back while the site lock is still held. */ @@ -2213,24 +2247,34 @@ private function rollback_unfinished_restore() { /** * @param array $txn Restore transaction. * - * @return string Where an unfinished rollback left the previous site files, and the previous DB once its import had started. + * @return string Where an unfinished rollback left the previous site files, configuration and DB. */ private function describe_rollback_leftovers( array $txn ) { // Describe what is on disk now: the swap may not have started, and the dump may be gone. $parts = []; if ( ! empty( $this->list_dir( $txn['stage'] . '/old' ) ) ) { - $parts[] = sprintf( 'The previous site files still staged are in %s/old', $txn['stage'] ); + $parts[] = sprintf( 'the previous site files still staged are in %s/old', $txn['stage'] ); + } + if ( ( ! empty( $txn['conf'] ) || ! empty( $txn['php_conf'] ) ) && is_dir( $txn['stage'] . '/config-before' ) ) { + $parts[] = sprintf( 'the previous site configuration is saved in %s/config-before', $txn['stage'] ); + } + // Not in the stage, see get_compose_snapshot_dir(). + if ( ! empty( $txn['compose'] ) && is_dir( $this->get_compose_snapshot_dir() ) ) { + $parts[] = sprintf( 'the previous custom docker-compose files are saved in %s', $this->get_compose_snapshot_dir() ); } if ( 'importing' === $txn['db'] ) { $parts[] = is_file( $txn['stage'] . '/db-before.sql' ) ? sprintf( 'the previous database is in %s/db-before.sql', $txn['stage'] ) : 'the copy of the previous database is missing'; + } elseif ( 'saved' === $txn['db'] && is_file( $txn['stage'] . '/db-before.sql' ) ) { + $parts[] = sprintf( 'the database was not changed (a copy taken before the restore is in %s/db-before.sql)', $txn['stage'] ); } if ( empty( $parts ) ) { - return sprintf( 'No previous site files are staged in %s', $txn['stage'] ); + return sprintf( 'The rollback state is kept in %s', $txn['stage'] ); } + $last = array_pop( $parts ); - return ucfirst( implode( ' and ', $parts ) ); + return ucfirst( empty( $parts ) ? $last : implode( ', ', $parts ) . ' and ' . $last ); } /** @@ -3710,10 +3754,14 @@ private function drop_deprecated_session_sid_settings( $content, $source, array // Next to the migration's copies (`ee site backup` and `ee site restore` empty EE_BACKUP_DIR//), but never in the same dir, even within the same second. $root = EE_BACKUP_DIR . '/session-sid-settings-' . date( 'Ymd-His' ) . '-restore-' . getmypid(); $file = '/' . $this->site_data['site_url'] . '/php.ini'; + // Journaled before the copy exists, so a rollback finds it even after a kill right after the write. + $this->restore_txn['sid_copy'] = $root . $file; try { + $this->write_restore_journal(); $this->fs->mkdir( dirname( $root . $file ), 0700 ); $this->fs->dumpFile( $root . $file, $content ); } catch ( \Exception $e ) { + unset( $this->restore_txn['sid_copy'] ); // rmdir() refuses a dir that holds other sites' copies. @rmdir( dirname( $root . $file ) ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. @rmdir( $root ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. @@ -3724,7 +3772,6 @@ private function drop_deprecated_session_sid_settings( $content, $source, array return $content; } - // The lines come from the archive: printable ASCII only, since an 8-bit terminal reads any byte 0x80-0x9F as a control character. $lines = array_map( function ( $line ) { diff --git a/src/helper/Site_Letsencrypt.php b/src/helper/Site_Letsencrypt.php index 54cf2ffe..b776efe5 100644 --- a/src/helper/Site_Letsencrypt.php +++ b/src/helper/Site_Letsencrypt.php @@ -487,7 +487,7 @@ public function loadDomainCertificates( array $domains ) { return $certificates; } - public function check( Array $domains, $wildcard = false, $preferred_challenge = '' ) { + public function check( Array $domains, $wildcard = false, $preferred_challenge = '', $force = false ) { $is_solver_dns = ( $wildcard || 'dns' === $preferred_challenge ) ? true : false; \EE::debug( ( 'Starting check with solver ' ) . ( $is_solver_dns ? 'dns' : 'http' ) ); if ( $is_solver_dns ) { @@ -535,7 +535,7 @@ public function check( Array $domains, $wildcard = false, $preferred_challenge = if ( $is_solver_dns && empty( get_config_value( 'cloudflare-api-key' ) ) ) { $primary_domain = str_replace( '*.', '', $domains[0] ); \EE::warning( "The previous ACME order for $primary_domain had expired or failed. A fresh DNS-01 challenge was issued and its new TXT record is printed above." ); - \EE::log( "Publish the new TXT record, then re-run: ee site ssl-verify $primary_domain" ); + \EE::log( "Publish the new TXT record, then re-run: ee site ssl-verify $primary_domain" . ( $force ? ' --force' : '' ) ); return false; } @@ -961,7 +961,7 @@ private function executeRenewal( $domain, array $alternativeNames, $email, $forc if ( $this->is_rate_limit_exception( $e ) ) { \EE::warning( 'Let\'s Encrypt rate limit hit for: ' . $domain . '. Please wait before retrying. Ref: https://letsencrypt.org/docs/rate-limits/' ); } - \EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domain ); + \EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domain . ( $force ? ' --force' : '' ) ); return false; } catch ( \Throwable $e ) { @@ -971,7 +971,7 @@ private function executeRenewal( $domain, array $alternativeNames, $email, $forc if ( $this->is_rate_limit_exception( $e ) ) { \EE::warning( 'Let\'s Encrypt rate limit hit for: ' . $domain . '. Please wait before retrying. Ref: https://letsencrypt.org/docs/rate-limits/' ); } - \EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domain ); + \EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domain . ( $force ? ' --force' : '' ) ); return false; } diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 3a9a9cd8..e969a888 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -1136,7 +1136,7 @@ private function stop_php_for_config_change( $php_conf_dir ) { * * @param string $volume Real path of config/php. * - * @throws \Exception When a running container still mounts it. + * @throws \Exception When a running container still mounts it, or the containers that mount it can't be listed. */ private static function check_config_volume_unused( $volume ) { @@ -1144,12 +1144,24 @@ private static function check_config_volume_unused( $volume ) { return; } $users = EE::launch( 'docker ps -q --filter ' . escapeshellarg( 'volume=' . basename( dirname( $volume ) ) ) ); - $ids = preg_split( '/\s+/', trim( $users->stdout ), -1, PREG_SPLIT_NO_EMPTY ); + if ( $users->return_code ) { + $error = trim( $users->stderr ); + throw new \Exception( 'Could not list the containers that mount the php config volume: ' . ( '' === $error ? 'docker ps exited with code ' . $users->return_code : $error ) . '; re-run the update once docker responds.' ); + } + $ids = preg_split( '/\s+/', trim( $users->stdout ), -1, PREG_SPLIT_NO_EMPTY ); // docker ps can still list a container stopped a moment ago; inspect waits for the engine to record the stop. $ids = array_filter( $ids, function ( $id ) { - return 'exited' !== \EE_DOCKER::container_status( $id ); + $status = \EE_DOCKER::container_status( $id ); + if ( false === $status ) { + // inspect also fails for a container removed since the listing; drop it only if a working listing no longer has it. + $all = EE::launch( 'docker ps -aq --no-trunc --filter ' . escapeshellarg( 'id=' . $id ) ); + + return $all->return_code || '' !== trim( $all->stdout ); + } + + return 'exited' !== $status; } ); - if ( $users->return_code || $ids ) { + if ( $ids ) { throw new \Exception( 'The php config volume is still mounted by a running container (' . implode( ', ', $ids ) . '); stop it and re-run the update.' ); } } @@ -1864,7 +1876,7 @@ protected function www_ssl_wrapper( $containers_to_start = [], $site_enable = fa // A manual DNS-01 challenge keeps site_ssl without a certificate until `ee site ssl-verify` completes it. if ( \EE\Site\Utils\is_le_ssl_pending( $this->site_data['site_url'], $this->site_data['site_ssl'] ) ) { $this->serve_without_https( $containers_to_start ); - EE::log( sprintf( '%1$s is served over HTTP until `ee site ssl-verify %1$s` succeeds.', $this->site_data['site_url'] ) ); + EE::log( sprintf( '%1$s is served over HTTP until `%2$s` succeeds.', $this->site_data['site_url'], $this->ssl_verify_command( $this->site_data['site_url'], $force ) ) ); return; } @@ -2309,7 +2321,7 @@ public function ssl_verify( $args = [], $assoc_args = [], $www_or_non_www = fals $preferred_challenge = get_preferred_ssl_challenge( $domains ); - if ( ! $client->check( $domains, $this->site_data['site_ssl_wildcard'], $preferred_challenge ) ) { + if ( ! $client->check( $domains, $this->site_data['site_ssl_wildcard'], $preferred_challenge, $force ) ) { $is_solver_dns = ( $this->site_data['site_ssl_wildcard'] || 'dns' === $preferred_challenge ) ? true : false; $api_key_present = ! empty( get_config_value( 'cloudflare-api-key' ) ); @@ -2320,7 +2332,7 @@ public function ssl_verify( $args = [], $assoc_args = [], $www_or_non_www = fals } // During a create, update or renewal, the caller says how to retry. if ( ! $called_by_ee ) { - EE::warning( sprintf( 'Check logs and retry `ee site ssl-verify %s` once the issue is resolved.', $this->site_data['site_url'] ) ); + EE::warning( sprintf( 'Check logs and retry `%s` once the issue is resolved.', $this->ssl_verify_command( $this->site_data['site_url'], $force ) ) ); } return false; diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index b2ae2d63..887f058e 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -826,6 +826,15 @@ function get_site_compose_files( $site_fs_path ) { // `admin-tools disable` leaves docker-compose-admin.yml in place, so the DB flag decides. foreach ( Site::where( 'site_fs_path', $site_fs_path ) as $site ) { if ( ! empty( $site->admin_tools ) ) { + // A failed v4.13.2 admin-tools migration puts back the old file, which mounts the EasyEngine DB into the site. + $admin_compose = @file_get_contents( $site_fs_path . '/docker-compose-admin.yml' ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- a missing file is skipped later. + // Comment lines don't count; any other mention of the DB path does, as a mount can be written in several forms. + if ( defined( 'DB' ) && false !== $admin_compose && preg_match( '/^(?![ \t]*#).*' . preg_quote( DB, '/' ) . '/m', $admin_compose ) ) { + EE::warning( sprintf( 'Not applying %1$s/docker-compose-admin.yml: it still mounts the EasyEngine database. Run `ee admin-tools enable %2$s --force` to bring admin-tools back.', $site_fs_path, $site->site_url ) ); + + return []; + } + return [ 'docker-compose-admin.yml' ]; } }