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 class DatabaseController extends ClientApiController
{ {
/** private DeployServerDatabaseService $deployDatabaseService;
* @var \Pterodactyl\Services\Databases\DeployServerDatabaseService private DatabaseRepository $repository;
*/ private DatabaseManagementService $managementService;
private $deployDatabaseService; private DatabasePasswordService $passwordService;
/**
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
*/
private $repository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $managementService;
/**
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
*/
private $passwordService;
/** /**
* DatabaseController constructor. * DatabaseController constructor.

View File

@ -28,6 +28,7 @@ use Illuminate\Contracts\Encryption\Encrypter;
* @property int $daemonListen * @property int $daemonListen
* @property int $daemonSFTP * @property int $daemonSFTP
* @property string $daemonBase * @property string $daemonBase
* @property int|null $database_host_id
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property \Pterodactyl\Models\Location $location * @property \Pterodactyl\Models\Location $location
@ -239,6 +240,16 @@ class Node extends Model
return $this->hasMany(Allocation::class); 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. * 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 class DeployServerDatabaseService
{ {
/** private DatabaseManagementService $managementService;
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $managementService;
/** /**
* ServerDatabaseCreationService constructor. * ServerDatabaseCreationService constructor.
@ -35,21 +32,22 @@ class DeployServerDatabaseService
Assert::notEmpty($data['database'] ?? null); Assert::notEmpty($data['database'] ?? null);
Assert::notEmpty($data['remote'] ?? null); Assert::notEmpty($data['remote'] ?? null);
$hosts = DatabaseHost::query()->get()->toBase(); $databaseHostId = $server->node->database_host_id;
if ($hosts->isEmpty()) { if (is_null($databaseHostId)) {
throw new NoSuitableDatabaseHostException(); if (!config('pterodactyl.client_features.databases.allow_random')) {
} else {
$nodeHosts = $hosts->where('node_id', $server->node_id)->toBase();
if ($nodeHosts->isEmpty() && !config('pterodactyl.client_features.databases.allow_random')) {
throw new NoSuitableDatabaseHostException(); throw new NoSuitableDatabaseHostException();
} }
$hosts = DatabaseHost::query()->get()->toBase();
if ($hosts->isEmpty()) {
throw new NoSuitableDatabaseHostException();
}
$databaseHostId = $hosts->random()->id;
} }
return $this->managementService->create($server, [ return $this->managementService->create($server, [
'database_host_id' => $nodeHosts->isEmpty() 'database_host_id' => $databaseHostId,
? $hosts->random()->id
: $nodeHosts->random()->id,
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id), 'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
'remote' => $data['remote'], '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');
});
}
}