1
0
Fork 0
mirror of synced 2024-07-02 13:10:38 +12:00
appwrite/app/tasks/doctor.php

244 lines
8.8 KiB
PHP
Raw Normal View History

2020-03-01 19:33:19 +13:00
<?php
2020-07-29 07:48:51 +12:00
global $cli;
2020-03-01 19:33:19 +13:00
2020-06-17 23:39:46 +12:00
use Appwrite\ClamAV\Network;
use Utopia\Logger\Logger;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage;
2020-06-29 08:45:36 +12:00
use Utopia\App;
2020-03-01 19:33:19 +13:00
use Utopia\CLI\Console;
2020-06-15 20:01:48 +12:00
use Utopia\Domains\Domain;
2020-03-01 19:33:19 +13:00
2020-06-15 20:01:48 +12:00
$cli
->task('doctor')
->desc('Validate server health')
2020-06-29 08:45:36 +12:00
->action(function () use ($register) {
2020-06-15 20:01:48 +12:00
Console::log(" __ ____ ____ _ _ ____ __ ____ ____ __ __
/ _\ ( _ \( _ \/ )( \( _ \( )(_ _)( __) ( )/ \
/ \ ) __/ ) __/\ /\ / ) / )( )( ) _) _ )(( O )
\_/\_/(__) (__) (_/\_)(__\_)(__) (__) (____)(_)(__)\__/ ");
2022-05-24 02:54:50 +12:00
Console::log("\n" . '👩‍⚕️ Running ' . APP_NAME . ' Doctor for version ' . App::getEnv('_APP_VERSION', 'UNKNOWN') . ' ...' . "\n");
2020-06-15 20:01:48 +12:00
Console::log('Checking for production best practices...');
2020-06-29 08:45:36 +12:00
$domain = new Domain(App::getEnv('_APP_DOMAIN'));
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12: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() . ')');
2020-06-15 20:01:48 +12:00
}
2020-06-29 08:45:36 +12:00
$domain = new Domain(App::getEnv('_APP_DOMAIN_TARGET'));
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12: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() . ')');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_OPENSSL_KEY_V1') === 'your-secret-key' || empty(App::getEnv('_APP_OPENSSL_KEY_V1'))) {
Console::log('🔴 Not using a unique secret key for encryption');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Using a unique secret key for encryption');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_ENV', 'development') !== 'production') {
Console::log('🔴 App environment is set for development');
2022-05-24 02:54:50 +12:00
} else {
Console::log('🟢 App environment is set for production');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if ('enabled' !== App::getEnv('_APP_OPTIONS_ABUSE', 'disabled')) {
Console::log('🔴 Abuse protection is disabled');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Abuse protection is enabled');
}
2021-05-13 02:53:25 +12:00
$authWhitelistRoot = App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', null);
2020-06-29 08:45:36 +12:00
$authWhitelistEmails = App::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null);
$authWhitelistIPs = App::getEnv('_APP_CONSOLE_WHITELIST_IPS', null);
2020-06-17 23:39:46 +12:00
2022-05-24 02:54:50 +12:00
if (
empty($authWhitelistRoot)
2021-02-24 00:29:12 +13:00
&& empty($authWhitelistEmails)
2020-06-17 23:39:46 +12:00
&& empty($authWhitelistIPs)
) {
Console::log('🔴 Console access limits are disabled');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Console access limits are enabled');
}
2022-05-24 02:54:50 +12:00
if ('enabled' !== App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled')) {
Console::log('🔴 HTTPS force option is disabled');
2022-05-24 02:54:50 +12:00
} else {
Console::log('🟢 HTTPS force option is enabled');
2020-06-16 01:54:19 +12:00
}
$providerName = App::getEnv('_APP_LOGGING_PROVIDER', '');
$providerConfig = App::getEnv('_APP_LOGGING_CONFIG', '');
2022-05-24 02:54:50 +12:00
if (empty($providerName) || empty($providerConfig) || !Logger::hasProvider($providerName)) {
Console::log('🔴 Logging adapter is disabled');
} else {
Console::log('🟢 Logging adapter is enabled (' . $providerName . ')');
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
try {
2022-05-24 02:54:50 +12:00
Console::log("\n" . 'Checking connectivity...');
2020-06-15 20:01:48 +12:00
} catch (\Throwable $th) {
//throw $th;
}
try {
$register->get('db'); /* @var $db PDO */
Console::success('Database............connected 👍');
} catch (\Throwable $th) {
Console::error('Database.........disconnected 👎');
}
try {
$register->get('cache');
Console::success('Queue...............connected 👍');
} catch (\Throwable $th) {
Console::error('Queue............disconnected 👎');
}
try {
$register->get('cache');
Console::success('Cache...............connected 👍');
} catch (\Throwable $th) {
Console::error('Cache............disconnected 👎');
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
try {
2022-05-24 02:54:50 +12:00
$antivirus = new Network(
App::getEnv('_APP_STORAGE_ANTIVIRUS_HOST', 'clamav'),
(int) App::getEnv('_APP_STORAGE_ANTIVIRUS_PORT', 3310)
);
2022-05-24 02:54:50 +12:00
if ((@$antivirus->ping())) {
2021-12-16 00:02:01 +13:00
Console::success('Antivirus...........connected 👍');
2022-05-24 02:54:50 +12:00
} else {
2021-12-16 00:02:01 +13:00
Console::error('Antivirus........disconnected 👎');
}
} catch (\Throwable $th) {
2021-12-16 00:02:01 +13:00
Console::error('Antivirus........disconnected 👎');
2020-06-17 23:39:46 +12:00
}
}
2020-06-15 20:01:48 +12: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';
2020-06-15 20:01:48 +12:00
$mail->send();
Console::success('SMTP................connected 👍');
} catch (\Throwable $th) {
Console::error('SMTP.............disconnected 👎');
}
2020-06-29 08:45:36 +12:00
$host = App::getEnv('_APP_STATSD_HOST', 'telegraf');
$port = App::getEnv('_APP_STATSD_PORT', 8125);
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if ($fp = @\fsockopen('udp://' . $host, $port, $errCode, $errStr, 2)) {
2020-06-15 20:01:48 +12:00
Console::success('StatsD..............connected 👍');
2020-06-20 23:20:49 +12:00
\fclose($fp);
2020-06-15 20:01:48 +12:00
} else {
Console::error('StatsD...........disconnected 👎');
}
2020-06-29 08:45:36 +12:00
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
$port = App::getEnv('_APP_INFLUXDB_PORT', '');
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if ($fp = @\fsockopen($host, $port, $errCode, $errStr, 2)) {
2020-06-15 20:01:48 +12:00
Console::success('InfluxDB............connected 👍');
2020-06-20 23:20:49 +12:00
\fclose($fp);
2020-06-15 20:01:48 +12:00
} else {
Console::error('InfluxDB.........disconnected 👎');
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
Console::log('');
Console::log('Checking volumes...');
2022-05-24 02:54:50 +12:00
foreach (
[
2020-06-17 23:18:28 +12:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-05-24 02:54:50 +12:00
] as $key => $volume
) {
2020-06-17 23:18:28 +12:00
$device = new Local($volume);
2020-06-15 20:01:48 +12:00
2020-06-20 23:20:49 +12:00
if (\is_readable($device->getRoot())) {
2022-05-24 02:54:50 +12:00
Console::success('🟢 ' . $key . ' Volume is readable');
} else {
Console::error('🔴 ' . $key . ' Volume is unreadable');
2020-06-17 23:18:28 +12:00
}
2020-06-20 23:20:49 +12:00
if (\is_writable($device->getRoot())) {
2022-05-24 02:54:50 +12:00
Console::success('🟢 ' . $key . ' Volume is writeable');
} else {
Console::error('🔴 ' . $key . ' Volume is unwriteable');
2020-06-17 23:18:28 +12:00
}
2020-06-15 20:01:48 +12:00
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
2020-06-17 23:18:28 +12:00
Console::log('');
Console::log('Checking disk space usage...');
2022-05-24 02:54:50 +12:00
foreach (
[
2020-06-17 23:18:28 +12:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-05-24 02:54:50 +12:00
] as $key => $volume
) {
2020-06-17 23:18:28 +12:00
$device = new Local($volume);
$percentage = (($device->getPartitionTotalSpace() - $device->getPartitionFreeSpace())
/ $device->getPartitionTotalSpace()) * 100;
2022-05-24 02:54:50 +12:00
$message = $key . ' Volume has ' . Storage::human($device->getPartitionFreeSpace()) . ' free space (' . \round($percentage, 2) . '% used)';
2020-06-17 23:18:28 +12:00
if ($percentage < 80) {
Console::success('🟢 ' . $message);
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:18:28 +12:00
Console::error('🔴 ' . $message);
}
2020-06-15 20:01:48 +12:00
}
2020-06-15 20:01:48 +12:00
try {
2022-05-24 02:54:50 +12:00
if (App::isProduction()) {
Console::log('');
2022-05-24 02:54:50 +12:00
$version = \json_decode(@\file_get_contents(App::getEnv('_APP_HOME', 'http://localhost') . '/v1/health/version'), true);
if ($version && isset($version['version'])) {
2022-05-24 02:54:50 +12:00
if (\version_compare($version['version'], App::getEnv('_APP_VERSION', 'UNKNOWN')) === 0) {
Console::info('You are running the latest version of ' . APP_NAME . '! 🥳');
} else {
Console::info('A new version (' . $version['version'] . ') is available! 🥳' . "\n");
}
} else {
2022-05-24 02:54:50 +12:00
Console::error('Failed to check for a newer version' . "\n");
2020-06-15 20:01:48 +12:00
}
}
} catch (\Throwable $th) {
2022-05-24 02:54:50 +12:00
Console::error('Failed to check for a newer version' . "\n");
2020-06-15 20:01:48 +12:00
}
2020-10-30 09:11:16 +13:00
});