2020-04-17 17:21:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
|
|
|
|
|
2020-08-21 04:07:53 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2020-12-06 20:53:55 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-04-17 17:21:51 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-12-06 20:53:55 +00:00
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
2020-12-27 19:56:28 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2020-12-06 20:53:55 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-17 17:21:51 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2020-08-28 02:35:22 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2020-04-17 17:21:51 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;
|
|
|
|
|
|
|
|
class BackupStatusController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-12-06 20:53:55 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $backupManager;
|
|
|
|
|
2020-04-17 17:21:51 +00:00
|
|
|
/**
|
|
|
|
* BackupStatusController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
|
2020-12-06 20:53:55 +00:00
|
|
|
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
|
2020-04-17 17:21:51 +00:00
|
|
|
*/
|
2020-12-06 20:53:55 +00:00
|
|
|
public function __construct(BackupRepository $repository, BackupManager $backupManager)
|
2020-04-17 17:21:51 +00:00
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
2020-12-06 20:53:55 +00:00
|
|
|
$this->backupManager = $backupManager;
|
2020-04-17 17:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles updating the state of a backup.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest $request
|
|
|
|
* @param string $backup
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-08-28 02:35:22 +00:00
|
|
|
*
|
2020-12-06 20:53:55 +00:00
|
|
|
* @throws \Exception
|
2020-04-17 17:21:51 +00:00
|
|
|
*/
|
|
|
|
public function __invoke(ReportBackupCompleteRequest $request, string $backup)
|
|
|
|
{
|
2020-08-28 02:35:22 +00:00
|
|
|
/** @var \Pterodactyl\Models\Backup $model */
|
2020-12-27 19:56:28 +00:00
|
|
|
$model = Backup::query()->where('uuid', $backup)->firstOrFail();
|
2020-08-28 02:35:22 +00:00
|
|
|
|
2020-10-31 23:44:20 +00:00
|
|
|
if (! is_null($model->completed_at)) {
|
2020-08-28 02:35:22 +00:00
|
|
|
throw new BadRequestHttpException(
|
|
|
|
'Cannot update the status of a backup that is already marked as completed.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-28 02:59:01 +00:00
|
|
|
$successful = $request->input('successful') ? true : false;
|
2020-12-06 20:53:55 +00:00
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
$model->fill([
|
2020-08-28 02:59:01 +00:00
|
|
|
'is_successful' => $successful,
|
|
|
|
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
|
|
|
|
'bytes' => $successful ? $request->input('size') : 0,
|
2020-08-21 04:07:53 +00:00
|
|
|
'completed_at' => CarbonImmutable::now(),
|
2020-08-28 02:59:01 +00:00
|
|
|
])->save();
|
2020-04-17 17:21:51 +00:00
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
// Check if we are using the s3 backup adapter. If so, make sure we mark the backup as
|
|
|
|
// being completed in S3 correctly.
|
2020-12-06 20:53:55 +00:00
|
|
|
$adapter = $this->backupManager->adapter();
|
|
|
|
if ($adapter instanceof AwsS3Adapter) {
|
2020-12-27 19:56:28 +00:00
|
|
|
$this->completeMultipartUpload($model, $adapter, $successful);
|
|
|
|
}
|
2020-12-06 20:53:55 +00:00
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
2020-12-06 20:53:55 +00:00
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
/**
|
|
|
|
* Marks a multipart upload in a given S3-compatiable instance as failed or successful for
|
|
|
|
* the given backup.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
|
|
|
* @param \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter
|
|
|
|
* @param bool $successful
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
protected function completeMultipartUpload(Backup $backup, AwsS3Adapter $adapter, bool $successful)
|
|
|
|
{
|
|
|
|
// This should never really happen, but if it does don't let us fall victim to Amazon's
|
|
|
|
// wildly fun error messaging. Just stop the process right here.
|
|
|
|
if (empty($backup->upload_id)) {
|
|
|
|
// A failed backup doesn't need to error here, this can happen if the backup encouters
|
|
|
|
// an error before we even start the upload. AWS gives you tooling to clear these failed
|
|
|
|
// multipart uploads as needed too.
|
2020-12-06 20:53:55 +00:00
|
|
|
if (! $successful) {
|
2020-12-27 19:56:28 +00:00
|
|
|
return;
|
2020-12-06 20:53:55 +00:00
|
|
|
}
|
2020-12-27 19:56:28 +00:00
|
|
|
throw new DisplayException('Cannot complete backup request: no upload_id present on model.');
|
2020-12-06 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
$params = [
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
|
|
|
|
'UploadId' => $backup->upload_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$client = $adapter->getClient();
|
|
|
|
if (! $successful) {
|
|
|
|
$client->execute($client->getCommand('AbortMultipartUpload', $params));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise send a CompleteMultipartUpload request.
|
|
|
|
$params['MultipartUpload'] = [
|
|
|
|
'Parts' => $client->execute($client->getCommand('ListParts', $params))['Parts'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$client->execute($client->getCommand('CompleteMultipartUpload', $params));
|
2020-04-17 17:21:51 +00:00
|
|
|
}
|
|
|
|
}
|