2020-04-04 03:45:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Admin\Servers;
|
|
|
|
|
2022-11-15 01:25:07 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2020-04-04 03:45:37 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-11-28 16:56:03 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2020-04-04 03:45:37 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2022-10-29 23:58:55 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2020-04-04 20:10:18 +00:00
|
|
|
use Pterodactyl\Models\ServerTransfer;
|
2022-11-15 01:25:07 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2022-11-15 01:25:07 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private AlertsMessageBag $alert,
|
|
|
|
private AllocationRepositoryInterface $allocationRepository,
|
2022-11-15 01:25:07 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private DaemonTransferRepository $daemonTransferRepository,
|
2022-11-28 16:56:03 +00:00
|
|
|
private NodeJWTService $nodeJWTService
|
2020-04-04 03:45:37 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a transfer of a server to a new node.
|
|
|
|
*
|
2020-04-04 06:50:06 +00:00
|
|
|
* @throws \Throwable
|
2020-04-04 03:45:37 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function transfer(Request $request, Server $server): RedirectResponse
|
2021-01-23 20:09:16 +00:00
|
|
|
{
|
2020-04-04 03:45:37 +00:00
|
|
|
$validatedData = $request->validate([
|
|
|
|
'node_id' => 'required|exists:nodes,id',
|
|
|
|
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
|
|
|
|
'allocation_additional' => 'nullable',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$node_id = $validatedData['node_id'];
|
2020-04-04 20:10:18 +00:00
|
|
|
$allocation_id = intval($validatedData['allocation_id']);
|
|
|
|
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
// Check if the node is viable for the transfer.
|
2022-11-28 16:56:03 +00:00
|
|
|
$node = Node::query()->findOrFail($node_id);
|
2022-11-15 01:25:07 +00:00
|
|
|
if (!$node->isViable($server->memory, $server->disk)) {
|
|
|
|
$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.servers.view.manage', $server->id);
|
|
|
|
}
|
2020-04-05 00:28:09 +00:00
|
|
|
|
2022-11-15 01:25:07 +00:00
|
|
|
$server->validateTransferState();
|
2022-10-05 01:57:24 +00:00
|
|
|
|
2022-11-15 01:25:07 +00:00
|
|
|
$this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) {
|
2021-01-23 20:33:34 +00:00
|
|
|
$transfer = new ServerTransfer();
|
2020-04-04 20:10:18 +00:00
|
|
|
|
|
|
|
$transfer->server_id = $server->id;
|
|
|
|
$transfer->old_node = $server->node_id;
|
|
|
|
$transfer->new_node = $node_id;
|
|
|
|
$transfer->old_allocation = $server->allocation_id;
|
|
|
|
$transfer->new_allocation = $allocation_id;
|
2022-11-28 16:56:03 +00:00
|
|
|
$transfer->old_additional_allocations = $server->allocations->where('id', '!=', $server->allocation_id)->pluck('id')->all();
|
2020-12-24 18:12:01 +00:00
|
|
|
$transfer->new_additional_allocations = $additional_allocations;
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
$transfer->save();
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
// Add the allocations to the server, so they cannot be automatically assigned while the transfer is in progress.
|
2020-04-04 22:16:18 +00:00
|
|
|
$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);
|
|
|
|
|
2022-11-15 01:25:07 +00:00
|
|
|
// Generate a token for the destination node that the source node can use to authenticate with.
|
|
|
|
$token = $this->nodeJWTService
|
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
|
|
|
->setSubject($server->uuid)
|
|
|
|
->handle($transfer->newNode, $server->uuid, 'sha256');
|
2020-04-04 03:45:37 +00:00
|
|
|
|
2022-11-15 01:25:07 +00:00
|
|
|
// Notify the source node of the pending outgoing transfer.
|
|
|
|
$this->daemonTransferRepository->setServer($server)->notify($transfer->newNode, $token);
|
|
|
|
|
|
|
|
return $transfer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
return redirect()->route('admin.servers.view.manage', $server->id);
|
|
|
|
}
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* Assigns the specified allocations to the specified server.
|
|
|
|
*/
|
2020-04-04 20:10:18 +00:00
|
|
|
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
|
|
|
|
{
|
|
|
|
$allocations = $additional_allocations;
|
2022-10-14 16:59:20 +00:00
|
|
|
$allocations[] = $allocation_id;
|
2020-04-04 20:10:18 +00:00
|
|
|
|
|
|
|
$unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id);
|
|
|
|
|
|
|
|
$updateIds = [];
|
|
|
|
foreach ($allocations as $allocation) {
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!in_array($allocation, $unassigned)) {
|
2020-04-04 20:10:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updateIds[] = $allocation;
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($updateIds)) {
|
2020-04-04 20:10:18 +00:00
|
|
|
$this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 03:45:37 +00:00
|
|
|
}
|