2015-12-06 13:58:49 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2015-12-06 13:58:49 -05:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Database\Factories\ServerFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2024-10-21 19:18:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
|
use Illuminate\Notifications\DatabaseNotification;
|
|
|
|
|
use Illuminate\Notifications\DatabaseNotificationCollection;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Pterodactyl\Contracts\Models\Identifiable;
|
2021-01-30 13:28:31 -08:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
|
|
|
|
|
use Pterodactyl\Support\JsonValueGuard;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-08-17 16:03:10 -07:00
|
|
|
/**
|
2022-05-29 18:20:54 -04:00
|
|
|
* \Pterodactyl\Models\Server.
|
2022-05-22 14:10:01 -04:00
|
|
|
*
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property string|null $external_id
|
|
|
|
|
* @property string $uuid
|
|
|
|
|
* @property string $uuidShort
|
|
|
|
|
* @property int $node_id
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $description
|
2021-01-30 10:10:29 -08:00
|
|
|
* @property string|null $status
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property bool $skip_scripts
|
|
|
|
|
* @property int $owner_id
|
|
|
|
|
* @property int $memory
|
|
|
|
|
* @property int $swap
|
|
|
|
|
* @property int $disk
|
|
|
|
|
* @property int $io
|
|
|
|
|
* @property int $cpu
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property string|null $threads
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property bool $oom_disabled
|
|
|
|
|
* @property int $allocation_id
|
|
|
|
|
* @property int $egg_id
|
|
|
|
|
* @property string $startup
|
|
|
|
|
* @property string $image
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $allocation_limit
|
|
|
|
|
* @property int|null $database_limit
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $backup_limit
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Carbon|null $created_at
|
|
|
|
|
* @property Carbon|null $updated_at
|
|
|
|
|
* @property Carbon|null $installed_at
|
|
|
|
|
* @property Collection|ActivityLog[] $activity
|
2022-05-29 18:20:54 -04:00
|
|
|
* @property int|null $activity_count
|
2024-10-21 19:18:20 -06:00
|
|
|
* @property Allocation|null $allocation
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Allocation[] $allocations
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $allocations_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Backup[] $backups
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $backups_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Database[] $databases
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $databases_count
|
2024-10-21 19:18:20 -06:00
|
|
|
* @property Egg|null $egg
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Mount[] $mounts
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $mounts_count
|
2024-10-21 19:18:20 -06:00
|
|
|
* @property Node $node
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property DatabaseNotificationCollection<int, DatabaseNotification>|DatabaseNotification[] $notifications
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $notifications_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Schedule[] $schedules
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $schedules_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|Subuser[] $subusers
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $subusers_count
|
2024-10-21 19:18:20 -06:00
|
|
|
* @property ServerTransfer|null $transfer
|
|
|
|
|
* @property User $user
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|EggVariable[] $variables
|
2022-05-22 14:10:01 -04:00
|
|
|
* @property int|null $variables_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Collection|ServerVariable[] $serverVariables
|
2022-05-22 14:10:01 -04:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @method static ServerFactory factory(...$parameters)
|
|
|
|
|
* @method static Builder|Server newModelQuery()
|
|
|
|
|
* @method static Builder|Server newQuery()
|
|
|
|
|
* @method static Builder|Server query()
|
|
|
|
|
* @method static Builder|Server whereAllocationId($value)
|
|
|
|
|
* @method static Builder|Server whereAllocationLimit($value)
|
|
|
|
|
* @method static Builder|Server whereBackupLimit($value)
|
|
|
|
|
* @method static Builder|Server whereCpu($value)
|
|
|
|
|
* @method static Builder|Server whereCreatedAt($value)
|
|
|
|
|
* @method static Builder|Server whereDatabaseLimit($value)
|
|
|
|
|
* @method static Builder|Server whereDescription($value)
|
|
|
|
|
* @method static Builder|Server whereDisk($value)
|
|
|
|
|
* @method static Builder|Server whereEggId($value)
|
|
|
|
|
* @method static Builder|Server whereExternalId($value)
|
|
|
|
|
* @method static Builder|Server whereId($value)
|
|
|
|
|
* @method static Builder|Server whereImage($value)
|
|
|
|
|
* @method static Builder|Server whereIo($value)
|
|
|
|
|
* @method static Builder|Server whereMemory($value)
|
|
|
|
|
* @method static Builder|Server whereName($value)
|
|
|
|
|
* @method static Builder|Server whereNodeId($value)
|
|
|
|
|
* @method static Builder|Server whereOomDisabled($value)
|
|
|
|
|
* @method static Builder|Server whereOwnerId($value)
|
|
|
|
|
* @method static Builder|Server whereSkipScripts($value)
|
|
|
|
|
* @method static Builder|Server whereStartup($value)
|
|
|
|
|
* @method static Builder|Server whereStatus($value)
|
|
|
|
|
* @method static Builder|Server whereSwap($value)
|
|
|
|
|
* @method static Builder|Server whereThreads($value)
|
|
|
|
|
* @method static Builder|Server whereUpdatedAt($value)
|
|
|
|
|
* @method static Builder|Server whereUuid($value)
|
|
|
|
|
* @method static Builder|Server whereUuidShort($value)
|
2022-10-14 10:59:20 -06:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model
|
2019-08-17 16:03:10 -07:00
|
|
|
*/
|
2026-02-14 11:21:57 -08:00
|
|
|
#[Attributes\Identifiable('serv')]
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Guarded(['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at'])]
|
2026-02-14 11:21:57 -08:00
|
|
|
class Server extends Model implements Identifiable
|
2015-12-06 13:58:49 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @use HasFactory<ServerFactory> */
|
2024-10-21 19:18:20 -06:00
|
|
|
use HasFactory;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-02-14 11:21:57 -08:00
|
|
|
use HasRealtimeIdentifier;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Notifiable;
|
2016-10-27 20:05:29 -04:00
|
|
|
|
2018-01-19 21:47:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
|
* API representation using fractal.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string RESOURCE_NAME = 'server';
|
2020-04-03 13:46:45 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string STATUS_INSTALLING = 'installing';
|
|
|
|
|
|
|
|
|
|
public const string STATUS_INSTALL_FAILED = 'install_failed';
|
|
|
|
|
|
|
|
|
|
public const string STATUS_REINSTALL_FAILED = 'reinstall_failed';
|
|
|
|
|
|
|
|
|
|
public const string STATUS_SUSPENDED = 'suspended';
|
|
|
|
|
|
|
|
|
|
public const string STATUS_RESTORING_BACKUP = 'restoring_backup';
|
|
|
|
|
|
|
|
|
|
private const string ADMIN_ROUTE_BINDING_FIELD = 'admin_identifier';
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-08-03 13:41:24 -07:00
|
|
|
/**
|
|
|
|
|
* Default values when creating the model. We want to switch to disabling OOM killer
|
|
|
|
|
* on server instances unless the user specifies otherwise in the request.
|
|
|
|
|
*/
|
|
|
|
|
protected $attributes = [
|
2021-01-17 15:51:56 -08:00
|
|
|
'status' => self::STATUS_INSTALLING,
|
2019-08-03 13:41:24 -07:00
|
|
|
'oom_disabled' => true,
|
2022-09-25 13:16:58 -06:00
|
|
|
'installed_at' => null,
|
2019-08-03 13:41:24 -07:00
|
|
|
];
|
|
|
|
|
|
2020-10-31 11:14:28 -07:00
|
|
|
/**
|
|
|
|
|
* The default relationships to load for all server models.
|
|
|
|
|
*/
|
|
|
|
|
protected $with = ['allocation'];
|
|
|
|
|
|
2015-12-15 15:08:41 -05:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return array<string, string>
|
2017-03-14 21:18:36 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'node_id' => 'integer',
|
|
|
|
|
'skip_scripts' => 'boolean',
|
|
|
|
|
'owner_id' => 'integer',
|
|
|
|
|
'memory' => 'integer',
|
|
|
|
|
'swap' => 'integer',
|
|
|
|
|
'disk' => 'integer',
|
|
|
|
|
'io' => 'integer',
|
|
|
|
|
'cpu' => 'integer',
|
|
|
|
|
'oom_disabled' => 'boolean',
|
|
|
|
|
'allocation_id' => 'integer',
|
|
|
|
|
'egg_id' => 'integer',
|
|
|
|
|
'database_limit' => 'integer',
|
|
|
|
|
'allocation_limit' => 'integer',
|
|
|
|
|
'backup_limit' => 'integer',
|
|
|
|
|
self::CREATED_AT => 'datetime',
|
|
|
|
|
self::UPDATED_AT => 'datetime',
|
|
|
|
|
'deleted_at' => 'datetime',
|
|
|
|
|
'installed_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
2016-01-26 22:17:51 -05:00
|
|
|
|
2019-11-24 15:08:54 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the format for server allocations when communicating with the Daemon.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @return array<string, list<int>> ports keyed by the allocation IP
|
2019-11-24 15:08:54 -08:00
|
|
|
*/
|
|
|
|
|
public function getAllocationMappings(): array
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$mappings = [];
|
|
|
|
|
foreach ($this->allocations as $allocation) {
|
|
|
|
|
if ($allocation->node_id === $this->node_id) {
|
|
|
|
|
$mappings[$allocation->ip] ??= [];
|
|
|
|
|
$mappings[$allocation->ip][] = $allocation->port;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mappings;
|
2019-11-24 15:08:54 -08:00
|
|
|
}
|
|
|
|
|
|
2018-08-25 15:07:42 -07:00
|
|
|
public function isInstalled(): bool
|
|
|
|
|
{
|
2021-01-17 15:51:56 -08:00
|
|
|
return $this->status !== self::STATUS_INSTALLING && $this->status !== self::STATUS_INSTALL_FAILED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isSuspended(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->status === self::STATUS_SUSPENDED;
|
2018-08-25 15:07:42 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Whether a reinstall would run the egg's install script. A server that skips its install
|
|
|
|
|
* script can still be reinstalled while its install is unfinished or has failed.
|
|
|
|
|
*/
|
|
|
|
|
public function canBeReinstalled(): bool
|
|
|
|
|
{
|
|
|
|
|
return ! $this->skip_scripts || in_array($this->status, [
|
|
|
|
|
self::STATUS_INSTALLING,
|
|
|
|
|
self::STATUS_INSTALL_FAILED,
|
|
|
|
|
self::STATUS_REINSTALL_FAILED,
|
|
|
|
|
], true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function resolveRouteBinding($value, $field = null): ?\Illuminate\Database\Eloquent\Model
|
|
|
|
|
{
|
|
|
|
|
if ($field !== self::ADMIN_ROUTE_BINDING_FIELD) {
|
|
|
|
|
return parent::resolveRouteBinding($value, $field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! is_int($value) && ! is_string($value)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SAFETY: the type check above limits route binding values to integers and strings.
|
|
|
|
|
$routeValue = (string) $value;
|
|
|
|
|
if (ctype_digit($routeValue)) {
|
|
|
|
|
$server = self::query()->whereKey($routeValue)->first();
|
|
|
|
|
if (($server) !== null) {
|
|
|
|
|
return $server;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::query()->whereUuidOrShort($routeValue)->first();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 16:50:28 -05:00
|
|
|
/**
|
|
|
|
|
* Gets the user who owns the server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return BelongsTo<User, $this>
|
2017-02-03 16:50:28 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function user(): BelongsTo
|
2017-02-03 16:50:28 -05:00
|
|
|
{
|
2017-02-05 18:00:39 -05:00
|
|
|
return $this->belongsTo(User::class, 'owner_id');
|
2017-02-03 16:50:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-09 19:38:54 -05:00
|
|
|
/**
|
|
|
|
|
* Gets the subusers associated with a server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Subuser, $this>
|
2017-02-09 19:38:54 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function subusers(): HasMany
|
2017-02-09 19:38:54 -05:00
|
|
|
{
|
2019-12-28 11:39:44 -08:00
|
|
|
return $this->hasMany(Subuser::class, 'server_id', 'id');
|
2017-02-09 19:38:54 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-05 19:19:46 -05:00
|
|
|
/**
|
|
|
|
|
* Gets the default allocation for a server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasOne<Allocation, $this>
|
2017-02-05 19:19:46 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function allocation(): HasOne
|
2017-02-05 19:19:46 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasOne(Allocation::class, 'id', 'allocation_id')->chaperone('server');
|
2017-02-05 19:19:46 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-02 16:24:08 -05:00
|
|
|
/**
|
|
|
|
|
* Gets all allocations associated with this server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Allocation, $this>
|
2017-02-02 16:24:08 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function allocations(): HasMany
|
2017-02-02 16:24:08 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasMany(Allocation::class, 'server_id')->chaperone('server');
|
2017-02-02 16:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* Gets information for the egg associated with this server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasOne<Egg, $this>
|
2017-02-02 16:24:08 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function egg(): HasOne
|
2017-02-02 16:24:08 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasOne(Egg::class, 'id', 'egg_id');
|
2017-02-02 16:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* The tag slugs that describe this server's game, taken from its egg. These are
|
|
|
|
|
* what feature gating matches on, replacing the nest a server used to carry.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return string[]
|
2017-02-02 16:24:08 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function tagSlugs(): array
|
2017-02-02 16:24:08 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$this->loadMissing('egg.tags');
|
|
|
|
|
|
|
|
|
|
return $this->egg === null ? [] : JsonValueGuard::stringList($this->egg->tags->pluck('slug')->all());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether this server's egg carries the given tag slug.
|
|
|
|
|
*/
|
|
|
|
|
public function hasTag(string $slug): bool
|
|
|
|
|
{
|
|
|
|
|
return in_array($slug, $this->tagSlugs(), true);
|
2017-02-02 16:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets information for the service variables associated with this server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<EggVariable, $this>
|
2017-02-02 16:24:08 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function variables(): HasMany
|
2017-02-02 16:24:08 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
// WARNING: lazy/instance use only. The join below captures $this->id, and an
|
|
|
|
|
// eager load (with()/load()/loadMissing()) rebuilds the relation on a blank
|
|
|
|
|
// model, degrading the join to "server_id is null" and nulling server_value.
|
2020-08-22 15:43:28 -07:00
|
|
|
return $this->hasMany(EggVariable::class, 'egg_id', 'egg_id')
|
|
|
|
|
->select(['egg_variables.*', 'server_variables.variable_value as server_value'])
|
2026-10-05 18:10:18 -04:00
|
|
|
->leftJoin('server_variables', function (JoinClause $join): void {
|
2020-08-23 08:45:39 -07:00
|
|
|
// Don't forget to join against the server ID as well since the way we're using this relationship
|
2022-10-14 10:59:20 -06:00
|
|
|
// would actually return all the variables and their values for _all_ servers using that egg,
|
2020-08-23 08:45:39 -07:00
|
|
|
// rather than only the server for this model.
|
|
|
|
|
//
|
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2250
|
|
|
|
|
$join->on('server_variables.variable_id', 'egg_variables.id')
|
|
|
|
|
->where('server_variables.server_id', $this->id);
|
|
|
|
|
});
|
2017-02-02 16:24:08 -05:00
|
|
|
}
|
2017-02-02 18:21:36 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Gets the environment variable overrides stored specifically for this server.
|
|
|
|
|
*
|
|
|
|
|
* @return HasMany<ServerVariable, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function serverVariables(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ServerVariable::class);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 18:21:36 -05:00
|
|
|
/**
|
|
|
|
|
* Gets information for the node associated with this server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return BelongsTo<Node, $this>
|
2017-02-02 18:21:36 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function node(): BelongsTo
|
2017-02-02 18:21:36 -05:00
|
|
|
{
|
2017-02-05 18:00:39 -05:00
|
|
|
return $this->belongsTo(Node::class);
|
2017-02-02 18:21:36 -05:00
|
|
|
}
|
2017-02-02 19:41:38 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets information for the tasks associated with this server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Schedule, $this>
|
2017-02-02 19:41:38 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function schedules(): HasMany
|
2017-02-02 19:41:38 -05:00
|
|
|
{
|
2017-09-23 20:45:25 -05:00
|
|
|
return $this->hasMany(Schedule::class);
|
2017-02-02 19:41:38 -05:00
|
|
|
}
|
2017-02-09 17:43:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets all databases associated with a server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Database, $this>
|
2017-02-09 17:43:54 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function databases(): HasMany
|
2017-02-09 17:43:54 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasMany(Database::class)->chaperone('server');
|
2017-02-09 17:43:54 -05:00
|
|
|
}
|
2017-04-07 20:28:58 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-09-23 20:45:25 -05:00
|
|
|
* Returns the location that a server belongs to.
|
2017-04-07 20:28:58 -04:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* The server's location is whatever its node belongs to, so this resolves
|
|
|
|
|
* through the node with local keys - the same native emulation of an inverse
|
|
|
|
|
* through-relation used elsewhere in this codebase.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasOneThrough<Location, Node, $this>
|
2017-04-07 20:28:58 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function location(): HasOneThrough
|
2017-04-07 20:28:58 -04:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->hasOneThrough(Location::class, Node::class, 'id', 'id', 'node_id', 'location_id');
|
2017-09-23 20:45:25 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 14:10:18 -06:00
|
|
|
/**
|
2020-04-04 16:28:02 -06:00
|
|
|
* Returns the associated server transfer.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasOne<ServerTransfer, $this>
|
2020-04-04 14:10:18 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function transfer(): HasOne
|
2020-04-04 14:10:18 -06:00
|
|
|
{
|
2020-12-16 09:34:47 -07:00
|
|
|
return $this->hasOne(ServerTransfer::class)->whereNull('successful')->orderByDesc('id');
|
2020-04-04 14:10:18 -06:00
|
|
|
}
|
2020-04-04 16:28:02 -06:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Backup, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function backups(): HasMany
|
2020-04-04 10:59:25 -07:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Backup::class);
|
|
|
|
|
}
|
2020-05-21 14:23:12 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns all mounts that have this server has mounted.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasManyThrough<Mount, MountServer, $this>
|
2020-05-21 14:23:12 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function mounts(): HasManyThrough
|
2020-05-21 14:23:12 -06:00
|
|
|
{
|
2020-09-13 09:59:54 -07:00
|
|
|
return $this->hasManyThrough(Mount::class, MountServer::class, 'server_id', 'id', 'id', 'mount_id');
|
2020-05-21 14:23:12 -06:00
|
|
|
}
|
2021-01-17 10:49:36 -08:00
|
|
|
|
|
|
|
|
/**
|
2022-05-29 16:19:04 -04:00
|
|
|
* Returns all of the activity log entries where the server is the subject.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return MorphToMany<ActivityLog, $this>
|
2021-01-17 10:49:36 -08:00
|
|
|
*/
|
2022-05-29 16:19:04 -04:00
|
|
|
public function activity(): MorphToMany
|
2021-01-17 10:49:36 -08:00
|
|
|
{
|
2022-05-29 16:19:04 -04:00
|
|
|
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
|
2021-01-17 10:49:36 -08:00
|
|
|
}
|
2021-01-30 13:28:31 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the server is currently in a user-accessible state. If not, an
|
|
|
|
|
* exception is raised. This should be called whenever something needs to make
|
|
|
|
|
* sure the server is not in a weird state that should block user access.
|
|
|
|
|
*
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws ServerStateConflictException
|
2021-01-30 13:28:31 -08:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function validateCurrentState(): void
|
2021-01-30 13:28:31 -08:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($this->isSuspended()
|
|
|
|
|
|| $this->node->isUnderMaintenance()
|
|
|
|
|
|| ! $this->isInstalled()
|
|
|
|
|
|| $this->status === self::STATUS_RESTORING_BACKUP
|
|
|
|
|
|| ($this->transfer) !== null, ServerStateConflictException::class, $this);
|
2021-01-30 13:28:31 -08:00
|
|
|
}
|
2022-10-04 19:57:24 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the server is currently in a transferable state. If not, an
|
|
|
|
|
* exception is raised. This should be called whenever something needs to make
|
|
|
|
|
* sure the server is able to be transferred and is not currently being transferred
|
|
|
|
|
* or installed.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function validateTransferState(): void
|
2022-10-04 19:57:24 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if(! $this->isInstalled()
|
|
|
|
|
|| $this->status === self::STATUS_RESTORING_BACKUP
|
|
|
|
|
|| ($this->transfer) !== null, ServerStateConflictException::class, $this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Attribute<int, never>
|
|
|
|
|
*/
|
|
|
|
|
protected function adminIdentifier(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(get: fn (): int => $this->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Match a server by its full uuid or its eight-character short uuid.
|
|
|
|
|
*
|
|
|
|
|
* @param Builder<self> $query
|
|
|
|
|
*/
|
|
|
|
|
#[Scope]
|
|
|
|
|
protected function whereUuidOrShort(Builder $query, string $uuid): void
|
|
|
|
|
{
|
|
|
|
|
$query->where(fn (Builder $inner) => $inner->where('uuidShort', $uuid)->orWhere('uuid', $uuid));
|
2022-10-04 19:57:24 -06:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|