2020-04-10 05:08:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Backups;
|
|
|
|
|
2020-05-10 02:19:45 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-05-10 02:19:45 +00:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2021-05-04 04:26:09 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
|
2020-05-10 02:19:45 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
class DeleteBackupService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository
|
|
|
|
*/
|
|
|
|
private $daemonBackupRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $manager;
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* DeleteBackupService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConnectionInterface $connection,
|
|
|
|
BackupRepository $repository,
|
2020-05-10 02:43:58 +00:00
|
|
|
BackupManager $manager,
|
2020-04-10 05:08:09 +00:00
|
|
|
DaemonBackupRepository $daemonBackupRepository
|
|
|
|
) {
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->daemonBackupRepository = $daemonBackupRepository;
|
|
|
|
$this->connection = $connection;
|
2020-05-10 02:43:58 +00:00
|
|
|
$this->manager = $manager;
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-13 17:26:47 +00:00
|
|
|
* Deletes a backup from the system. If the backup is stored in S3 a request
|
|
|
|
* will be made to delete that backup from the disk as well.
|
2020-04-10 05:08:09 +00:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function handle(Backup $backup)
|
|
|
|
{
|
2021-06-13 17:26:47 +00:00
|
|
|
// If the backup is marked as failed it can still be deleted, even if locked
|
|
|
|
// since the UI doesn't allow you to unlock a failed backup in the first place.
|
|
|
|
//
|
|
|
|
// I also don't really see any reason you'd have a locked, failed backup to keep
|
|
|
|
// around. The logic that updates the backup to the failed state will also remove
|
|
|
|
// the lock, so this condition should really never happen.
|
2021-08-03 21:46:08 +00:00
|
|
|
if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {
|
2021-05-04 04:26:09 +00:00
|
|
|
throw new BackupLockedException();
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
|
|
|
|
$this->deleteFromS3($backup);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
2020-05-10 02:19:45 +00:00
|
|
|
try {
|
|
|
|
$this->daemonBackupRepository->setServer($backup->server)->delete($backup);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
$previous = $exception->getPrevious();
|
|
|
|
// Don't fail the request if the Daemon responds with a 404, just assume the backup
|
|
|
|
// doesn't actually exist and remove it's reference from the Panel as well.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$previous instanceof ClientException || $previous->getResponse()->getStatusCode() !== Response::HTTP_NOT_FOUND) {
|
2020-05-10 02:19:45 +00:00
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
$this->repository->delete($backup->id);
|
|
|
|
});
|
|
|
|
}
|
2020-05-10 02:43:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a backup from an S3 disk.
|
|
|
|
*
|
2020-05-10 03:01:45 +00:00
|
|
|
* @throws \Throwable
|
2020-05-10 02:43:58 +00:00
|
|
|
*/
|
|
|
|
protected function deleteFromS3(Backup $backup)
|
|
|
|
{
|
2020-05-10 03:01:45 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
|
|
|
$this->repository->delete($backup->id);
|
2020-05-10 02:43:58 +00:00
|
|
|
|
2020-05-10 03:01:45 +00:00
|
|
|
/** @var \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter */
|
|
|
|
$adapter = $this->manager->adapter(Backup::ADAPTER_AWS_S3);
|
|
|
|
|
2021-10-30 20:41:38 +00:00
|
|
|
// @phpstan-ignore-next-line this is defined on the actual S3Client class, just not on the interface.
|
2020-05-10 03:01:45 +00:00
|
|
|
$adapter->getClient()->deleteObject([
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
|
|
|
|
]);
|
|
|
|
});
|
2020-05-10 02:43:58 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|