2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2022-01-18 02:56:57 +00:00
|
|
|
use Illuminate\Support\Str;
|
2019-12-08 19:29:46 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2020-04-10 22:15:38 +00:00
|
|
|
use Illuminate\Container\Container;
|
2017-01-25 00:15:03 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2022-11-28 16:56:03 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-04-10 22:15:38 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2019-09-06 04:53:33 +00:00
|
|
|
/**
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property int $id
|
|
|
|
* @property string $uuid
|
|
|
|
* @property bool $public
|
|
|
|
* @property string $name
|
|
|
|
* @property string|null $description
|
|
|
|
* @property int $location_id
|
2022-12-15 01:04:16 +00:00
|
|
|
* @property int|null $database_host_id
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property string $scheme
|
2022-12-15 01:04:16 +00:00
|
|
|
* @property string $fqdn
|
|
|
|
* @property int $listen_port_http
|
|
|
|
* @property int $listen_port_sftp
|
|
|
|
* @property int $public_port_http
|
|
|
|
* @property int $public_port_sftp
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property bool $behind_proxy
|
|
|
|
* @property bool $maintenance_mode
|
|
|
|
* @property int $memory
|
|
|
|
* @property int $memory_overallocate
|
2022-11-28 16:56:03 +00:00
|
|
|
* @property int $sum_memory
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property int $disk
|
|
|
|
* @property int $disk_overallocate
|
2022-11-28 16:56:03 +00:00
|
|
|
* @property int $sum_disk
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property int $upload_size
|
|
|
|
* @property string $daemon_token_id
|
|
|
|
* @property string $daemon_token
|
2022-12-15 01:04:16 +00:00
|
|
|
* @property string $daemon_base
|
2022-11-28 16:56:03 +00:00
|
|
|
* @property int $servers_count
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
2022-12-15 01:04:16 +00:00
|
|
|
* @property Allocation[]|Collection $allocations
|
|
|
|
* @property \Pterodactyl\Models\DatabaseHost|null $databaseHost
|
2022-11-28 16:56:03 +00:00
|
|
|
* @property Location $location
|
|
|
|
* @property Mount[]|Collection $mounts
|
2022-12-15 01:04:16 +00:00
|
|
|
* @property int[]|\Illuminate\Support\Collection $ports
|
2022-11-28 16:56:03 +00:00
|
|
|
* @property Server[]|Collection $servers
|
2019-09-06 04:53:33 +00:00
|
|
|
*/
|
2020-04-04 06:22:35 +00:00
|
|
|
class Node extends Model
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2020-04-04 06:22:35 +00:00
|
|
|
use Notifiable;
|
2017-01-25 00:15:03 +00:00
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
* API representation using fractal.
|
|
|
|
*/
|
2021-01-23 20:33:34 +00:00
|
|
|
public const RESOURCE_NAME = 'node';
|
2018-01-26 03:26:06 +00:00
|
|
|
|
2022-12-15 01:04:16 +00:00
|
|
|
/**
|
|
|
|
* The default location of server files on the Wings instance.
|
|
|
|
*/
|
|
|
|
public const DEFAULT_DAEMON_BASE = '/var/lib/pterodactyl/volumes';
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
public const DAEMON_TOKEN_ID_LENGTH = 16;
|
|
|
|
public const DAEMON_TOKEN_LENGTH = 64;
|
2017-10-27 04:49:54 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*/
|
|
|
|
protected $table = 'nodes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*/
|
2020-04-10 22:15:38 +00:00
|
|
|
protected $hidden = ['daemon_token_id', 'daemon_token'];
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'location_id' => 'integer',
|
2022-12-15 01:04:16 +00:00
|
|
|
'database_host_id' => 'integer',
|
|
|
|
'listen_port_http' => 'integer',
|
|
|
|
'listen_port_sftp' => 'integer',
|
|
|
|
'public_port_http' => 'integer',
|
|
|
|
'public_port_sftp' => 'integer',
|
2017-08-05 22:20:07 +00:00
|
|
|
'memory' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'behind_proxy' => 'boolean',
|
2018-03-26 18:56:58 +00:00
|
|
|
'public' => 'boolean',
|
2018-05-31 14:34:35 +00:00
|
|
|
'maintenance_mode' => 'boolean',
|
2017-08-05 22:20:07 +00:00
|
|
|
];
|
2016-01-27 03:17:51 +00:00
|
|
|
|
2016-01-05 04:59:45 +00:00
|
|
|
/**
|
2017-02-03 20:19:14 +00:00
|
|
|
* Fields that are mass assignable.
|
2016-01-05 04:59:45 +00:00
|
|
|
*/
|
2017-02-03 20:19:14 +00:00
|
|
|
protected $fillable = [
|
2022-12-15 01:04:16 +00:00
|
|
|
'public', 'name', 'location_id', 'database_host_id',
|
|
|
|
'listen_port_http', 'listen_port_sftp', 'public_port_http', 'public_port_sftp',
|
2017-04-28 02:52:37 +00:00
|
|
|
'fqdn', 'scheme', 'behind_proxy',
|
|
|
|
'memory', 'memory_overallocate', 'disk',
|
2022-12-15 01:04:16 +00:00
|
|
|
'disk_overallocate', 'upload_size', 'daemon_base',
|
2018-05-31 14:34:35 +00:00
|
|
|
'description', 'maintenance_mode',
|
2017-02-03 20:19:14 +00:00
|
|
|
];
|
2016-01-05 04:59:45 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public static array $validationRules = [
|
2019-09-05 05:19:57 +00:00
|
|
|
'name' => 'required|regex:/^([\w .-]{1,100})$/',
|
2020-04-18 00:52:40 +00:00
|
|
|
'description' => 'string|nullable',
|
2019-09-05 05:19:57 +00:00
|
|
|
'location_id' => 'required|exists:locations,id',
|
2022-12-15 01:04:16 +00:00
|
|
|
'database_host_id' => 'sometimes|nullable|exists:database_hosts,id',
|
2017-08-05 22:20:07 +00:00
|
|
|
'public' => 'boolean',
|
2019-09-05 05:19:57 +00:00
|
|
|
'fqdn' => 'required|string',
|
2022-12-15 01:04:16 +00:00
|
|
|
'listen_port_http' => 'required|numeric|between:1,65535',
|
|
|
|
'listen_port_sftp' => 'required|numeric|between:1,65535',
|
|
|
|
'public_port_http' => 'required|numeric|between:1,65535',
|
|
|
|
'public_port_sftp' => 'required|numeric|between:1,65535',
|
2019-09-05 05:19:57 +00:00
|
|
|
'scheme' => 'required',
|
2017-08-05 22:20:07 +00:00
|
|
|
'behind_proxy' => 'boolean',
|
2019-09-05 05:19:57 +00:00
|
|
|
'memory' => 'required|numeric|min:1',
|
|
|
|
'memory_overallocate' => 'required|numeric|min:-1',
|
|
|
|
'disk' => 'required|numeric|min:1',
|
|
|
|
'disk_overallocate' => 'required|numeric|min:-1',
|
2022-12-15 01:04:16 +00:00
|
|
|
'daemon_base' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
|
2018-05-31 14:34:35 +00:00
|
|
|
'maintenance_mode' => 'boolean',
|
2018-09-03 21:53:58 +00:00
|
|
|
'upload_size' => 'int|between:1,1024',
|
2017-08-05 22:20:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default values for specific columns that are generally not changed on base installs.
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
2022-12-15 01:04:16 +00:00
|
|
|
'listen_port_http' => 8080,
|
|
|
|
'listen_port_sftp' => 2022,
|
|
|
|
'public_port_http' => 8080,
|
|
|
|
'public_port_sftp' => 2022,
|
2017-08-05 22:20:07 +00:00
|
|
|
'public' => true,
|
|
|
|
'behind_proxy' => false,
|
|
|
|
'memory_overallocate' => 0,
|
|
|
|
'disk_overallocate' => 0,
|
2022-12-15 01:04:16 +00:00
|
|
|
'daemon_base' => self::DEFAULT_DAEMON_BASE,
|
2018-05-31 14:34:35 +00:00
|
|
|
'maintenance_mode' => false,
|
2017-08-05 22:20:07 +00:00
|
|
|
];
|
2017-03-03 22:30:39 +00:00
|
|
|
|
2018-07-21 06:45:07 +00:00
|
|
|
/**
|
|
|
|
* Get the connection address to use when making calls to this node.
|
|
|
|
*/
|
|
|
|
public function getConnectionAddress(): string
|
|
|
|
{
|
2022-12-15 01:08:50 +00:00
|
|
|
return sprintf('%s://%s:%s', $this->scheme, $this->fqdn, $this->public_port_http);
|
2018-07-21 06:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 17:06:09 +00:00
|
|
|
/**
|
2020-04-10 15:38:20 +00:00
|
|
|
* Returns the configuration as an array.
|
2017-01-07 17:06:09 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getConfiguration(): array
|
2017-01-07 17:39:41 +00:00
|
|
|
{
|
2020-04-10 15:38:20 +00:00
|
|
|
return [
|
2019-12-08 19:29:46 +00:00
|
|
|
'debug' => false,
|
2020-04-10 22:15:38 +00:00
|
|
|
'uuid' => $this->uuid,
|
|
|
|
'token_id' => $this->daemon_token_id,
|
|
|
|
'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token),
|
2019-12-08 19:29:46 +00:00
|
|
|
'api' => [
|
2017-01-07 17:06:09 +00:00
|
|
|
'host' => '0.0.0.0',
|
2022-12-15 01:04:16 +00:00
|
|
|
'port' => $this->listen_port_http,
|
2017-01-07 17:39:41 +00:00
|
|
|
'ssl' => [
|
2021-01-23 20:33:34 +00:00
|
|
|
'enabled' => (!$this->behind_proxy && $this->scheme === 'https'),
|
2022-01-18 02:56:57 +00:00
|
|
|
'cert' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/fullchain.pem',
|
|
|
|
'key' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/privkey.pem',
|
2017-01-07 17:39:41 +00:00
|
|
|
],
|
2019-12-08 19:29:46 +00:00
|
|
|
'upload_limit' => $this->upload_size,
|
2017-01-07 17:39:41 +00:00
|
|
|
],
|
2019-12-08 19:29:46 +00:00
|
|
|
'system' => [
|
2022-12-15 01:04:16 +00:00
|
|
|
'data' => $this->daemon_base,
|
2019-12-08 19:29:46 +00:00
|
|
|
'sftp' => [
|
2022-12-15 01:04:16 +00:00
|
|
|
'bind_port' => $this->listen_port_sftp,
|
2017-10-25 04:35:25 +00:00
|
|
|
],
|
2019-12-08 19:29:46 +00:00
|
|
|
],
|
2020-09-13 16:59:54 +00:00
|
|
|
'allowed_mounts' => $this->mounts->pluck('source')->toArray(),
|
2019-12-08 19:29:46 +00:00
|
|
|
'remote' => route('index'),
|
2017-01-07 17:39:41 +00:00
|
|
|
];
|
2020-04-10 15:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the configuration in Yaml format.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getYamlConfiguration(): string
|
2020-04-10 22:15:38 +00:00
|
|
|
{
|
2020-09-22 23:09:28 +00:00
|
|
|
return Yaml::dump($this->getConfiguration(), 4, 2, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
|
2020-04-10 15:38:20 +00:00
|
|
|
}
|
2017-01-07 17:06:09 +00:00
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
/**
|
2020-04-10 15:38:20 +00:00
|
|
|
* Returns the configuration in JSON format.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getJsonConfiguration(bool $pretty = false): string
|
2020-04-10 22:15:38 +00:00
|
|
|
{
|
2020-04-10 15:38:20 +00:00
|
|
|
return json_encode($this->getConfiguration(), $pretty ? JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : JSON_UNESCAPED_SLASHES);
|
2017-01-07 17:06:09 +00:00
|
|
|
}
|
2017-02-03 20:19:14 +00:00
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
/**
|
|
|
|
* Helper function to return the decrypted key for a node.
|
|
|
|
*/
|
|
|
|
public function getDecryptedKey(): string
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
return (string) Container::getInstance()->make(Encrypter::class)->decrypt(
|
2020-04-10 22:15:38 +00:00
|
|
|
$this->daemon_token
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-06 23:02:30 +00:00
|
|
|
public function isUnderMaintenance(): bool
|
|
|
|
{
|
|
|
|
return $this->maintenance_mode;
|
|
|
|
}
|
|
|
|
|
2022-12-15 01:04:16 +00:00
|
|
|
/**
|
|
|
|
* Gets the allocations associated with a node.
|
|
|
|
*/
|
|
|
|
public function allocations(): HasMany
|
2020-09-13 16:59:54 +00:00
|
|
|
{
|
2022-12-15 01:04:16 +00:00
|
|
|
return $this->hasMany(Allocation::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the database host associated with a node.
|
|
|
|
*/
|
|
|
|
public function databaseHost(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(DatabaseHost::class);
|
2020-09-13 16:59:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 20:19:14 +00:00
|
|
|
/**
|
|
|
|
* Gets the location associated with a node.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function location(): BelongsTo
|
2017-02-03 20:19:14 +00:00
|
|
|
{
|
2017-02-05 23:00:39 +00:00
|
|
|
return $this->belongsTo(Location::class);
|
2017-02-03 20:19:14 +00:00
|
|
|
}
|
2017-02-03 21:50:28 +00:00
|
|
|
|
|
|
|
/**
|
2022-12-15 01:04:16 +00:00
|
|
|
* Returns a HasManyThrough relationship for all the mounts associated with a node.
|
2017-02-03 21:50:28 +00:00
|
|
|
*/
|
2022-12-15 01:04:16 +00:00
|
|
|
public function mounts(): HasManyThrough
|
2017-02-03 21:50:28 +00:00
|
|
|
{
|
2022-12-15 01:04:16 +00:00
|
|
|
return $this->hasManyThrough(Mount::class, MountNode::class, 'node_id', 'id', 'id', 'mount_id');
|
2017-02-03 21:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-15 01:04:16 +00:00
|
|
|
* Gets the servers associated with a node.
|
2017-02-03 21:50:28 +00:00
|
|
|
*/
|
2022-12-15 01:04:16 +00:00
|
|
|
public function servers(): HasMany
|
2017-02-03 21:50:28 +00:00
|
|
|
{
|
2022-12-15 01:04:16 +00:00
|
|
|
return $this->hasMany(Server::class);
|
2017-02-03 21:50:28 +00:00
|
|
|
}
|
2020-04-04 03:45:37 +00:00
|
|
|
|
2022-11-28 16:56:03 +00:00
|
|
|
public function loadServerSums(): self
|
|
|
|
{
|
|
|
|
$this->loadSum('servers as sum_memory', 'memory');
|
|
|
|
$this->loadSum('servers as sum_disk', 'disk');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-04-04 03:45:37 +00:00
|
|
|
/**
|
|
|
|
* Returns a boolean if the node is viable for an additional server to be placed on it.
|
|
|
|
*/
|
2022-11-28 16:56:03 +00:00
|
|
|
public function isViable(int $memory = 0, int $disk = 0): bool
|
2020-04-04 22:24:58 +00:00
|
|
|
{
|
2022-11-28 16:56:03 +00:00
|
|
|
$this->loadServerSums();
|
|
|
|
|
2022-11-25 20:29:04 +00:00
|
|
|
$memoryLimit = $this->memory * (1.0 + ($this->memory_overallocate / 100.0));
|
|
|
|
$diskLimit = $this->disk * (1.0 + ($this->disk_overallocate / 100.0));
|
2020-04-04 03:45:37 +00:00
|
|
|
|
|
|
|
return ($this->sum_memory + $memory) <= $memoryLimit && ($this->sum_disk + $disk) <= $diskLimit;
|
|
|
|
}
|
2018-04-08 20:36:40 +00:00
|
|
|
}
|