Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Server.php
14039 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Illuminate\Notifications\Notifiable;
6
use Illuminate\Database\Query\JoinClause;
7
use Znck\Eloquent\Traits\BelongsToThrough;
8
use Pterodactyl\Contracts\Models\Identifiable;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
12
use Illuminate\Database\Eloquent\Relations\BelongsTo;
13
use Illuminate\Database\Eloquent\Factories\HasFactory;
14
use Illuminate\Database\Eloquent\Relations\MorphToMany;
15
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
16
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
17
18
/**
19
* \Pterodactyl\Models\Server.
20
*
21
* @property int $id
22
* @property string|null $external_id
23
* @property string $uuid
24
* @property string $uuidShort
25
* @property int $node_id
26
* @property string $name
27
* @property string $description
28
* @property string|null $status
29
* @property bool $skip_scripts
30
* @property int $owner_id
31
* @property int $memory
32
* @property int $swap
33
* @property int $disk
34
* @property int $io
35
* @property int $cpu
36
* @property string|null $threads
37
* @property bool $oom_disabled
38
* @property int $allocation_id
39
* @property int $nest_id
40
* @property int $egg_id
41
* @property string $startup
42
* @property string $image
43
* @property int|null $allocation_limit
44
* @property int|null $database_limit
45
* @property int $backup_limit
46
* @property \Illuminate\Support\Carbon|null $created_at
47
* @property \Illuminate\Support\Carbon|null $updated_at
48
* @property \Illuminate\Support\Carbon|null $installed_at
49
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLog[] $activity
50
* @property int|null $activity_count
51
* @property Allocation|null $allocation
52
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Allocation[] $allocations
53
* @property int|null $allocations_count
54
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Backup[] $backups
55
* @property int|null $backups_count
56
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Database[] $databases
57
* @property int|null $databases_count
58
* @property Egg|null $egg
59
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Mount[] $mounts
60
* @property int|null $mounts_count
61
* @property Nest $nest
62
* @property Node $node
63
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
64
* @property int|null $notifications_count
65
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Schedule[] $schedules
66
* @property int|null $schedules_count
67
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Subuser[] $subusers
68
* @property int|null $subusers_count
69
* @property ServerTransfer|null $transfer
70
* @property User $user
71
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\EggVariable[] $variables
72
* @property int|null $variables_count
73
*
74
* @method static \Database\Factories\ServerFactory factory(...$parameters)
75
* @method static \Illuminate\Database\Eloquent\Builder|Server newModelQuery()
76
* @method static \Illuminate\Database\Eloquent\Builder|Server newQuery()
77
* @method static \Illuminate\Database\Eloquent\Builder|Server query()
78
* @method static \Illuminate\Database\Eloquent\Builder|Server whereAllocationId($value)
79
* @method static \Illuminate\Database\Eloquent\Builder|Server whereAllocationLimit($value)
80
* @method static \Illuminate\Database\Eloquent\Builder|Server whereBackupLimit($value)
81
* @method static \Illuminate\Database\Eloquent\Builder|Server whereCpu($value)
82
* @method static \Illuminate\Database\Eloquent\Builder|Server whereCreatedAt($value)
83
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDatabaseLimit($value)
84
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDescription($value)
85
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDisk($value)
86
* @method static \Illuminate\Database\Eloquent\Builder|Server whereEggId($value)
87
* @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalId($value)
88
* @method static \Illuminate\Database\Eloquent\Builder|Server whereId($value)
89
* @method static \Illuminate\Database\Eloquent\Builder|Server whereImage($value)
90
* @method static \Illuminate\Database\Eloquent\Builder|Server whereIo($value)
91
* @method static \Illuminate\Database\Eloquent\Builder|Server whereMemory($value)
92
* @method static \Illuminate\Database\Eloquent\Builder|Server whereName($value)
93
* @method static \Illuminate\Database\Eloquent\Builder|Server whereNestId($value)
94
* @method static \Illuminate\Database\Eloquent\Builder|Server whereNodeId($value)
95
* @method static \Illuminate\Database\Eloquent\Builder|Server whereOomDisabled($value)
96
* @method static \Illuminate\Database\Eloquent\Builder|Server whereOwnerId($value)
97
* @method static \Illuminate\Database\Eloquent\Builder|Server whereSkipScripts($value)
98
* @method static \Illuminate\Database\Eloquent\Builder|Server whereStartup($value)
99
* @method static \Illuminate\Database\Eloquent\Builder|Server whereStatus($value)
100
* @method static \Illuminate\Database\Eloquent\Builder|Server whereSwap($value)
101
* @method static \Illuminate\Database\Eloquent\Builder|Server whereThreads($value)
102
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUpdatedAt($value)
103
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUuid($value)
104
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUuidShort($value)
105
*
106
* @mixin \Eloquent
107
*/
108
#[Attributes\Identifiable('serv')]
109
class Server extends Model implements Identifiable
110
{
111
/** @use HasFactory<\Database\Factories\ServerFactory> */
112
use HasFactory;
113
use BelongsToThrough;
114
use Notifiable;
115
use HasRealtimeIdentifier;
116
117
/**
118
* The resource name for this model when it is transformed into an
119
* API representation using fractal.
120
*/
121
public const RESOURCE_NAME = 'server';
122
public const STATUS_INSTALLING = 'installing';
123
public const STATUS_INSTALL_FAILED = 'install_failed';
124
public const STATUS_REINSTALL_FAILED = 'reinstall_failed';
125
public const STATUS_SUSPENDED = 'suspended';
126
public const STATUS_RESTORING_BACKUP = 'restoring_backup';
127
128
/**
129
* The table associated with the model.
130
*/
131
protected $table = 'servers';
132
133
/**
134
* Default values when creating the model. We want to switch to disabling OOM killer
135
* on server instances unless the user specifies otherwise in the request.
136
*/
137
protected $attributes = [
138
'status' => self::STATUS_INSTALLING,
139
'oom_disabled' => true,
140
'installed_at' => null,
141
];
142
143
/**
144
* The default relationships to load for all server models.
145
*/
146
protected $with = ['allocation'];
147
148
/**
149
* Fields that are not mass assignable.
150
*/
151
protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at'];
152
153
public static array $validationRules = [
154
'external_id' => 'sometimes|nullable|string|between:1,191|unique:servers',
155
'owner_id' => 'required|integer|exists:users,id',
156
'name' => 'required|string|min:1|max:191',
157
'node_id' => 'required|exists:nodes,id',
158
'description' => 'string',
159
'status' => 'nullable|string',
160
'memory' => 'required|numeric|min:0',
161
'swap' => 'required|numeric|min:-1',
162
'io' => 'required|numeric|between:10,1000',
163
'cpu' => 'required|numeric|min:0',
164
'threads' => 'nullable|regex:/^[0-9-,]+$/',
165
'oom_disabled' => 'sometimes|boolean',
166
'disk' => 'required|numeric|min:0',
167
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
168
'nest_id' => 'required|exists:nests,id',
169
'egg_id' => 'required|exists:eggs,id',
170
'startup' => 'required|string',
171
'skip_scripts' => 'sometimes|boolean',
172
'image' => ['required', 'string', 'max:191', 'regex:/^~?[\w\.\/\-:@ ]*$/'],
173
'database_limit' => 'present|nullable|integer|min:0',
174
'allocation_limit' => 'sometimes|nullable|integer|min:0',
175
'backup_limit' => 'present|nullable|integer|min:0',
176
];
177
178
/**
179
* Cast values to correct type.
180
*/
181
protected $casts = [
182
'node_id' => 'integer',
183
'skip_scripts' => 'boolean',
184
'owner_id' => 'integer',
185
'memory' => 'integer',
186
'swap' => 'integer',
187
'disk' => 'integer',
188
'io' => 'integer',
189
'cpu' => 'integer',
190
'oom_disabled' => 'boolean',
191
'allocation_id' => 'integer',
192
'nest_id' => 'integer',
193
'egg_id' => 'integer',
194
'database_limit' => 'integer',
195
'allocation_limit' => 'integer',
196
'backup_limit' => 'integer',
197
self::CREATED_AT => 'datetime',
198
self::UPDATED_AT => 'datetime',
199
'deleted_at' => 'datetime',
200
'installed_at' => 'datetime',
201
];
202
203
/**
204
* Returns the format for server allocations when communicating with the Daemon.
205
*/
206
public function getAllocationMappings(): array
207
{
208
return $this->allocations->where('node_id', $this->node_id)->groupBy('ip')->map(function ($item) {
209
return $item->pluck('port');
210
})->toArray();
211
}
212
213
public function isInstalled(): bool
214
{
215
return $this->status !== self::STATUS_INSTALLING && $this->status !== self::STATUS_INSTALL_FAILED;
216
}
217
218
public function isSuspended(): bool
219
{
220
return $this->status === self::STATUS_SUSPENDED;
221
}
222
223
/**
224
* Gets the user who owns the server.
225
*
226
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>
227
*/
228
public function user(): BelongsTo
229
{
230
return $this->belongsTo(User::class, 'owner_id');
231
}
232
233
/**
234
* Gets the subusers associated with a server.
235
*
236
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Subuser, $this>
237
*/
238
public function subusers(): HasMany
239
{
240
return $this->hasMany(Subuser::class, 'server_id', 'id');
241
}
242
243
/**
244
* Gets the default allocation for a server.
245
*
246
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\Pterodactyl\Models\Allocation, $this>
247
*/
248
public function allocation(): HasOne
249
{
250
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
251
}
252
253
/**
254
* Gets all allocations associated with this server.
255
*
256
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Allocation, $this>
257
*/
258
public function allocations(): HasMany
259
{
260
return $this->hasMany(Allocation::class, 'server_id');
261
}
262
263
/**
264
* Gets information for the nest associated with this server.
265
*
266
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\Nest, $this>
267
*/
268
public function nest(): BelongsTo
269
{
270
return $this->belongsTo(Nest::class);
271
}
272
273
/**
274
* Gets information for the egg associated with this server.
275
*
276
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\Pterodactyl\Models\Egg, $this>
277
*/
278
public function egg(): HasOne
279
{
280
return $this->hasOne(Egg::class, 'id', 'egg_id');
281
}
282
283
/**
284
* Gets information for the service variables associated with this server.
285
*
286
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\EggVariable, $this>
287
*/
288
public function variables(): HasMany
289
{
290
return $this->hasMany(EggVariable::class, 'egg_id', 'egg_id')
291
->select(['egg_variables.*', 'server_variables.variable_value as server_value'])
292
->leftJoin('server_variables', function (JoinClause $join) {
293
// Don't forget to join against the server ID as well since the way we're using this relationship
294
// would actually return all the variables and their values for _all_ servers using that egg,
295
// rather than only the server for this model.
296
//
297
// @see https://github.com/pterodactyl/panel/issues/2250
298
$join->on('server_variables.variable_id', 'egg_variables.id')
299
->where('server_variables.server_id', $this->id);
300
});
301
}
302
303
/**
304
* Gets information for the node associated with this server.
305
*
306
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\Node, $this>
307
*/
308
public function node(): BelongsTo
309
{
310
return $this->belongsTo(Node::class);
311
}
312
313
/**
314
* Gets information for the tasks associated with this server.
315
*
316
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Schedule, $this>
317
*/
318
public function schedules(): HasMany
319
{
320
return $this->hasMany(Schedule::class);
321
}
322
323
/**
324
* Gets all databases associated with a server.
325
*
326
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Database, $this>
327
*/
328
public function databases(): HasMany
329
{
330
return $this->hasMany(Database::class);
331
}
332
333
/**
334
* Returns the location that a server belongs to.
335
*
336
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Pterodactyl\Models\Location, \Pterodactyl\Models\Node>
337
*
338
* @throws \Exception
339
*/
340
public function location(): \Znck\Eloquent\Relations\BelongsToThrough
341
{
342
return $this->belongsToThrough(Location::class, Node::class); // @phpstan-ignore return.type
343
}
344
345
/**
346
* Returns the associated server transfer.
347
*
348
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\Pterodactyl\Models\ServerTransfer, $this>
349
*/
350
public function transfer(): HasOne
351
{
352
return $this->hasOne(ServerTransfer::class)->whereNull('successful')->orderByDesc('id');
353
}
354
355
/**
356
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Backup, $this>
357
*/
358
public function backups(): HasMany
359
{
360
return $this->hasMany(Backup::class);
361
}
362
363
/**
364
* Returns all mounts that have this server has mounted.
365
*
366
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough<\Pterodactyl\Models\Mount, \Pterodactyl\Models\MountServer, $this>
367
*/
368
public function mounts(): HasManyThrough
369
{
370
return $this->hasManyThrough(Mount::class, MountServer::class, 'server_id', 'id', 'id', 'mount_id');
371
}
372
373
/**
374
* Returns all of the activity log entries where the server is the subject.
375
*
376
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\Pterodactyl\Models\ActivityLog, $this>
377
*/
378
public function activity(): MorphToMany
379
{
380
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
381
}
382
383
/**
384
* Checks if the server is currently in a user-accessible state. If not, an
385
* exception is raised. This should be called whenever something needs to make
386
* sure the server is not in a weird state that should block user access.
387
*
388
* @throws ServerStateConflictException
389
*/
390
public function validateCurrentState()
391
{
392
if (
393
$this->isSuspended()
394
|| $this->node->isUnderMaintenance()
395
|| !$this->isInstalled()
396
|| $this->status === self::STATUS_RESTORING_BACKUP
397
|| !is_null($this->transfer)
398
) {
399
throw new ServerStateConflictException($this);
400
}
401
}
402
403
/**
404
* Checks if the server is currently in a transferable state. If not, an
405
* exception is raised. This should be called whenever something needs to make
406
* sure the server is able to be transferred and is not currently being transferred
407
* or installed.
408
*/
409
public function validateTransferState()
410
{
411
if (
412
!$this->isInstalled()
413
|| $this->status === self::STATUS_RESTORING_BACKUP
414
|| !is_null($this->transfer)
415
) {
416
throw new ServerStateConflictException($this);
417
}
418
}
419
}
420
421