1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00
appwrite/app/tasks/doctor.php

239 lines
8.3 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;
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 )
\_/\_/(__) (__) (_/\_)(__\_)(__) (__) (____)(_)(__)\__/ ");
2020-06-29 08:45:36 +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
2020-06-17 23:39:46 +12:00
if(!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 Hostname has no public suffix ('.$domain->get().')');
2020-06-17 23:39:46 +12:00
}
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
2020-06-17 23:39:46 +12:00
if(!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 CNAME target has no public suffix ('.$domain->get().')');
2020-06-17 23:39:46 +12:00
}
else {
Console::log('🟢 CNAME target has a public suffix ('.$domain->get().')');
2020-06-15 20:01:48 +12:00
}
2020-11-04 23:15:25 +13: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');
2020-06-17 23:39:46 +12:00
}
else {
Console::log('🟢 Using a unique secret key for encryption');
2020-06-15 20:01:48 +12:00
}
if(App::getEnv('_APP_ENV', 'development') !== 'production') {
Console::log('🔴 App environment is set for development');
2020-06-17 23:39:46 +12:00
}
else {
Console::log('🟢 App environment is set for production');
2020-06-15 20:01:48 +12:00
}
2020-11-04 23:15:25 +13:00
if('enabled' !== App::getEnv('_APP_OPTIONS_ABUSE', 'disabled')) {
Console::log('🔴 Abuse protection is disabled');
2020-06-17 23:39:46 +12:00
}
else {
Console::log('🟢 Abuse protection is enabled');
}
2020-06-29 08:45:36 +12:00
$authWhitelistEmails = App::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null);
$authWhitelistIPs = App::getEnv('_APP_CONSOLE_WHITELIST_IPS', null);
$authWhitelistDomains = App::getEnv('_APP_CONSOLE_WHITELIST_DOMAINS', null);
2020-06-17 23:39:46 +12:00
if(empty($authWhitelistEmails)
&& empty($authWhitelistDomains)
&& empty($authWhitelistIPs)
) {
Console::log('🔴 Console access limits are disabled');
}
else {
Console::log('🟢 Console access limits are enabled');
}
2020-11-04 23:15:25 +13:00
if('enabled' !== App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled')) {
Console::log('🔴 HTTPS force option is disabled');
2020-06-17 23:39:46 +12:00
}
else {
Console::log('🟢 HTTPS force option is enabled');
2020-06-16 01:54:19 +12:00
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
try {
Console::log("\n".'Checking connectivity...');
} 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 👎');
}
2020-06-29 08:45:36 +12:00
if(App::getEnv('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
try {
2021-01-03 08:45:59 +13:00
$antiVirus = new Network(App::getEnv('_APP_STORAGE_ANTIVIRUS_HOST', 'clamav'),
(int) App::getEnv('_APP_STORAGE_ANTIVIRUS_PORT', 3310));
if((@$antiVirus->ping())) {
Console::success('AntiVirus...........connected 👍');
}
else {
Console::error('AntiVirus........disconnected 👎');
}
} catch (\Throwable $th) {
2020-06-17 23:39:46 +12:00
Console::error('AntiVirus........disconnected 👎');
}
}
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-11-19 22:00:24 +13:00
var_dump($th);
2020-06-15 20:01:48 +12:00
}
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
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
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...');
2020-06-17 23:18:28 +12:00
foreach ([
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
] as $key => $volume) {
$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())) {
2020-06-17 23:18:28 +12:00
Console::success('🟢 '.$key.' Volume is readable');
}
else {
Console::error('🔴 '.$key.' Volume is unreadable');
}
2020-06-20 23:20:49 +12:00
if (\is_writable($device->getRoot())) {
2020-06-17 23:18:28 +12:00
Console::success('🟢 '.$key.' Volume is writeable');
}
else {
Console::error('🔴 '.$key.' Volume is unwriteable');
}
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...');
foreach ([
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
] as $key => $volume) {
$device = new Local($volume);
$percentage = (($device->getPartitionTotalSpace() - $device->getPartitionFreeSpace())
/ $device->getPartitionTotalSpace()) * 100;
2020-06-20 23:20:49 +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);
}
else {
Console::error('🔴 ' . $message);
}
2020-06-15 20:01:48 +12:00
}
2020-06-15 20:01:48 +12:00
try {
if(App::isProduction()) {
Console::log('');
$version = \json_decode(@\file_get_contents(App::getEnv('_APP_HOME', 'http://localhost').'/v1/health/version'), true);
if ($version && isset($version['version'])) {
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 {
Console::error('Failed to check for a newer version'."\n");
2020-06-15 20:01:48 +12:00
}
}
} catch (\Throwable $th) {
2020-06-17 23:51:20 +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
});