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
|
|
|
|
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\CLI\CLI;
|
|
|
|
use Utopia\CLI\Console;
|
2022-10-18 05:43:51 +13:00
|
|
|
use Utopia\Cache\Adapter\Sharding;
|
|
|
|
use Utopia\Cache\Cache;
|
|
|
|
use Utopia\Config\Config;
|
|
|
|
use Utopia\Database\Database;
|
2022-04-10 21:38:22 +12:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2022-10-18 05:43:51 +13:00
|
|
|
use InfluxDB\Database as InfluxDatabase;
|
2022-11-02 18:48:17 +13:00
|
|
|
use Utopia\Database\Document;
|
2022-10-18 05:43:51 +13:00
|
|
|
|
|
|
|
function getInfluxDB(): InfluxDatabase
|
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
|
|
|
$client = $register->get('influxdb'); /** @var InfluxDB\Client $client */
|
|
|
|
$attempts = 0;
|
|
|
|
$max = 10;
|
|
|
|
$sleep = 1;
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
Console::warning("InfluxDB not ready. Retrying connection ({$attempts})...");
|
|
|
|
if ($attempts >= $max) {
|
|
|
|
throw new \Exception('InfluxDB database not ready yet');
|
|
|
|
}
|
|
|
|
sleep($sleep);
|
|
|
|
}
|
|
|
|
} while ($attempts < $max);
|
|
|
|
return $database;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConsoleDB(): Database
|
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
|
|
|
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
|
2022-10-19 21:35:30 +13:00
|
|
|
|
2022-10-24 14:34:12 +13:00
|
|
|
$dbAdapter = $pools
|
|
|
|
->get('console')
|
|
|
|
->pop()
|
|
|
|
->getResource()
|
|
|
|
;
|
2022-10-18 05:43:51 +13:00
|
|
|
|
|
|
|
$database = new Database($dbAdapter, getCache());
|
|
|
|
|
|
|
|
$database->setNamespace('console');
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
}
|
|
|
|
|
2022-11-02 18:48:17 +13:00
|
|
|
function getProjectDB(Document $project): Database
|
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
|
|
|
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
|
|
|
|
|
|
|
|
if ($project->isEmpty() || $project->getId() === 'console') {
|
|
|
|
return getConsoleDB();
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbAdapter = $pools
|
|
|
|
->get($project->getAttribute('database'))
|
|
|
|
->pop()
|
|
|
|
->getResource()
|
|
|
|
;
|
|
|
|
|
|
|
|
$database = new Database($dbAdapter, getCache());
|
|
|
|
$database->setNamespace('_' . $project->getInternalId());
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
}
|
|
|
|
|
2022-10-18 05:43:51 +13:00
|
|
|
function getCache(): Cache
|
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
|
|
|
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
|
2022-10-19 21:35:30 +13:00
|
|
|
|
2022-10-18 05:43:51 +13:00
|
|
|
$list = Config::getParam('pools-cache', []);
|
|
|
|
$adapters = [];
|
2022-10-19 21:35:30 +13:00
|
|
|
|
2022-10-18 05:43:51 +13:00
|
|
|
foreach ($list as $value) {
|
|
|
|
$adapters[] = $pools
|
|
|
|
->get($value)
|
|
|
|
->pop()
|
|
|
|
->getResource()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Cache(new Sharding($adapters));
|
|
|
|
}
|
2022-04-10 21:38:22 +12:00
|
|
|
|
|
|
|
Authorization::disable();
|
2020-07-29 07:48:51 +12:00
|
|
|
|
|
|
|
$cli = new CLI();
|
|
|
|
|
|
|
|
include 'tasks/doctor.php';
|
2020-12-15 05:39:44 +13:00
|
|
|
include 'tasks/maintenance.php';
|
2022-11-03 04:08:13 +13:00
|
|
|
include 'tasks/volume-sync.php';
|
2020-07-29 07:48:51 +12:00
|
|
|
include 'tasks/install.php';
|
|
|
|
include 'tasks/migrate.php';
|
|
|
|
include 'tasks/sdks.php';
|
2022-01-02 01:07:11 +13:00
|
|
|
include 'tasks/specs.php';
|
2020-07-29 07:48:51 +12:00
|
|
|
include 'tasks/ssl.php';
|
2020-10-15 18:03:38 +13:00
|
|
|
include 'tasks/vars.php';
|
2021-08-10 20:44:31 +12:00
|
|
|
include 'tasks/usage.php';
|
2020-07-29 07:48:51 +12:00
|
|
|
|
|
|
|
$cli
|
|
|
|
->task('version')
|
|
|
|
->desc('Get the server version')
|
|
|
|
->action(function () {
|
|
|
|
Console::log(App::getEnv('_APP_VERSION', 'UNKNOWN'));
|
|
|
|
});
|
|
|
|
|
2022-10-16 11:44:03 +13:00
|
|
|
$cli
|
|
|
|
->error(function ($error) {
|
2022-10-19 21:35:30 +13:00
|
|
|
if (App::getEnv('_APP_ENV', 'development')) {
|
2022-10-16 11:44:03 +13:00
|
|
|
Console::error($error);
|
|
|
|
} else {
|
|
|
|
Console::error($error->getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$cli->run();
|