From 4c54fa106f4acb94eb8762cb2988b9c661580ad7 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sat, 28 Feb 2026 16:01:44 +0800 Subject: [PATCH] fix: litespeed disable-cache WP-CLI implementation, metrics service_count arithmetic, ttlpub real WP-CLI fetch --- mbadmin.jps | 3 +- scripts/litespeed_metrics.sh | 19 +++--- scripts/manage_litespeed.php | 116 +++++++++++++++++++++++++++-------- 3 files changed, 102 insertions(+), 36 deletions(-) diff --git a/mbadmin.jps b/mbadmin.jps index 814b366..a8e9427 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -758,8 +758,9 @@ actions: message: "${response.out}" litespeed_ttlpub: - cmd[cp]: + user: root commands: - - echo '{"status":"info","message":"TTL settings are managed at server level in Virtuozzo LLSMP - check LiteSpeed admin console"}' + - WP=/home/litespeed/bin/wp; [ ! -x "$WP" ] && WP=$(command -v wp 2>/dev/null); WP_ROOT=/var/www/webroot/ROOT; if [ -z "$WP" ] || [ ! -f "$WP_ROOT/wp-config.php" ]; then echo '{"status":"error","message":"WP-CLI or WordPress not found"}'; exit 0; fi; TTL_PUB=$(sudo -u litespeed "$WP" --path="$WP_ROOT" litespeed-option get cache-ttl_pub 2>/dev/null || echo "unknown"); TTL_PRIV=$(sudo -u litespeed "$WP" --path="$WP_ROOT" litespeed-option get cache-ttl_priv 2>/dev/null || echo "unknown"); TTL_FP=$(sudo -u litespeed "$WP" --path="$WP_ROOT" litespeed-option get cache-ttl_frontpage 2>/dev/null || echo "unknown"); TTL_FEED=$(sudo -u litespeed "$WP" --path="$WP_ROOT" litespeed-option get cache-ttl_feed 2>/dev/null || echo "unknown"); printf '{"status":"success","ttl":{"public_seconds":"%s","private_seconds":"%s","frontpage_seconds":"%s","feed_seconds":"%s"}}' "$TTL_PUB" "$TTL_PRIV" "$TTL_FP" "$TTL_FEED" - return: type: info message: "${response.out}" diff --git a/scripts/litespeed_metrics.sh b/scripts/litespeed_metrics.sh index b57ac1a..2589e9f 100644 --- a/scripts/litespeed_metrics.sh +++ b/scripts/litespeed_metrics.sh @@ -90,11 +90,9 @@ if [[ -n "$cdn_status" ]]; then cdn_enabled="false" fi - # Count active nodes - node_count=$(echo "$active_nodes" | wc -l) - if [[ "$node_count" -le 1 ]]; then - node_count="0" - fi + # Count active nodes (wc -l always returns at least 1 for empty input) + node_count=$(echo "$active_nodes" | grep -c '[^[:space:]]' 2>/dev/null || echo "0") + node_count=${node_count:-0} echo ' "enabled": "'$cdn_enabled'",' echo ' "active_nodes": "'$node_count'",' @@ -127,8 +125,9 @@ if [[ -n "$services_list" ]]; then cdn_available="true" fi - # Count total services - service_count=$(echo "$services_list" | grep -c ":" 2>/dev/null || echo "0") + # Count total services (force integer - grep -c can produce multi-token output) + service_count=$(echo "$services_list" | grep -c ":" 2>/dev/null | tr -d '[:space:]' | grep -oP '^[0-9]+' || echo "0") + service_count=${service_count:-0} echo ' "image_optimization_available": "'$img_optm_available'",' echo ' "cdn_available": "'$cdn_available'",' @@ -168,7 +167,9 @@ if [[ "$cdn_enabled" == "true" ]]; then fi # QUIC.cloud connected adds 20 points -if [[ "$service_count" -gt 0 ]]; then +service_count_int=$(echo "$service_count" | grep -oP '^[0-9]+' || echo "0") +service_count_int=${service_count_int:-0} +if [[ "$service_count_int" -gt 0 ]]; then health_score=$((health_score + 20)) fi @@ -201,7 +202,7 @@ if [[ "$cdn_enabled" != "true" ]]; then recommendations+=("Enable CDN") fi -if [[ "$service_count" -eq 0 ]]; then +if [[ "$service_count_int" -eq 0 ]]; then recommendations+=("Connect to QUIC.cloud Services") fi diff --git a/scripts/manage_litespeed.php b/scripts/manage_litespeed.php index 7ad79f2..4af3449 100644 --- a/scripts/manage_litespeed.php +++ b/scripts/manage_litespeed.php @@ -59,26 +59,87 @@ function get_lsws_version() { return 'unknown'; } -// Function to purge cache via admin console (if configured) -function purge_cache() { - // For Virtuozzo LLSMP, we'll restart the server as a basic cache clear - return restart_lsws(); +// Find WP-CLI binary +function find_wp_cli() { + $candidates = ['/home/litespeed/bin/wp', '/usr/local/bin/wp', '/usr/bin/wp']; + foreach ($candidates as $path) { + if (file_exists($path) && is_executable($path)) { + return $path; + } + } + $result = run_command('command -v wp'); + if ($result['return_var'] === 0 && !empty($result['output'][0])) { + return trim($result['output'][0]); + } + return null; } -// Function to enable/disable cache in httpd_config.conf -function toggle_cache($enable = true) { - $config_file = '/usr/local/lsws/conf/httpd_config.conf'; - if (!file_exists($config_file) || !is_writable($config_file)) { - return false; +// Find WordPress root +function find_wp_root() { + $candidates = ['/var/www/webroot/ROOT', '/var/www/webroot/ROOT/']; + foreach ($candidates as $path) { + if (file_exists($path . '/wp-config.php')) { + return $path; + } } - - // This is a simplified example - actual implementation would need - // to parse and modify the LiteSpeed configuration properly - $backup_file = $config_file . '.backup.' . date('YmdHis'); - copy($config_file, $backup_file); - - // For demonstration - actual cache configuration would be more complex - return reload_lsws(); + return null; +} + +// Run a WP-CLI command as litespeed user +function run_wp_cli($wp, $wp_root, $args) { + $cmd = "sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " " . $args . " 2>&1"; + return run_command($cmd); +} + +// Function to purge cache: WP-CLI flush first, lsws restart as fallback +function purge_cache() { + $wp = find_wp_cli(); + $wp_root = find_wp_root(); + + if ($wp && $wp_root) { + $result = run_wp_cli($wp, $wp_root, 'litespeed-purge all'); + if ($result['return_var'] === 0) { + return ['ok' => true, 'method' => 'wp-cli litespeed-purge all']; + } + // Fallback: flush cache via lscmctl if available + $lsc = run_command('/usr/local/lsws/bin/lscmctl purge --all 2>&1'); + if ($lsc['return_var'] === 0) { + return ['ok' => true, 'method' => 'lscmctl purge --all']; + } + } + // Last resort: restart lsws clears in-memory cache + $restarted = restart_lsws(); + return ['ok' => $restarted, 'method' => 'lsws restart']; +} + +// Enable or disable LiteSpeed Cache via WP-CLI plugin option +function toggle_cache($enable = true) { + $wp = find_wp_cli(); + $wp_root = find_wp_root(); + + if (!$wp) { + return ['ok' => false, 'error' => 'WP-CLI not found. Check /home/litespeed/bin/wp or /usr/local/bin/wp']; + } + if (!$wp_root) { + return ['ok' => false, 'error' => 'WordPress not found at /var/www/webroot/ROOT']; + } + + // Check plugin active first + $check = run_wp_cli($wp, $wp_root, 'plugin is-active litespeed-cache'); + if ($check['return_var'] !== 0) { + return ['ok' => false, 'error' => 'LiteSpeed Cache plugin is not active in WordPress']; + } + + $value = $enable ? '1' : '0'; + $result = run_wp_cli($wp, $wp_root, "litespeed-option set cache {$value}"); + if ($result['return_var'] !== 0) { + $detail = implode(' ', $result['output']); + return ['ok' => false, 'error' => "litespeed-option set cache {$value} failed: {$detail}"]; + } + + // Reload lsws to apply + reload_lsws(); + return ['ok' => true, 'error' => null]; } switch ($action) { @@ -127,26 +188,29 @@ switch ($action) { break; case 'purge': - if (purge_cache()) { - echo json_encode(['status' => 'success', 'message' => 'Cache purged (server restarted)']); + $purge = purge_cache(); + if ($purge['ok']) { + echo json_encode(['status' => 'success', 'message' => 'Cache purged successfully', 'method' => $purge['method']]); } else { - echo json_encode(['status' => 'error', 'message' => 'Failed to purge cache']); + echo json_encode(['status' => 'error', 'message' => 'Failed to purge cache via all methods']); } break; case 'enable-cache': - if (toggle_cache(true)) { - echo json_encode(['status' => 'success', 'message' => 'Cache enabled and server reloaded']); + $toggle = toggle_cache(true); + if ($toggle['ok']) { + echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache enabled and server reloaded']); } else { - echo json_encode(['status' => 'error', 'message' => 'Failed to enable cache']); + echo json_encode(['status' => 'error', 'message' => 'Failed to enable cache: ' . $toggle['error']]); } break; case 'disable-cache': - if (toggle_cache(false)) { - echo json_encode(['status' => 'success', 'message' => 'Cache disabled and server reloaded']); + $toggle = toggle_cache(false); + if ($toggle['ok']) { + echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache disabled and server reloaded']); } else { - echo json_encode(['status' => 'error', 'message' => 'Failed to disable cache']); + echo json_encode(['status' => 'error', 'message' => 'Failed to disable cache: ' . $toggle['error']]); } break;