fix: litespeed disable-cache WP-CLI implementation, metrics service_count arithmetic, ttlpub real WP-CLI fetch

main
Anthony 2026-02-28 16:01:44 +08:00
parent 3c2d2fbbe7
commit 4c54fa106f
3 changed files with 102 additions and 36 deletions

View File

@ -758,8 +758,9 @@ actions:
message: "${response.out}" message: "${response.out}"
litespeed_ttlpub: litespeed_ttlpub:
- cmd[cp]: - cmd[cp]:
user: root
commands: 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: - return:
type: info type: info
message: "${response.out}" message: "${response.out}"

View File

@ -90,11 +90,9 @@ if [[ -n "$cdn_status" ]]; then
cdn_enabled="false" cdn_enabled="false"
fi fi
# Count active nodes # Count active nodes (wc -l always returns at least 1 for empty input)
node_count=$(echo "$active_nodes" | wc -l) node_count=$(echo "$active_nodes" | grep -c '[^[:space:]]' 2>/dev/null || echo "0")
if [[ "$node_count" -le 1 ]]; then node_count=${node_count:-0}
node_count="0"
fi
echo ' "enabled": "'$cdn_enabled'",' echo ' "enabled": "'$cdn_enabled'",'
echo ' "active_nodes": "'$node_count'",' echo ' "active_nodes": "'$node_count'",'
@ -127,8 +125,9 @@ if [[ -n "$services_list" ]]; then
cdn_available="true" cdn_available="true"
fi fi
# Count total services # Count total services (force integer - grep -c can produce multi-token output)
service_count=$(echo "$services_list" | grep -c ":" 2>/dev/null || echo "0") 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 ' "image_optimization_available": "'$img_optm_available'",'
echo ' "cdn_available": "'$cdn_available'",' echo ' "cdn_available": "'$cdn_available'",'
@ -168,7 +167,9 @@ if [[ "$cdn_enabled" == "true" ]]; then
fi fi
# QUIC.cloud connected adds 20 points # 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)) health_score=$((health_score + 20))
fi fi
@ -201,7 +202,7 @@ if [[ "$cdn_enabled" != "true" ]]; then
recommendations+=("Enable CDN") recommendations+=("Enable CDN")
fi fi
if [[ "$service_count" -eq 0 ]]; then if [[ "$service_count_int" -eq 0 ]]; then
recommendations+=("Connect to QUIC.cloud Services") recommendations+=("Connect to QUIC.cloud Services")
fi fi

View File

@ -59,26 +59,87 @@ function get_lsws_version() {
return 'unknown'; return 'unknown';
} }
// Function to purge cache via admin console (if configured) // 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;
}
// 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;
}
}
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() { function purge_cache() {
// For Virtuozzo LLSMP, we'll restart the server as a basic cache clear $wp = find_wp_cli();
return restart_lsws(); $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'];
} }
// Function to enable/disable cache in httpd_config.conf // Enable or disable LiteSpeed Cache via WP-CLI plugin option
function toggle_cache($enable = true) { function toggle_cache($enable = true) {
$config_file = '/usr/local/lsws/conf/httpd_config.conf'; $wp = find_wp_cli();
if (!file_exists($config_file) || !is_writable($config_file)) { $wp_root = find_wp_root();
return false;
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'];
} }
// This is a simplified example - actual implementation would need // Check plugin active first
// to parse and modify the LiteSpeed configuration properly $check = run_wp_cli($wp, $wp_root, 'plugin is-active litespeed-cache');
$backup_file = $config_file . '.backup.' . date('YmdHis'); if ($check['return_var'] !== 0) {
copy($config_file, $backup_file); return ['ok' => false, 'error' => 'LiteSpeed Cache plugin is not active in WordPress'];
}
// For demonstration - actual cache configuration would be more complex $value = $enable ? '1' : '0';
return reload_lsws(); $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) { switch ($action) {
@ -127,26 +188,29 @@ switch ($action) {
break; break;
case 'purge': case 'purge':
if (purge_cache()) { $purge = purge_cache();
echo json_encode(['status' => 'success', 'message' => 'Cache purged (server restarted)']); if ($purge['ok']) {
echo json_encode(['status' => 'success', 'message' => 'Cache purged successfully', 'method' => $purge['method']]);
} else { } 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; break;
case 'enable-cache': case 'enable-cache':
if (toggle_cache(true)) { $toggle = toggle_cache(true);
echo json_encode(['status' => 'success', 'message' => 'Cache enabled and server reloaded']); if ($toggle['ok']) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache enabled and server reloaded']);
} else { } else {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable cache']); echo json_encode(['status' => 'error', 'message' => 'Failed to enable cache: ' . $toggle['error']]);
} }
break; break;
case 'disable-cache': case 'disable-cache':
if (toggle_cache(false)) { $toggle = toggle_cache(false);
echo json_encode(['status' => 'success', 'message' => 'Cache disabled and server reloaded']); if ($toggle['ok']) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache disabled and server reloaded']);
} else { } else {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable cache']); echo json_encode(['status' => 'error', 'message' => 'Failed to disable cache: ' . $toggle['error']]);
} }
break; break;