pelican-panel-mirror/app/Services/Servers/ServerDeletionService.php
Boy132 d555c42644
Update all dependencies (#712)
* update composer.lock

* run pint

* fix phpstan

* update migrations (sqlite `dropForeign`)

* fix migrations

* Reset these back for now

* Alphabetize the rules

* run `php artisan filament:upgrade`

---------

Co-authored-by: Lance Pioch <git@lance.sh>
2024-11-22 09:27:57 +01:00

85 lines
2.8 KiB
PHP

<?php
namespace App\Services\Servers;
use Exception;
use Illuminate\Http\Response;
use App\Models\Server;
use Illuminate\Database\ConnectionInterface;
use App\Repositories\Daemon\DaemonServerRepository;
use App\Services\Databases\DatabaseManagementService;
use App\Exceptions\Http\Connection\DaemonConnectionException;
class ServerDeletionService
{
protected bool $force = false;
/**
* ServerDeletionService constructor.
*/
public function __construct(
private ConnectionInterface $connection,
private DaemonServerRepository $daemonServerRepository,
private DatabaseManagementService $databaseManagementService
) {}
/**
* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.
*/
public function withForce(bool $bool = true): self
{
$this->force = $bool;
return $this;
}
/**
* Delete a server from the panel and remove any associated databases from hosts.
*
* @throws \Throwable
* @throws \App\Exceptions\DisplayException
*/
public function handle(Server $server): void
{
try {
$this->daemonServerRepository->setServer($server)->delete();
} catch (DaemonConnectionException $exception) {
// If there is an error not caused a 404 error and this isn't a forced delete,
// go ahead and bail out. We specifically ignore a 404 since that can be assumed
// to be a safe error, meaning the server doesn't exist at all on daemon so there
// is no reason we need to bail out from that.
if (!$this->force && $exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
throw $exception;
}
logger()->warning($exception);
} catch (Exception $exception) {
report($exception);
}
$this->connection->transaction(function () use ($server) {
foreach ($server->databases as $database) {
try {
$this->databaseManagementService->delete($database);
} catch (\Exception $exception) {
if (!$this->force) {
throw $exception;
}
// Oh well, just try to delete the database entry we have from the database
// so that the server itself can be deleted. This will leave it dangling on
// the host instance, but we couldn't delete it anyways so not sure how we would
// handle this better anyways.
$database->delete();
logger()->warning($exception);
}
}
$server->allocations()->update(['server_id' => null]);
$server->delete();
});
}
}