2018-03-02 03:27:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Databases;
|
|
|
|
|
2020-10-11 18:59:46 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-03-02 03:27:37 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
2020-10-11 18:59:46 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2018-03-02 03:27:37 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
|
|
|
|
|
|
|
|
class DeployServerDatabaseService
|
|
|
|
{
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* DeployServerDatabaseService constructor.
|
2018-03-02 03:27:37 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private DatabaseManagementService $managementService)
|
2020-10-11 18:59:46 +00:00
|
|
|
{
|
2018-03-02 03:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-24 03:07:37 +00:00
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
|
2018-03-02 03:27:37 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
|
|
|
|
*/
|
|
|
|
public function handle(Server $server, array $data): Database
|
|
|
|
{
|
2020-10-11 18:59:46 +00:00
|
|
|
Assert::notEmpty($data['database'] ?? null);
|
|
|
|
Assert::notEmpty($data['remote'] ?? null);
|
2018-03-02 03:27:37 +00:00
|
|
|
|
2022-12-15 19:39:37 +00:00
|
|
|
$databaseHostId = $server->node->database_host_id;
|
|
|
|
if (is_null($databaseHostId)) {
|
|
|
|
if (!config('pterodactyl.client_features.databases.allow_random')) {
|
|
|
|
throw new NoSuitableDatabaseHostException();
|
|
|
|
}
|
2018-03-02 03:27:37 +00:00
|
|
|
|
2022-12-15 19:39:37 +00:00
|
|
|
$hosts = DatabaseHost::query()->get()->toBase();
|
|
|
|
if ($hosts->isEmpty()) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new NoSuitableDatabaseHostException();
|
2018-03-02 03:27:37 +00:00
|
|
|
}
|
2022-12-15 19:39:37 +00:00
|
|
|
|
2023-02-23 19:49:07 +00:00
|
|
|
/** @var \Pterodactyl\Models\DatabaseHost $databaseHost */
|
|
|
|
$databaseHost = $hosts->random();
|
|
|
|
$databaseHostId = $databaseHost->id;
|
2018-03-02 03:27:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 03:07:37 +00:00
|
|
|
return $this->managementService->create($server, [
|
2022-12-15 19:39:37 +00:00
|
|
|
'database_host_id' => $databaseHostId,
|
2020-10-11 18:59:46 +00:00
|
|
|
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
|
|
|
|
'remote' => $data['remote'],
|
2018-03-02 03:27:37 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|