Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/ServerDeletionService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Server;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Database\ConnectionInterface;
9
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
10
use Pterodactyl\Services\Databases\DatabaseManagementService;
11
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
12
13
class ServerDeletionService
14
{
15
protected bool $force = false;
16
17
/**
18
* ServerDeletionService constructor.
19
*/
20
public function __construct(
21
private ConnectionInterface $connection,
22
private DaemonServerRepository $daemonServerRepository,
23
private DatabaseManagementService $databaseManagementService,
24
) {
25
}
26
27
/**
28
* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.
29
*/
30
public function withForce(bool $bool = true): self
31
{
32
$this->force = $bool;
33
34
return $this;
35
}
36
37
/**
38
* Delete a server from the panel, clear any allocation notes, and remove any associated databases from hosts.
39
*
40
* @throws \Throwable
41
* @throws \Pterodactyl\Exceptions\DisplayException
42
*/
43
public function handle(Server $server): void
44
{
45
try {
46
$this->daemonServerRepository->setServer($server)->delete();
47
} catch (DaemonConnectionException $exception) {
48
// If there is an error not caused a 404 error and this isn't a forced delete,
49
// go ahead and bail out. We specifically ignore a 404 since that can be assumed
50
// to be a safe error, meaning the server doesn't exist at all on Wings so there
51
// is no reason we need to bail out from that.
52
if (!$this->force && $exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
53
throw $exception;
54
}
55
56
Log::warning($exception);
57
}
58
59
$this->connection->transaction(function () use ($server) {
60
foreach ($server->databases as $database) {
61
try {
62
$this->databaseManagementService->delete($database);
63
} catch (\Exception $exception) {
64
if (!$this->force) {
65
throw $exception;
66
}
67
68
// Oh well, just try to delete the database entry we have from the database
69
// so that the server itself can be deleted. This will leave it dangling on
70
// the host instance, but we couldn't delete it anyways so not sure how we would
71
// handle this better anyways.
72
//
73
// @see https://github.com/pterodactyl/panel/issues/2085
74
$database->delete();
75
76
Log::warning($exception);
77
}
78
}
79
80
// clear any allocation notes for the server
81
$server->allocations()->update(['notes' => null]);
82
83
84
$server->delete();
85
});
86
}
87
}
88
89