nodes: rename port columns, add public_ port columns

This commit is contained in:
Matthew Penner 2021-02-11 10:21:32 -07:00
parent b7ee2195d7
commit 5f56ff0fed
9 changed files with 116 additions and 45 deletions

View File

@ -21,6 +21,10 @@ class StoreNodeRequest extends ApplicationApiRequest
'name', 'name',
'location_id', 'location_id',
'fqdn', 'fqdn',
'listen_port_http',
'listen_port_sftp',
'public_port_http',
'public_port_sftp',
'scheme', 'scheme',
'behind_proxy', 'behind_proxy',
'memory', 'memory',
@ -28,12 +32,8 @@ class StoreNodeRequest extends ApplicationApiRequest
'disk', 'disk',
'disk_overallocate', 'disk_overallocate',
'upload_size', 'upload_size',
'daemonListen', 'daemon_base',
'daemonSFTP',
'daemonBase',
])->mapWithKeys(function ($value, $key) { ])->mapWithKeys(function ($value, $key) {
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
return [snake_case($key) => $value]; return [snake_case($key) => $value];
})->toArray(); })->toArray();
} }
@ -62,11 +62,7 @@ class StoreNodeRequest extends ApplicationApiRequest
public function validated() public function validated()
{ {
$response = parent::validated(); $response = parent::validated();
$response['daemonListen'] = $response['daemon_listen']; $response['daemon_base'] = $response['daemon_base'] ?? (new Node())->getAttribute('daemon_base');
$response['daemonSFTP'] = $response['daemon_sftp'];
$response['daemonBase'] = $response['daemon_base'] ?? (new Node())->getAttribute('daemonBase');
unset($response['daemon_base'], $response['daemon_listen'], $response['daemon_sftp']);
return $response; return $response;
} }

View File

