2018-03-02 03:27:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Databases;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
|
|
|
|
|
|
|
|
class DeployServerDatabaseService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $databaseHostRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
|
|
|
*/
|
|
|
|
private $managementService;
|
2018-03-03 01:37:21 +00:00
|
|
|
|
2018-03-02 03:27:37 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ServerDatabaseCreationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
2018-03-02 03:27:37 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $databaseHostRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
|
2018-03-02 03:27:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
DatabaseRepositoryInterface $repository,
|
|
|
|
DatabaseHostRepositoryInterface $databaseHostRepository,
|
|
|
|
DatabaseManagementService $managementService
|
|
|
|
) {
|
|
|
|
$this->databaseHostRepository = $databaseHostRepository;
|
|
|
|
$this->managementService = $managementService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
|
2018-03-03 01:37:21 +00:00
|
|
|
$hosts = $this->databaseHostRepository->setColumns(['id'])->findWhere([
|
2018-03-02 03:27:37 +00:00
|
|
|
['node_id', '=', $server->node_id],
|
2018-03-03 01:37:21 +00:00
|
|
|
]);
|
2018-03-02 03:27:37 +00:00
|
|
|
|
2018-03-03 01:37:21 +00:00
|
|
|
if ($hosts->isEmpty() && ! $allowRandom) {
|
2018-03-02 03:27:37 +00:00
|
|
|
throw new NoSuitableDatabaseHostException;
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:37:21 +00:00
|
|
|
if ($hosts->isEmpty()) {
|
|
|
|
$hosts = $this->databaseHostRepository->setColumns(['id'])->all();
|
|
|
|
if ($hosts->isEmpty()) {
|
2018-03-02 03:27:37 +00:00
|
|
|
throw new NoSuitableDatabaseHostException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:37:21 +00:00
|
|
|
$host = $hosts->random();
|
|
|
|
|
2020-06-24 03:07:37 +00:00
|
|
|
return $this->managementService->create($server, [
|
2018-03-02 03:27:37 +00:00
|
|
|
'database_host_id' => $host->id,
|
|
|
|
'database' => array_get($data, 'database'),
|
|
|
|
'remote' => array_get($data, 'remote'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|