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