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;
|
2021-01-19 05:14:49 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-12-06 20:53:55 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-04-17 17:21:51 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 20:19:04 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
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;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* BackupStatusController constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private BackupManager $backupManager)
|
2020-04-17 17:21:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles updating the state of a backup.
|
|
|
|
*
|
2021-01-17 23:22:02 +00:00
|
|
|
* @throws \Throwable
|
2020-04-17 17:21:51 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function index(ReportBackupCompleteRequest $request, string $backup): JsonResponse
|
2020-04-17 17:21:51 +00:00
|
|
|
{
|
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
|
|
|
|
2021-07-18 15:46:20 +00:00
|
|
|
if ($model->is_successful) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new BadRequestHttpException('Cannot update the status of a backup that is already marked as completed.');
|
2020-08-28 02:35:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 22:48:35 +00:00
|
|
|
$action = $request->boolean('successful') ? 'server:backup.complete' : 'server:backup.fail';
|
2022-05-29 20:19:04 +00:00
|
|
|
$log = Activity::event($action)->subject($model, $model->server)->property('name', $model->name);
|
2020-04-17 17:21:51 +00:00
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
$log->transaction(function () use ($model, $request) {
|
2021-07-18 15:46:20 +00:00
|
|
|
$successful = $request->boolean('successful');
|
2022-05-29 20:19:04 +00:00
|
|
|
|
2021-01-17 23:22:02 +00:00
|
|
|
$model->fill([
|
|
|
|
'is_successful' => $successful,
|
2021-06-13 17:26:47 +00:00
|
|
|
// Change the lock state to unlocked if this was a failed backup so that it can be
|
|
|
|
// deleted easily. Also does not make sense to have a locked backup on the system
|
|
|
|
// that is failed.
|
|
|
|
'is_locked' => $successful ? $model->is_locked : false,
|
2021-01-17 23:22:02 +00:00
|
|
|
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
|
|
|
|
'bytes' => $successful ? $request->input('size') : 0,
|
|
|
|
'completed_at' => CarbonImmutable::now(),
|
|
|
|
])->save();
|
|
|
|
|
|
|
|
// Check if we are using the s3 backup adapter. If so, make sure we mark the backup as
|
|
|
|
// being completed in S3 correctly.
|
|
|
|
$adapter = $this->backupManager->adapter();
|
2022-10-14 16:59:20 +00:00
|
|
|
if ($adapter instanceof S3Filesystem) {
|
2022-09-26 17:15:52 +00:00
|
|
|
$this->completeMultipartUpload($model, $adapter, $successful, $request->input('parts'));
|
2021-01-17 23:22:02 +00:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2021-01-19 05:14:49 +00:00
|
|
|
/**
|
|
|
|
* Handles toggling the restoration status of a server. The server status field should be
|
|
|
|
* set back to null, even if the restoration failed. This is not an unsolvable state for
|
|
|
|
* the server, and the user can keep trying to restore, or just use the reinstall button.
|
|
|
|
*
|
|
|
|
* The only thing the successful field does is update the entry value for the audit logs
|
|
|
|
* table tracking for this restoration.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-09-26 17:15:52 +00:00
|
|
|
public function restore(Request $request, string $backup): JsonResponse
|
2021-01-19 05:14:49 +00:00
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\Backup $model */
|
|
|
|
$model = Backup::query()->where('uuid', $backup)->firstOrFail();
|
2022-05-29 20:19:04 +00:00
|
|
|
|
|
|
|
$model->server->update(['status' => null]);
|
|
|
|
|
|
|
|
Activity::event($request->boolean('successful') ? 'server:backup.restore-complete' : 'server.backup.restore-failed')
|
|
|
|
->subject($model, $model->server)
|
|
|
|
->property('name', $model->name)
|
|
|
|
->log();
|
2021-01-19 05:14:49 +00:00
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
/**
|
2022-09-26 17:15:52 +00:00
|
|
|
* Marks a multipart upload in a given S3-compatible instance as failed or successful for
|
2020-12-27 19:56:28 +00:00
|
|
|
* the given backup.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function completeMultipartUpload(Backup $backup, S3Filesystem $adapter, bool $successful, ?array $parts): void
|
2020-12-27 19:56:28 +00:00
|
|
|
{
|
|
|
|
// 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)) {
|
2022-09-26 17:15:52 +00:00
|
|
|
// A failed backup doesn't need to error here, this can happen if the backup encounters
|
2020-12-27 19:56:28 +00:00
|
|
|
// an error before we even start the upload. AWS gives you tooling to clear these failed
|
|
|
|
// multipart uploads as needed too.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$successful) {
|
2020-12-27 19:56:28 +00:00
|
|
|
return;
|
2020-12-06 20:53:55 +00:00
|
|
|
}
|
2022-09-26 17:15:52 +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();
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$successful) {
|
2020-12-27 19:56:28 +00:00
|
|
|
$client->execute($client->getCommand('AbortMultipartUpload', $params));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise send a CompleteMultipartUpload request.
|
|
|
|
$params['MultipartUpload'] = [
|
2022-09-26 17:15:52 +00:00
|
|
|
'Parts' => [],
|
2020-12-27 19:56:28 +00:00
|
|
|
];
|
|
|
|
|
2022-09-26 17:15:52 +00:00
|
|
|
if (is_null($parts)) {
|
|
|
|
$params['MultipartUpload']['Parts'] = $client->execute($client->getCommand('ListParts', $params))['Parts'];
|
|
|
|
} else {
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$params['MultipartUpload']['Parts'][] = [
|
|
|
|
'ETag' => $part['etag'],
|
|
|
|
'PartNumber' => $part['part_number'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:56:28 +00:00
|
|
|
$client->execute($client->getCommand('CompleteMultipartUpload', $params));
|
2020-04-17 17:21:51 +00:00
|
|
|
}
|
|
|
|
}
|