Fix server creation logic

This commit is contained in:
Dane Everitt 2019-12-16 21:02:30 -08:00
parent 85b47ceb79
commit 2a92304023
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 35 additions and 10 deletions

View file

@ -111,16 +111,16 @@ class CreateServerController extends Controller
* *
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
* @throws \Throwable
*/ */
public function store(ServerFormRequest $request) public function store(ServerFormRequest $request)
{ {
$server = $this->creationService->handle( $server = $this->creationService->handle(
$request->validated() $request->except(['_token'])
); );
$this->alert->success( $this->alert->success(

View file

@ -3,9 +3,9 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Concerns\Searchable; use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@ -273,12 +273,16 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
*/ */
public function getByUuid(string $uuid): Server public function getByUuid(string $uuid): Server
{ {
Assert::notEmpty($uuid, 'Expected non-empty string as first argument passed to ' . __METHOD__);
try { try {
return $this->getBuilder()->with('nest', 'node')->where(function ($query) use ($uuid) { /** @var \Pterodactyl\Models\Server $model */
$model = $this->getBuilder()
->with('nest', 'node')
->where(function (Builder $query) use ($uuid) {
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid); $query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
})->firstOrFail($this->getColumns()); })
->firstOrFail($this->getColumns());
return $model;
} catch (ModelNotFoundException $exception) { } catch (ModelNotFoundException $exception) {
throw new RecordNotFoundException; throw new RecordNotFoundException;
} }

View file

@ -18,6 +18,7 @@ use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Services\Deployment\FindViableNodesService; use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Services\Deployment\AllocationSelectionService; use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class ServerCreationService class ServerCreationService
{ {
@ -71,6 +72,11 @@ class ServerCreationService
*/ */
private $daemonServerRepository; private $daemonServerRepository;
/**
* @var \Pterodactyl\Services\Servers\ServerDeletionService
*/
private $serverDeletionService;
/** /**
* CreationService constructor. * CreationService constructor.
* *
@ -81,6 +87,7 @@ class ServerCreationService
* @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository * @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository
* @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService * @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Services\Servers\ServerDeletionService $serverDeletionService
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository * @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
@ -93,6 +100,7 @@ class ServerCreationService
EggRepository $eggRepository, EggRepository $eggRepository,
FindViableNodesService $findViableNodesService, FindViableNodesService $findViableNodesService,
ServerConfigurationStructureService $configurationStructureService, ServerConfigurationStructureService $configurationStructureService,
ServerDeletionService $serverDeletionService,
ServerRepository $repository, ServerRepository $repository,
ServerVariableRepository $serverVariableRepository, ServerVariableRepository $serverVariableRepository,
VariableValidatorService $validatorService VariableValidatorService $validatorService
@ -107,6 +115,7 @@ class ServerCreationService
$this->repository = $repository; $this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository; $this->serverVariableRepository = $serverVariableRepository;
$this->daemonServerRepository = $daemonServerRepository; $this->daemonServerRepository = $daemonServerRepository;
$this->serverDeletionService = $serverDeletionService;
} }
/** /**
@ -157,14 +166,26 @@ class ServerCreationService
// Create the server and assign any additional allocations to it. // Create the server and assign any additional allocations to it.
$server = $this->createModel($data); $server = $this->createModel($data);
$this->storeAssignedAllocations($server, $data); $this->storeAssignedAllocations($server, $data);
$this->storeEggVariables($server, $eggVariableData); $this->storeEggVariables($server, $eggVariableData);
// Due to the design of the Daemon, we need to persist this server to the disk
// before we can actually create it on the Daemon.
//
// If that connection fails out we will attempt to perform a cleanup by just
// deleting the server itself from the system.
$this->connection->commit();
$structure = $this->configurationStructureService->handle($server); $structure = $this->configurationStructureService->handle($server);
$this->connection->transaction(function () use ($server, $structure) { try {
$this->daemonServerRepository->setServer($server)->create($structure); $this->daemonServerRepository->setServer($server)->create($structure);
}); } catch (DaemonConnectionException $exception) {
$this->serverDeletionService->withForce(true)->handle($server);
throw $exception;
}
return $server; return $server;
} }