Hit the daemon endpoints to actually generate a backup

This commit is contained in:
Dane Everitt 2020-04-04 20:09:33 -07:00
parent be05d2df81
commit 7b69b4b824
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 63 additions and 12 deletions

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Repositories\Wings; namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
@ -10,6 +11,33 @@ use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class DaemonBackupRepository extends DaemonRepository class DaemonBackupRepository extends DaemonRepository
{ {
/**
* Tells the remote Daemon to begin generating a backup for the server.
*
* @param \Pterodactyl\Models\Backup $backup
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function backup(Backup $backup): ResponseInterface
{
Assert::isInstanceOf($this->server, Server::class);
try {
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/backup', $this->server->uuid),
[
'json' => [
'uuid' => $backup->uuid,
'ignored_files' => explode(PHP_EOL, $backup->ignored_files),
],
]
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/** /**
* Returns a stream of a backup's contents from the Wings instance so that we * Returns a stream of a backup's contents from the Wings instance so that we
* do not need to send the user directly to the Daemon. * do not need to send the user directly to the Daemon.

View file

@ -6,7 +6,9 @@ use Ramsey\Uuid\Uuid;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
use Pterodactyl\Models\Backup; use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Eloquent\BackupRepository; use Pterodactyl\Repositories\Eloquent\BackupRepository;
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
class InitiateBackupService class InitiateBackupService
{ {
@ -20,14 +22,31 @@ class InitiateBackupService
*/ */
private $repository; private $repository;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository
*/
private $daemonBackupRepository;
/** /**
* InitiateBackupService constructor. * InitiateBackupService constructor.
* *
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository * @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonBackupRepository $daemonBackupRepository
*/ */
public function __construct(BackupRepository $repository) public function __construct(
{ BackupRepository $repository,
ConnectionInterface $connection,
DaemonBackupRepository $daemonBackupRepository
) {
$this->repository = $repository; $this->repository = $repository;
$this->connection = $connection;
$this->daemonBackupRepository = $daemonBackupRepository;
} }
/** /**
@ -50,19 +69,23 @@ class InitiateBackupService
* @param string|null $name * @param string|null $name
* @return \Pterodactyl\Models\Backup * @return \Pterodactyl\Models\Backup
* *
* @throws \Exception * @throws \Throwable
*/ */
public function handle(Server $server, string $name = null): Backup public function handle(Server $server, string $name = null): Backup
{ {
/** @var \Pterodactyl\Models\Backup $backup */ return $this->connection->transaction(function () use ($server, $name) {
$backup = $this->repository->create([ /** @var \Pterodactyl\Models\Backup $backup */
'server_id' => $server->id, $backup = $this->repository->create([
'uuid' => Uuid::uuid4()->toString(), 'server_id' => $server->id,
'name' => trim($name) ?: sprintf('Backup at %s', CarbonImmutable::now()->toDateTimeString()), 'uuid' => Uuid::uuid4()->toString(),
'ignored_files' => $this->ignoredFiles ?? '', 'name' => trim($name) ?: sprintf('Backup at %s', CarbonImmutable::now()->toDateTimeString()),
'disk' => 'local', 'ignored_files' => $this->ignoredFiles ?? '',
], true, true); 'disk' => 'local',
], true, true);
return $backup; $this->daemonBackupRepository->setServer($server)->backup($backup);
return $backup;
});
} }
} }