From a75a347d6547a87be9e83759592bf8aa24e2875b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 17 Jan 2021 15:51:56 -0800 Subject: [PATCH] Remove suspended & installing fields, replace with single status field --- .../Admin/Servers/ServerViewController.php | 2 +- .../Controllers/Admin/ServersController.php | 4 +- .../Remote/SftpAuthenticationController.php | 6 +- .../Admin/Servers/ServerInstalled.php | 2 +- .../Server/AuthenticateServerAccess.php | 2 +- .../Server/AccessingValidServer.php | 4 +- app/Models/Server.php | 22 +++++--- .../Servers/ReinstallServerService.php | 2 +- .../ServerConfigurationStructureService.php | 4 +- .../Servers/ServerCreationService.php | 2 +- app/Services/Servers/SuspensionService.php | 7 +-- .../Api/Application/ServerTransformer.php | 4 +- .../Api/Client/ServerTransformer.php | 4 +- database/factories/ModelFactory.php | 3 +- ...52623_add_generic_server_status_column.php | 55 +++++++++++++++++++ resources/views/admin/servers/index.blade.php | 4 +- .../servers/partials/navigation.blade.php | 2 +- .../views/admin/servers/view/index.blade.php | 8 +-- .../views/admin/servers/view/manage.blade.php | 4 +- .../Client/Server/SettingsControllerTest.php | 6 +- .../Servers/ServerCreationServiceTest.php | 2 +- .../StartupModificationServiceTest.php | 2 +- .../Servers/SuspensionServiceTest.php | 15 ++--- .../Server/AccessingValidServerTest.php | 8 +-- 24 files changed, 115 insertions(+), 59 deletions(-) create mode 100644 database/migrations/2021_01_17_152623_add_generic_server_status_column.php diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 5c2440b24..64c2b7f49 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -207,7 +207,7 @@ class ServerViewController extends Controller */ public function manage(Request $request, Server $server) { - if ($server->installed > 1) { + if ($server->status === Server::STATUS_INSTALL_FAILED) { throw new DisplayException( 'This server is in a failed install state and cannot be recovered. Please delete and re-create the server.' ); diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index bec5ac4aa..29016f792 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -228,12 +228,12 @@ class ServersController extends Controller */ public function toggleInstall(Server $server) { - if ($server->installed > 1) { + if ($server->status === Server::STATUS_INSTALL_FAILED) { throw new DisplayException(trans('admin/server.exceptions.marked_as_failed')); } $this->repository->update($server->id, [ - 'installed' => ! $server->installed, + 'status' => $server->isInstalled() ? Server::STATUS_INSTALLING : null, ], true, true); $this->alert->success(trans('admin/server.alerts.install_toggled'))->flash(); diff --git a/app/Http/Controllers/Api/Remote/SftpAuthenticationController.php b/app/Http/Controllers/Api/Remote/SftpAuthenticationController.php index cab532e81..efa6fe48c 100644 --- a/app/Http/Controllers/Api/Remote/SftpAuthenticationController.php +++ b/app/Http/Controllers/Api/Remote/SftpAuthenticationController.php @@ -118,10 +118,8 @@ class SftpAuthenticationController extends Controller // Remember, for security purposes, only reveal the existence of the server to people that // have provided valid credentials, and have permissions to know about it. - if ($server->installed !== 1 || $server->suspended) { - throw new BadRequestHttpException( - 'Server is not installed or is currently suspended.' - ); + if ($server->isSuspended() || !$server->isInstalled()) { + throw new BadRequestHttpException('Server is not installed or is currently suspended.'); } return new JsonResponse([ diff --git a/app/Http/Middleware/Admin/Servers/ServerInstalled.php b/app/Http/Middleware/Admin/Servers/ServerInstalled.php index 2f0a384f3..69c7d5488 100644 --- a/app/Http/Middleware/Admin/Servers/ServerInstalled.php +++ b/app/Http/Middleware/Admin/Servers/ServerInstalled.php @@ -29,7 +29,7 @@ class ServerInstalled ); } - if ($server->installed !== 1) { + if (! $server->isInstalled()) { throw new HttpException( Response::HTTP_FORBIDDEN, 'Access to this resource is not allowed due to the current installation state.' ); diff --git a/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php b/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php index e9eaa143e..902a5e4df 100644 --- a/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php +++ b/app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php @@ -64,7 +64,7 @@ class AuthenticateServerAccess } } - if ($server->suspended && ! $request->routeIs('api:client:server.resources')) { + if ($server->isSuspended() && ! $request->routeIs('api:client:server.resources')) { throw new BadRequestHttpException( 'This server is currently suspended and the functionality requested is unavailable.' ); diff --git a/app/Http/Middleware/Server/AccessingValidServer.php b/app/Http/Middleware/Server/AccessingValidServer.php index 2491414c7..77a9e0beb 100644 --- a/app/Http/Middleware/Server/AccessingValidServer.php +++ b/app/Http/Middleware/Server/AccessingValidServer.php @@ -63,7 +63,7 @@ class AccessingValidServer $isApiRequest = $request->expectsJson() || $request->is(...$this->config->get('pterodactyl.json_routes', [])); $server = $this->repository->getByUuid($attributes instanceof Server ? $attributes->uuid : $attributes); - if ($server->suspended) { + if ($server->isSuspended()) { if ($isApiRequest) { throw new AccessDeniedHttpException('Server is suspended and cannot be accessed.'); } @@ -73,7 +73,7 @@ class AccessingValidServer // Servers can have install statuses other than 1 or 0, so don't check // for a bool-type operator here. - if ($server->installed !== 1) { + if (! $server->isInstalled()) { if ($isApiRequest) { throw new ConflictHttpException('Server is still completing the installation process.'); } diff --git a/app/Models/Server.php b/app/Models/Server.php index b65ef662e..775fbf3c3 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -15,8 +15,8 @@ use Znck\Eloquent\Traits\BelongsToThrough; * @property int $node_id * @property string $name * @property string $description + * @property string|null $status * @property bool $skip_scripts - * @property bool $suspended * @property int $owner_id * @property int $memory * @property int $swap @@ -30,7 +30,6 @@ use Znck\Eloquent\Traits\BelongsToThrough; * @property int $egg_id * @property string $startup * @property string $image - * @property int $installed * @property int $allocation_limit * @property int $database_limit * @property int $backup_limit @@ -64,9 +63,9 @@ class Server extends Model */ const RESOURCE_NAME = 'server'; - const STATUS_INSTALLING = 0; - const STATUS_INSTALLED = 1; - const STATUS_INSTALL_FAILED = 2; + const STATUS_INSTALLING = 'installing'; + const STATUS_INSTALL_FAILED = 'install_failed'; + const STATUS_SUSPENDED = 'suspended'; /** * The table associated with the model. @@ -82,6 +81,7 @@ class Server extends Model * @var array */ protected $attributes = [ + 'status' => self::STATUS_INSTALLING, 'oom_disabled' => true, ]; @@ -104,7 +104,7 @@ class Server extends Model * * @var array */ - protected $guarded = ['id', 'installed', self::CREATED_AT, self::UPDATED_AT, 'deleted_at']; + protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at']; /** * @var array @@ -115,6 +115,7 @@ class Server extends Model 'name' => 'required|string|min:1|max:191', 'node_id' => 'required|exists:nodes,id', 'description' => 'string', + 'status' => 'nullable|string', 'memory' => 'required|numeric|min:0', 'swap' => 'required|numeric|min:-1', 'io' => 'required|numeric|between:10,1000', @@ -142,7 +143,6 @@ class Server extends Model protected $casts = [ 'node_id' => 'integer', 'skip_scripts' => 'boolean', - 'suspended' => 'boolean', 'owner_id' => 'integer', 'memory' => 'integer', 'swap' => 'integer', @@ -153,7 +153,6 @@ class Server extends Model 'allocation_id' => 'integer', 'nest_id' => 'integer', 'egg_id' => 'integer', - 'installed' => 'integer', 'database_limit' => 'integer', 'allocation_limit' => 'integer', 'backup_limit' => 'integer', @@ -176,7 +175,12 @@ class Server extends Model */ public function isInstalled(): bool { - return $this->installed === 1; + return $this->status !== self::STATUS_INSTALLING && $this->status !== self::STATUS_INSTALL_FAILED; + } + + public function isSuspended(): bool + { + return $this->status === self::STATUS_SUSPENDED; } /** diff --git a/app/Services/Servers/ReinstallServerService.php b/app/Services/Servers/ReinstallServerService.php index 6f5b56083..b922464ca 100644 --- a/app/Services/Servers/ReinstallServerService.php +++ b/app/Services/Servers/ReinstallServerService.php @@ -44,7 +44,7 @@ class ReinstallServerService public function handle(Server $server) { return $this->connection->transaction(function () use ($server) { - $server->forceFill(['installed' => Server::STATUS_INSTALLING])->save(); + $server->fill(['status' => Server::STATUS_INSTALLING])->save(); $this->daemonServerRepository->setServer($server)->reinstall(); diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index b942a270a..1bffdb547 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -60,7 +60,7 @@ class ServerConfigurationStructureService { return [ 'uuid' => $server->uuid, - 'suspended' => $server->suspended, + 'suspended' => $server->isSuspended(), 'environment' => $this->environment->handle($server), 'invocation' => $server->startup, 'skip_egg_scripts' => $server->skip_scripts, @@ -137,7 +137,7 @@ class ServerConfigurationStructureService 'skip_scripts' => $server->skip_scripts, ], 'rebuild' => false, - 'suspended' => (int)$server->suspended, + 'suspended' => $server->isSuspended() ? 1 : 0, ]; } } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 76d371ab2..7c11dab9b 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -229,8 +229,8 @@ class ServerCreationService 'node_id' => Arr::get($data, 'node_id'), 'name' => Arr::get($data, 'name'), 'description' => Arr::get($data, 'description') ?? '', + 'status' => Server::STATUS_INSTALLING, 'skip_scripts' => Arr::get($data, 'skip_scripts') ?? isset($data['skip_scripts']), - 'suspended' => false, 'owner_id' => Arr::get($data, 'owner_id'), 'memory' => Arr::get($data, 'memory'), 'swap' => Arr::get($data, 'swap'), diff --git a/app/Services/Servers/SuspensionService.php b/app/Services/Servers/SuspensionService.php index 87fd0a334..3f3035cb0 100644 --- a/app/Services/Servers/SuspensionService.php +++ b/app/Services/Servers/SuspensionService.php @@ -6,7 +6,6 @@ use Webmozart\Assert\Assert; use Pterodactyl\Models\Server; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Repositories\Wings\DaemonServerRepository; -use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Pterodactyl\Exceptions\Http\Server\ServerTransferringException; class SuspensionService @@ -54,7 +53,7 @@ class SuspensionService // Nothing needs to happen if we're suspending the server and it is already // suspended in the database. Additionally, nothing needs to happen if the server // is not suspended and we try to un-suspend the instance. - if ($isSuspending === $server->suspended) { + if ($isSuspending === $server->isSuspended()) { return; } @@ -63,9 +62,9 @@ class SuspensionService throw new ServerTransferringException; } - $this->connection->transaction(function () use ($action, $server) { + $this->connection->transaction(function () use ($action, $server, $isSuspending) { $server->update([ - 'suspended' => $action === self::ACTION_SUSPEND, + 'status' => $isSuspending ? Server::STATUS_SUSPENDED : null, ]); // Only send the suspension request to wings if the server is not currently being transferred. diff --git a/app/Transformers/Api/Application/ServerTransformer.php b/app/Transformers/Api/Application/ServerTransformer.php index 10c343d8c..70a346247 100644 --- a/app/Transformers/Api/Application/ServerTransformer.php +++ b/app/Transformers/Api/Application/ServerTransformer.php @@ -66,7 +66,7 @@ class ServerTransformer extends BaseTransformer 'identifier' => $server->uuidShort, 'name' => $server->name, 'description' => $server->description, - 'suspended' => (bool) $server->suspended, + 'suspended' => $server->isSuspended(), 'limits' => [ 'memory' => $server->memory, 'swap' => $server->swap, @@ -88,7 +88,7 @@ class ServerTransformer extends BaseTransformer 'container' => [ 'startup_command' => $server->startup, 'image' => $server->image, - 'installed' => (int) $server->installed === 1, + 'installed' => $server->isInstalled() ? 1 : 0, 'environment' => $this->environmentService->handle($server), ], $server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at), diff --git a/app/Transformers/Api/Client/ServerTransformer.php b/app/Transformers/Api/Client/ServerTransformer.php index 0673f9b57..79102bb33 100644 --- a/app/Transformers/Api/Client/ServerTransformer.php +++ b/app/Transformers/Api/Client/ServerTransformer.php @@ -70,8 +70,8 @@ class ServerTransformer extends BaseClientTransformer 'allocations' => $server->allocation_limit, 'backups' => $server->backup_limit, ], - 'is_suspended' => $server->suspended, - 'is_installing' => $server->installed !== 1, + 'is_suspended' => $server->isSuspended(), + 'is_installing' => ! $server->isInstalled(), 'is_transferring' => ! is_null($server->transfer), ]; } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 4997a9b6f..f48c8e6e7 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -27,14 +27,13 @@ $factory->define(Pterodactyl\Models\Server::class, function (Faker $faker) { 'name' => $faker->firstName, 'description' => implode(' ', $faker->sentences()), 'skip_scripts' => 0, - 'suspended' => 0, + 'status' => null, 'memory' => 512, 'swap' => 0, 'disk' => 512, 'io' => 500, 'cpu' => 0, 'oom_disabled' => 0, - 'installed' => 1, 'database_limit' => null, 'allocation_limit' => null, 'created_at' => Carbon::now(), diff --git a/database/migrations/2021_01_17_152623_add_generic_server_status_column.php b/database/migrations/2021_01_17_152623_add_generic_server_status_column.php new file mode 100644 index 000000000..536b5642b --- /dev/null +++ b/database/migrations/2021_01_17_152623_add_generic_server_status_column.php @@ -0,0 +1,55 @@ +string('status')->nullable()->after('description'); + }); + + DB::transaction(function () { + DB::update('UPDATE servers SET `status` = \'suspended\' WHERE `suspended` = 1'); + DB::update('UPDATE servers SET `status` = \'installing\' WHERE `installed` = 0'); + DB::update('UPDATE servers SET `status` = \'install_failed\' WHERE `installed` = 2'); + }); + + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('suspended'); + $table->dropColumn('installed'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->unsignedTinyInteger('suspended')->default(0); + $table->unsignedTinyInteger('installed')->default(0); + }); + + DB::transaction(function () { + DB::update('UPDATE servers SET `suspended` = 1 WHERE `status` = \'suspended\''); + DB::update('UPDATE servers SET `installed` = 1 WHERE `status` IS NULL'); + DB::update('UPDATE servers SET `installed` = 2 WHERE `status` = \'install_failed\''); + }); + + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('status'); + }); + } +} diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index ce039944f..e152158ce 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -57,9 +57,9 @@ {{ $server->allocation->alias }}:{{ $server->allocation->port }} - @if($server->suspended) + @if($server->isSuspended()) Suspended - @elseif(! $server->installed) + @elseif(! $server->isInstalled()) Installing @else Active diff --git a/resources/views/admin/servers/partials/navigation.blade.php b/resources/views/admin/servers/partials/navigation.blade.php index 0474787de..964eac8e3 100644 --- a/resources/views/admin/servers/partials/navigation.blade.php +++ b/resources/views/admin/servers/partials/navigation.blade.php @@ -8,7 +8,7 @@