Path: blob/1.0-develop/app/Http/Controllers/Admin/Servers/ServerTransferController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Admin\Servers;34use Carbon\CarbonImmutable;5use Illuminate\Http\Request;6use Pterodactyl\Models\Server;7use Illuminate\Http\RedirectResponse;8use Prologue\Alerts\AlertsMessageBag;9use Pterodactyl\Models\ServerTransfer;10use Illuminate\Database\ConnectionInterface;11use Pterodactyl\Http\Controllers\Controller;12use Pterodactyl\Services\Nodes\NodeJWTService;13use Pterodactyl\Repositories\Eloquent\NodeRepository;14use Pterodactyl\Repositories\Wings\DaemonTransferRepository;15use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;1617class ServerTransferController extends Controller18{19/**20* ServerTransferController constructor.21*/22public function __construct(23private AlertsMessageBag $alert,24private AllocationRepositoryInterface $allocationRepository,25private ConnectionInterface $connection,26private DaemonTransferRepository $daemonTransferRepository,27private NodeJWTService $nodeJWTService,28private NodeRepository $nodeRepository,29) {30}3132/**33* Starts a transfer of a server to a new node.34*35* @throws \Throwable36*/37public function transfer(Request $request, Server $server): RedirectResponse38{39$validatedData = $request->validate([40'node_id' => 'required|exists:nodes,id',41'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',42'allocation_additional' => 'nullable',43]);4445$node_id = $validatedData['node_id'];46$allocation_id = intval($validatedData['allocation_id']);47$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);4849// Check if the node is viable for the transfer.50$node = $this->nodeRepository->getNodeWithResourceUsage($node_id);51if (!$node->isViable($server->memory, $server->disk)) {52$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();5354return redirect()->route('admin.servers.view.manage', $server->id);55}5657$server->validateTransferState();5859$this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) {60// Create a new ServerTransfer entry.61$transfer = new ServerTransfer();6263$transfer->server_id = $server->id;64$transfer->old_node = $server->node_id;65$transfer->new_node = $node_id;66$transfer->old_allocation = $server->allocation_id;67$transfer->new_allocation = $allocation_id;68$transfer->old_additional_allocations = $server->allocations->where('id', '!=', $server->allocation_id)->pluck('id')->values()->toArray();69$transfer->new_additional_allocations = $additional_allocations;7071$transfer->save();7273// Add the allocations to the server, so they cannot be automatically assigned while the transfer is in progress.74$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);7576// Generate a token for the destination node that the source node can use to authenticate with.77$token = $this->nodeJWTService78->setExpiresAt(CarbonImmutable::now()->addMinutes(15))79->setSubject($server->uuid)80->handle($transfer->newNode, $server->uuid, 'sha256');8182// Notify the source node of the pending outgoing transfer.83$this->daemonTransferRepository->setServer($server)->notify($transfer->newNode, $token);8485return $transfer;86});8788$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();8990return redirect()->route('admin.servers.view.manage', $server->id);91}9293/**94* Assigns the specified allocations to the specified server.95*/96private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)97{98$allocations = $additional_allocations;99$allocations[] = $allocation_id;100101$unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id);102103$updateIds = [];104foreach ($allocations as $allocation) {105if (!in_array($allocation, $unassigned)) {106continue;107}108109$updateIds[] = $allocation;110}111112if (!empty($updateIds)) {113$this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);114}115}116}117118119