Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DaemonConnectionException #885

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Console/Commands/Server/BulkPowerActionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Factory as ValidatorFactory;
use App\Repositories\Daemon\DaemonPowerRepository;
use App\Exceptions\Http\Connection\DaemonConnectionException;
use Exception;

class BulkPowerActionCommand extends Command
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public function handle(): void

try {
$powerRepository->setServer($server)->send($action);
} catch (DaemonConnectionException $exception) {
} catch (Exception $exception) {
$this->output->error(trans('command/messages.server.power.action_failed', [
'name' => $server->name,
'id' => $server->id,
Expand Down
73 changes: 0 additions & 73 deletions app/Exceptions/Http/Connection/DaemonConnectionException.php

This file was deleted.

26 changes: 15 additions & 11 deletions app/Filament/Server/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Filament\Server\Pages;

use App\Enums\ServerState;
use App\Exceptions\Http\Connection\DaemonConnectionException;
use App\Facades\Activity;
use App\Models\Permission;
use App\Models\Server;
use App\Repositories\Daemon\DaemonServerRepository;
use Exception;
use Filament\Facades\Filament;
use Filament\Forms\Components\Actions\Action;
Expand All @@ -18,8 +18,6 @@
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Support\Enums\Alignment;
use GuzzleHttp\Exception\TransferException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Number;

class Settings extends ServerFormPage
Expand Down Expand Up @@ -202,25 +200,31 @@ public function form(Form $form): Form
->modalHeading('Are you sure you want to reinstall the server?')
->modalDescription('Some files may be deleted or modified during this process, please back up your data before continuing.')
->modalSubmitActionLabel('Yes, Reinstall')
->action(function (Server $server) {
->action(function (Server $server, DaemonServerRepository $serverRepository) {
abort_unless(auth()->user()->can(Permission::ACTION_SETTINGS_REINSTALL, $server), 403);

$server->fill(['status' => ServerState::Installing])->save();

try {
Http::daemon($server->node)->post(sprintf(
'/api/servers/%s/reinstall',
$server->uuid
));
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
$serverRepository->reinstall();
} catch (Exception $exception) {
report($exception);

Notification::make()
->danger()
->title('Server Reinstall failed')
->body($exception->getMessage())
->send();

return;
}

Activity::event('server:settings.reinstall')
->log();

Notification::make()
->success()
->title('Server Reinstall Started')
->title('Server Reinstall started')
->send();
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ protected function returnNoContent(): Response
{
return new Response('', Response::HTTP_NO_CONTENT);
}

/**
* Return an HTTP/406 response for the API.
*/
protected function returnNotAcceptable(): Response
{
return new Response('', Response::HTTP_NOT_ACCEPTABLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Services\Servers\ReinstallServerService;
use App\Services\Servers\SuspensionService;
use App\Services\Servers\TransferServerService;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Response;

class ServerManagementController extends ApplicationApiController
Expand Down Expand Up @@ -80,19 +81,19 @@ public function startTransfer(ServerWriteRequest $request, Server $server): Resp
}

// Node was not viable
return new Response('', Response::HTTP_NOT_ACCEPTABLE);
return $this->returnNotAcceptable();
}

/**
* Cancels a transfer of a server to a new node.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function cancelTransfer(ServerWriteRequest $request, Server $server): Response
{
if (!$transfer = $server->transfer) {
// Server is not transferring
return new Response('', Response::HTTP_NOT_ACCEPTABLE);
return $this->returnNotAcceptable();
}

$transfer->successful = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace App\Http\Controllers\Api\Application\Servers;

use App\Exceptions\Model\DataValidationException;
use App\Models\User;
use App\Models\Server;
use App\Services\Servers\StartupModificationService;
use App\Transformers\Api\Application\ServerTransformer;
use App\Http\Controllers\Api\Application\ApplicationApiController;
use App\Http\Requests\Api\Application\Servers\UpdateServerStartupRequest;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Validation\ValidationException;

class StartupController extends ApplicationApiController
{
Expand All @@ -22,9 +25,9 @@ public function __construct(private StartupModificationService $modificationServ
/**
* Update the startup and environment settings for a specific server.
*
* @throws \Illuminate\Validation\ValidationException
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws \App\Exceptions\Model\DataValidationException
* @throws ValidationException
* @throws ConnectionException
* @throws DataValidationException
*/
public function index(UpdateServerStartupRequest $request, Server $server): array
{
Expand Down
16 changes: 8 additions & 8 deletions app/Http/Controllers/Api/Client/Servers/CommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Http\Controllers\Api\Client\ClientApiController;
use App\Http\Requests\Api\Client\Servers\SendCommandRequest;
use App\Exceptions\Http\Connection\DaemonConnectionException;
use Exception;
use Illuminate\Http\Client\ConnectionException;

class CommandController extends ClientApiController
{
/**
* Send a command to a running server.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function index(SendCommandRequest $request, Server $server): Response
{
try {
$server->send($request->input('command'));
} catch (DaemonConnectionException $exception) {
} catch (Exception $exception) {
$previous = $exception->getPrevious();

if ($previous instanceof BadResponseException) {
if (
$previous->getResponse() instanceof ResponseInterface
&& $previous->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY
) {
if ($previous->getResponse() instanceof ResponseInterface && $previous->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY) {
throw new HttpException(Response::HTTP_BAD_GATEWAY, 'Server must be online in order to send commands.', $exception);
}
}

throw $exception;
}

Activity::event('server:console.command')->property('command', $request->input('command'))->log();
Activity::event('server:console.command')
->property('command', $request->input('command'))
->log();

return $this->returnNoContent();
}
Expand Down
31 changes: 20 additions & 11 deletions app/Http/Controllers/Api/Client/Servers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Http\Requests\Api\Client\Servers\Files\DecompressFilesRequest;
use App\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
use App\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest;
use Illuminate\Http\Client\ConnectionException;

class FileController extends ClientApiController
{
Expand All @@ -38,7 +39,7 @@ public function __construct(
/**
* Returns a listing of files in a given directory.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function directory(ListFilesRequest $request, Server $server): array
{
Expand All @@ -63,7 +64,9 @@ public function contents(GetFileContentsRequest $request, Server $server): Respo
config('panel.files.max_edit_size')
);

Activity::event('server:file.read')->property('file', $request->get('file'))->log();
Activity::event('server:file.read')
->property('file', $request->get('file'))
->log();

return new Response($response, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
Expand Down Expand Up @@ -102,13 +105,17 @@ public function download(GetFileContentsRequest $request, Server $server): array
/**
* Writes the contents of the specified file to the server.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function write(WriteFileContentRequest $request, Server $server): JsonResponse
{
$this->fileRepository->setServer($server)->putContent($request->get('file'), $request->getContent());
$this->fileRepository
->setServer($server)
->putContent($request->get('file'), $request->getContent());

Activity::event('server:file.write')->property('file', $request->get('file'))->log();
Activity::event('server:file.write')
->property('file', $request->get('file'))
->log();

return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
Expand Down Expand Up @@ -154,21 +161,23 @@ public function rename(RenameFileRequest $request, Server $server): JsonResponse
/**
* Copies a file on the server.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function copy(CopyFileRequest $request, Server $server): JsonResponse
{
$this->fileRepository
->setServer($server)
->copyFile($request->input('location'));

Activity::event('server:file.copy')->property('file', $request->input('location'))->log();
Activity::event('server:file.copy')
->property('file', $request->input('location'))
->log();

return new JsonResponse([], Response::HTTP_NO_CONTENT);
}

/**
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function compress(CompressFilesRequest $request, Server $server): array
{
Expand All @@ -188,7 +197,7 @@ public function compress(CompressFilesRequest $request, Server $server): array
}

/**
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function decompress(DecompressFilesRequest $request, Server $server): JsonResponse
{
Expand All @@ -210,7 +219,7 @@ public function decompress(DecompressFilesRequest $request, Server $server): Jso
/**
* Deletes files or folders for the server in the given root directory.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function delete(DeleteFileRequest $request, Server $server): JsonResponse
{
Expand All @@ -230,7 +239,7 @@ public function delete(DeleteFileRequest $request, Server $server): JsonResponse
/**
* Updates file permissions for file(s) in the given root directory.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function chmod(ChmodFilesRequest $request, Server $server): JsonResponse
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Repositories\Daemon\DaemonServerRepository;
use App\Http\Controllers\Api\Client\ClientApiController;
use App\Http\Requests\Api\Client\Servers\GetServerRequest;
use Illuminate\Http\Client\ConnectionException;

class ResourceUtilizationController extends ClientApiController
{
Expand All @@ -25,7 +26,7 @@ public function __construct(private Repository $cache, private DaemonServerRepos
* 20 seconds at a time to ensure that repeated requests to this endpoint do not cause
* a flood of unnecessary API calls.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws ConnectionException
*/
public function __invoke(GetServerRequest $request, Server $server): array
{
Expand Down
Loading
Loading