diff --git a/mbadmin.jps b/mbadmin.jps index b7cd16a..491a884 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -652,8 +652,23 @@ actions: disable_opcache: - cmd[cp]: user: root - commands: - - php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable + commands: + - | + set -euo pipefail + TMP_OUTPUT="$(mktemp)" + cleanup() { + rm -f "${TMP_OUTPUT}" + } + trap cleanup EXIT + + php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable > "${TMP_OUTPUT}" + + if grep -Eq '"status"[[:space:]]*:[[:space:]]*"error"' "${TMP_OUTPUT}"; then + cat "${TMP_OUTPUT}" + exit 1 + fi + + cat "${TMP_OUTPUT}" - restartNodes: - nodeGroup: "cp" reboot: true diff --git a/scripts/toggle_opcache.php b/scripts/toggle_opcache.php index 3053f39..995a535 100644 --- a/scripts/toggle_opcache.php +++ b/scripts/toggle_opcache.php @@ -35,14 +35,49 @@ function find_php_config_files() { '/etc/php/8.2/cli/conf.d/', '/etc/php/8.1/cli/conf.d/', '/etc/php/8.0/cli/conf.d/', + '/usr/local/lsws/lsphp/etc/php.d/', '/usr/local/lsws/lsphp84/etc/php.d/', '/usr/local/lsws/lsphp83/etc/php.d/', '/usr/local/lsws/lsphp82/etc/php.d/', '/usr/local/lsws/lsphp81/etc/php.d/', '/usr/local/lsws/lsphp80/etc/php.d/' ]; + + $possible_files = [ + '/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini', + '/etc/php.d/10-opcache.ini' + ]; $config_files = []; + + foreach ($possible_files as $file) { + if (is_file($file)) { + $config_files[] = $file; + } + } + + $loaded_ini = php_ini_loaded_file(); + if ($loaded_ini !== false && is_file($loaded_ini)) { + $content = file_get_contents($loaded_ini); + if ($content !== false && preg_match('/^\s*;?\s*opcache\.enable\s*=\s*/mi', $content)) { + $config_files[] = $loaded_ini; + } + } + + $scanned_ini = php_ini_scanned_files(); + if ($scanned_ini !== false && trim($scanned_ini) !== '') { + foreach (explode(',', $scanned_ini) as $ini_file) { + $ini_file = trim($ini_file); + if ($ini_file === '' || !is_file($ini_file)) { + continue; + } + + if (stripos(basename($ini_file), 'opcache') !== false) { + $config_files[] = $ini_file; + } + } + } + foreach ($possible_paths as $path) { if (is_dir($path)) { $files = glob($path . '*opcache*'); @@ -52,7 +87,7 @@ function find_php_config_files() { } } - return $config_files; + return array_values(array_unique($config_files)); } // Function to modify OPcache configuration @@ -65,26 +100,35 @@ function modify_opcache_config($enable) { $success = false; foreach ($config_files as $file) { - if (is_writable($file)) { - $content = file_get_contents($file); - if ($content !== false) { - // Replace opcache.enable setting - if ($enable) { - $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); - $content = preg_replace('/^;opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); - } else { - $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=0', $content); - } - - // If no opcache.enable line exists, add it - if (!preg_match('/^opcache\.enable\s*=/m', $content)) { - $content .= "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; - } - - if (file_put_contents($file, $content) !== false) { - $success = true; - } - } + if (!is_file($file)) { + continue; + } + + $content = file_get_contents($file); + if ($content === false) { + continue; + } + + $replacement_count = 0; + $updated_content = preg_replace( + '/^\s*;?\s*opcache\.enable\s*=\s*.*$/mi', + 'opcache.enable=' . ($enable ? '1' : '0'), + $content, + -1, + $replacement_count + ); + + if ($updated_content === null) { + continue; + } + + // If no opcache.enable line exists, add it once. + if ($replacement_count === 0) { + $updated_content = rtrim($updated_content) . "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; + } + + if (file_put_contents($file, $updated_content) !== false) { + $success = true; } }