mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 19:14:45 +02:00
Add api for server transfers (#153)
* add application api endpoint to start server transfer * add basics for "cancel transfer" endpoint * wire up wings cancel * lint
This commit is contained in:
parent
afd9f2eb0e
commit
6bdd1b3ccb
@ -2,21 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin\Servers;
|
namespace App\Http\Controllers\Admin\Servers;
|
||||||
|
|
||||||
use App\Exceptions\Http\Connection\DaemonConnectionException;
|
|
||||||
use App\Models\Allocation;
|
|
||||||
use App\Models\Node;
|
|
||||||
use Carbon\CarbonImmutable;
|
|
||||||
use GuzzleHttp\Exception\TransferException;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use App\Models\Server;
|
|
||||||
use Illuminate\Http\RedirectResponse;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
|
||||||
use Lcobucci\JWT\Token\Plain;
|
|
||||||
use Prologue\Alerts\AlertsMessageBag;
|
|
||||||
use App\Models\ServerTransfer;
|
|
||||||
use Illuminate\Database\ConnectionInterface;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Services\Nodes\NodeJWTService;
|
use App\Models\Server;
|
||||||
|
use App\Services\Servers\TransferServerService;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Prologue\Alerts\AlertsMessageBag;
|
||||||
|
|
||||||
class ServerTransferController extends Controller
|
class ServerTransferController extends Controller
|
||||||
{
|
{
|
||||||
@ -25,30 +16,10 @@ class ServerTransferController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private AlertsMessageBag $alert,
|
private AlertsMessageBag $alert,
|
||||||
private ConnectionInterface $connection,
|
private TransferServerService $transferServerService,
|
||||||
private NodeJWTService $nodeJWTService,
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function notify(Server $server, Plain $token): void
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
Http::daemon($server->node)->post('/api/transfer', [
|
|
||||||
'json' => [
|
|
||||||
'server_id' => $server->uuid,
|
|
||||||
'url' => $server->node->getConnectionAddress() . "/api/servers/$server->uuid/archive",
|
|
||||||
'token' => 'Bearer ' . $token->toString(),
|
|
||||||
'server' => [
|
|
||||||
'uuid' => $server->uuid,
|
|
||||||
'start_on_completion' => false,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
])->toPsrResponse();
|
|
||||||
} catch (TransferException $exception) {
|
|
||||||
throw new DaemonConnectionException($exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a transfer of a server to a new node.
|
* Starts a transfer of a server to a new node.
|
||||||
*
|
*
|
||||||
@ -62,85 +33,12 @@ class ServerTransferController extends Controller
|
|||||||
'allocation_additional' => 'nullable',
|
'allocation_additional' => 'nullable',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$node_id = $validatedData['node_id'];
|
if ($this->transferServerService->handle($server, $validatedData)) {
|
||||||
$allocation_id = intval($validatedData['allocation_id']);
|
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();
|
||||||
$additional_allocations = array_map('intval', $validatedData['allocation_additional'] ?? []);
|
} else {
|
||||||
|
|
||||||
// Check if the node is viable for the transfer.
|
|
||||||
$node = Node::query()
|
|
||||||
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemon_listen', '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')
|
|
||||||
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
|
|
||||||
->where('nodes.id', $node_id)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if (!$node->isViable($server->memory, $server->disk)) {
|
|
||||||
$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();
|
$this->alert->danger(trans('admin/server.alerts.transfer_not_viable'))->flash();
|
||||||
|
|
||||||
return redirect()->route('admin.servers.view.manage', $server->id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$server->validateTransferState();
|
|
||||||
|
|
||||||
$this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) {
|
|
||||||
// Create a new ServerTransfer entry.
|
|
||||||
$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 = $server->allocations->where('id', '!=', $server->allocation_id)->pluck('id')->all();
|
|
||||||
$transfer->new_additional_allocations = $additional_allocations;
|
|
||||||
|
|
||||||
$transfer->save();
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// Generate a token for the destination node that the source node can use to authenticate with.
|
|
||||||
$token = $this->nodeJWTService
|
|
||||||
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
|
||||||
->setSubject($server->uuid)
|
|
||||||
->handle($transfer->newNode, $server->uuid, 'sha256');
|
|
||||||
|
|
||||||
// Notify the source node of the pending outgoing transfer.
|
|
||||||
$this->notify($server, $token);
|
|
||||||
|
|
||||||
return $transfer;
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->alert->success(trans('admin/server.alerts.transfer_started'))->flash();
|
|
||||||
|
|
||||||
return redirect()->route('admin.servers.view.manage', $server->id);
|
return redirect()->route('admin.servers.view.manage', $server->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assigns the specified allocations to the specified server.
|
|
||||||
*/
|
|
||||||
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
|
|
||||||
{
|
|
||||||
$allocations = $additional_allocations;
|
|
||||||
$allocations[] = $allocation_id;
|
|
||||||
|
|
||||||
$node = Node::query()->findOrFail($node_id);
|
|
||||||
$unassigned = $node->allocations()
|
|
||||||
->whereNull('server_id')
|
|
||||||
->pluck('id')
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
$updateIds = [];
|
|
||||||
foreach ($allocations as $allocation) {
|
|
||||||
if (!in_array($allocation, $unassigned)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$updateIds[] = $allocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($updateIds)) {
|
|
||||||
Allocation::query()->whereIn('id', $updateIds)->update(['server_id' => $server->id]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api\Application\Servers;
|
namespace App\Http\Controllers\Api\Application\Servers;
|
||||||
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
use App\Models\Server;
|
|
||||||
use App\Services\Servers\SuspensionService;
|
|
||||||
use App\Services\Servers\ReinstallServerService;
|
|
||||||
use App\Http\Requests\Api\Application\Servers\ServerWriteRequest;
|
|
||||||
use App\Http\Controllers\Api\Application\ApplicationApiController;
|
use App\Http\Controllers\Api\Application\ApplicationApiController;
|
||||||
|
use App\Http\Requests\Api\Application\Servers\ServerWriteRequest;
|
||||||
|
use App\Models\Server;
|
||||||
|
use App\Repositories\Daemon\DaemonServerRepository;
|
||||||
|
use App\Services\Servers\ReinstallServerService;
|
||||||
|
use App\Services\Servers\SuspensionService;
|
||||||
|
use App\Services\Servers\TransferServerService;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
class ServerManagementController extends ApplicationApiController
|
class ServerManagementController extends ApplicationApiController
|
||||||
{
|
{
|
||||||
@ -16,7 +18,9 @@ class ServerManagementController extends ApplicationApiController
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ReinstallServerService $reinstallServerService,
|
private ReinstallServerService $reinstallServerService,
|
||||||
private SuspensionService $suspensionService
|
private SuspensionService $suspensionService,
|
||||||
|
private TransferServerService $transferServerService,
|
||||||
|
private DaemonServerRepository $daemonServerRepository,
|
||||||
) {
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
@ -57,4 +61,44 @@ class ServerManagementController extends ApplicationApiController
|
|||||||
|
|
||||||
return $this->returnNoContent();
|
return $this->returnNoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a transfer of a server to a new node.
|
||||||
|
*/
|
||||||
|
public function startTransfer(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
$validatedData = $request->validate([
|
||||||
|
'node_id' => 'required|exists:nodes,id',
|
||||||
|
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
|
||||||
|
'allocation_additional' => 'nullable',
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($this->transferServerService->handle($server, $validatedData)) {
|
||||||
|
// Transfer started
|
||||||
|
$this->returnNoContent();
|
||||||
|
} else {
|
||||||
|
// Node was not viable
|
||||||
|
return new Response('', Response::HTTP_NOT_ACCEPTABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels a transfer of a server to a new node.
|
||||||
|
*
|
||||||
|
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
|
||||||
|
*/
|
||||||
|
public function cancelTransfer(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
if (!$transfer = $server->transfer) {
|
||||||
|
// Server is not transferring
|
||||||
|
return new Response('', Response::HTTP_NOT_ACCEPTABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
$transfer->successful = true;
|
||||||
|
$transfer->save();
|
||||||
|
|
||||||
|
$this->daemonServerRepository->setServer($server)->cancelTransfer();
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,46 @@ class DaemonServerRepository extends DaemonRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels a server transfer.
|
||||||
|
*
|
||||||
|
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
|
||||||
|
*/
|
||||||
|
public function cancelTransfer(): void
|
||||||
|
{
|
||||||
|
Assert::isInstanceOf($this->server, Server::class);
|
||||||
|
|
||||||
|
if ($transfer = $this->server->transfer) {
|
||||||
|
// Source node
|
||||||
|
$this->setNode($transfer->oldNode);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->getHttpClient()->delete(sprintf(
|
||||||
|
'/api/servers/%s/transfer',
|
||||||
|
$this->server->uuid
|
||||||
|
));
|
||||||
|
} catch (TransferException $exception) {
|
||||||
|
throw new DaemonConnectionException($exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destination node
|
||||||
|
$this->setNode($transfer->newNode);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->getHttpClient()->delete('/api/transfer', [
|
||||||
|
'json' => [
|
||||||
|
'server_id' => $this->server->uuid,
|
||||||
|
'server' => [
|
||||||
|
'uuid' => $this->server->uuid,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
} catch (TransferException $exception) {
|
||||||
|
throw new DaemonConnectionException($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Revokes a single user's JTI by using their ID. This is simply a helper function to
|
* Revokes a single user's JTI by using their ID. This is simply a helper function to
|
||||||
* make it easier to revoke tokens on the fly. This ensures that the JTI key is formatted
|
* make it easier to revoke tokens on the fly. This ensures that the JTI key is formatted
|
||||||
|
131
app/Services/Servers/TransferServerService.php
Normal file
131
app/Services/Servers/TransferServerService.php
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Servers;
|
||||||
|
|
||||||
|
use App\Exceptions\Http\Connection\DaemonConnectionException;
|
||||||
|
use App\Models\Allocation;
|
||||||
|
use App\Models\Node;
|
||||||
|
use App\Models\Server;
|
||||||
|
use App\Models\ServerTransfer;
|
||||||
|
use App\Services\Nodes\NodeJWTService;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use GuzzleHttp\Exception\TransferException;
|
||||||
|
use Illuminate\Database\ConnectionInterface;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Lcobucci\JWT\Token\Plain;
|
||||||
|
|
||||||
|
class TransferServerService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* TransferService constructor.
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private ConnectionInterface $connection,
|
||||||
|
private NodeJWTService $nodeJWTService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private function notify(Server $server, Plain $token): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Http::daemon($server->node)->post('/api/transfer', [
|
||||||
|
'json' => [
|
||||||
|
'server_id' => $server->uuid,
|
||||||
|
'url' => $server->node->getConnectionAddress() . "/api/servers/$server->uuid/archive",
|
||||||
|
'token' => 'Bearer ' . $token->toString(),
|
||||||
|
'server' => [
|
||||||
|
'uuid' => $server->uuid,
|
||||||
|
'start_on_completion' => false,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])->toPsrResponse();
|
||||||
|
} catch (TransferException $exception) {
|
||||||
|
throw new DaemonConnectionException($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a transfer of a server to a new node.
|
||||||
|
*
|
||||||
|
* @throws \Throwable
|
||||||
|
*/
|
||||||
|
public function handle(Server $server, array $data): bool
|
||||||
|
{
|
||||||
|
$node_id = $data['node_id'];
|
||||||
|
$allocation_id = intval($data['allocation_id']);
|
||||||
|
$additional_allocations = array_map('intval', $data['allocation_additional'] ?? []);
|
||||||
|
|
||||||
|
// Check if the node is viable for the transfer.
|
||||||
|
$node = Node::query()
|
||||||
|
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemon_listen', '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')
|
||||||
|
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
|
||||||
|
->where('nodes.id', $node_id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$node->isViable($server->memory, $server->disk)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$server->validateTransferState();
|
||||||
|
|
||||||
|
$this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) {
|
||||||
|
// Create a new ServerTransfer entry.
|
||||||
|
$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 = $server->allocations->where('id', '!=', $server->allocation_id)->pluck('id')->all();
|
||||||
|
$transfer->new_additional_allocations = $additional_allocations;
|
||||||
|
|
||||||
|
$transfer->save();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Generate a token for the destination node that the source node can use to authenticate with.
|
||||||
|
$token = $this->nodeJWTService
|
||||||
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
||||||
|
->setSubject($server->uuid)
|
||||||
|
->handle($transfer->newNode, $server->uuid, 'sha256');
|
||||||
|
|
||||||
|
// Notify the source node of the pending outgoing transfer.
|
||||||
|
$this->notify($server, $token);
|
||||||
|
|
||||||
|
return $transfer;
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assigns the specified allocations to the specified server.
|
||||||
|
*/
|
||||||
|
private function assignAllocationsToServer(Server $server, int $node_id, int $allocation_id, array $additional_allocations)
|
||||||
|
{
|
||||||
|
$allocations = $additional_allocations;
|
||||||
|
$allocations[] = $allocation_id;
|
||||||
|
|
||||||
|
$node = Node::query()->findOrFail($node_id);
|
||||||
|
$unassigned = $node->allocations()
|
||||||
|
->whereNull('server_id')
|
||||||
|
->pluck('id')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$updateIds = [];
|
||||||
|
foreach ($allocations as $allocation) {
|
||||||
|
if (!in_array($allocation, $unassigned)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$updateIds[] = $allocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($updateIds)) {
|
||||||
|
Allocation::query()->whereIn('id', $updateIds)->update(['server_id' => $server->id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -69,6 +69,8 @@ Route::prefix('/servers')->group(function () {
|
|||||||
Route::post('/{server:id}/suspend', [Application\Servers\ServerManagementController::class, 'suspend'])->name('api.application.servers.suspend');
|
Route::post('/{server:id}/suspend', [Application\Servers\ServerManagementController::class, 'suspend'])->name('api.application.servers.suspend');
|
||||||
Route::post('/{server:id}/unsuspend', [Application\Servers\ServerManagementController::class, 'unsuspend'])->name('api.application.servers.unsuspend');
|
Route::post('/{server:id}/unsuspend', [Application\Servers\ServerManagementController::class, 'unsuspend'])->name('api.application.servers.unsuspend');
|
||||||
Route::post('/{server:id}/reinstall', [Application\Servers\ServerManagementController::class, 'reinstall'])->name('api.application.servers.reinstall');
|
Route::post('/{server:id}/reinstall', [Application\Servers\ServerManagementController::class, 'reinstall'])->name('api.application.servers.reinstall');
|
||||||
|
Route::post('/{server:id}/transfer', [Application\Servers\ServerManagementController::class, 'startTransfer'])->name('api.application.servers.transfer');
|
||||||
|
Route::post('/{server:id}/transfer/cancel', [Application\Servers\ServerManagementController::class, 'cancelTransfer'])->name('api.application.servers.transfer.cancel');
|
||||||
|
|
||||||
Route::delete('/{server:id}', [Application\Servers\ServerController::class, 'delete']);
|
Route::delete('/{server:id}', [Application\Servers\ServerController::class, 'delete']);
|
||||||
Route::delete('/{server:id}/{force?}', [Application\Servers\ServerController::class, 'delete']);
|
Route::delete('/{server:id}/{force?}', [Application\Servers\ServerController::class, 'delete']);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user