Files
panel-pelican-dev/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php

97 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Remote\Servers;
2025-08-09 23:53:45 +02:00
use App\Http\Requests\Api\Remote\ServerRequest;
2024-03-16 14:35:13 -04:00
use App\Models\Server;
2024-03-16 15:01:21 -04:00
use App\Repositories\Daemon\DaemonServerRepository;
use Illuminate\Http\Response;
2020-04-04 16:24:58 -06:00
use Illuminate\Http\JsonResponse;
2024-03-12 22:39:16 -04:00
use App\Models\Allocation;
2020-04-04 16:24:58 -06:00
use Illuminate\Database\ConnectionInterface;
2024-03-12 22:39:16 -04:00
use App\Http\Controllers\Controller;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Illuminate\Http\Client\ConnectionException;
class ServerTransferController extends Controller
{
/**
* ServerTransferController constructor.
*/
public function __construct(
private ConnectionInterface $connection,
2024-03-16 15:01:21 -04:00
private DaemonServerRepository $daemonServerRepository,
) {}
/**
* The daemon notifies us about a transfer failure.
*
* @throws \Throwable
*/
2025-08-09 23:53:45 +02:00
public function failure(ServerRequest $request, Server $server): JsonResponse
{
$transfer = $server->transfer;
if (is_null($transfer)) {
throw new ConflictHttpException('Server is not being transferred.');
}
2025-08-09 23:53:45 +02:00
$this->connection->transaction(function () use ($transfer) {
$transfer->forceFill(['successful' => false])->saveOrFail();
if ($transfer->new_allocation || $transfer->new_additional_allocations) {
$allocations = array_merge([$transfer->new_allocation], $transfer->new_additional_allocations);
Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);
}
});
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* The daemon notifies us about a transfer success.
*
* @throws \Throwable
*/
2025-08-09 23:53:45 +02:00
public function success(ServerRequest $request, Server $server): JsonResponse
{
$transfer = $server->transfer;
if (is_null($transfer)) {
throw new ConflictHttpException('Server is not being transferred.');
}
2025-08-09 23:53:45 +02:00
/** @var Server $server */
$server = $this->connection->transaction(function () use ($server, $transfer) {
$data = [];
if ($transfer->old_allocation || $transfer->old_additional_allocations) {
$allocations = array_merge([$transfer->old_allocation], $transfer->old_additional_allocations);
// Remove the old allocations for the server and re-assign the server to the new
// primary allocation and node.
Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);
$data['allocation_id'] = $transfer->new_allocation;
}
2025-08-09 23:53:45 +02:00
$data['node_id'] = $transfer->new_node;
$server->update($data);
$server = $server->fresh();
$server->transfer->update(['successful' => true]);
return $server;
});
// Delete the server from the old node making sure to point it to the old node so
// that we do not delete it from the new node the server was transferred to.
try {
$this->daemonServerRepository
->setServer($server)
->setNode($transfer->oldNode)
->delete();
} catch (ConnectionException $exception) {
2024-03-16 23:27:24 -04:00
logger()->warning($exception, ['transfer_id' => $server->transfer->id]);
}
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
}