Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/Servers/ServerTransferController.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin\Servers;
4
5
use Carbon\CarbonImmutable;
6
use Illuminate\Http\Request;
7
use Pterodactyl\Models\Server;
8
use Illuminate\Http\RedirectResponse;
9
use Prologue\Alerts\AlertsMessageBag;
10
use Pterodactyl\Models\ServerTransfer;
11
use Illuminate\Database\ConnectionInterface;
12
use Pterodactyl\Http\Controllers\Controller;
13
use Pterodactyl\Services\Nodes\NodeJWTService;
14
use Pterodactyl\Repositories\Eloquent\NodeRepository;
15
use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
16
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
17
18
class ServerTransferController extends Controller
19
{
20
/**
21
* ServerTransferController constructor.
22
*/
23
public function __construct(
24
private AlertsMessageBag $alert,
25
private AllocationRepositoryInterface $allocationRepository,
26
private ConnectionInterface $connection,
27
private DaemonTransferRepository $daemonTransferRepository,
28
private NodeJWTService $nodeJWTService,
29
private NodeRepository $nodeRepository,
30
) {
31
}
32
33
/**
34
* Starts a transfer of a server to a new node.
35
*
36
* @throws \Throwable
37
*/
38
public function transfer(Request $request, Server $server): RedirectResponse
39
{
40
$validatedData = $request->validate([
41
'node_id' => 'required|exists:nodes,id',
42
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
43
'allocation_additional' => 'nullable',
44
]);
45
46
$node_id = $validatedData['node_id'];
47
$allocation_id = intval($validatedData['allocation_id']);
48
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
49
50
// Check if the node is viable for the transfer.
51
$node = $this->nodeRepository->getNodeWithResourceUsage($node_id);
52
if (!$node->isViable($server->memory, $server->disk)) {
53
$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();
54
55
return redirect()->route('admin.servers.view.manage', $server->id);
56
}
57
58
$server->validateTransferState();
59
60
$this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) {
61
// Create a new ServerTransfer entry.
62
$transfer = new ServerTransfer();
63
64
$transfer->server_id = $server->id;
65
$transfer->old_node = $server->node_id;
66
$transfer->new_node = $node_id;
67
$transfer->old_allocation = $server->allocation_id;
68
$transfer->new_allocation = $allocation_id;
69
$transfer->old_additional_allocations = $server->allocations->where('id', '!=', $server->allocation_id)->pluck('id')->values()->toArray();
70
$transfer->new_additional_allocations = $additional_allocations;
71
72
$transfer->save();
73
74
// Add the allocations to the server, so they cannot be automatically assigned while the transfer is in progress.
75
$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);
76
77
// Generate a token for the destination node that the source node can use to authenticate with.
78
$token = $this->nodeJWTService
79
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
80
->setSubject($server->uuid)
81
->handle($transfer->newNode, $server->uuid, 'sha256');
82
83
// Notify the source node of the pending outgoing transfer.
84
$this->daemonTransferRepository->setServer($server)->notify($transfer->newNode, $token);
85
86
return $transfer;
87
});
88
89
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();
90
91
return redirect()->route('admin.servers.view.manage', $server->id);
92
}
93
94
/**
95
* Assigns the specified allocations to the specified server.
96
*/
97
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
98
{
99
$allocations = $additional_allocations;
100
$allocations[] = $allocation_id;
101
102
$unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id);
103
104
$updateIds = [];
105
foreach ($allocations as $allocation) {
106
if (!in_array($allocation, $unassigned)) {
107
continue;
108
}
109
110
$updateIds[] = $allocation;
111
}
112
113
if (!empty($updateIds)) {
114
$this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);
115
}
116
}
117
}
118
119