2020-07-29 07:48:51 +12:00
|
|
|
<?php
|
2022-05-24 02:54:50 +12:00
|
|
|
|
|
|
|
require_once __DIR__ . '/init.php';
|
|
|
|
require_once __DIR__ . '/controllers/general.php';
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2022-11-16 07:13:17 +13:00
|
|
|
use Appwrite\Event\Func;
|
2022-12-07 04:06:40 +13:00
|
|
|
use Appwrite\Event\Audit;
|
2022-11-14 23:29:10 +13:00
|
|
|
use Appwrite\Platform\Appwrite;
|
2022-10-28 16:36:20 +13:00
|
|
|
use Utopia\CLI\CLI;
|
2022-04-10 21:38:22 +12:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2022-08-02 13:32:46 +12:00
|
|
|
use Utopia\Platform\Service;
|
2022-10-28 16:36:20 +13:00
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\CLI\Console;
|
2022-11-14 23:46:11 +13:00
|
|
|
use Utopia\Cache\Adapter\Sharding;
|
2022-10-28 16:36:20 +13:00
|
|
|
use Utopia\Cache\Cache;
|
2022-11-14 23:46:11 +13:00
|
|
|
use Utopia\Config\Config;
|
2022-10-28 16:36:20 +13:00
|
|
|
use Utopia\Database\Database;
|
2022-11-14 23:46:11 +13:00
|
|
|
use Utopia\Database\Document;
|
2022-10-28 21:56:45 +13:00
|
|
|
use Utopia\Logger\Log;
|
2022-11-14 23:46:11 +13:00
|
|
|
use Utopia\Pools\Group;
|
2022-10-28 21:49:05 +13:00
|
|
|
use Utopia\Registry\Registry;
|
2022-04-10 21:38:22 +12:00
|
|
|
|
|
|
|
Authorization::disable();
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2022-10-28 16:36:20 +13:00
|
|
|
CLI::setResource('register', fn()=>$register);
|
|
|
|
|
2022-11-14 23:46:11 +13:00
|
|
|
CLI::setResource('cache', function ($pools) {
|
|
|
|
$list = Config::getParam('pools-cache', []);
|
|
|
|
$adapters = [];
|
|
|
|
|
|
|
|
foreach ($list as $value) {
|
|
|
|
$adapters[] = $pools
|
|
|
|
->get($value)
|
|
|
|
->pop()
|
|
|
|
->getResource()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Cache(new Sharding($adapters));
|
|
|
|
}, ['pools']);
|
|
|
|
|
|
|
|
CLI::setResource('pools', function (Registry $register) {
|
|
|
|
return $register->get('pools');
|
2022-10-28 21:49:05 +13:00
|
|
|
}, ['register']);
|
2022-10-28 16:36:20 +13:00
|
|
|
|
2022-11-14 23:46:11 +13:00
|
|
|
CLI::setResource('dbForConsole', function ($pools, $cache) {
|
|
|
|
$dbAdapter = $pools
|
|
|
|
->get('console')
|
|
|
|
->pop()
|
|
|
|
->getResource()
|
|
|
|
;
|
2022-10-28 16:36:20 +13:00
|
|
|
|
2022-11-14 23:46:11 +13:00
|
|
|
$database = new Database($dbAdapter, $cache);
|
2022-10-28 16:36:20 +13:00
|
|
|
|
2022-11-14 23:46:11 +13:00
|
|
|
$database->setNamespace('console');
|
2022-10-28 16:36:20 +13:00
|
|
|
|
|
|
|
return $database;
|
2022-11-14 23:46:11 +13:00
|
|
|
}, ['pools', 'cache']);
|
|
|
|
|
|
|
|
CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, $cache) {
|
2022-11-16 02:34:13 +13:00
|
|
|
$databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools
|
2022-11-16 02:30:19 +13:00
|
|
|
|
|
|
|
$getProjectDB = function (Document $project) use ($pools, $dbForConsole, $cache, &$databases) {
|
2022-11-14 23:46:11 +13:00
|
|
|
if ($project->isEmpty() || $project->getId() === 'console') {
|
|
|
|
return $dbForConsole;
|
|
|
|
}
|
|
|
|
|
2022-11-16 02:30:19 +13:00
|
|
|
$databaseName = $project->getAttribute('database');
|
|
|
|
|
2022-11-16 02:35:13 +13:00
|
|
|
if (isset($databases[$databaseName])) {
|
2022-11-16 02:30:19 +13:00
|
|
|
return $databases[$databaseName];
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:54:54 +13:00
|
|
|
$dbAdapter = $pools
|
2022-11-16 02:30:19 +13:00
|
|
|
->get($databaseName)
|
2022-11-16 01:54:54 +13:00
|
|
|
->pop()
|
|
|
|
->getResource();
|
2022-11-14 23:46:11 +13:00
|
|
|
|
|
|
|
$database = new Database($dbAdapter, $cache);
|
|
|
|
$database->setNamespace('_' . $project->getInternalId());
|
|
|
|
|
2022-11-16 02:30:19 +13:00
|
|
|
$databases[$databaseName] = $database;
|
|
|
|
|
2022-11-16 01:54:54 +13:00
|
|
|
return $database;
|
2022-11-14 23:46:11 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
return $getProjectDB;
|
|
|
|
}, ['pools', 'dbForConsole', 'cache']);
|
2022-10-28 16:36:20 +13:00
|
|
|
|
2022-10-28 22:00:59 +13:00
|
|
|
CLI::setResource('influxdb', function (Registry $register) {
|
2022-11-14 23:46:11 +13:00
|
|
|
$client = $register->get('influxdb'); /** @var InfluxDB\Client $client */
|
2022-10-28 21:49:05 +13:00
|
|
|
$attempts = 0;
|
|
|
|
$max = 10;
|
|
|
|
$sleep = 1;
|
2022-10-28 22:00:59 +13:00
|
|
|
|
2022-10-28 21:49:05 +13:00
|
|
|
do { // check if telegraf database is ready
|
|
|
|
try {
|
|
|
|
$attempts++;
|
|
|
|
$database = $client->selectDB('telegraf');
|
|
|
|
if (in_array('telegraf', $client->listDatabases())) {
|
|
|
|
break; // leave the do-while if successful
|
|
|
|
}
|
2022-11-14 23:46:11 +13:00
|
|
|
} catch (\Throwable $th) {
|
2022-10-28 21:49:05 +13:00
|
|
|
Console::warning("InfluxDB not ready. Retrying connection ({$attempts})...");
|
|
|
|
if ($attempts >= $max) {
|
|
|
|
throw new \Exception('InfluxDB database not ready yet');
|
|
|
|
}
|
|
|
|
sleep($sleep);
|
2022-10-28 17:09:34 +13:00
|
|
|
}
|
2022-10-28 21:49:05 +13:00
|
|
|
} while ($attempts < $max);
|
|
|
|
return $database;
|
|
|
|
}, ['register']);
|
2022-10-28 17:09:34 +13:00
|
|
|
|
2022-11-16 23:33:11 +13:00
|
|
|
CLI::setResource('queueForFunctions', function (Group $pools) {
|
2022-11-16 07:13:17 +13:00
|
|
|
return new Func($pools->get('queue')->pop()->getResource());
|
|
|
|
}, ['pools']);
|
|
|
|
|
2022-12-07 04:06:40 +13:00
|
|
|
CLI::setResource('audits', function (Group $pools) {
|
|
|
|
return new Audit($pools->get('queue')->pop()->getResource());
|
|
|
|
}, ['pools']);
|
|
|
|
|
2022-10-28 22:00:59 +13:00
|
|
|
CLI::setResource('logError', function (Registry $register) {
|
|
|
|
return function (Throwable $error, string $namespace, string $action) use ($register) {
|
2022-10-28 21:56:45 +13:00
|
|
|
$logger = $register->get('logger');
|
|
|
|
|
|
|
|
if ($logger) {
|
|
|
|
$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
|
|
|
|
|
|
|
|
$log = new Log();
|
|
|
|
$log->setNamespace($namespace);
|
|
|
|
$log->setServer(\gethostname());
|
|
|
|
$log->setVersion($version);
|
|
|
|
$log->setType(Log::TYPE_ERROR);
|
|
|
|
$log->setMessage($error->getMessage());
|
|
|
|
|
|
|
|
$log->addTag('code', $error->getCode());
|
|
|
|
$log->addTag('verboseType', get_class($error));
|
|
|
|
|
|
|
|
$log->addExtra('file', $error->getFile());
|
|
|
|
$log->addExtra('line', $error->getLine());
|
|
|
|
$log->addExtra('trace', $error->getTraceAsString());
|
|
|
|
$log->addExtra('detailedTrace', $error->getTrace());
|
|
|
|
|
|
|
|
$log->setAction($action);
|
|
|
|
|
|
|
|
$isProduction = App::getEnv('_APP_ENV', 'development') === 'production';
|
|
|
|
$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
|
|
|
|
|
|
|
|
$responseCode = $logger->addLog($log);
|
|
|
|
Console::info('Usage stats log pushed with status code: ' . $responseCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Console::warning("Failed: {$error->getMessage()}");
|
|
|
|
Console::warning($error->getTraceAsString());
|
|
|
|
};
|
2022-10-30 18:38:22 +13:00
|
|
|
}, ['register']);
|
2022-10-28 21:56:45 +13:00
|
|
|
|
2022-11-14 23:29:10 +13:00
|
|
|
$platform = new Appwrite();
|
|
|
|
$platform->init(Service::TYPE_CLI);
|
2022-07-13 18:26:22 +12:00
|
|
|
|
2022-11-14 23:29:10 +13:00
|
|
|
$cli = $platform->getCli();
|
2022-07-13 18:26:22 +12:00
|
|
|
$cli->run();
|