Skip to content

Commit 6ba6c34

Browse files
committed
Add ServerTransfer relation in Models/Server.php, notify the new daemon about the incoming server transfer
1 parent 86b7b6e commit 6ba6c34

File tree

6 files changed

+79
-19
lines changed

6 files changed

+79
-19
lines changed

app/Http/Controllers/Admin/Servers/ServerTransferController.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Pterodactyl\Http\Controllers\Admin\Servers;
44

5-
use Illuminate\Bus\Dispatcher;
65
use Illuminate\Http\Request;
76
use Prologue\Alerts\AlertsMessageBag;
7+
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
88
use Pterodactyl\Http\Controllers\Controller;
99
use Pterodactyl\Models\Server;
10+
use Pterodactyl\Models\ServerTransfer;
1011
use Pterodactyl\Repositories\Eloquent\ServerRepository;
1112
use Pterodactyl\Repositories\Eloquent\LocationRepository;
1213
use Pterodactyl\Repositories\Eloquent\NodeRepository;
@@ -21,9 +22,9 @@ class ServerTransferController extends Controller
2122
private $alert;
2223

2324
/**
24-
* @var \Illuminate\Bus\Dispatcher
25+
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
2526
*/
26-
private $dispatcher;
27+
private $allocationRepository;
2728

2829
/**
2930
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
@@ -54,7 +55,7 @@ class ServerTransferController extends Controller
5455
* ServerTransferController constructor.
5556
*
5657
* @param \Prologue\Alerts\AlertsMessageBag $alert
57-
* @param \Illuminate\Bus\Dispatcher $dispatcher
58+
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository,
5859
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
5960
* @param \Pterodactyl\Repositories\Eloquent\LocationRepository $locationRepository
6061
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
@@ -63,15 +64,15 @@ class ServerTransferController extends Controller
6364
*/
6465
public function __construct(
6566
AlertsMessageBag $alert,
66-
Dispatcher $dispatcher,
67+
AllocationRepositoryInterface $allocationRepository,
6768
ServerRepository $repository,
6869
LocationRepository $locationRepository,
6970
NodeRepository $nodeRepository,
7071
SuspensionService $suspensionService,
7172
TransferService $transferService
7273
) {
7374
$this->alert = $alert;
74-
$this->dispatcher = $dispatcher;
75+
$this->allocationRepository = $allocationRepository;
7576
$this->repository = $repository;
7677
$this->locationRepository = $locationRepository;
7778
$this->nodeRepository = $nodeRepository;
@@ -97,12 +98,26 @@ public function transfer(Request $request, Server $server)
9798
]);
9899

99100
$node_id = $validatedData['node_id'];
100-
$allocation_id = $validatedData['allocation_id'];
101-
$additional_allocations = $validatedData['allocation_additional'] ?? [];
101+
$allocation_id = intval($validatedData['allocation_id']);
102+
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
102103

103104
// Check if the node is viable for the transfer.
104105
$node = $this->nodeRepository->getNodeWithResourceUsage($node_id);
105106
if ($node->isViable($server->memory, $server->disk)) {
107+
//$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);
108+
109+
/*$transfer = new ServerTransfer;
110+
111+
$transfer->server_id = $server->id;
112+
$transfer->old_node = $server->node_id;
113+
$transfer->new_node = $node_id;
114+
$transfer->old_allocation = $server->allocation_id;
115+
$transfer->new_allocation = $allocation_id;
116+
$transfer->old_additional_allocations = json_encode($server->allocations->where('id', '!=', $server->allocation_id)->pluck('id'));
117+
$transfer->new_additional_allocations = json_encode($additional_allocations);
118+
119+
$transfer->save();*/
120+
106121
// Suspend the server and request an archive to be created.
107122
// $this->suspensionService->toggle($server, 'suspend');
108123
$this->transferService->requestArchive($server);
@@ -114,4 +129,25 @@ public function transfer(Request $request, Server $server)
114129

115130
return redirect()->route('admin.servers.view.manage', $server->id);
116131
}
132+
133+
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
134+
{
135+
$allocations = $additional_allocations;
136+
array_push($allocations, $allocation_id);
137+
138+
$unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id);
139+
140+
$updateIds = [];
141+
foreach ($allocations as $allocation) {
142+
if (! in_array($allocation, $unassigned)) {
143+
continue;
144+
}
145+
146+
$updateIds[] = $allocation;
147+
}
148+
149+
if (! empty($updateIds)) {
150+
$this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);
151+
}
152+
}
117153
}

