From bba7449dbe8d9d591a45f961e0e2c4e52bb50f0a Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 1 Dec 2026 07:45:05 +0530 Subject: [PATCH 1/3] fix(logrotate): set up rotation once instead of force-rotating on every migration run The ee-setup-logrotate step runs on every migration run: once per update, and on every command of a nightly build. It validated the EasyEngine logrotate configs, cleaned up legacy configs and ran `logrotate -f` on all of them each time, even when nothing had changed, so every update and every nightly command rotated all EasyEngine logs. Now the step only acts on what changed: - configs already in place: nothing is written, validated or rotated; - a missing config: it is written, validated and force-rotated once, as before; - a changed rule: the config is rewritten and validated, and the next scheduled logrotate run applies it; - legacy per-site and nginx-proxy configs are still removed whenever one exists. --- php/EE/Logrotate/Utils.php | 58 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/php/EE/Logrotate/Utils.php b/php/EE/Logrotate/Utils.php index 5a9549e1b..fbd523336 100644 --- a/php/EE/Logrotate/Utils.php +++ b/php/EE/Logrotate/Utils.php @@ -351,14 +351,30 @@ private static function setup_logrotate_config() { } } - $ee_log_written = self::write_config( self::EE_LOG_CONFIG_FILE, self::get_ee_log_config() ); - $sites_written = self::write_config( self::SITES_CONFIG_FILE, self::get_sites_config() ); - $proxy_written = self::write_config( self::NGINX_PROXY_CONFIG_FILE, self::get_nginx_proxy_config() ); + $configs = [ + self::EE_LOG_CONFIG_FILE => self::get_ee_log_config(), + self::SITES_CONFIG_FILE => self::get_sites_config(), + self::NGINX_PROXY_CONFIG_FILE => self::get_nginx_proxy_config(), + ]; + $states = []; + + foreach ( $configs as $file => $content ) { + $states[ $file ] = self::write_config( $file, $content ); + } - if ( $ee_log_written && $sites_written && $proxy_written && self::validate_configs( self::get_config_files() ) ) { - self::cleanup_legacy_configs(); - self::force_rotate_configs( self::get_config_files() ); + if ( in_array( false, $states, true ) ) { + return; } + + $written = array_keys( array_diff( $states, [ 'unchanged' ] ) ); + if ( ! self::validate_configs( $written ) ) { + return; + } + + self::cleanup_legacy_configs(); + + // Only a new config rotates now; a changed rule takes effect at the next scheduled run. + self::force_rotate_configs( array_keys( $states, 'created', true ) ); } /** @@ -406,20 +422,22 @@ private static function command_exists( $command ) { * * @param string $file Config file path. * @param string $content Config content. - * @return bool + * @return string|false 'unchanged', 'created' or 'updated', or false when it can't be written. */ private static function write_config( $file, $content ) { - if ( file_exists( $file ) && is_readable( $file ) && $content === file_get_contents( $file ) ) { - return true; + $exists = file_exists( $file ); + + if ( $exists && is_readable( $file ) && $content === file_get_contents( $file ) ) { + return 'unchanged'; } - if ( file_exists( $file ) && ! is_writable( $file ) ) { + if ( $exists && ! is_writable( $file ) ) { \EE::warning( 'EasyEngine logrotate config is not writable: ' . $file ); return false; } - if ( ! file_exists( $file ) && ! is_writable( dirname( $file ) ) ) { + if ( ! $exists && ! is_writable( dirname( $file ) ) ) { \EE::warning( 'EasyEngine logrotate config directory is not writable: ' . dirname( $file ) ); return false; } @@ -435,7 +453,7 @@ private static function write_config( $file, $content ) { \EE::debug( 'EasyEngine logrotate config written: ' . $file ); - return true; + return $exists ? 'updated' : 'created'; } /** @@ -466,7 +484,7 @@ private static function validate_configs( array $files ) { } /** - * Force rotate EasyEngine logs once after setup or update. + * Force rotate EasyEngine logs once after their config is first set up. * * @param array $files Config files. * @return bool @@ -492,20 +510,6 @@ private static function force_rotate_configs( array $files ) { return $is_rotated; } - /** - * Return generated EasyEngine logrotate config files. - * - * @return array - */ - private static function get_config_files() { - - return [ - self::EE_LOG_CONFIG_FILE, - self::SITES_CONFIG_FILE, - self::NGINX_PROXY_CONFIG_FILE, - ]; - } - /** * Remove old per-site/proxy configs that duplicate the wildcard configs. */ From dffcf2930c756794be6413fdeb32fd502e53fef7 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 1 Dec 2026 07:47:21 +0530 Subject: [PATCH 2/3] fix(logrotate): validate a config before installing it A rule that logrotate rejected was left in place, so the next run saw unchanged content, skipped validation and removed the legacy configs while the replacement was invalid. A config created in a run where another config failed also lost its first rotation. Write each config to a temporary file that logrotate ignores, validate it, and only then move it into place, so a rejected rule never lands and the next run retries it. Remove legacy configs only when all three configs are in place, and rotate each newly created config even when another one failed. --- php/EE/Logrotate/Utils.php | 69 ++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/php/EE/Logrotate/Utils.php b/php/EE/Logrotate/Utils.php index fbd523336..44c3dac57 100644 --- a/php/EE/Logrotate/Utils.php +++ b/php/EE/Logrotate/Utils.php @@ -356,25 +356,24 @@ private static function setup_logrotate_config() { self::SITES_CONFIG_FILE => self::get_sites_config(), self::NGINX_PROXY_CONFIG_FILE => self::get_nginx_proxy_config(), ]; - $states = []; + $all_in_place = true; + $created = []; foreach ( $configs as $file => $content ) { - $states[ $file ] = self::write_config( $file, $content ); + $state = self::write_config( $file, $content ); + $all_in_place = $all_in_place && false !== $state; + if ( 'created' === $state ) { + $created[] = $file; + } } - if ( in_array( false, $states, true ) ) { - return; + // Legacy configs go only once every replacement is in place, or their logs would stop rotating. + if ( $all_in_place ) { + self::cleanup_legacy_configs(); } - $written = array_keys( array_diff( $states, [ 'unchanged' ] ) ); - if ( ! self::validate_configs( $written ) ) { - return; - } - - self::cleanup_legacy_configs(); - // Only a new config rotates now; a changed rule takes effect at the next scheduled run. - self::force_rotate_configs( array_keys( $states, 'created', true ) ); + self::force_rotate_configs( $created ); } /** @@ -442,45 +441,43 @@ private static function write_config( $file, $content ) { return false; } - if ( false === file_put_contents( $file, $content ) ) { + // Validated before it replaces the config, so a rejected rule never lands and the next run retries it. + // The trailing ~ is in logrotate's tabooext, so a run in between never reads the new file. + $new = $file . '.ee-new~'; + + if ( false === file_put_contents( $new, $content ) || ! chmod( $new, 0644 ) ) { \EE::warning( 'Unable to write EasyEngine logrotate config: ' . $file ); - return false; - } + } elseif ( self::validate_config( $new ) && rename( $new, $file ) ) { + \EE::debug( 'EasyEngine logrotate config written: ' . $file ); - if ( ! chmod( $file, 0644 ) ) { - \EE::warning( 'Unable to set permissions on EasyEngine logrotate config: ' . $file ); + return $exists ? 'updated' : 'created'; } - \EE::debug( 'EasyEngine logrotate config written: ' . $file ); + if ( file_exists( $new ) ) { + unlink( $new ); + } - return $exists ? 'updated' : 'created'; + return false; } /** - * Validate generated logrotate configs. + * Validate a generated logrotate config. * - * @param array $files Config files. + * @param string $file Config file path. * @return bool */ - private static function validate_configs( array $files ) { + private static function validate_config( $file ) { - $is_valid = true; - - foreach ( $files as $file ) { - $command = 'logrotate -d ' . escapeshellarg( $file ) . ' 2>&1'; - $result = \EE::launch( $command, false, true ); - - if ( 0 === $result->return_code ) { - \EE::debug( 'EasyEngine logrotate config validated: ' . $file ); - continue; - } + $result = \EE::launch( 'logrotate -d ' . escapeshellarg( $file ) . ' 2>&1', false, true ); - $message = trim( $result->stdout ); - \EE::warning( 'Unable to validate EasyEngine logrotate config: ' . $file . ( $message ? '. ' . $message : '' ) ); - $is_valid = false; + if ( 0 === $result->return_code ) { + return true; } - return $is_valid; + $message = trim( $result->stdout ); + \EE::warning( 'Unable to validate EasyEngine logrotate config: ' . $file . ( $message ? '. ' . $message : '' ) ); + + return false; } /** From 2e26aa07860adc8fda969666c4bfd9dd348a7ec8 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Tue, 1 Dec 2026 07:50:29 +0530 Subject: [PATCH 3/3] fix(logrotate): give each run its own staging file Two migration runs writing the same config shared one staging path, so the first rename could take the second run's file away and make it skip the legacy cleanup. Add the process ID to the staging name; it still ends in ~, so logrotate never reads it, and it is removed on every failure path. --- php/EE/Logrotate/Utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/EE/Logrotate/Utils.php b/php/EE/Logrotate/Utils.php index 44c3dac57..d730592b0 100644 --- a/php/EE/Logrotate/Utils.php +++ b/php/EE/Logrotate/Utils.php @@ -442,8 +442,8 @@ private static function write_config( $file, $content ) { } // Validated before it replaces the config, so a rejected rule never lands and the next run retries it. - // The trailing ~ is in logrotate's tabooext, so a run in between never reads the new file. - $new = $file . '.ee-new~'; + // The pid keeps concurrent runs apart; the trailing ~ is in logrotate's tabooext, so logrotate never reads it. + $new = $file . '.ee-new-' . getmypid() . '~'; if ( false === file_put_contents( $new, $content ) || ! chmod( $new, 0644 ) ) { \EE::warning( 'Unable to write EasyEngine logrotate config: ' . $file );