Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Server.php
7432 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
public function user(): BelongsTo
224
{
225
return $this->belongsTo(User::class, 'owner_id');
226
}
227
228
/**
229
* Gets the subusers associated with a server.
230
*/
231
public function subusers(): HasMany
232
{
233
return $this->hasMany(Subuser::class, 'server_id', 'id');
234
}
235
236
/**
237
* Gets the default allocation for a server.
238
*/
239
public function allocation(): HasOne
240
{
241
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
242
}
243
244
/**
245
* Gets all allocations associated with this server.
246
*/
247
public function allocations(): HasMany
248
{
249
return $this->hasMany(Allocation::class, 'server_id');
250
}
251
252
/**
253
* Gets information for the nest associated with this server.
254
*/
255
public function nest(): BelongsTo
256
{
257
return $this->belongsTo(Nest::class);
258
}
259
260
/**
261
* Gets information for the egg associated with this server.
262
*/
263
public function egg(): HasOne
264
{
265
return $this->hasOne(Egg::class, 'id', 'egg_id');
266
}
267
268
/**
269
* Gets information for the service variables associated with this server.
270
*/
271
public function variables(): HasMany
272
{
273
return $this->hasMany(EggVariable::class, 'egg_id', 'egg_id')
274
->select(['egg_variables.*', 'server_variables.variable_value as server_value'])
275
->leftJoin('server_variables', function (JoinClause $join) {
276
// Don't forget to join against the server ID as well since the way we're using this relationship
277
// would actually return all the variables and their values for _all_ servers using that egg,
278
// rather than only the server for this model.
279
//
280
// @see https://github.com/pterodactyl/panel/issues/2250
281
$join->on('server_variables.variable_id', 'egg_variables.id')
282
->where('server_variables.server_id', $this->id);
283
});
284
}
285
286
/**
287
* Gets information for the node associated with this server.
288
*/
289
public function node(): BelongsTo
290
{
291
return $this->belongsTo(Node::class);
292
}
293
294
/**
295
* Gets information for the tasks associated with this server.
296
*/
297
public function schedules(): HasMany
298
{
299
return $this->hasMany(Schedule::class);
300
}
301
302
/**
303
* Gets all databases associated with a server.
304
*/
305
public function databases(): HasMany
306
{
307
return $this->hasMany(Database::class);
308
}
309
310
/**
311
* Returns the location that a server belongs to.
312
*
313
* @throws \Exception
314
*/
315
public function location(): \Znck\Eloquent\Relations\BelongsToThrough
316
{
317
return $this->belongsToThrough(Location::class, Node::class);
318
}
319
320
/**
321
* Returns the associated server transfer.
322
*/
323
public function transfer(): HasOne
324
{
325
return $this->hasOne(ServerTransfer::class)->whereNull('successful')->orderByDesc('id');
326
}
327
328
public function backups(): HasMany
329
{
330
return $this->hasMany(Backup::class);
331
}
332
333
/**
334
* Returns all mounts that have this server has mounted.
335
*/
336
public function mounts(): HasManyThrough
337
{
338
return $this->hasManyThrough(Mount::class, MountServer::class, 'server_id', 'id', 'id', 'mount_id');
339
}
340
341
/**
342
* Returns all of the activity log entries where the server is the subject.
343
*/
344
public function activity(): MorphToMany
345
{
346
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
347
}
348
349
/**
350
* Checks if the server is currently in a user-accessible state. If not, an
351
* exception is raised. This should be called whenever something needs to make
352
* sure the server is not in a weird state that should block user access.
353
*
354
* @throws ServerStateConflictException
355
*/
356
public function validateCurrentState()
357
{
358
if (
359
$this->isSuspended()
360
|| $this->node->isUnderMaintenance()
361
|| !$this->isInstalled()
362
|| $this->status === self::STATUS_RESTORING_BACKUP
363
|| !is_null($this->transfer)
364
) {
365
throw new ServerStateConflictException($this);
366
}
367
}
368
369
/**
370
* Checks if the server is currently in a transferable state. If not, an
371
* exception is raised. This should be called whenever something needs to make
372
* sure the server is able to be transferred and is not currently being transferred
373
* or installed.
374
*/
375
public function validateTransferState()
376
{
377
if (
378
!$this->isInstalled()
379
|| $this->status === self::STATUS_RESTORING_BACKUP
380
|| !is_null($this->transfer)
381
) {
382
throw new ServerStateConflictException($this);
383
}
384
}
385
}
386
387