Pterodactyl-Panel/app/Models/Node.php

225 lines
6.1 KiB
PHP
Raw Normal View History

<?php
2016-12-07 23:46:38 +01:00
namespace Pterodactyl\Models;
use Illuminate\Notifications\Notifiable;
use Pterodactyl\Models\Traits\Searchable;
class Node extends Validable
{
use Notifiable, Searchable;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
const RESOURCE_NAME = 'node';
const DAEMON_SECRET_LENGTH = 36;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'nodes';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['daemonSecret'];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
'location_id' => 'integer',
'memory' => 'integer',
'disk' => 'integer',
'daemonListen' => 'integer',
'daemonSFTP' => 'integer',
'behind_proxy' => 'boolean',
'public' => 'boolean',
'maintenance_mode' => 'boolean',
];
2016-01-27 04:17:51 +01:00
2016-01-05 05:59:45 +01:00
/**
* Fields that are mass assignable.
2016-01-05 05:59:45 +01:00
*
* @var array
*/
protected $fillable = [
'public', 'name', 'location_id',
'fqdn', 'scheme', 'behind_proxy',
'memory', 'memory_overallocate', 'disk',
'disk_overallocate', 'upload_size',
'daemonSecret', 'daemonBase',
'daemonSFTP', 'daemonListen',
'description', 'maintenance_mode',
];
2016-01-05 05:59:45 +01:00
2017-03-20 00:36:50 +01:00
/**
* Fields that are searchable.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'fqdn' => 8,
'location.short' => 4,
'location.long' => 4,
];
/**
* @var array
*/
public static $validationRules = [
'name' => 'required|regex:/^([\w .-]{1,100})$/',
2018-03-26 20:57:24 +02:00
'description' => 'string',
'location_id' => 'required|exists:locations,id',
'public' => 'boolean',
'fqdn' => 'required|string',
'scheme' => 'required',
'behind_proxy' => 'boolean',
'memory' => 'required|numeric|min:1',
'memory_overallocate' => 'required|numeric|min:-1',
'disk' => 'required|numeric|min:1',
'disk_overallocate' => 'required|numeric|min:-1',
'daemonBase' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'required|numeric|between:1,65535',
'daemonListen' => 'required|numeric|between:1,65535',
'maintenance_mode' => 'boolean',
'upload_size' => 'int|between:1,1024',
];
/**
* Default values for specific columns that are generally not changed on base installs.
*
* @var array
*/
protected $attributes = [
'public' => true,
'behind_proxy' => false,
'memory_overallocate' => 0,
'disk_overallocate' => 0,
'daemonBase' => '/srv/daemon-data',
'daemonSFTP' => 2022,
'daemonListen' => 8080,
'maintenance_mode' => false,
];
/**
* Get the connection address to use when making calls to this node.
*
* @return string
*/
public function getConnectionAddress(): string
{
return sprintf('%s://%s:%s', $this->scheme, $this->fqdn, $this->daemonListen);
}
/**
2017-01-07 18:39:41 +01:00
* Returns the configuration in JSON format.
*
2017-08-22 05:10:48 +02:00
* @param bool $pretty
2017-03-20 00:36:50 +01:00
* @return string
*/
2017-01-07 18:39:41 +01:00
public function getConfigurationAsJson($pretty = false)
{
$config = [
'web' => [
'host' => '0.0.0.0',
'listen' => $this->daemonListen,
2017-01-07 18:39:41 +01:00
'ssl' => [
'enabled' => (! $this->behind_proxy && $this->scheme === 'https'),
'certificate' => '/etc/letsencrypt/live/' . $this->fqdn . '/fullchain.pem',
'key' => '/etc/letsencrypt/live/' . $this->fqdn . '/privkey.pem',
2017-01-07 18:39:41 +01:00
],
],
'docker' => [
'container' => [
'user' => null,
],
'network' => [
'name' => 'pterodactyl_nw',
],
'socket' => '/var/run/docker.sock',
2017-01-07 18:39:41 +01:00
'autoupdate_images' => true,
],
2018-02-17 23:21:37 +01:00
'filesystem' => [
'server_logs' => '/tmp/pterodactyl',
],
'internals' => [
'disk_use_seconds' => 30,
'set_permissions_on_boot' => true,
'throttle' => [
'enabled' => true,
'kill_at_count' => 5,
'decay' => 10,
2018-03-09 06:35:36 +01:00
'lines' => 1000,
'check_interval_ms' => 100,
],
],
2017-01-07 18:39:41 +01:00
'sftp' => [
'path' => $this->daemonBase,
'ip' => '0.0.0.0',
'port' => $this->daemonSFTP,
'keypair' => [
'bits' => 2048,
'e' => 65537,
],
2017-01-07 18:39:41 +01:00
],
'logger' => [
'path' => 'logs/',
'src' => false,
'level' => 'info',
'period' => '1d',
2017-01-07 18:39:41 +01:00
'count' => 3,
],
'remote' => [
'base' => route('index'),
2017-01-07 18:39:41 +01:00
],
'uploads' => [
'size_limit' => $this->upload_size,
],
'keys' => [$this->daemonSecret],
];
2017-02-16 19:31:25 +01:00
return json_encode($config, ($pretty) ? JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : JSON_UNESCAPED_SLASHES);
}
/**
* Gets the location associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function location()
{
return $this->belongsTo(Location::class);
}
/**
* Gets the servers associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servers()
{
return $this->hasMany(Server::class);
}
/**
* Gets the allocations associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function allocations()
{
return $this->hasMany(Allocation::class);
}
2018-04-08 22:36:40 +02:00
}