2020-04-04 03:45:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Admin\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Dispatcher;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\LocationRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Pterodactyl\Services\Servers\SuspensionService;
|
|
|
|
use Pterodactyl\Services\Servers\TransferService;
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
|
|
|
private $alert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Bus\Dispatcher
|
|
|
|
*/
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-04 03:45:37 +00:00
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*
|
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Illuminate\Bus\Dispatcher $dispatcher
|
|
|
|
* @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-04 03:45:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
|
|
|
Dispatcher $dispatcher,
|
|
|
|
ServerRepository $repository,
|
|
|
|
LocationRepository $locationRepository,
|
2020-04-04 06:50:06 +00:00
|
|
|
NodeRepository $nodeRepository,
|
|
|
|
SuspensionService $suspensionService,
|
|
|
|
TransferService $transferService
|
2020-04-04 03:45:37 +00:00
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$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-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'];
|
|
|
|
$allocation_id = $validatedData['allocation_id'];
|
|
|
|
$additional_allocations = $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)) {
|
2020-04-04 06:50:06 +00:00
|
|
|
// Suspend the server and request an archive to be created.
|
|
|
|
// $this->suspensionService->toggle($server, 'suspend');
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|