2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2020-06-24 03:26:48 +00:00
|
|
|
use Exception;
|
2020-10-09 04:08:55 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-10-09 04:08:55 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerDeletionService
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
protected bool $force = false;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* ServerDeletionService constructor.
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private DaemonServerRepository $daemonServerRepository,
|
|
|
|
private DatabaseManagementService $databaseManagementService
|
2017-07-25 02:34:10 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function withForce(bool $bool = true): self
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
|
|
|
$this->force = $bool;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a server from the panel and remove any associated databases from hosts.
|
|
|
|
*
|
2019-11-25 04:19:31 +00:00
|
|
|
* @throws \Throwable
|
2017-07-25 02:34:10 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(Server $server): void
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->setServer($server)->delete();
|
2019-12-22 23:08:11 +00:00
|
|
|
} catch (DaemonConnectionException $exception) {
|
2020-10-09 04:08:55 +00:00
|
|
|
// 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.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->force && $exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
|
2019-12-22 23:08:11 +00:00
|
|
|
throw $exception;
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
Log::warning($exception);
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->connection->transaction(function () use ($server) {
|
2020-10-09 04:08:55 +00:00
|
|
|
foreach ($server->databases as $database) {
|
2020-06-24 03:26:48 +00:00
|
|
|
try {
|
2020-10-09 04:08:55 +00:00
|
|
|
$this->databaseManagementService->delete($database);
|
2020-06-24 03:26:48 +00:00
|
|
|
} catch (Exception $exception) {
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->force) {
|
2020-06-24 03:26:48 +00:00
|
|
|
throw $exception;
|
|
|
|
}
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
// 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);
|
2020-06-24 03:26:48 +00:00
|
|
|
}
|
2020-10-09 04:08:55 +00:00
|
|
|
}
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2020-10-09 04:08:55 +00:00
|
|
|
$server->delete();
|
2019-11-25 04:19:31 +00:00
|
|
|
});
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|