2020-04-04 03:45:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Admin\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2020-04-04 20:10:18 +00:00
|
|
|
use Pterodactyl\Models\ServerTransfer;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Services\Servers\TransferService;
|
|
|
|
use Pterodactyl\Services\Servers\SuspensionService;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
2020-04-04 03:45:37 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\LocationRepository;
|
2020-04-05 00:28:09 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
|
|
|
private $alert;
|
|
|
|
|
|
|
|
/**
|
2020-04-04 20:10:18 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
2020-04-04 03:45:37 +00:00
|
|
|
*/
|
2020-04-04 20:10:18 +00:00
|
|
|
private $allocationRepository;
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\LocationRepository
|
|
|
|
*/
|
|
|
|
private $locationRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
|
|
|
|
*/
|
|
|
|
private $nodeRepository;
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\SuspensionService
|
|
|
|
*/
|
|
|
|
private $suspensionService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\TransferService
|
|
|
|
*/
|
|
|
|
private $transferService;
|
|
|
|
|
2020-04-05 00:28:09 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository
|
|
|
|
*/
|
|
|
|
private $daemonConfigurationRepository;
|
|
|
|
|
2020-04-04 03:45:37 +00:00
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*
|
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
2020-04-04 22:16:18 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
|
2020-04-04 03:45:37 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\LocationRepository $locationRepository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
|
2020-04-04 06:50:06 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
|
|
|
|
* @param \Pterodactyl\Services\Servers\TransferService $transferService
|
2020-04-05 00:28:09 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository $daemonConfigurationRepository
|
2020-04-04 03:45:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
2020-04-04 20:10:18 +00:00
|
|
|
AllocationRepositoryInterface $allocationRepository,
|
2020-04-04 03:45:37 +00:00
|
|
|
ServerRepository $repository,
|
|
|
|
LocationRepository $locationRepository,
|
2020-04-04 06:50:06 +00:00
|
|
|
NodeRepository $nodeRepository,
|
|
|
|
SuspensionService $suspensionService,
|
2020-04-05 00:28:09 +00:00
|
|
|
TransferService $transferService,
|
|
|
|
DaemonConfigurationRepository $daemonConfigurationRepository
|
2020-04-04 03:45:37 +00:00
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
2020-04-04 20:10:18 +00:00
|
|
|
$this->allocationRepository = $allocationRepository;
|
2020-04-04 03:45:37 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
$this->locationRepository = $locationRepository;
|
|
|
|
$this->nodeRepository = $nodeRepository;
|
2020-04-04 06:50:06 +00:00
|
|
|
$this->suspensionService = $suspensionService;
|
|
|
|
$this->transferService = $transferService;
|
2020-04-05 00:28:09 +00:00
|
|
|
$this->daemonConfigurationRepository = $daemonConfigurationRepository;
|
2020-04-04 03:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a transfer of a server to a new node.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2020-04-04 06:50:06 +00:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
2020-04-04 03:45:37 +00:00
|
|
|
*/
|
|
|
|
public function transfer(Request $request, Server $server)
|
|
|
|
{
|
|
|
|
$validatedData = $request->validate([
|
|
|
|
'node_id' => 'required|exists:nodes,id',
|
|
|
|
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
|
|
|
|
'allocation_additional' => 'nullable',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$node_id = $validatedData['node_id'];
|
2020-04-04 20:10:18 +00:00
|
|
|
$allocation_id = intval($validatedData['allocation_id']);
|
|
|
|
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
// Check if the node is viable for the transfer.
|
|
|
|
$node = $this->nodeRepository->getNodeWithResourceUsage($node_id);
|
|
|
|
if ($node->isViable($server->memory, $server->disk)) {
|
2020-04-05 00:28:09 +00:00
|
|
|
// Check if the selected daemon is online.
|
|
|
|
$this->daemonConfigurationRepository->setNode($node)->getSystemInformation();
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
// Suspend the server and request an archive to be created.
|
|
|
|
$this->suspensionService->toggle($server, 'suspend');
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
// Create a new ServerTransfer entry.
|
|
|
|
$transfer = new ServerTransfer;
|
2020-04-04 20:10:18 +00:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
$transfer->save();
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
// 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);
|
|
|
|
|
2020-04-05 00:28:09 +00:00
|
|
|
// Request an archive from the server's current daemon. (this also checks if the daemon is online)
|
2020-04-04 06:50:06 +00:00
|
|
|
$this->transferService->requestArchive($server);
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();
|
|
|
|
} else {
|
|
|
|
$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('admin.servers.view.manage', $server->id);
|
|
|
|
}
|
2020-04-04 20:10:18 +00:00
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* Assigns the specified allocations to the specified server.
|
|
|
|
*
|
|
|
|
* @param Server $server
|
|
|
|
* @param int $node_id
|
|
|
|
* @param int $allocation_id
|
|
|
|
* @param array $additional_allocations
|
|
|
|
*/
|
2020-04-04 20:10:18 +00:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 03:45:37 +00:00
|
|
|
}
|