Path: blob/1.0-develop/app/Services/Servers/ServerDeletionService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Illuminate\Http\Response;5use Pterodactyl\Models\Server;6use Illuminate\Support\Facades\Log;7use Illuminate\Database\ConnectionInterface;8use Pterodactyl\Repositories\Wings\DaemonServerRepository;9use Pterodactyl\Services\Databases\DatabaseManagementService;10use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1112class ServerDeletionService13{14protected bool $force = false;1516/**17* ServerDeletionService constructor.18*/19public function __construct(20private ConnectionInterface $connection,21private DaemonServerRepository $daemonServerRepository,22private DatabaseManagementService $databaseManagementService,23) {24}2526/**27* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.28*/29public function withForce(bool $bool = true): self30{31$this->force = $bool;3233return $this;34}3536/**37* Delete a server from the panel, clear any allocation notes, and remove any associated databases from hosts.38*39* @throws \Throwable40* @throws \Pterodactyl\Exceptions\DisplayException41*/42public function handle(Server $server): void43{44try {45$this->daemonServerRepository->setServer($server)->delete();46} catch (DaemonConnectionException $exception) {47// If there is an error not caused a 404 error and this isn't a forced delete,48// go ahead and bail out. We specifically ignore a 404 since that can be assumed49// to be a safe error, meaning the server doesn't exist at all on Wings so there50// is no reason we need to bail out from that.51if (!$this->force && $exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {52throw $exception;53}5455Log::warning($exception);56}5758$this->connection->transaction(function () use ($server) {59foreach ($server->databases as $database) {60try {61$this->databaseManagementService->delete($database);62} catch (\Exception $exception) {63if (!$this->force) {64throw $exception;65}6667// Oh well, just try to delete the database entry we have from the database68// so that the server itself can be deleted. This will leave it dangling on69// the host instance, but we couldn't delete it anyways so not sure how we would70// handle this better anyways.71//72// @see https://github.com/pterodactyl/panel/issues/208573$database->delete();7475Log::warning($exception);76}77}7879// clear any allocation notes for the server80$server->allocations()->update(['notes' => null]);818283$server->delete();84});85}86}878889