misc_pterodactyl-panel/app/Services/Servers/ServerDeletionService.php

115 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Servers;
use Exception;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class ServerDeletionService
{
/**
* @var bool
*/
protected $force = false;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $databaseManagementService;
/**
* DeletionService constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
*/
public function __construct(
2017-08-13 19:55:09 +00:00
ConnectionInterface $connection,
DaemonServerRepository $daemonServerRepository,
DatabaseManagementService $databaseManagementService
) {
2017-08-13 19:55:09 +00:00
$this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository;
$this->databaseManagementService = $databaseManagementService;
}
/**
* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.
*
2017-08-22 03:10:48 +00:00
* @param bool $bool
* @return $this
*/
public function withForce($bool = true)
{
$this->force = $bool;
return $this;
}
/**
* Delete a server from the panel and remove any associated databases from hosts.
*
* @param \Pterodactyl\Models\Server $server
2017-08-13 19:55:09 +00:00
*
* @throws \Throwable
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function handle(Server $server)
{
try {
$this->daemonServerRepository->setServer($server)->delete();
2019-12-22 23:08:11 +00:00
} 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 Wings so there
// is no reason we need to bail out from that.
if (! $this->force && $exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
2019-12-22 23:08:11 +00:00
throw $exception;
}
Log::warning($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.
//
// @see https://github.com/pterodactyl/panel/issues/2085
$database->delete();
Log::warning($exception);
}
}
$server->delete();
});
}
}