app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Http\JsonResponse;
77
use Illuminate\Http\Request;
88
use Illuminate\Http\Response;
9+
use Illuminate\Support\Facades\Log;
910
use Lcobucci\JWT\Builder;
1011
use Lcobucci\JWT\Signer\Hmac\Sha256;
1112
use Lcobucci\JWT\Signer\Key;
@@ -53,13 +54,16 @@ public function __construct(
5354
* The daemon notifies us about the archive status.
5455
*
5556
* @param \Illuminate\Http\Request $request
56-
* @param \Pterodactyl\Models\Server $server
57+
* @param string $uuid
5758
* @return \Illuminate\Http\JsonResponse
5859
*
5960
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
61+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6062
*/
61-
public function archive(Request $request, Server $server)
63+
public function archive(Request $request, string $uuid)
6264
{
65+
$server = $this->repository->getByUuid($uuid);
66+
6367
// Unsuspend the server and don't continue the transfer.
6468
if (!$request->input('successful')) {
6569
// $this->suspensionService->toggle($server, 'unsuspend');
@@ -75,10 +79,16 @@ public function archive(Request $request, Server $server)
7579
->issuedAt($now->getTimestamp())
7680
->canOnlyBeUsedAfter($now->getTimestamp())
7781
->expiresAt($now->addMinutes(15)->getTimestamp())
78-
->relatedTo($server->id, true)
82+
->relatedTo($server->uuid, true)
7983
->getToken($signer, new Key($server->node->daemonSecret));
8084

81-
$this->daemonTransferRepository->notify($server, $token->__toString());
85+
// On the daemon transfer repository, make sure to set the node after the server
86+
// because setServer() tells the repository to use the server's node and not the one
87+
// we want to specify.
88+
$this->daemonTransferRepository
89+
->setServer($server)
90+
->setNode($this->nodeRepository->find($server->transfer->new_node))
91+
->notify($server, $server->node, $token->__toString());
8292

8393
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
8494
}

app/Models/Server.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @property \Pterodactyl\Models\Location $location
5252
* @property \Pterodactyl\Models\DaemonKey $key
5353
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
54+
* @property \Pterodactyl\Models\ServerTransfer $transfer
5455
*/
5556
class Server extends Validable
5657
{
@@ -337,4 +338,14 @@ public function keys()
337338
{
338339
return $this->hasMany(DaemonKey::class);
339340
}
341+
342+
/**
343+
* Returns all of the daemon keys belonging to this server.
344+
*
345+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
346+
*/
347+
public function transfer()
348+
{
349+
return $this->hasOne(ServerTransfer::class)->orderByDesc('id');
350+
}
340351
}

app/Repositories/Wings/DaemonTransferRepository.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22

33
namespace Pterodactyl\Repositories\Wings;
44

5+
use Pterodactyl\Models\Node;
56
use Pterodactyl\Models\Server;
67
use GuzzleHttp\Exception\TransferException;
78
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
89

910
class DaemonTransferRepository extends DaemonRepository
1011
{
11-
1212
/**
1313
* @param Server $server
14+
* @param Node $node
1415
* @param string $token
1516
*
1617
* @throws DaemonConnectionException
1718
*/
18-
public function notify(Server $server, string $token): void
19-
{
19+
public function notify(Server $server, Node $node, string $token): void {
2020
try {
2121
$this->getHttpClient()->post('/api/transfer', [
2222
'json' => [
23-
'url' => $server->node->getConnectionAddress() . sprintf('/api/servers/%s/archive', $server->uuid),
24-
'token' => $token,
23+
'server_id' => $server->uuid,
24+
'url' => $node->getConnectionAddress() . sprintf('/api/servers/%s/archive', $server->uuid),
25+
'token' => 'Bearer ' . $token,
2526
],
2627
]);
27-
} catch (TransferException $exception) {
28+
} catch(TransferException $exception) {
2829
throw new DaemonConnectionException($exception);
2930
}
3031
}

resources/views/admin/servers/view/manage.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@include('admin.servers.partials.navigation')
2424
<div class="row">
2525
<div class="col-sm-4">
26-
<div class="box box-primary">
26+
<div class="box box-danger">
2727
<div class="box-header with-border">
2828
<h3 class="box-title">Reinstall Server</h3>
2929
</div>

routes/api-remote.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
Route::post('/install', 'Servers\ServerInstallController@store');
1414
Route::post('/archive', 'Servers\ServerTransferController@archive');
1515
});
16+
17+

0 commit comments

Comments
 (0)