@ -2,6 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
@ -14,7 +15,12 @@ use Illuminate\Contracts\Encryption\Encrypter;
* @property string $name * @property string $name
* @property string|null $description * @property string|null $description
* @property int $location_id * @property int $location_id
* @property int|null $database_host_id
* @property string $fqdn * @property string $fqdn
* @property int $listen_port_http
* @property int $public_port_http
* @property int $listen_port_sftp
* @property int $public_port_sftp
* @property string $scheme * @property string $scheme
* @property bool $behind_proxy * @property bool $behind_proxy
* @property bool $maintenance_mode * @property bool $maintenance_mode
@ -25,10 +31,7 @@ use Illuminate\Contracts\Encryption\Encrypter;
* @property int $upload_size * @property int $upload_size
* @property string $daemon_token_id * @property string $daemon_token_id
* @property string $daemon_token * @property string $daemon_token
* @property int $daemonListen * @property string $daemon_base
* @property int $daemonSFTP
* @property string $daemonBase
* @property int|null $database_host_id
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property \Pterodactyl\Models\Location $location * @property \Pterodactyl\Models\Location $location
@ -71,10 +74,13 @@ class Node extends Model
*/ */
protected $casts = [ protected $casts = [
'location_id' => 'integer', 'location_id' => 'integer',
'database_host_id' => 'integer',
'listen_port_http' => 'integer',
'listen_port_sftp' => 'integer',
'public_port_http' => 'integer',
'public_port_sftp' => 'integer',
'memory' => 'integer', 'memory' => 'integer',
'disk' => 'integer', 'disk' => 'integer',
'daemonListen' => 'integer',
'daemonSFTP' => 'integer',
'behind_proxy' => 'boolean', 'behind_proxy' => 'boolean',
'public' => 'boolean', 'public' => 'boolean',
'maintenance_mode' => 'boolean', 'maintenance_mode' => 'boolean',
@ -86,11 +92,11 @@ class Node extends Model
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'public', 'name', 'location_id', 'public', 'name', 'location_id', 'database_host_id',
'listen_port_http', 'listen_port_sftp', 'public_port_http', 'public_port_sftp',
'fqdn', 'scheme', 'behind_proxy', 'fqdn', 'scheme', 'behind_proxy',
'memory', 'memory_overallocate', 'disk', 'memory', 'memory_overallocate', 'disk',
'disk_overallocate', 'upload_size', 'daemonBase', 'disk_overallocate', 'upload_size', 'daemon_base',
'daemonSFTP', 'daemonListen',
'description', 'maintenance_mode', 'description', 'maintenance_mode',
]; ];
@ -101,17 +107,20 @@ class Node extends Model
'name' => 'required|regex:/^([\w .-]{1,100})$/', 'name' => 'required|regex:/^([\w .-]{1,100})$/',
'description' => 'string|nullable', 'description' => 'string|nullable',
'location_id' => 'required|exists:locations,id', 'location_id' => 'required|exists:locations,id',
'database_host_id' => 'required|exists:database_hosts,id',
'public' => 'boolean', 'public' => 'boolean',
'fqdn' => 'required|string', 'fqdn' => 'required|string',
'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',
'scheme' => 'required', 'scheme' => 'required',
'behind_proxy' => 'boolean', 'behind_proxy' => 'boolean',
'memory' => 'required|numeric|min:1', 'memory' => 'required|numeric|min:1',
'memory_overallocate' => 'required|numeric|min:-1', 'memory_overallocate' => 'required|numeric|min:-1',
'disk' => 'required|numeric|min:1', 'disk' => 'required|numeric|min:1',
'disk_overallocate' => 'required|numeric|min:-1', 'disk_overallocate' => 'required|numeric|min:-1',
'daemonBase' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/', 'daemon_base' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'required|numeric|between:1,65535',
'daemonListen' => 'required|numeric|between:1,65535',
'maintenance_mode' => 'boolean', 'maintenance_mode' => 'boolean',
'upload_size' => 'int|between:1,1024', 'upload_size' => 'int|between:1,1024',
]; ];
@ -122,13 +131,15 @@ class Node extends Model
* @var array * @var array
*/ */
protected $attributes = [ protected $attributes = [
'listen_port_http' => 8080,
'listen_port_sftp' => 2022,
'public_port_http' => 8080,
'public_port_sftp' => 2022,
'public' => true, 'public' => true,
'behind_proxy' => false, 'behind_proxy' => false,
'memory_overallocate' => 0, 'memory_overallocate' => 0,
'disk_overallocate' => 0, 'disk_overallocate' => 0,
'daemonBase' => '/var/lib/pterodactyl/volumes', 'daemon_base' => '/var/lib/pterodactyl/volumes',
'daemonSFTP' => 2022,
'daemonListen' => 8080,
'maintenance_mode' => false, 'maintenance_mode' => false,
]; ];
@ -137,13 +148,14 @@ class Node extends Model
*/ */
public function getConnectionAddress(): string public function getConnectionAddress(): string
{ {
return sprintf('%s://%s:%s', $this->scheme, $this->fqdn, $this->daemonListen); return sprintf('%s://%s:%s', $this->scheme, $this->fqdn, $this->public_port_http);
} }
/** /**
* Returns the configuration as an array. * Returns the configuration as an array.
* *
* @return array * @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/ */
public function getConfiguration() public function getConfiguration()
{ {
@ -154,18 +166,18 @@ class Node extends Model
'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token), 'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token),
'api' => [ 'api' => [
'host' => '0.0.0.0', 'host' => '0.0.0.0',
'port' => $this->daemonListen, 'port' => $this->listen_port_http,
'ssl' => [ 'ssl' => [
'enabled' => (!$this->behind_proxy && $this->scheme === 'https'), 'enabled' => (!$this->behind_proxy && $this->scheme === 'https'),
'cert' => '/etc/letsencrypt/live/' . $this->fqdn . '/fullchain.pem', 'cert' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/fullchain.pem',
'key' => '/etc/letsencrypt/live/' . $this->fqdn . '/privkey.pem', 'key' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/privkey.pem',
], ],
'upload_limit' => $this->upload_size, 'upload_limit' => $this->upload_size,
], ],
'system' => [ 'system' => [
'data' => $this->daemonBase, 'data' => $this->daemon_base,
'sftp' => [ 'sftp' => [
'bind_port' => $this->daemonSFTP, 'bind_port' => $this->listen_port_sftp,
], ],
], ],
'allowed_mounts' => $this->mounts->pluck('source')->toArray(), 'allowed_mounts' => $this->mounts->pluck('source')->toArray(),
@ -177,6 +189,7 @@ class Node extends Model
* Returns the configuration in Yaml format. * Returns the configuration in Yaml format.
* *
* @return string * @return string
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/ */
public function getYamlConfiguration() public function getYamlConfiguration()
{ {
@ -187,6 +200,7 @@ class Node extends Model
* Returns the configuration in JSON format. * Returns the configuration in JSON format.
* *
* @return string * @return string
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/ */
public function getJsonConfiguration(bool $pretty = false) public function getJsonConfiguration(bool $pretty = false)
{ {
@ -195,6 +209,8 @@ class Node extends Model
/** /**
* Helper function to return the decrypted key for a node. * Helper function to return the decrypted key for a node.
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/ */
public function getDecryptedKey(): string public function getDecryptedKey(): string
{ {

View File

@ -144,7 +144,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
public function getNodeWithResourceUsage(int $node_id): Node public function getNodeWithResourceUsage(int $node_id): Node
{ {
$instance = $this->getBuilder() $instance = $this->getBuilder()
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemonListen', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate']) ->select(['nodes.id', 'nodes.fqdn', 'nodes.public_port_http', 'nodes.scheme', 'nodes.daemon_token', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate'])
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk') ->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id') ->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.id', $node_id); ->where('nodes.id', $node_id);

View File

@ -28,13 +28,7 @@ class NodeTransformer extends BaseTransformer
*/ */
public function transform(Node $model): array public function transform(Node $model): array
{ {
$response = collect($model->toArray())->mapWithKeys(function ($value, $key) { $response = $model->toArray();
// I messed up early in 2016 when I named this column as poorly
// as I did. This is the tragic result of my mistakes.
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
return [snake_case($key) => $value];
})->toArray();
$response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at); $response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at);
$response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at); $response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at);

View File

@ -46,7 +46,7 @@ class ServerTransformer extends BaseClientTransformer
'node' => $server->node->name, 'node' => $server->node->name,
'sftp_details' => [ 'sftp_details' => [
'ip' => $server->node->fqdn, 'ip' => $server->node->fqdn,
'port' => $server->node->daemonSFTP, 'port' => $server->node->public_port_sftp,
], ],
'description' => $server->description, 'description' => $server->description,
'limits' => [ 'limits' => [

View File

@ -27,6 +27,10 @@ class NodeFactory extends Factory
'public' => true, 'public' => true,
'name' => $this->faker->firstName, 'name' => $this->faker->firstName,
'fqdn' => $this->faker->ipv4, 'fqdn' => $this->faker->ipv4,
'listen_port_http' => 8080,
'listen_port_sftp' => 2022,
'public_port_http' => 8080,
'public_port_sftp' => 2022,
'scheme' => 'http', 'scheme' => 'http',
'behind_proxy' => false, 'behind_proxy' => false,
'memory' => 1024, 'memory' => 1024,
@ -36,9 +40,7 @@ class NodeFactory extends Factory
'upload_size' => 100, 'upload_size' => 100,
'daemon_token_id' => Str::random(Node::DAEMON_TOKEN_ID_LENGTH), 'daemon_token_id' => Str::random(Node::DAEMON_TOKEN_ID_LENGTH),
'daemon_token' => Crypt::encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH)), 'daemon_token' => Crypt::encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH)),
'daemonListen' => 8080, 'daemon_base' => '/var/lib/pterodactyl/volumes',
'daemonSFTP' => 2022,
'daemonBase' => '/var/lib/pterodactyl/volumes',
]; ];
} }
} }

