2020-04-04 06:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Response;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-12-24 18:10:40 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Pterodactyl\Models\ServerTransfer;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2020-04-05 00:28:09 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-04-04 06:50:06 +00:00
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private ServerRepository $repository,
|
2022-11-15 01:25:07 +00:00
|
|
|
private DaemonServerRepository $daemonServerRepository
|
2020-04-04 06:50:06 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer failure.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function failure(string $uuid): JsonResponse
|
2020-04-04 22:16:18 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
return $this->processFailedTransfer($server->transfer);
|
2020-04-04 22:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer success.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-08-18 18:58:41 +00:00
|
|
|
public function success(string $uuid): JsonResponse
|
2020-04-04 22:16:18 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
$transfer = $server->transfer;
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $this->connection->transaction(function () use ($server, $transfer) {
|
2021-08-18 18:58:41 +00:00
|
|
|
$allocations = array_merge([$transfer->old_allocation], $transfer->old_additional_allocations);
|
2020-12-24 18:10:40 +00:00
|
|
|
|
|
|
|
// 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]);
|
|
|
|
$server->update([
|
|
|
|
'allocation_id' => $transfer->new_allocation,
|
|
|
|
'node_id' => $transfer->new_node,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
2021-08-18 18:58:41 +00:00
|
|
|
// that we do not delete it from the new node the server was transferred to.
|
2020-12-24 18:10:40 +00:00
|
|
|
try {
|
|
|
|
$this->daemonServerRepository
|
|
|
|
->setServer($server)
|
|
|
|
->setNode($transfer->oldNode)
|
|
|
|
->delete();
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
Log::warning($exception, ['transfer_id' => $server->transfer->id]);
|
|
|
|
}
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Release all the reserved allocations for this transfer and mark it as failed in
|
2020-12-24 18:10:40 +00:00
|
|
|
* the database.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function processFailedTransfer(ServerTransfer $transfer): JsonResponse
|
2020-12-24 18:10:40 +00:00
|
|
|
{
|
|
|
|
$this->connection->transaction(function () use (&$transfer) {
|
|
|
|
$transfer->forceFill(['successful' => false])->saveOrFail();
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2021-08-18 18:58:41 +00:00
|
|
|
$allocations = array_merge([$transfer->new_allocation], $transfer->new_additional_allocations);
|
2020-12-24 18:10:40 +00:00
|
|
|
Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);
|
|
|
|
});
|
2020-04-05 00:28:09 +00:00
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
}
|