Skip to content

Commit 2a92304

Browse files
committed
Fix server creation logic
1 parent 85b47ce commit 2a92304

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

app/Http/Controllers/Admin/Servers/CreateServerController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ public function index()
111111
*
112112
* @throws \Illuminate\Validation\ValidationException
113113
* @throws \Pterodactyl\Exceptions\DisplayException
114-
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
115114
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
116115
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
117116
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
118117
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
118+
* @throws \Throwable
119119
*/
120120
public function store(ServerFormRequest $request)
121121
{
122122
$server = $this->creationService->handle(
123-
$request->validated()
123+
$request->except(['_token'])
124124
);
125125

126126
$this->alert->success(

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Pterodactyl\Repositories\Eloquent;
44

55
use Pterodactyl\Models\User;
6-
use Webmozart\Assert\Assert;
76
use Pterodactyl\Models\Server;
87
use Illuminate\Support\Collection;
8+
use Illuminate\Database\Eloquent\Builder;
99
use Pterodactyl\Repositories\Concerns\Searchable;
1010
use Illuminate\Database\Eloquent\ModelNotFoundException;
1111
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -273,12 +273,16 @@ public function filterUserAccessServers(User $user, int $level, $paginate = 25)
273273
*/
274274
public function getByUuid(string $uuid): Server
275275
{
276-
Assert::notEmpty($uuid, 'Expected non-empty string as first argument passed to ' . __METHOD__);
277-
278276
try {
279-
return $this->getBuilder()->with('nest', 'node')->where(function ($query) use ($uuid) {
280-
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
281-
})->firstOrFail($this->getColumns());
277+
/** @var \Pterodactyl\Models\Server $model */
278+
$model = $this->getBuilder()
279+
->with('nest', 'node')
280+
->where(function (Builder $query) use ($uuid) {
281+
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
282+
})
283+
->firstOrFail($this->getColumns());
284+
285+
return $model;
282286
} catch (ModelNotFoundException $exception) {
283287
throw new RecordNotFoundException;
284288
}

app/Services/Servers/ServerCreationService.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Pterodactyl\Services\Deployment\FindViableNodesService;
1919
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
2020
use Pterodactyl\Services\Deployment\AllocationSelectionService;
21+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2122

2223
class ServerCreationService
2324
{
@@ -71,6 +72,11 @@ class ServerCreationService
7172
*/
7273
private $daemonServerRepository;
7374

75+
/**
76+
* @var \Pterodactyl\Services\Servers\ServerDeletionService
77+
*/
78+
private $serverDeletionService;
79+
7480
/**
7581
* CreationService constructor.
7682
*
@@ -81,6 +87,7 @@ class ServerCreationService
8187
* @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository
8288
* @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService
8389
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
90+
* @param \Pterodactyl\Services\Servers\ServerDeletionService $serverDeletionService
8491
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
8592
* @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository
8693
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
@@ -93,6 +100,7 @@ public function __construct(
93100
EggRepository $eggRepository,
94101
FindViableNodesService $findViableNodesService,
95102
ServerConfigurationStructureService $configurationStructureService,
103+
ServerDeletionService $serverDeletionService,
96104
ServerRepository $repository,
97105
ServerVariableRepository $serverVariableRepository,
98106
VariableValidatorService $validatorService
@@ -107,6 +115,7 @@ public function __construct(
107115
$this->repository = $repository;
108116
$this->serverVariableRepository = $serverVariableRepository;
109117
$this->daemonServerRepository = $daemonServerRepository;
118+
$this->serverDeletionService = $serverDeletionService;
110119
}
111120

112121
/**
@@ -157,14 +166,26 @@ public function handle(array $data, DeploymentObject $deployment = null): Server
157166

158167
// Create the server and assign any additional allocations to it.
159168
$server = $this->createModel($data);
169+
160170
$this->storeAssignedAllocations($server, $data);
161171
$this->storeEggVariables($server, $eggVariableData);
162172

173+
// Due to the design of the Daemon, we need to persist this server to the disk
174+
// before we can actually create it on the Daemon.
175+
//
176+
// If that connection fails out we will attempt to perform a cleanup by just
177+
// deleting the server itself from the system.
178+
$this->connection->commit();
179+
163180
$structure = $this->configurationStructureService->handle($server);
164181

165-
$this->connection->transaction(function () use ($server, $structure) {
182+
try {
166183
$this->daemonServerRepository->setServer($server)->create($structure);
167-
});
184+
} catch (DaemonConnectionException $exception) {
185+
$this->serverDeletionService->withForce(true)->handle($server);
186+
187+
throw $exception;
188+
}
168189

169190
return $server;
170191
}

0 commit comments

Comments
 (0)