diff --git a/php/EE/Logrotate/Utils.php b/php/EE/Logrotate/Utils.php index 5a9549e1b..d730592b0 100644 --- a/php/EE/Logrotate/Utils.php +++ b/php/EE/Logrotate/Utils.php @@ -351,14 +351,29 @@ 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(), + ]; + $all_in_place = true; + $created = []; + + foreach ( $configs as $file => $content ) { + $state = self::write_config( $file, $content ); + $all_in_place = $all_in_place && false !== $state; + if ( 'created' === $state ) { + $created[] = $file; + } + } - if ( $ee_log_written && $sites_written && $proxy_written && self::validate_configs( self::get_config_files() ) ) { + // Legacy configs go only once every replacement is in place, or their logs would stop rotating. + if ( $all_in_place ) { self::cleanup_legacy_configs(); - self::force_rotate_configs( self::get_config_files() ); } + + // Only a new config rotates now; a changed rule takes effect at the next scheduled run. + self::force_rotate_configs( $created ); } /** @@ -406,67 +421,67 @@ 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; } - 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 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 ); - 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 true; + 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 ) { - - $is_valid = true; + private static function validate_config( $file ) { - 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; } /** - * 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 +507,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. */