feat(database-hosts): allow linking of multiple nodes

This commit is contained in:
Matthew Penner 2021-01-30 12:46:27 -07:00
parent 72983e8385
commit 2b5cc99abd
4 changed files with 73 additions and 33 deletions

View file

@ -18,25 +18,10 @@ use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\RotatePasswordRequest
class DatabaseController extends ClientApiController
{
/**
* @var \Pterodactyl\Services\Databases\DeployServerDatabaseService
*/
private $deployDatabaseService;
/**
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
*/
private $repository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $managementService;
/**
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
*/
private $passwordService;
private DeployServerDatabaseService $deployDatabaseService;
private DatabaseRepository $repository;
private DatabaseManagementService $managementService;
private DatabasePasswordService $passwordService;
/**
* DatabaseController constructor.

View file

@ -28,6 +28,7 @@ use Illuminate\Contracts\Encryption\Encrypter;
* @property int $daemonListen
* @property int $daemonSFTP
* @property string $daemonBase
* @property int|null $database_host_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Pterodactyl\Models\Location $location
@ -239,6 +240,16 @@ class Node extends Model
return $this->hasMany(Allocation::class);
}
/**
* Gets the database host associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function databaseHost()
{
return $this->belongsTo(DatabaseHost::class);
}
/**
* Returns a boolean if the node is viable for an additional server to be placed on it.
*/

View file

@ -10,10 +10,7 @@ use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
class DeployServerDatabaseService
{
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $managementService;
private DatabaseManagementService $managementService;
/**
* ServerDatabaseCreationService constructor.
@ -35,21 +32,22 @@ class DeployServerDatabaseService
Assert::notEmpty($data['database'] ?? null);
Assert::notEmpty($data['remote'] ?? null);
$hosts = DatabaseHost::query()->get()->toBase();
if ($hosts->isEmpty()) {
throw new NoSuitableDatabaseHostException();
} else {
$nodeHosts = $hosts->where('node_id', $server->node_id)->toBase();
if ($nodeHosts->isEmpty() && !config('pterodactyl.client_features.databases.allow_random')) {
$databaseHostId = $server->node->database_host_id;
if (is_null($databaseHostId)) {
if (!config('pterodactyl.client_features.databases.allow_random')) {
throw new NoSuitableDatabaseHostException();
}
$hosts = DatabaseHost::query()->get()->toBase();
if ($hosts->isEmpty()) {
throw new NoSuitableDatabaseHostException();
}
$databaseHostId = $hosts->random()->id;
}
return $this->managementService->create($server, [
'database_host_id' => $nodeHosts->isEmpty()
? $hosts->random()->id
: $nodeHosts->random()->id,
'database_host_id' => $databaseHostId,
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
'remote' => $data['remote'],
]);

View file

@ -0,0 +1,46 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddDatabaseHostIdColumnToNodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('database_hosts', function (Blueprint $table) {
$table->dropForeign(['node_id']);
$table->dropColumn('node_id');
});
Schema::table('nodes', function (Blueprint $table) {
$table->integer('database_host_id')->nullable()->unsigned()->after('daemonBase');
$table->index('database_host_id')->nullable();
$table->foreign('database_host_id')->references('id')->on('database_hosts')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('nodes', function (Blueprint $table) {
$table->dropForeign(['database_host_id']);
$table->dropColumn('database_host_id');
});
Schema::table('database_hosts', function (Blueprint $table) {
$table->integer('node_id')->nullable()->unsigned()->after('max_databases');
$table->index('node_id')->nullable();
$table->foreign('node_id')->references('id')->on('nodes');
});
}
}