2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-25 02:34:10 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
|
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var bool
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
protected $force = false;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $daemonServerRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $databaseRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $databaseManagementService;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-25 04:19:31 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $repository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-13 20:40:31 +00:00
|
|
|
* @var \Psr\Log\LoggerInterface
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
private $writer;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DeletionService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2019-11-25 04:19:31 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\DatabaseRepository $databaseRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
|
2019-11-25 04:19:31 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Psr\Log\LoggerInterface $writer
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-08-13 19:55:09 +00:00
|
|
|
ConnectionInterface $connection,
|
2019-11-25 04:19:31 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository,
|
|
|
|
DatabaseRepository $databaseRepository,
|
2017-07-25 02:34:10 +00:00
|
|
|
DatabaseManagementService $databaseManagementService,
|
2019-11-25 04:19:31 +00:00
|
|
|
ServerRepository $repository,
|
|
|
|
LoggerInterface $writer
|
2017-07-25 02:34:10 +00:00
|
|
|
) {
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection = $connection;
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->databaseRepository = $databaseRepository;
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->databaseManagementService = $databaseManagementService;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
$this->writer = $writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-07-25 02:34:10 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2019-11-25 04:19:31 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2017-08-13 19:55:09 +00:00
|
|
|
*
|
2019-11-25 04:19:31 +00:00
|
|
|
* @throws \Throwable
|
2017-07-25 02:34:10 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2019-11-25 04:19:31 +00:00
|
|
|
public function handle(Server $server)
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->setServer($server)->delete();
|
2017-07-25 02:34:10 +00:00
|
|
|
} catch (RequestException $exception) {
|
|
|
|
$response = $exception->getResponse();
|
|
|
|
|
|
|
|
if (is_null($response) || (! is_null($response) && $response->getStatusCode() !== 404)) {
|
|
|
|
// If not forcing the deletion, throw an exception, otherwise just log it and
|
|
|
|
// continue with server deletion process in the panel.
|
|
|
|
if (! $this->force) {
|
2018-01-28 23:14:14 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
} else {
|
|
|
|
$this->writer->warning($exception);
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->connection->transaction(function () use ($server) {
|
|
|
|
$this->databaseRepository->setColumns('id')->findWhere([['server_id', '=', $server->id]])->each(function ($item) {
|
|
|
|
$this->databaseManagementService->delete($item->id);
|
|
|
|
});
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->repository->delete($server->id);
|
|
|
|
});
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|