forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerCreationService.php
More file actions
216 lines (189 loc) · 8.39 KB
/
ServerCreationService.php
File metadata and controls
216 lines (189 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Services\Deployment\AllocationSelectionService;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class ServerCreationService
{
/**
* ServerCreationService constructor.
*/
public function __construct(
private AllocationSelectionService $allocationSelectionService,
private ConnectionInterface $connection,
private DaemonServerRepository $daemonServerRepository,
private FindViableNodesService $findViableNodesService,
private ServerRepository $repository,
private ServerDeletionService $serverDeletionService,
private ServerVariableRepository $serverVariableRepository,
private VariableValidatorService $validatorService,
) {
}
/**
* Create a server on the Panel and trigger a request to the Daemon to begin the server
* creation process. This function will attempt to set as many additional values
* as possible given the input data. For example, if an allocation_id is passed with
* no node_id the node_is will be picked from the allocation.
*
* @throws \Throwable
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Illuminate\Validation\ValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
*/
public function handle(array $data, ?DeploymentObject $deployment = null): Server
{
// If a deployment object has been passed we need to get the allocation
// that the server should use, and assign the node from that allocation.
if ($deployment instanceof DeploymentObject) {
$allocation = $this->configureDeployment($data, $deployment);
$data['allocation_id'] = $allocation->id;
$data['node_id'] = $allocation->node_id;
}
// Auto-configure the node based on the selected allocation
// if no node was defined.
if (empty($data['node_id'])) {
Assert::false(empty($data['allocation_id']), 'Expected a non-empty allocation_id in server creation data.');
$data['node_id'] = Allocation::query()->findOrFail($data['allocation_id'])->node_id;
}
if (empty($data['nest_id'])) {
Assert::false(empty($data['egg_id']), 'Expected a non-empty egg_id in server creation data.');
$data['nest_id'] = Egg::query()->findOrFail($data['egg_id'])->nest_id;
}
$eggVariableData = $this->validatorService
->setUserLevel(User::USER_LEVEL_ADMIN)
->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
// 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.
/** @var Server $server */
$server = $this->connection->transaction(function () use ($data, $eggVariableData) {
// Create the server and assign any additional allocations to it.
$server = $this->createModel($data);
$this->storeAssignedAllocations($server, $data);
$this->storeEggVariables($server, $eggVariableData);
return $server;
}, 5);
try {
$this->daemonServerRepository->setServer($server)->create(
Arr::get($data, 'start_on_completion', false) ?? false
);
} catch (DaemonConnectionException $exception) {
$this->serverDeletionService->withForce()->handle($server);
throw $exception;
}
return $server;
}
/**
* Gets an allocation to use for automatic deployment.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
private function configureDeployment(array $data, DeploymentObject $deployment): Allocation
{
/** @var Collection $nodes */
$nodes = $this->findViableNodesService->setLocations($deployment->getLocations())
->setDisk(Arr::get($data, 'disk'))
->setMemory(Arr::get($data, 'memory'))
->handle();
return $this->allocationSelectionService->setDedicated($deployment->isDedicated())
->setNodes($nodes->pluck('id')->toArray())
->setPorts($deployment->getPorts())
->handle();
}
/**
* Store the server in the database and return the model.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
private function createModel(array $data): Server
{
$uuid = $this->generateUniqueUuidCombo();
/** @var Server $model */
$model = $this->repository->create([
'external_id' => Arr::get($data, 'external_id'),
'uuid' => $uuid,
'uuidShort' => substr($uuid, 0, 8),
'node_id' => Arr::get($data, 'node_id'),
'name' => Arr::get($data, 'name'),
'description' => Arr::get($data, 'description') ?? '',
'status' => Server::STATUS_INSTALLING,
'skip_scripts' => Arr::get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
'owner_id' => Arr::get($data, 'owner_id'),
'memory' => Arr::get($data, 'memory'),
'swap' => Arr::get($data, 'swap'),
'disk' => Arr::get($data, 'disk'),
'io' => Arr::get($data, 'io'),
'cpu' => Arr::get($data, 'cpu'),
'threads' => Arr::get($data, 'threads'),
'oom_disabled' => Arr::get($data, 'oom_disabled') ?? true,
'allocation_id' => Arr::get($data, 'allocation_id'),
'nest_id' => Arr::get($data, 'nest_id'),
'egg_id' => Arr::get($data, 'egg_id'),
'startup' => Arr::get($data, 'startup'),
'image' => Arr::get($data, 'image'),
'database_limit' => Arr::get($data, 'database_limit') ?? 0,
'allocation_limit' => Arr::get($data, 'allocation_limit') ?? 0,
'backup_limit' => Arr::get($data, 'backup_limit') ?? 0,
]);
return $model;
}
/**
* Configure the allocations assigned to this server.
*/
private function storeAssignedAllocations(Server $server, array $data): void
{
$records = [$data['allocation_id']];
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
$records = array_merge($records, $data['allocation_additional']);
}
Allocation::query()->whereIn('id', $records)->update([
'server_id' => $server->id,
]);
}
/**
* Process environment variables passed for this server and store them in the database.
*/
private function storeEggVariables(Server $server, Collection $variables): void
{
$records = $variables->map(function ($result) use ($server) {
return [
'server_id' => $server->id,
'variable_id' => $result->id,
'variable_value' => $result->value ?? '',
];
})->toArray();
if (!empty($records)) {
$this->serverVariableRepository->insert($records);
}
}
/**
* Create a unique UUID and UUID-Short combo for a server.
*/
private function generateUniqueUuidCombo(): string
{
$uuid = Uuid::uuid4()->toString();
if (!$this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {
return $this->generateUniqueUuidCombo();
}
return $uuid;
}
}