2015-12-06 19:58:49 +01:00
|
|
|
<?php
|
2016-12-07 23:46:38 +01:00
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2017-01-25 01:15:03 +01:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2019-09-05 06:21:07 +02:00
|
|
|
use Pterodactyl\Models\Traits\Searchable;
|
2017-09-24 03:45:25 +02:00
|
|
|
use Znck\Eloquent\Traits\BelongsToThrough;
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2019-08-18 01:03:10 +02: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
|
|
|
|
* @property bool $skip_scripts
|
2019-11-24 21:50:16 +01:00
|
|
|
* @property int $suspended
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property int $owner_id
|
|
|
|
* @property int $memory
|
|
|
|
* @property int $swap
|
|
|
|
* @property int $disk
|
|
|
|
* @property int $io
|
|
|
|
* @property int $cpu
|
2020-04-04 02:50:07 +02:00
|
|
|
* @property string $threads
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property bool $oom_disabled
|
|
|
|
* @property int $allocation_id
|
|
|
|
* @property int $nest_id
|
|
|
|
* @property int $egg_id
|
|
|
|
* @property int|null $pack_id
|
|
|
|
* @property string $startup
|
|
|
|
* @property string $image
|
|
|
|
* @property int $installed
|
|
|
|
* @property int $allocation_limit
|
|
|
|
* @property int $database_limit
|
2020-04-26 21:12:29 +02:00
|
|
|
* @property int $backup_limit
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
|
|
|
*
|
|
|
|
* @property \Pterodactyl\Models\User $user
|
2019-12-28 21:03:19 +01:00
|
|
|
* @property \Pterodactyl\Models\User[]|\Illuminate\Database\Eloquent\Collection $subusers
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property \Pterodactyl\Models\Allocation $allocation
|
2019-12-28 21:03:19 +01:00
|
|
|
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property \Pterodactyl\Models\Pack|null $pack
|
|
|
|
* @property \Pterodactyl\Models\Node $node
|
|
|
|
* @property \Pterodactyl\Models\Nest $nest
|
|
|
|
* @property \Pterodactyl\Models\Egg $egg
|
2019-12-28 21:03:19 +01:00
|
|
|
* @property \Pterodactyl\Models\ServerVariable[]|\Illuminate\Database\Eloquent\Collection $variables
|
|
|
|
* @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule
|
|
|
|
* @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases
|
2019-08-18 01:03:10 +02:00
|
|
|
* @property \Pterodactyl\Models\Location $location
|
|
|
|
* @property \Pterodactyl\Models\DaemonKey $key
|
2019-12-28 21:03:19 +01:00
|
|
|
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
|
2020-04-04 22:10:18 +02:00
|
|
|
* @property \Pterodactyl\Models\ServerTransfer $transfer
|
2020-04-04 19:59:25 +02:00
|
|
|
* @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups
|
2020-05-21 22:23:12 +02:00
|
|
|
* @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts
|
2019-08-18 01:03:10 +02:00
|
|
|
*/
|
2020-04-04 08:22:35 +02:00
|
|
|
class Server extends Model
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2020-04-04 08:22:35 +02:00
|
|
|
use BelongsToThrough;
|
|
|
|
use Notifiable;
|
|
|
|
use Searchable;
|
2016-10-28 02:05:29 +02:00
|
|
|
|
2018-01-20 04:47:06 +01:00
|
|
|
/**
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
* API representation using fractal.
|
|
|
|
*/
|
|
|
|
const RESOURCE_NAME = 'server';
|
|
|
|
|
2020-04-03 22:46:45 +02:00
|
|
|
const STATUS_INSTALLING = 0;
|
|
|
|
const STATUS_INSTALLED = 1;
|
|
|
|
const STATUS_INSTALL_FAILED = 2;
|
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'servers';
|
|
|
|
|
2019-08-03 22:41:24 +02: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.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'oom_disabled' => true,
|
|
|
|
];
|
|
|
|
|
2016-10-28 02:05:29 +02:00
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-10-25 06:35:25 +02:00
|
|
|
protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at'];
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2015-12-15 21:08:41 +01:00
|
|
|
/**
|
|
|
|
* Fields that are not mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-10-25 06:35:25 +02:00
|
|
|
protected $guarded = ['id', 'installed', self::CREATED_AT, self::UPDATED_AT, 'deleted_at'];
|
2015-12-15 21:08:41 +01:00
|
|
|
|
2017-08-06 00:20:07 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 07:19:57 +02:00
|
|
|
public static $validationRules = [
|
|
|
|
'external_id' => 'sometimes|nullable|string|between:1,191|unique:servers',
|
|
|
|
'owner_id' => 'required|integer|exists:users,id',
|
|
|
|
'name' => 'required|string|min:1|max:255',
|
|
|
|
'node_id' => 'required|exists:nodes,id',
|
2018-01-01 19:23:45 +01:00
|
|
|
'description' => 'string',
|
2019-09-05 07:19:57 +02:00
|
|
|
'memory' => 'required|numeric|min:0',
|
|
|
|
'swap' => 'required|numeric|min:-1',
|
|
|
|
'io' => 'required|numeric|between:10,1000',
|
|
|
|
'cpu' => 'required|numeric|min:0',
|
2020-04-04 02:59:47 +02:00
|
|
|
'threads' => 'nullable|regex:/^[0-9-,]+$/',
|
2019-09-05 07:19:57 +02:00
|
|
|
'oom_disabled' => 'sometimes|boolean',
|
|
|
|
'disk' => 'required|numeric|min:0',
|
|
|
|
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
|
|
|
|
'nest_id' => 'required|exists:nests,id',
|
|
|
|
'egg_id' => 'required|exists:eggs,id',
|
|
|
|
'pack_id' => 'sometimes|nullable|numeric|min:0',
|
|
|
|
'startup' => 'required|string',
|
|
|
|
'skip_scripts' => 'sometimes|boolean',
|
|
|
|
'image' => 'required|string|max:255',
|
2019-03-03 22:44:28 +01:00
|
|
|
'installed' => 'in:0,1,2',
|
2019-09-05 07:19:57 +02:00
|
|
|
'database_limit' => 'present|nullable|integer|min:0',
|
|
|
|
'allocation_limit' => 'sometimes|nullable|integer|min:0',
|
2020-05-05 04:06:20 +02:00
|
|
|
'backup_limit' => 'present|integer|min:0',
|
2017-07-20 03:49:41 +02:00
|
|
|
];
|
|
|
|
|
2017-03-15 02:18:36 +01:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'node_id' => 'integer',
|
2017-04-21 23:52:44 +02:00
|
|
|
'skip_scripts' => 'boolean',
|
2017-03-15 02:18:36 +01:00
|
|
|
'suspended' => 'integer',
|
|
|
|
'owner_id' => 'integer',
|
|
|
|
'memory' => 'integer',
|
|
|
|
'swap' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'io' => 'integer',
|
|
|
|
'cpu' => 'integer',
|
2019-08-03 22:41:24 +02:00
|
|
|
'oom_disabled' => 'boolean',
|
2017-03-15 02:18:36 +01:00
|
|
|
'allocation_id' => 'integer',
|
2017-10-07 06:57:53 +02:00
|
|
|
'nest_id' => 'integer',
|
|
|
|
'egg_id' => 'integer',
|
2017-03-15 02:18:36 +01:00
|
|
|
'pack_id' => 'integer',
|
|
|
|
'installed' => 'integer',
|
2018-03-02 03:08:27 +01:00
|
|
|
'database_limit' => 'integer',
|
|
|
|
'allocation_limit' => 'integer',
|
2020-04-26 21:12:29 +02:00
|
|
|
'backup_limit' => 'integer',
|
2017-03-15 02:18:36 +01:00
|
|
|
];
|
2016-01-27 04:17:51 +01:00
|
|
|
|
2017-03-15 02:18:36 +01:00
|
|
|
/**
|
|
|
|
* Parameters for search querying.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-06 00:20:07 +02:00
|
|
|
protected $searchableColumns = [
|
2018-02-24 18:57:12 +01:00
|
|
|
'name' => 100,
|
|
|
|
'uuid' => 80,
|
|
|
|
'uuidShort' => 80,
|
|
|
|
'external_id' => 50,
|
|
|
|
'user.email' => 40,
|
|
|
|
'user.username' => 30,
|
2018-01-06 01:49:00 +01:00
|
|
|
'node.name' => 10,
|
2018-02-24 18:57:12 +01:00
|
|
|
'pack.name' => 10,
|
2017-03-15 02:18:36 +01:00
|
|
|
];
|
2017-02-19 01:31:44 +01:00
|
|
|
|
2019-11-25 00:08:54 +01:00
|
|
|
/**
|
|
|
|
* Returns the format for server allocations when communicating with the Daemon.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllocationMappings(): array
|
|
|
|
{
|
2020-04-05 00:16:18 +02:00
|
|
|
return $this->allocations->where('node_id', $this->node_id)->groupBy('ip')->map(function ($item) {
|
2019-11-25 00:08:54 +01:00
|
|
|
return $item->pluck('port');
|
|
|
|
})->toArray();
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:07:42 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isInstalled(): bool
|
|
|
|
{
|
|
|
|
return $this->installed === 1;
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:50:28 +01:00
|
|
|
/**
|
|
|
|
* Gets the user who owns the server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 22:50:28 +01:00
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(User::class, 'owner_id');
|
2017-02-03 22:50:28 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:38:54 +01:00
|
|
|
/**
|
|
|
|
* Gets the subusers associated with a server.
|
|
|
|
*
|
2019-12-28 20:39:44 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
2017-02-10 01:38:54 +01:00
|
|
|
*/
|
|
|
|
public function subusers()
|
|
|
|
{
|
2019-12-28 20:39:44 +01:00
|
|
|
return $this->hasMany(Subuser::class, 'server_id', 'id');
|
2017-02-10 01:38:54 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 01:19:46 +01:00
|
|
|
/**
|
|
|
|
* Gets the default allocation for a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function allocation()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:24:08 +01:00
|
|
|
/**
|
|
|
|
* Gets all allocations associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function allocations()
|
|
|
|
{
|
2017-02-03 22:50:28 +01:00
|
|
|
return $this->hasMany(Allocation::class, 'server_id');
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the pack associated with this server.
|
|
|
|
*
|
2017-03-15 02:18:36 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 22:24:08 +01:00
|
|
|
*/
|
|
|
|
public function pack()
|
|
|
|
{
|
2017-03-15 02:18:36 +01:00
|
|
|
return $this->belongsTo(Pack::class);
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 06:57:53 +02:00
|
|
|
* Gets information for the nest associated with this server.
|
2017-02-02 22:24:08 +01:00
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 22:24:08 +01:00
|
|
|
*/
|
2017-10-07 06:57:53 +02:00
|
|
|
public function nest()
|
2017-02-02 22:24:08 +01:00
|
|
|
{
|
2017-10-07 06:57:53 +02:00
|
|
|
return $this->belongsTo(Nest::class);
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 06:57:53 +02:00
|
|
|
* Gets information for the egg associated with this server.
|
2017-02-02 22:24:08 +01:00
|
|
|
*
|
2020-01-19 00:26:15 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
2017-02-02 22:24:08 +01:00
|
|
|
*/
|
2017-10-07 06:57:53 +02:00
|
|
|
public function egg()
|
2017-02-02 22:24:08 +01:00
|
|
|
{
|
2020-01-19 00:26:15 +01:00
|
|
|
return $this->hasOne(Egg::class, 'id', 'egg_id');
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service variables associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function variables()
|
|
|
|
{
|
2017-02-12 22:03:17 +01:00
|
|
|
return $this->hasMany(ServerVariable::class);
|
2017-02-02 22:24:08 +01:00
|
|
|
}
|
2017-02-03 00:21:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the node associated with this server.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 00:21:36 +01:00
|
|
|
*/
|
|
|
|
public function node()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(Node::class);
|
2017-02-03 00:21:36 +01:00
|
|
|
}
|
2017-02-03 01:41:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the tasks associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2017-09-24 03:45:25 +02:00
|
|
|
public function schedule()
|
2017-02-03 01:41:38 +01:00
|
|
|
{
|
2017-09-24 03:45:25 +02:00
|
|
|
return $this->hasMany(Schedule::class);
|
2017-02-03 01:41:38 +01:00
|
|
|
}
|
2017-02-09 23:43:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all databases associated with a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function databases()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Database::class);
|
|
|
|
}
|
2017-04-08 02:28:58 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-24 03:45:25 +02:00
|
|
|
* Returns the location that a server belongs to.
|
2017-04-08 02:28:58 +02:00
|
|
|
*
|
2017-09-24 03:45:25 +02:00
|
|
|
* @return \Znck\Eloquent\Relations\BelongsToThrough
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2017-04-08 02:28:58 +02:00
|
|
|
*/
|
|
|
|
public function location()
|
|
|
|
{
|
2017-09-24 03:45:25 +02:00
|
|
|
return $this->belongsToThrough(Location::class, Node::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the key belonging to the server owner.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
2017-09-25 04:12:30 +02:00
|
|
|
public function key()
|
2017-09-24 03:45:25 +02:00
|
|
|
{
|
|
|
|
return $this->hasOne(DaemonKey::class, 'user_id', 'owner_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the daemon keys belonging to this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function keys()
|
|
|
|
{
|
|
|
|
return $this->hasMany(DaemonKey::class);
|
2017-04-08 02:28:58 +02:00
|
|
|
}
|
2020-04-04 22:10:18 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-05 00:28:02 +02:00
|
|
|
* Returns the associated server transfer.
|
2020-04-04 22:10:18 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function transfer()
|
|
|
|
{
|
|
|
|
return $this->hasOne(ServerTransfer::class)->orderByDesc('id');
|
|
|
|
}
|
2020-04-05 00:28:02 +02:00
|
|
|
|
2020-04-04 19:59:25 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function backups()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Backup::class);
|
|
|
|
}
|
2020-05-21 22:23:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all mounts that have this server has mounted.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function mounts()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Mount::class);
|
|
|
|
}
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|