Add ServerTransfer relation in Models/Server.php, notify the new daemon about the incoming server transfer

This commit is contained in:
Matthew Penner 2020-04-04 14:10:18 -06:00
parent 86b7b6ecc3
commit 6ba6c34252
6 changed files with 79 additions and 19 deletions

View file

@ -2,11 +2,12 @@
namespace Pterodactyl\Http\Controllers\Admin\Servers;
use Illuminate\Bus\Dispatcher;
use Illuminate\Http\Request;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\ServerTransfer;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository;
@ -21,9 +22,9 @@ class ServerTransferController extends Controller
private $alert;
/**
* @var \Illuminate\Bus\Dispatcher
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/
private $dispatcher;
private $allocationRepository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
@ -54,7 +55,7 @@ class ServerTransferController extends Controller
* ServerTransferController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Illuminate\Bus\Dispatcher $dispatcher
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository,
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Repositories\Eloquent\LocationRepository $locationRepository
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
@ -63,7 +64,7 @@ class ServerTransferController extends Controller
*/
public function __construct(
AlertsMessageBag $alert,
Dispatcher $dispatcher,
AllocationRepositoryInterface $allocationRepository,
ServerRepository $repository,
LocationRepository $locationRepository,
NodeRepository $nodeRepository,
@ -71,7 +72,7 @@ class ServerTransferController extends Controller
TransferService $transferService
) {
$this->alert = $alert;
$this->dispatcher = $dispatcher;
$this->allocationRepository = $allocationRepository;
$this->repository = $repository;
$this->locationRepository = $locationRepository;
$this->nodeRepository = $nodeRepository;
@ -97,12 +98,26 @@ class ServerTransferController extends Controller
]);
$node_id = $validatedData['node_id'];
$allocation_id = $validatedData['allocation_id'];
$additional_allocations = $validatedData['allocation_additional'] ?? [];
$allocation_id = intval($validatedData['allocation_id']);
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
// Check if the node is viable for the transfer.
$node = $this->nodeRepository->getNodeWithResourceUsage($node_id);
if ($node->isViable($server->memory, $server->disk)) {
//$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);
/*$transfer = new ServerTransfer;
$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;
$transfer->old_additional_allocations = json_encode($server->allocations->where('id', '!=', $server->allocation_id)->pluck('id'));
$transfer->new_additional_allocations = json_encode($additional_allocations);
$transfer->save();*/
// Suspend the server and request an archive to be created.
// $this->suspensionService->toggle($server, 'suspend');
$this->transferService->requestArchive($server);
@ -114,4 +129,25 @@ class ServerTransferController extends Controller
return redirect()->route('admin.servers.view.manage', $server->id);
}
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
{
$allocations = $additional_allocations;
array_push($allocations, $allocation_id);
$unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id);
$updateIds = [];
foreach ($allocations as $allocation) {
if (! in_array($allocation, $unassigned)) {
continue;
}
$updateIds[] = $allocation;
}
if (! empty($updateIds)) {
$this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]);
}
}
}

View file

@ -6,6 +6,7 @@ use Cake\Chronos\Chronos;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;
@ -53,13 +54,16 @@ class ServerTransferController extends Controller
* The daemon notifies us about the archive status.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @param string $uuid
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function archive(Request $request, Server $server)
public function archive(Request $request, string $uuid)
{
$server = $this->repository->getByUuid($uuid);
// Unsuspend the server and don't continue the transfer.
if (!$request->input('successful')) {
// $this->suspensionService->toggle($server, 'unsuspend');
@ -75,10 +79,16 @@ class ServerTransferController extends Controller
->issuedAt($now->getTimestamp())
->canOnlyBeUsedAfter($now->getTimestamp())
->expiresAt($now->addMinutes(15)->getTimestamp())
->relatedTo($server->id, true)
->relatedTo($server->uuid, true)
->getToken($signer, new Key($server->node->daemonSecret));
$this->daemonTransferRepository->notify($server, $token->__toString());
// On the daemon transfer repository, make sure to set the node after the server
// because setServer() tells the repository to use the server's node and not the one
// we want to specify.
$this->daemonTransferRepository
->setServer($server)
->setNode($this->nodeRepository->find($server->transfer->new_node))
->notify($server, $server->node, $token->__toString());
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
}

View file

@ -51,6 +51,7 @@ use Znck\Eloquent\Traits\BelongsToThrough;
* @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\DaemonKey $key
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
* @property \Pterodactyl\Models\ServerTransfer $transfer
*/
class Server extends Validable
{
@ -337,4 +338,14 @@ class Server extends Validable
{
return $this->hasMany(DaemonKey::class);
}
/**
* Returns all of the daemon keys belonging to this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function transfer()
{
return $this->hasOne(ServerTransfer::class)->orderByDesc('id');
}
}

View file

@ -2,29 +2,30 @@
namespace Pterodactyl\Repositories\Wings;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class DaemonTransferRepository extends DaemonRepository
{
/**
* @param Server $server
* @param Node $node
* @param string $token
*
* @throws DaemonConnectionException
*/
public function notify(Server $server, string $token): void
{
public function notify(Server $server, Node $node, string $token): void {
try {
$this->getHttpClient()->post('/api/transfer', [
'json' => [
'url' => $server->node->getConnectionAddress() . sprintf('/api/servers/%s/archive', $server->uuid),
'token' => $token,
'server_id' => $server->uuid,
'url' => $node->getConnectionAddress() . sprintf('/api/servers/%s/archive', $server->uuid),
'token' => 'Bearer ' . $token,
],
]);
} catch (TransferException $exception) {
} catch(TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}

View file

@ -23,7 +23,7 @@
@include('admin.servers.partials.navigation')
<div class="row">
<div class="col-sm-4">
<div class="box box-primary">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Reinstall Server</h3>
</div>

View file

@ -13,3 +13,5 @@ Route::group(['prefix' => '/servers/{uuid}'], function () {
Route::post('/install', 'Servers\ServerInstallController@store');
Route::post('/archive', 'Servers\ServerTransferController@archive');
});