2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-07-25 02:34:10 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2018-05-13 20:40:31 +00:00
|
|
|
use Psr\Log\LoggerInterface as Writer;
|
2017-07-25 02:34:10 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerDeletionService
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-08-13 19:55:09 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2017-08-13 19:55:09 +00:00
|
|
|
protected $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-13 19:55:09 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2017-08-13 19:55:09 +00:00
|
|
|
protected $daemonServerRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-19 03:32:19 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
protected $databaseManagementService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $databaseRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $force = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2018-05-13 20:40:31 +00:00
|
|
|
* @var \Psr\Log\LoggerInterface
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
protected $writer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DeletionService constructor.
|
|
|
|
*
|
2017-08-13 19:55:09 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-07-25 02:34:10 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
|
2017-10-19 03:32:19 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
|
2017-07-25 02:34:10 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
2018-05-13 20:40:31 +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,
|
2017-07-25 02:34:10 +00:00
|
|
|
DaemonServerRepositoryInterface $daemonServerRepository,
|
|
|
|
DatabaseRepositoryInterface $databaseRepository,
|
|
|
|
DatabaseManagementService $databaseManagementService,
|
|
|
|
ServerRepositoryInterface $repository,
|
|
|
|
Writer $writer
|
|
|
|
) {
|
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection = $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->databaseManagementService = $databaseManagementService;
|
|
|
|
$this->databaseRepository = $databaseRepository;
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @param int|\Pterodactyl\Models\Server $server
|
2017-08-13 19:55:09 +00:00
|
|
|
*
|
2017-07-25 02:34:10 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
public function handle($server)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection->beginTransaction();
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->databaseRepository->setColumns('id')->findWhere([['server_id', '=', $server->id]])->each(function ($item) {
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->databaseManagementService->delete($item->id);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->repository->delete($server->id);
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection->commit();
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|