|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Deployer; |
| 6 | + |
| 7 | +use Deployer\Exception\RunException; |
| 8 | + |
| 9 | +task('requirements:health', function (): void { |
| 10 | + set('requirements_rows', []); |
| 11 | + |
| 12 | + if (!get('requirements_check_health_enabled')) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + // 1. PHP-FPM |
| 17 | + try { |
| 18 | + $phpVersion = trim(run('php -r "echo PHP_MAJOR_VERSION.\'.\'.PHP_MINOR_VERSION;" 2>/dev/null')); |
| 19 | + $fpmService = isServiceActive("php$phpVersion-fpm", 'php-fpm'); |
| 20 | + |
| 21 | + if ($fpmService !== null) { |
| 22 | + addRequirementRow('PHP-FPM', REQUIREMENT_OK, "Active ($fpmService)"); |
| 23 | + } else { |
| 24 | + addRequirementRow('PHP-FPM', REQUIREMENT_FAIL, 'Process not found'); |
| 25 | + } |
| 26 | + } catch (RunException) { |
| 27 | + addRequirementRow('PHP-FPM', REQUIREMENT_SKIP, 'Could not determine PHP version'); |
| 28 | + } |
| 29 | + |
| 30 | + // 2. Webserver |
| 31 | + $webserver = isServiceActive('nginx', 'apache2', 'httpd'); |
| 32 | + |
| 33 | + if ($webserver !== null) { |
| 34 | + addRequirementRow('Webserver', REQUIREMENT_OK, "Active ($webserver)"); |
| 35 | + } else { |
| 36 | + addRequirementRow('Webserver', REQUIREMENT_FAIL, 'No nginx, apache2 or httpd process found'); |
| 37 | + } |
| 38 | + |
| 39 | + // 3. Database server |
| 40 | + $db = detectDatabaseProduct(); |
| 41 | + $dbLabel = $db !== null ? $db['label'] : 'Database'; |
| 42 | + $adminCmd = ($db !== null && $db['product'] === 'mariadb') ? 'mariadb-admin' : 'mysqladmin'; |
| 43 | + $dbChecked = false; |
| 44 | + |
| 45 | + try { |
| 46 | + run("$adminCmd ping --silent 2>&1 || true"); |
| 47 | + addRequirementRow('Database server', REQUIREMENT_OK, "$dbLabel responding"); |
| 48 | + $dbChecked = true; |
| 49 | + } catch (RunException) { |
| 50 | + // Admin tool not available — fall through to process check |
| 51 | + } |
| 52 | + |
| 53 | + if (!$dbChecked) { |
| 54 | + $dbProcess = isServiceActive('mysqld', 'mariadbd'); |
| 55 | + |
| 56 | + if ($dbProcess !== null) { |
| 57 | + addRequirementRow('Database server', REQUIREMENT_OK, "$dbLabel process running ($dbProcess)"); |
| 58 | + } else { |
| 59 | + addRequirementRow('Database server', REQUIREMENT_FAIL, 'No mysqld or mariadbd process found'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // 4. HTTP response |
| 64 | + $url = get('requirements_health_url'); |
| 65 | + |
| 66 | + try { |
| 67 | + $httpCode = (int) trim(run( |
| 68 | + sprintf("curl -s -o /dev/null -w '%%{http_code}' --max-time 5 %s 2>/dev/null", escapeshellarg($url)) |
| 69 | + )); |
| 70 | + |
| 71 | + if ($httpCode >= 200 && $httpCode < 500) { |
| 72 | + addRequirementRow('HTTP response', REQUIREMENT_OK, "HTTP $httpCode from $url"); |
| 73 | + } elseif ($httpCode === 0) { |
| 74 | + addRequirementRow('HTTP response', REQUIREMENT_FAIL, "No response from $url (connection refused or timeout)"); |
| 75 | + } else { |
| 76 | + addRequirementRow('HTTP response', REQUIREMENT_FAIL, "HTTP $httpCode from $url"); |
| 77 | + } |
| 78 | + } catch (RunException) { |
| 79 | + addRequirementRow('HTTP response', REQUIREMENT_SKIP, 'curl not available'); |
| 80 | + } |
| 81 | + |
| 82 | + renderRequirementsTable(); |
| 83 | +})->desc('Check service health'); |
0 commit comments