appwrite/src/Appwrite/Platform/Tasks/Doctor.php

279 lines
10 KiB
PHP
Raw Normal View History

2022-07-13 07:02:55 +00:00
<?php
2022-11-14 10:01:41 +00:00
namespace Appwrite\Platform\Tasks;
2022-07-13 07:02:55 +00:00
2024-03-06 17:34:21 +00:00
use Appwrite\ClamAV\Network;
2022-07-13 07:02:55 +00:00
use Utopia\App;
use Utopia\CLI\Console;
2022-10-16 17:48:53 +00:00
use Utopia\Config\Config;
2022-07-13 07:02:55 +00:00
use Utopia\Domains\Domain;
2024-03-06 17:34:21 +00:00
use Utopia\Logger\Logger;
2022-07-13 07:02:55 +00:00
use Utopia\Platform\Action;
2022-10-28 03:36:20 +00:00
use Utopia\Registry\Registry;
2024-03-06 17:34:21 +00:00
use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage;
2024-04-01 11:02:47 +00:00
use Utopia\System\System;
2022-07-13 07:02:55 +00:00
2022-07-14 02:04:31 +00:00
class Doctor extends Action
{
2022-08-02 01:58:36 +00:00
public static function getName(): string
{
return 'doctor';
2022-08-02 02:03:30 +00:00
}
2022-07-13 07:02:55 +00:00
public function __construct()
{
$this
->desc('Validate server health')
2022-10-28 03:36:20 +00:00
->inject('register')
2022-11-14 10:36:21 +00:00
->callback(fn (Registry $register) => $this->action($register));
2022-07-13 07:02:55 +00:00
}
2022-10-28 03:36:20 +00:00
public function action(Registry $register): void
2022-07-13 07:02:55 +00:00
{
Console::log(" __ ____ ____ _ _ ____ __ ____ ____ __ __
/ _\ ( _ \( _ \/ )( \( _ \( )(_ _)( __) ( )/ \
/ \ ) __/ ) __/\ /\ / ) / )( )( ) _) _ )(( O )
\_/\_/(__) (__) (_/\_)(__\_)(__) (__) (____)(_)(__)\__/ ");
2024-04-01 11:02:47 +00:00
Console::log("\n" . '👩‍⚕️ Running ' . APP_NAME . ' Doctor for version ' . System::getEnv('_APP_VERSION', 'UNKNOWN') . ' ...' . "\n");
2022-07-13 07:02:55 +00:00
2022-10-16 17:48:53 +00:00
Console::log('[Settings]');
2022-07-13 07:02:55 +00:00
2024-04-01 11:02:47 +00:00
$domain = new Domain(System::getEnv('_APP_DOMAIN'));
2022-07-13 07:02:55 +00:00
if (!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 Hostname has no public suffix (' . $domain->get() . ')');
} else {
Console::log('🟢 Hostname has a public suffix (' . $domain->get() . ')');
}
2024-04-01 11:02:47 +00:00
$domain = new Domain(System::getEnv('_APP_DOMAIN_TARGET'));
2022-07-13 07:02:55 +00:00
if (!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 CNAME target has no public suffix (' . $domain->get() . ')');
} else {
Console::log('🟢 CNAME target has a public suffix (' . $domain->get() . ')');
}
2024-04-01 11:02:47 +00:00
if (System::getEnv('_APP_OPENSSL_KEY_V1') === 'your-secret-key' || empty(System::getEnv('_APP_OPENSSL_KEY_V1'))) {
2022-07-13 07:02:55 +00:00
Console::log('🔴 Not using a unique secret key for encryption');
} else {
Console::log('🟢 Using a unique secret key for encryption');
}
2024-04-01 11:02:47 +00:00
if (System::getEnv('_APP_ENV', 'development') !== 'production') {
2022-07-13 07:02:55 +00:00
Console::log('🔴 App environment is set for development');
} else {
Console::log('🟢 App environment is set for production');
}
2024-04-01 11:02:47 +00:00
if ('enabled' !== System::getEnv('_APP_OPTIONS_ABUSE', 'disabled')) {
2022-07-13 07:02:55 +00:00
Console::log('🔴 Abuse protection is disabled');
} else {
Console::log('🟢 Abuse protection is enabled');
}
2024-04-01 11:02:47 +00:00
$authWhitelistRoot = System::getEnv('_APP_CONSOLE_WHITELIST_ROOT', null);
$authWhitelistEmails = System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null);
$authWhitelistIPs = System::getEnv('_APP_CONSOLE_WHITELIST_IPS', null);
2022-07-13 07:02:55 +00:00
if (
empty($authWhitelistRoot)
&& empty($authWhitelistEmails)
&& empty($authWhitelistIPs)
) {
Console::log('🔴 Console access limits are disabled');
} else {
Console::log('🟢 Console access limits are enabled');
}
2024-04-01 11:02:47 +00:00
if ('enabled' !== System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled')) {
2022-07-13 07:02:55 +00:00
Console::log('🔴 HTTPS force option is disabled');
} else {
Console::log('🟢 HTTPS force option is enabled');
}
2024-04-01 11:02:47 +00:00
if ('enabled' !== System::getEnv('_APP_OPTIONS_FUNCTIONS_FORCE_HTTPS', 'disabled')) {
2023-09-18 10:27:47 +00:00
Console::log('🔴 HTTPS force option is disabled for function domains');
} else {
Console::log('🟢 HTTPS force option is enabled for function domains');
}
2024-04-01 11:02:47 +00:00
$providerConfig = System::getEnv('_APP_LOGGING_CONFIG', '');
2022-07-13 07:02:55 +00:00
try {
$loggingProvider = new DSN($providerConfig ?? '');
$providerName = $loggingProvider->getScheme();
if (empty($providerName) || !Logger::hasProvider($providerName)) {
Console::log('🔴 Logging adapter is disabled');
} else {
Console::log('🟢 Logging adapter is enabled (' . $providerName . ')');
}
} catch (\Throwable $th) {
Console::log('🔴 Logging adapter is misconfigured');
2022-07-13 07:02:55 +00:00
}
\usleep(200 * 1000); // Sleep for 0.2 seconds
2022-07-13 07:02:55 +00:00
try {
2022-10-16 17:48:53 +00:00
Console::log("\n" . '[Connectivity]');
2022-07-13 07:02:55 +00:00
} catch (\Throwable $th) {
//throw $th;
}
2022-10-16 17:48:53 +00:00
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
2022-10-16 22:49:53 +00:00
2022-10-16 17:48:53 +00:00
$configs = [
'Console.DB' => Config::getParam('pools-console'),
'Projects.DB' => Config::getParam('pools-database'),
];
foreach ($configs as $key => $config) {
foreach ($config as $database) {
try {
2022-10-16 22:49:53 +00:00
$adapter = $pools->get($database)->pop()->getResource();
2022-10-19 08:35:30 +00:00
if ($adapter->ping()) {
Console::success('🟢 ' . str_pad("{$key}({$database})", 50, '.') . 'connected');
2022-10-16 17:48:53 +00:00
} else {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("{$key}({$database})", 47, '.') . 'disconnected');
2022-10-16 17:48:53 +00:00
}
} catch (\Throwable $th) {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("{$key}.({$database})", 47, '.') . 'disconnected');
2022-10-16 17:48:53 +00:00
}
}
2022-07-13 07:02:55 +00:00
}
2022-10-16 17:48:53 +00:00
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
$configs = [
'Cache' => Config::getParam('pools-cache'),
'Queue' => Config::getParam('pools-queue'),
'PubSub' => Config::getParam('pools-pubsub'),
];
foreach ($configs as $key => $config) {
foreach ($config as $pool) {
try {
2022-10-16 22:49:53 +00:00
$adapter = $pools->get($pool)->pop()->getResource();
2022-10-19 08:35:30 +00:00
if ($adapter->ping()) {
Console::success('🟢 ' . str_pad("{$key}({$pool})", 50, '.') . 'connected');
2022-10-16 17:48:53 +00:00
} else {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("{$key}({$pool})", 47, '.') . 'disconnected');
2022-10-16 17:48:53 +00:00
}
} catch (\Throwable $th) {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("{$key}({$pool})", 47, '.') . 'disconnected');
2022-10-16 17:48:53 +00:00
}
}
2022-07-13 07:02:55 +00:00
}
2024-04-01 11:02:47 +00:00
if (System::getEnv('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
2022-07-13 07:02:55 +00:00
try {
$antivirus = new Network(
2024-04-01 11:02:47 +00:00
System::getEnv('_APP_STORAGE_ANTIVIRUS_HOST', 'clamav'),
(int) System::getEnv('_APP_STORAGE_ANTIVIRUS_PORT', 3310)
2022-07-13 07:02:55 +00:00
);
if ((@$antivirus->ping())) {
2022-10-19 08:35:30 +00:00
Console::success('🟢 ' . str_pad("Antivirus", 50, '.') . 'connected');
2022-07-13 07:02:55 +00:00
} else {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("Antivirus", 47, '.') . 'disconnected');
2022-07-13 07:02:55 +00:00
}
} catch (\Throwable $th) {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("Antivirus", 47, '.') . 'disconnected');
2022-07-13 07:02:55 +00:00
}
}
try {
$mail = $register->get('smtp'); /* @var $mail \PHPMailer\PHPMailer\PHPMailer */
$mail->addAddress('demo@example.com', 'Example.com');
$mail->Subject = 'Test SMTP Connection';
$mail->Body = 'Hello World';
$mail->AltBody = 'Hello World';
$mail->send();
2022-10-19 08:35:30 +00:00
Console::success('🟢 ' . str_pad("SMTP", 50, '.') . 'connected');
2022-07-13 07:02:55 +00:00
} catch (\Throwable $th) {
2022-10-19 08:35:30 +00:00
Console::error('🔴 ' . str_pad("SMTP", 47, '.') . 'disconnected');
2022-07-13 07:02:55 +00:00
}
\usleep(200 * 1000); // Sleep for 0.2 seconds
2022-07-13 07:02:55 +00:00
Console::log('');
2022-10-16 17:48:53 +00:00
Console::log('[Volumes]');
2022-07-13 07:02:55 +00:00
foreach (
[
2024-03-06 17:34:21 +00:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-07-13 07:02:55 +00:00
] as $key => $volume
) {
$device = new Local($volume);
if (\is_readable($device->getRoot())) {
Console::success('🟢 ' . $key . ' Volume is readable');
} else {
Console::error('🔴 ' . $key . ' Volume is unreadable');
}
if (\is_writable($device->getRoot())) {
Console::success('🟢 ' . $key . ' Volume is writeable');
} else {
Console::error('🔴 ' . $key . ' Volume is unwriteable');
}
}
\usleep(200 * 1000); // Sleep for 0.2 seconds
2022-07-13 07:02:55 +00:00
Console::log('');
2022-10-16 17:48:53 +00:00
Console::log('[Disk Space]');
2022-07-13 07:02:55 +00:00
foreach (
[
2024-03-06 17:34:21 +00:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-07-13 07:02:55 +00:00
] as $key => $volume
) {
$device = new Local($volume);
$percentage = (($device->getPartitionTotalSpace() - $device->getPartitionFreeSpace())
/ $device->getPartitionTotalSpace()) * 100;
$message = $key . ' Volume has ' . Storage::human($device->getPartitionFreeSpace()) . ' free space (' . \round($percentage, 2) . '% used)';
if ($percentage < 80) {
Console::success('🟢 ' . $message);
} else {
Console::error('🔴 ' . $message);
}
}
try {
if (App::isProduction()) {
Console::log('');
2024-04-01 11:02:47 +00:00
$version = \json_decode(@\file_get_contents(System::getEnv('_APP_HOME', 'http://localhost') . '/version'), true);
2022-07-13 07:02:55 +00:00
if ($version && isset($version['version'])) {
2024-04-01 11:02:47 +00:00
if (\version_compare($version['version'], System::getEnv('_APP_VERSION', 'UNKNOWN')) === 0) {
2022-07-13 07:02:55 +00:00
Console::info('You are running the latest version of ' . APP_NAME . '! 🥳');
} else {
Console::info('A new version (' . $version['version'] . ') is available! 🥳' . "\n");
}
} else {
Console::error('Failed to check for a newer version' . "\n");
}
}
} catch (\Throwable $th) {
Console::error('Failed to check for a newer version' . "\n");
}
}
2022-07-14 02:04:31 +00:00
}