Check that the selected daemon is online for a transfer, delete the server from the old node when a transfer is successful

This commit is contained in:
Matthew Penner 2020-04-04 18:28:09 -06:00
parent 7b518325c4
commit b395440e4e
3 changed files with 41 additions and 16 deletions

View file

@ -12,6 +12,7 @@ use Pterodactyl\Services\Servers\SuspensionService;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
class ServerTransferController extends Controller class ServerTransferController extends Controller
@ -51,6 +52,11 @@ class ServerTransferController extends Controller
*/ */
private $transferService; private $transferService;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository
*/
private $daemonConfigurationRepository;
/** /**
* ServerTransferController constructor. * ServerTransferController constructor.
* *
@ -61,6 +67,7 @@ class ServerTransferController extends Controller
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository * @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService * @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
* @param \Pterodactyl\Services\Servers\TransferService $transferService * @param \Pterodactyl\Services\Servers\TransferService $transferService
* @param \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository $daemonConfigurationRepository
*/ */
public function __construct( public function __construct(
AlertsMessageBag $alert, AlertsMessageBag $alert,
@ -69,7 +76,8 @@ class ServerTransferController extends Controller
LocationRepository $locationRepository, LocationRepository $locationRepository,
NodeRepository $nodeRepository, NodeRepository $nodeRepository,
SuspensionService $suspensionService, SuspensionService $suspensionService,
TransferService $transferService TransferService $transferService,
DaemonConfigurationRepository $daemonConfigurationRepository
) { ) {
$this->alert = $alert; $this->alert = $alert;
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
@ -78,6 +86,7 @@ class ServerTransferController extends Controller
$this->nodeRepository = $nodeRepository; $this->nodeRepository = $nodeRepository;
$this->suspensionService = $suspensionService; $this->suspensionService = $suspensionService;
$this->transferService = $transferService; $this->transferService = $transferService;
$this->daemonConfigurationRepository = $daemonConfigurationRepository;
} }
/** /**
@ -104,6 +113,9 @@ class ServerTransferController extends Controller
// Check if the node is viable for the transfer. // Check if the node is viable for the transfer.
$node = $this->nodeRepository->getNodeWithResourceUsage($node_id); $node = $this->nodeRepository->getNodeWithResourceUsage($node_id);
if ($node->isViable($server->memory, $server->disk)) { if ($node->isViable($server->memory, $server->disk)) {
// Check if the selected daemon is online.
$this->daemonConfigurationRepository->setNode($node)->getSystemInformation();
// Suspend the server and request an archive to be created. // Suspend the server and request an archive to be created.
$this->suspensionService->toggle($server, 'suspend'); $this->suspensionService->toggle($server, 'suspend');
@ -123,7 +135,7 @@ class ServerTransferController extends Controller
// Add the allocations to the server so they cannot be automatically assigned while the transfer is in progress. // Add the allocations to the server so they cannot be automatically assigned while the transfer is in progress.
$this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations); $this->assignAllocationsToServer($server, $node_id, $allocation_id, $additional_allocations);
// Request an archive from the server's current daemon. // Request an archive from the server's current daemon. (this also checks if the daemon is online)
$this->transferService->requestArchive($server); $this->transferService->requestArchive($server);
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash(); $this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();

View file

@ -6,15 +6,16 @@ use Cake\Chronos\Chronos;
use Lcobucci\JWT\Builder; use Lcobucci\JWT\Builder;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\Signer\Key;
use Psr\Log\LoggerInterface;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\Signer\Hmac\Sha256;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\SuspensionService; use Pterodactyl\Services\Servers\SuspensionService;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Repositories\Wings\DaemonTransferRepository; use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
@ -42,6 +43,11 @@ class ServerTransferController extends Controller
*/ */
private $nodeRepository; private $nodeRepository;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/** /**
* @var \Pterodactyl\Repositories\Wings\DaemonTransferRepository * @var \Pterodactyl\Repositories\Wings\DaemonTransferRepository
*/ */
@ -57,6 +63,11 @@ class ServerTransferController extends Controller
*/ */
private $suspensionService; private $suspensionService;
/**
* @var \Psr\Log\LoggerInterface
*/
private $writer;
/** /**
* ServerTransferController constructor. * ServerTransferController constructor.
* *
@ -64,26 +75,32 @@ class ServerTransferController extends Controller
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository * @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
* @param \Pterodactyl\Repositories\Wings\DaemonTransferRepository $daemonTransferRepository * @param \Pterodactyl\Repositories\Wings\DaemonTransferRepository $daemonTransferRepository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService * @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
* @param \Psr\Log\LoggerInterface $writer
*/ */
public function __construct( public function __construct(
ConnectionInterface $connection, ConnectionInterface $connection,
ServerRepository $repository, ServerRepository $repository,
AllocationRepositoryInterface $allocationRepository, AllocationRepositoryInterface $allocationRepository,
NodeRepository $nodeRepository, NodeRepository $nodeRepository,
DaemonServerRepository $daemonServerRepository,
DaemonTransferRepository $daemonTransferRepository, DaemonTransferRepository $daemonTransferRepository,
ServerConfigurationStructureService $configurationStructureService, ServerConfigurationStructureService $configurationStructureService,
SuspensionService $suspensionService SuspensionService $suspensionService,
LoggerInterface $writer
) { ) {
$this->connection = $connection; $this->connection = $connection;
$this->repository = $repository; $this->repository = $repository;
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->nodeRepository = $nodeRepository; $this->nodeRepository = $nodeRepository;
$this->daemonServerRepository = $daemonServerRepository;
$this->daemonTransferRepository = $daemonTransferRepository; $this->daemonTransferRepository = $daemonTransferRepository;
$this->configurationStructureService = $configurationStructureService; $this->configurationStructureService = $configurationStructureService;
$this->suspensionService = $suspensionService; $this->suspensionService = $suspensionService;
$this->writer = $writer;
} }
/** /**
@ -162,15 +179,9 @@ class ServerTransferController extends Controller
$allocationIds = json_decode($transfer->new_additional_allocations); $allocationIds = json_decode($transfer->new_additional_allocations);
array_push($allocationIds, $transfer->new_allocation); array_push($allocationIds, $transfer->new_allocation);
// Begin a transaction.
$this->connection->beginTransaction();
// Remove the new allocations. // Remove the new allocations.
$this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]); $this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]);
// Commit the transaction.
$this->connection->commit();
// Unsuspend the server. // Unsuspend the server.
$this->suspensionService->toggle($server, 'unsuspend'); $this->suspensionService->toggle($server, 'unsuspend');
@ -190,8 +201,6 @@ class ServerTransferController extends Controller
$server = $this->repository->getByUuid($uuid); $server = $this->repository->getByUuid($uuid);
$transfer = $server->transfer; $transfer = $server->transfer;
// TODO: Notify old daemon about transfer and get it to remove server files.
$allocationIds = json_decode($transfer->old_additional_allocations); $allocationIds = json_decode($transfer->old_additional_allocations);
array_push($allocationIds, $transfer->old_allocation); array_push($allocationIds, $transfer->old_allocation);
@ -213,11 +222,15 @@ class ServerTransferController extends Controller
// Commit the transaction. // Commit the transaction.
$this->connection->commit(); $this->connection->commit();
// Delete the server from the old node
try {
$this->daemonServerRepository->setServer($server)->delete();
} catch (DaemonConnectionException $exception) {
$this->writer->warning($exception);
}
// Unsuspend the server // Unsuspend the server
$server->load('node'); $server->load('node');
Log::debug(json_encode($server));
Log::debug(json_encode($server->node_id));
Log::debug(json_encode($server->node));
$this->suspensionService->toggle($server, $this->suspensionService::ACTION_UNSUSPEND); $this->suspensionService->toggle($server, $this->suspensionService::ACTION_UNSUSPEND);
return JsonResponse::create([], Response::HTTP_NO_CONTENT); return JsonResponse::create([], Response::HTTP_NO_CONTENT);

View file

@ -183,7 +183,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
public function getNodeWithResourceUsage(int $node_id): Node public function getNodeWithResourceUsage(int $node_id): Node
{ {
$instance = $this->getBuilder() $instance = $this->getBuilder()
->select(['nodes.id', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate']) ->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemonSecret', 'nodes.daemonListen', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate'])
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk') ->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id') ->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.id', $node_id); ->where('nodes.id', $node_id);