diff --git a/app/Http/Controllers/Admin/Servers/CreateServerController.php b/app/Http/Controllers/Admin/Servers/CreateServerController.php index eb8f74da6..f63cf814b 100644 --- a/app/Http/Controllers/Admin/Servers/CreateServerController.php +++ b/app/Http/Controllers/Admin/Servers/CreateServerController.php @@ -111,16 +111,16 @@ class CreateServerController extends Controller * * @throws \Illuminate\Validation\ValidationException * @throws \Pterodactyl\Exceptions\DisplayException - * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException + * @throws \Throwable */ public function store(ServerFormRequest $request) { $server = $this->creationService->handle( - $request->validated() + $request->except(['_token']) ); $this->alert->success( diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php index 88556234a..874b1cc95 100644 --- a/app/Repositories/Eloquent/ServerRepository.php +++ b/app/Repositories/Eloquent/ServerRepository.php @@ -3,9 +3,9 @@ namespace Pterodactyl\Repositories\Eloquent; use Pterodactyl\Models\User; -use Webmozart\Assert\Assert; use Pterodactyl\Models\Server; use Illuminate\Support\Collection; +use Illuminate\Database\Eloquent\Builder; use Pterodactyl\Repositories\Concerns\Searchable; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Contracts\Pagination\LengthAwarePaginator; @@ -273,12 +273,16 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt */ public function getByUuid(string $uuid): Server { - Assert::notEmpty($uuid, 'Expected non-empty string as first argument passed to ' . __METHOD__); - try { - return $this->getBuilder()->with('nest', 'node')->where(function ($query) use ($uuid) { - $query->where('uuidShort', $uuid)->orWhere('uuid', $uuid); - })->firstOrFail($this->getColumns()); + /** @var \Pterodactyl\Models\Server $model */ + $model = $this->getBuilder() + ->with('nest', 'node') + ->where(function (Builder $query) use ($uuid) { + $query->where('uuidShort', $uuid)->orWhere('uuid', $uuid); + }) + ->firstOrFail($this->getColumns()); + + return $model; } catch (ModelNotFoundException $exception) { throw new RecordNotFoundException; } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index a76787933..9cc1535a4 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -18,6 +18,7 @@ use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Services\Deployment\FindViableNodesService; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Services\Deployment\AllocationSelectionService; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; class ServerCreationService { @@ -71,6 +72,11 @@ class ServerCreationService */ private $daemonServerRepository; + /** + * @var \Pterodactyl\Services\Servers\ServerDeletionService + */ + private $serverDeletionService; + /** * CreationService constructor. * @@ -81,6 +87,7 @@ class ServerCreationService * @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository * @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService + * @param \Pterodactyl\Services\Servers\ServerDeletionService $serverDeletionService * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository * @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService @@ -93,6 +100,7 @@ class ServerCreationService EggRepository $eggRepository, FindViableNodesService $findViableNodesService, ServerConfigurationStructureService $configurationStructureService, + ServerDeletionService $serverDeletionService, ServerRepository $repository, ServerVariableRepository $serverVariableRepository, VariableValidatorService $validatorService @@ -107,6 +115,7 @@ class ServerCreationService $this->repository = $repository; $this->serverVariableRepository = $serverVariableRepository; $this->daemonServerRepository = $daemonServerRepository; + $this->serverDeletionService = $serverDeletionService; } /** @@ -157,14 +166,26 @@ class ServerCreationService // Create the server and assign any additional allocations to it. $server = $this->createModel($data); + $this->storeAssignedAllocations($server, $data); $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); - $this->connection->transaction(function () use ($server, $structure) { + try { $this->daemonServerRepository->setServer($server)->create($structure); - }); + } catch (DaemonConnectionException $exception) { + $this->serverDeletionService->withForce(true)->handle($server); + + throw $exception; + } return $server; }