View File

@ -23,7 +23,7 @@ class AddNodes extends Migration
$table->mediumInteger('disk_overallocate')->unsigned()->nullable(); $table->mediumInteger('disk_overallocate')->unsigned()->nullable();
$table->char('daemonSecret', 36)->unique(); $table->char('daemonSecret', 36)->unique();
$table->smallInteger('daemonListen')->unsigned()->default(8080); $table->smallInteger('daemonListen')->unsigned()->default(8080);
$table->smallInteger('daemonSFTP')->unsgined()->default(2022); $table->smallInteger('daemonSFTP')->unsigned()->default(2022);
$table->string('daemonBase')->default('/home/daemon-files'); $table->string('daemonBase')->default('/home/daemon-files');
$table->timestamps(); $table->timestamps();
}); });

View File

@ -19,7 +19,7 @@ class AddDatabaseHostIdColumnToNodesTable extends Migration
}); });
Schema::table('nodes', function (Blueprint $table) { Schema::table('nodes', function (Blueprint $table) {
$table->integer('database_host_id')->nullable()->unsigned()->after('daemonBase'); $table->integer('database_host_id')->nullable()->unsigned()->after('location_id');
$table->index('database_host_id')->nullable(); $table->index('database_host_id')->nullable();
$table->foreign('database_host_id')->references('id')->on('database_hosts')->onDelete('set null'); $table->foreign('database_host_id')->references('id')->on('database_hosts')->onDelete('set null');
}); });

View File

@ -0,0 +1,63 @@
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePortColumnsOnNodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('nodes', function (Blueprint $table) {
$table->renameColumn('daemonListen', 'listen_port_http');
$table->renameColumn('daemonSFTP', 'listen_port_sftp');
$table->renameColumn('daemonBase', 'daemon_base');
});
Schema::table('nodes', function (Blueprint $table) {
$table->integer('listen_port_http')->unsigned()->default(8080)->after('fqdn')->change();
$table->integer('listen_port_sftp')->unsigned()->default(2022)->after('listen_port_sftp')->change();
$table->integer('public_port_http')->unsigned()->default(8080)->after('listen_port_http');
$table->integer('public_port_sftp')->unsigned()->default(2022)->after('listen_port_sftp');
});
DB::transaction(function () {
foreach (DB::select('SELECT id, listen_port_http, listen_port_sftp FROM nodes') as $datum) {
DB::update('UPDATE nodes SET public_port_http = ?, public_port_sftp = ? WHERE id = ?', [
$datum->listen_port_http,
$datum->listen_port_sftp,
$datum->id,
]);
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('nodes', function (Blueprint $table) {
$table->renameColumn('listen_port_http', 'daemonListen');
$table->renameColumn('listen_port_sftp', 'daemonSFTP');
$table->renameColumn('daemon_base', 'daemonBase');
$table->dropColumn('public_port_http');
$table->dropColumn('public_port_sftp');
});
Schema::table('nodes', function (Blueprint $table) {
$table->smallInteger('daemonListen')->unsigned()->default(8080)->after('daemon_token')->change();
$table->smallInteger('daemonSFTP')->unsigned()->default(2022)->after('daemonListen')->change();
});
}
}