From 5f56ff0fedbca946d94b42a68b07745d24663b19 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 11 Feb 2021 10:21:32 -0700 Subject: [PATCH] nodes: rename port columns, add public_ port columns --- .../Application/Nodes/StoreNodeRequest.php | 16 ++--- app/Models/Node.php | 58 ++++++++++------- app/Repositories/Eloquent/NodeRepository.php | 2 +- .../Api/Application/NodeTransformer.php | 8 +-- .../Api/Client/ServerTransformer.php | 2 +- database/Factories/NodeFactory.php | 8 ++- .../2016_01_23_200648_add_nodes.php | 2 +- ...database_host_id_column_to_nodes_table.php | 2 +- ...726_change_port_columns_on_nodes_table.php | 63 +++++++++++++++++++ 9 files changed, 116 insertions(+), 45 deletions(-) create mode 100644 database/migrations/2021_02_11_165726_change_port_columns_on_nodes_table.php diff --git a/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php b/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php index e196ba100..5a737d941 100644 --- a/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php +++ b/app/Http/Requests/Api/Application/Nodes/StoreNodeRequest.php @@ -21,6 +21,10 @@ class StoreNodeRequest extends ApplicationApiRequest 'name', 'location_id', 'fqdn', + 'listen_port_http', + 'listen_port_sftp', + 'public_port_http', + 'public_port_sftp', 'scheme', 'behind_proxy', 'memory', @@ -28,12 +32,8 @@ class StoreNodeRequest extends ApplicationApiRequest 'disk', 'disk_overallocate', 'upload_size', - 'daemonListen', - 'daemonSFTP', - 'daemonBase', + 'daemon_base', ])->mapWithKeys(function ($value, $key) { - $key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key; - return [snake_case($key) => $value]; })->toArray(); } @@ -62,11 +62,7 @@ class StoreNodeRequest extends ApplicationApiRequest public function validated() { $response = parent::validated(); - $response['daemonListen'] = $response['daemon_listen']; - $response['daemonSFTP'] = $response['daemon_sftp']; - $response['daemonBase'] = $response['daemon_base'] ?? (new Node())->getAttribute('daemonBase'); - - unset($response['daemon_base'], $response['daemon_listen'], $response['daemon_sftp']); + $response['daemon_base'] = $response['daemon_base'] ?? (new Node())->getAttribute('daemon_base'); return $response; } diff --git a/app/Models/Node.php b/app/Models/Node.php index a33f2f7f6..ae7cbf60f 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Models; +use Illuminate\Support\Str; use Symfony\Component\Yaml\Yaml; use Illuminate\Container\Container; use Illuminate\Notifications\Notifiable; @@ -14,7 +15,12 @@ use Illuminate\Contracts\Encryption\Encrypter; * @property string $name * @property string|null $description * @property int $location_id + * @property int|null $database_host_id * @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 bool $behind_proxy * @property bool $maintenance_mode @@ -25,10 +31,7 @@ use Illuminate\Contracts\Encryption\Encrypter; * @property int $upload_size * @property string $daemon_token_id * @property string $daemon_token - * @property int $daemonListen - * @property int $daemonSFTP - * @property string $daemonBase - * @property int|null $database_host_id + * @property string $daemon_base * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * @property \Pterodactyl\Models\Location $location @@ -71,10 +74,13 @@ class Node extends Model */ protected $casts = [ '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', 'disk' => 'integer', - 'daemonListen' => 'integer', - 'daemonSFTP' => 'integer', 'behind_proxy' => 'boolean', 'public' => 'boolean', 'maintenance_mode' => 'boolean', @@ -86,11 +92,11 @@ class Node extends Model * @var array */ 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', 'memory', 'memory_overallocate', 'disk', - 'disk_overallocate', 'upload_size', 'daemonBase', - 'daemonSFTP', 'daemonListen', + 'disk_overallocate', 'upload_size', 'daemon_base', 'description', 'maintenance_mode', ]; @@ -101,17 +107,20 @@ class Node extends Model 'name' => 'required|regex:/^([\w .-]{1,100})$/', 'description' => 'string|nullable', 'location_id' => 'required|exists:locations,id', + 'database_host_id' => 'required|exists:database_hosts,id', 'public' => 'boolean', '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', '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', + 'daemon_base' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/', 'maintenance_mode' => 'boolean', 'upload_size' => 'int|between:1,1024', ]; @@ -122,13 +131,15 @@ class Node extends Model * @var array */ protected $attributes = [ + 'listen_port_http' => 8080, + 'listen_port_sftp' => 2022, + 'public_port_http' => 8080, + 'public_port_sftp' => 2022, 'public' => true, 'behind_proxy' => false, 'memory_overallocate' => 0, 'disk_overallocate' => 0, - 'daemonBase' => '/var/lib/pterodactyl/volumes', - 'daemonSFTP' => 2022, - 'daemonListen' => 8080, + 'daemon_base' => '/var/lib/pterodactyl/volumes', 'maintenance_mode' => false, ]; @@ -137,13 +148,14 @@ class Node extends Model */ 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. * * @return array + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function getConfiguration() { @@ -154,18 +166,18 @@ class Node extends Model 'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token), 'api' => [ 'host' => '0.0.0.0', - 'port' => $this->daemonListen, + 'port' => $this->listen_port_http, 'ssl' => [ 'enabled' => (!$this->behind_proxy && $this->scheme === 'https'), - 'cert' => '/etc/letsencrypt/live/' . $this->fqdn . '/fullchain.pem', - 'key' => '/etc/letsencrypt/live/' . $this->fqdn . '/privkey.pem', + 'cert' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/fullchain.pem', + 'key' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/privkey.pem', ], 'upload_limit' => $this->upload_size, ], 'system' => [ - 'data' => $this->daemonBase, + 'data' => $this->daemon_base, 'sftp' => [ - 'bind_port' => $this->daemonSFTP, + 'bind_port' => $this->listen_port_sftp, ], ], 'allowed_mounts' => $this->mounts->pluck('source')->toArray(), @@ -177,6 +189,7 @@ class Node extends Model * Returns the configuration in Yaml format. * * @return string + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function getYamlConfiguration() { @@ -187,6 +200,7 @@ class Node extends Model * Returns the configuration in JSON format. * * @return string + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function getJsonConfiguration(bool $pretty = false) { @@ -195,6 +209,8 @@ class Node extends Model /** * Helper function to return the decrypted key for a node. + * + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function getDecryptedKey(): string { diff --git a/app/Repositories/Eloquent/NodeRepository.php b/app/Repositories/Eloquent/NodeRepository.php index 5d1a8bcac..125a21fb3 100644 --- a/app/Repositories/Eloquent/NodeRepository.php +++ b/app/Repositories/Eloquent/NodeRepository.php @@ -144,7 +144,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa public function getNodeWithResourceUsage(int $node_id): Node { $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') ->leftJoin('servers', 'servers.node_id', '=', 'nodes.id') ->where('nodes.id', $node_id); diff --git a/app/Transformers/Api/Application/NodeTransformer.php b/app/Transformers/Api/Application/NodeTransformer.php index ec89b6202..f6f2a2cd1 100644 --- a/app/Transformers/Api/Application/NodeTransformer.php +++ b/app/Transformers/Api/Application/NodeTransformer.php @@ -28,13 +28,7 @@ class NodeTransformer extends BaseTransformer */ public function transform(Node $model): array { - $response = collect($model->toArray())->mapWithKeys(function ($value, $key) { - // 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->toArray(); $response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at); $response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at); diff --git a/app/Transformers/Api/Client/ServerTransformer.php b/app/Transformers/Api/Client/ServerTransformer.php index 99771e676..ed10c76d5 100644 --- a/app/Transformers/Api/Client/ServerTransformer.php +++ b/app/Transformers/Api/Client/ServerTransformer.php @@ -46,7 +46,7 @@ class ServerTransformer extends BaseClientTransformer 'node' => $server->node->name, 'sftp_details' => [ 'ip' => $server->node->fqdn, - 'port' => $server->node->daemonSFTP, + 'port' => $server->node->public_port_sftp, ], 'description' => $server->description, 'limits' => [ diff --git a/database/Factories/NodeFactory.php b/database/Factories/NodeFactory.php index 683d8a482..ce2c89f46 100644 --- a/database/Factories/NodeFactory.php +++ b/database/Factories/NodeFactory.php @@ -27,6 +27,10 @@ class NodeFactory extends Factory 'public' => true, 'name' => $this->faker->firstName, 'fqdn' => $this->faker->ipv4, + 'listen_port_http' => 8080, + 'listen_port_sftp' => 2022, + 'public_port_http' => 8080, + 'public_port_sftp' => 2022, 'scheme' => 'http', 'behind_proxy' => false, 'memory' => 1024, @@ -36,9 +40,7 @@ class NodeFactory extends Factory 'upload_size' => 100, 'daemon_token_id' => Str::random(Node::DAEMON_TOKEN_ID_LENGTH), 'daemon_token' => Crypt::encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH)), - 'daemonListen' => 8080, - 'daemonSFTP' => 2022, - 'daemonBase' => '/var/lib/pterodactyl/volumes', + 'daemon_base' => '/var/lib/pterodactyl/volumes', ]; } } diff --git a/database/migrations/2016_01_23_200648_add_nodes.php b/database/migrations/2016_01_23_200648_add_nodes.php index 52c0a29e6..58571579a 100644 --- a/database/migrations/2016_01_23_200648_add_nodes.php +++ b/database/migrations/2016_01_23_200648_add_nodes.php @@ -23,7 +23,7 @@ class AddNodes extends Migration $table->mediumInteger('disk_overallocate')->unsigned()->nullable(); $table->char('daemonSecret', 36)->unique(); $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->timestamps(); }); diff --git a/database/migrations/2021_01_30_193343_add_database_host_id_column_to_nodes_table.php b/database/migrations/2021_01_30_193343_add_database_host_id_column_to_nodes_table.php index 31b569f88..cba228051 100644 --- a/database/migrations/2021_01_30_193343_add_database_host_id_column_to_nodes_table.php +++ b/database/migrations/2021_01_30_193343_add_database_host_id_column_to_nodes_table.php @@ -19,7 +19,7 @@ class AddDatabaseHostIdColumnToNodesTable extends Migration }); 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->foreign('database_host_id')->references('id')->on('database_hosts')->onDelete('set null'); }); diff --git a/database/migrations/2021_02_11_165726_change_port_columns_on_nodes_table.php b/database/migrations/2021_02_11_165726_change_port_columns_on_nodes_table.php new file mode 100644 index 000000000..b7ab73058 --- /dev/null +++ b/database/migrations/2021_02_11_165726_change_port_columns_on_nodes_table.php @@ -0,0 +1,63 @@ +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(); + }); + } +}