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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
|
|
|
*/
|
|
|
|
private $managementService;
|
2018-03-03 01:37:21 +00:00
|
|
|
|
2018-03-02 03:27:37 +00:00
|
|
|
/**
|
|
|
|
* ServerDatabaseCreationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
|
2018-03-02 03:27:37 +00:00
|
|
|
*/
|
2020-10-11 18:59:46 +00:00
|
|
|
public function __construct(DatabaseManagementService $managementService)
|
|
|
|
{
|
2018-03-02 03:27:37 +00:00
|
|
|
$this->managementService = $managementService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2018-03-02 03:27:37 +00:00
|
|
|
* @return \Pterodactyl\Models\Database
|
|
|
|
*
|
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
|
|
|
|
2020-10-11 18:59:46 +00:00
|
|
|
$hosts = DatabaseHost::query()->get()->toBase();
|
|
|
|
if ($hosts->isEmpty()) {
|
2018-03-02 03:27:37 +00:00
|
|
|
throw new NoSuitableDatabaseHostException;
|
2020-10-11 18:59:46 +00:00
|
|
|
} else {
|
|
|
|
$nodeHosts = $hosts->where('node_id', $server->node_id)->toBase();
|
2018-03-02 03:27:37 +00:00
|
|
|
|
2020-10-11 18:59:46 +00:00
|
|
|
if ($nodeHosts->isEmpty() && ! config('pterodactyl.client_features.databases.allow_random')) {
|
2018-03-02 03:27:37 +00:00
|
|
|
throw new NoSuitableDatabaseHostException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 03:07:37 +00:00
|
|
|
return $this->managementService->create($server, [
|
2020-10-11 18:59:46 +00:00
|
|
|
'database_host_id' => $nodeHosts->isEmpty()
|
|
|
|
? $hosts->random()->id
|
|
|
|
: $nodeHosts->random()->id,
|
|
|
|
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
|
|
|
|
'remote' => $data['remote'],
|
2018-03-02 03:27:37 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|