diff --git a/app/Exceptions/Http/Server/ServerStateConflictException.php b/app/Exceptions/Http/Server/ServerStateConflictException.php index b4951d8f2..1e20082a3 100644 --- a/app/Exceptions/Http/Server/ServerStateConflictException.php +++ b/app/Exceptions/Http/Server/ServerStateConflictException.php @@ -2,6 +2,7 @@ namespace App\Exceptions\Http\Server; +use App\Enums\ServerState; use App\Models\Server; use Symfony\Component\HttpKernel\Exception\ConflictHttpException; @@ -20,7 +21,7 @@ class ServerStateConflictException extends ConflictHttpException $message = 'The node of this server is currently under maintenance and the functionality requested is unavailable.'; } elseif (!$server->isInstalled()) { $message = 'This server has not yet completed its installation process, please try again later.'; - } elseif ($server->status === Server::STATUS_RESTORING_BACKUP) { + } elseif ($server->status === ServerState::RestoringBackup) { $message = 'This server is currently restoring from a backup, please try again later.'; } elseif (!is_null($server->transfer)) { $message = 'This server is currently being transferred to a new machine, please try again later.'; diff --git a/app/Filament/Resources/ServerResource.php b/app/Filament/Resources/ServerResource.php index e11fbd02a..452ae1be8 100644 --- a/app/Filament/Resources/ServerResource.php +++ b/app/Filament/Resources/ServerResource.php @@ -48,21 +48,15 @@ class ServerResource extends Resource return $details['state'] ?? 'unknown'; }) - ->options([ - 'running' => 'Running', - 'starting' => 'Starting', - 'stopping' => 'Stopping', - 'offline' => 'Offline', - 'unknown' => 'Unknown', - ]) - - ->colors([ - 'running' => 'success', - 'offline' => 'danger', - 'starting' => 'primary', - 'stopping' => 'warning', - 'unknown' => 'primary', - ]) + ->options(collect(ContainerStatus::cases())->mapWithKeys( + fn (ContainerStatus $status) => [$status->value => str($status->value)->ucwords()] + )) + ->colors(collect(ContainerStatus::cases())->mapWithKeys( + fn (ContainerStatus $status) => [$status->value => $status->color()] + )) + ->icons(collect(ContainerStatus::cases())->mapWithKeys( + fn (ContainerStatus $status) => [$status->value => $status->icon()] + )) ->grouped() ->columnSpanFull() ->inline(), @@ -73,23 +67,15 @@ class ServerResource extends Resource ->hiddenOn('create') ->disableOptionWhen(fn ($state, $value) => $state !== $value) ->formatStateUsing(fn ($state) => $state ?? 'none') - ->options([ - 'none' => 'None', - Server::STATUS_INSTALLING => str(Server::STATUS_INSTALLING)->title()->replace('_', ' '), - Server::STATUS_INSTALL_FAILED => str(Server::STATUS_INSTALL_FAILED)->title()->replace('_', ' '), - Server::STATUS_REINSTALL_FAILED => str(Server::STATUS_REINSTALL_FAILED)->title()->replace('_', ' '), - Server::STATUS_SUSPENDED => str(Server::STATUS_SUSPENDED)->title()->replace('_', ' '), - Server::STATUS_RESTORING_BACKUP => str(Server::STATUS_RESTORING_BACKUP)->title()->replace('_', ' '), - ]) - - ->colors([ - 'none' => 'primary', - Server::STATUS_INSTALLING => 'primary', - Server::STATUS_INSTALL_FAILED => 'danger', - Server::STATUS_REINSTALL_FAILED => 'danger', - Server::STATUS_SUSPENDED => 'danger', - Server::STATUS_RESTORING_BACKUP => 'primary', - ]) + ->options(collect(ServerState::cases())->mapWithKeys( + fn (ServerState $state) => [$state->value => str($state->value)->replace('_', ' ')->ucwords()] + )) + ->colors(collect(ServerState::cases())->mapWithKeys( + fn (ServerState $state) => [$state->value => $state->color()] + )) + ->icons(collect(ServerState::cases())->mapWithKeys( + fn (ServerState $state) => [$state->value => $state->icon()] + )) ->grouped() ->columnSpanFull() ->inline(), diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 5763ee238..29a0e6322 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin\Servers; +use App\Enums\ServerState; use App\Models\DatabaseHost; use App\Models\Egg; use App\Models\Mount; @@ -110,7 +111,7 @@ class ServerViewController extends Controller */ public function manage(Request $request, Server $server): View { - if ($server->status === Server::STATUS_INSTALL_FAILED) { + if ($server->status === ServerState::InstallFailed) { 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 5d5250df7..9aa0af24e 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Enums\ServerState; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; @@ -71,11 +72,11 @@ class ServersController extends Controller */ public function toggleInstall(Server $server): RedirectResponse { - if ($server->status === Server::STATUS_INSTALL_FAILED) { + if ($server->status === ServerState::InstallFailed) { throw new DisplayException(trans('admin/server.exceptions.marked_as_failed')); } - $server->status = $server->isInstalled() ? Server::STATUS_INSTALLING : null; + $server->status = $server->isInstalled() ? ServerState::Installing : null; $server->save(); $this->alert->success(trans('admin/server.alerts.install_toggled'))->flash(); diff --git a/app/Http/Controllers/Api/Client/Servers/BackupController.php b/app/Http/Controllers/Api/Client/Servers/BackupController.php index f59b087aa..ab1e441e2 100644 --- a/app/Http/Controllers/Api/Client/Servers/BackupController.php +++ b/app/Http/Controllers/Api/Client/Servers/BackupController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api\Client\Servers; +use App\Enums\ServerState; use Illuminate\Http\Request; use App\Models\Backup; use App\Models\Server; @@ -212,7 +213,7 @@ class BackupController extends ClientApiController // Update the status right away for the server so that we know not to allow certain // actions against it via the Panel API. - $server->update(['status' => Server::STATUS_RESTORING_BACKUP]); + $server->update(['status' => ServerState::RestoringBackup]); $this->daemonRepository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate')); }); diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php index cc3439580..824453045 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api\Remote\Servers; +use App\Enums\ServerState; use App\Models\Backup; use Illuminate\Http\Request; use App\Models\Server; @@ -81,7 +82,7 @@ class ServerDetailsController extends Controller ->latest('timestamp'), ]) ->where('node_id', $node->id) - ->where('status', Server::STATUS_RESTORING_BACKUP) + ->where('status', ServerState::RestoringBackup) ->get(); $this->connection->transaction(function () use ($node, $servers) { @@ -108,7 +109,7 @@ class ServerDetailsController extends Controller // Update any server marked as installing or restoring as being in a normal state // at this point in the process. Server::query()->where('node_id', $node->id) - ->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP]) + ->whereIn('status', [ServerState::Installing, ServerState::RestoringBackup]) ->update(['status' => null]); }); diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php index 98a8f37d8..49eaf2c51 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api\Remote\Servers; +use App\Enums\ServerState; use Illuminate\Http\Response; use App\Models\Server; use Illuminate\Http\JsonResponse; @@ -36,16 +37,16 @@ class ServerInstallController extends Controller // Make sure the type of failure is accurate if (!$request->boolean('successful')) { - $status = Server::STATUS_INSTALL_FAILED; + $status = ServerState::InstallFailed; if ($request->boolean('reinstall')) { - $status = Server::STATUS_REINSTALL_FAILED; + $status = ServerState::ReinstallFailed; } } // Keep the server suspended if it's already suspended - if ($server->status === Server::STATUS_SUSPENDED) { - $status = Server::STATUS_SUSPENDED; + if ($server->status === ServerState::Suspended) { + $status = ServerState::Suspended; } $previouslyInstalledAt = $server->installed_at; diff --git a/app/Models/Server.php b/app/Models/Server.php index b450973b4..e6896d16d 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Enums\ServerState; use App\Exceptions\Http\Connection\DaemonConnectionException; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Notifications\Notifiable; @@ -111,13 +112,7 @@ class Server extends Model * API representation using fractal. */ public const RESOURCE_NAME = 'server'; - - public const STATUS_INSTALLING = 'installing'; - public const STATUS_INSTALL_FAILED = 'install_failed'; - public const STATUS_REINSTALL_FAILED = 'reinstall_failed'; - public const STATUS_SUSPENDED = 'suspended'; - public const STATUS_RESTORING_BACKUP = 'restoring_backup'; - + /** * The table associated with the model. */ @@ -128,7 +123,7 @@ class Server extends Model * on server instances unless the user specifies otherwise in the request. */ protected $attributes = [ - 'status' => self::STATUS_INSTALLING, + 'status' => ServerState::Installing, 'oom_disabled' => true, 'installed_at' => null, ]; @@ -171,6 +166,7 @@ class Server extends Model { return [ 'node_id' => 'integer', + 'status' => ServerState::class, 'skip_scripts' => 'boolean', 'owner_id' => 'integer', 'memory' => 'integer', @@ -203,12 +199,12 @@ class Server extends Model public function isInstalled(): bool { - return $this->status !== self::STATUS_INSTALLING && $this->status !== self::STATUS_INSTALL_FAILED; + return $this->status !== ServerState::Installing && $this->status !== ServerState::InstallFailed; } public function isSuspended(): bool { - return $this->status === self::STATUS_SUSPENDED; + return $this->status === ServerState::Suspended; } /** @@ -359,7 +355,7 @@ class Server extends Model $this->isSuspended() || $this->node->isUnderMaintenance() || !$this->isInstalled() || - $this->status === self::STATUS_RESTORING_BACKUP || + $this->status === ServerState::RestoringBackup || !is_null($this->transfer) ) { throw new ServerStateConflictException($this); @@ -376,7 +372,7 @@ class Server extends Model { if ( !$this->isInstalled() || - $this->status === self::STATUS_RESTORING_BACKUP || + $this->status === ServerState::RestoringBackup || !is_null($this->transfer) ) { throw new ServerStateConflictException($this); diff --git a/app/Services/Servers/ReinstallServerService.php b/app/Services/Servers/ReinstallServerService.php index 475decef4..301de9711 100644 --- a/app/Services/Servers/ReinstallServerService.php +++ b/app/Services/Servers/ReinstallServerService.php @@ -2,6 +2,7 @@ namespace App\Services\Servers; +use App\Enums\ServerState; use App\Models\Server; use Illuminate\Database\ConnectionInterface; use App\Repositories\Daemon\DaemonServerRepository; @@ -25,7 +26,7 @@ class ReinstallServerService public function handle(Server $server): Server { return $this->connection->transaction(function () use ($server) { - $server->fill(['status' => Server::STATUS_INSTALLING])->save(); + $server->fill(['status' => ServerState::Installing])->save(); $this->daemonServerRepository->setServer($server)->reinstall(); diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index d544d7d9d..7ba8b73e9 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -2,6 +2,7 @@ namespace App\Services\Servers; +use App\Enums\ServerState; use App\Models\ServerVariable; use Ramsey\Uuid\Uuid; use Illuminate\Support\Arr; @@ -132,7 +133,7 @@ class ServerCreationService 'node_id' => Arr::get($data, 'node_id'), 'name' => Arr::get($data, 'name'), 'description' => Arr::get($data, 'description') ?? '', - 'status' => Server::STATUS_INSTALLING, + 'status' => ServerState::Installing, 'skip_scripts' => Arr::get($data, 'skip_scripts') ?? isset($data['skip_scripts']), 'owner_id' => Arr::get($data, 'owner_id'), 'memory' => Arr::get($data, 'memory'), diff --git a/app/Services/Servers/SuspensionService.php b/app/Services/Servers/SuspensionService.php index 196a0a680..5e317fc1b 100644 --- a/app/Services/Servers/SuspensionService.php +++ b/app/Services/Servers/SuspensionService.php @@ -2,6 +2,7 @@ namespace App\Services\Servers; +use App\Enums\ServerState; use Webmozart\Assert\Assert; use App\Models\Server; use App\Repositories\Daemon\DaemonServerRepository; @@ -44,7 +45,7 @@ class SuspensionService // Update the server's suspension status. $server->update([ - 'status' => $isSuspending ? Server::STATUS_SUSPENDED : null, + 'status' => $isSuspending ? ServerState::Suspended : null, ]); try { @@ -53,7 +54,7 @@ class SuspensionService } catch (\Exception $exception) { // Rollback the server's suspension status if daemon fails to sync the server. $server->update([ - 'status' => $isSuspending ? null : Server::STATUS_SUSPENDED, + 'status' => $isSuspending ? null : ServerState::Suspended, ]); throw $exception; } diff --git a/tests/Integration/Api/Client/Server/SettingsControllerTest.php b/tests/Integration/Api/Client/Server/SettingsControllerTest.php index b40a484c4..dab6264fa 100644 --- a/tests/Integration/Api/Client/Server/SettingsControllerTest.php +++ b/tests/Integration/Api/Client/Server/SettingsControllerTest.php @@ -2,6 +2,7 @@ namespace App\Tests\Integration\Api\Client\Server; +use App\Enums\ServerState; use Illuminate\Http\Response; use App\Models\Server; use App\Models\Permission; @@ -93,7 +94,7 @@ class SettingsControllerTest extends ClientApiIntegrationTestCase ->assertStatus(Response::HTTP_ACCEPTED); $server = $server->refresh(); - $this->assertSame(Server::STATUS_INSTALLING, $server->status); + $this->assertSame(ServerState::Installing, $server->status); } /** diff --git a/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php b/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php index f460ecc50..b2d5d4919 100644 --- a/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php +++ b/tests/Integration/Api/Remote/SftpAuthenticationControllerTest.php @@ -2,6 +2,7 @@ namespace App\Tests\Integration\Api\Remote; +use App\Enums\ServerState; use App\Models\Node; use App\Models\User; use App\Models\Server; @@ -207,9 +208,9 @@ class SftpAuthenticationControllerTest extends IntegrationTestCase public static function serverStateDataProvider(): array { return [ - 'installing' => [Server::STATUS_INSTALLING], - 'suspended' => [Server::STATUS_SUSPENDED], - 'restoring a backup' => [Server::STATUS_RESTORING_BACKUP], + 'installing' => [ServerState::Installing->value], + 'suspended' => [ServerState::Suspended->value], + 'restoring a backup' => [ServerState::RestoringBackup->value], ]; } diff --git a/tests/Integration/Jobs/Schedule/RunTaskJobTest.php b/tests/Integration/Jobs/Schedule/RunTaskJobTest.php index de082823c..c6ececf5a 100644 --- a/tests/Integration/Jobs/Schedule/RunTaskJobTest.php +++ b/tests/Integration/Jobs/Schedule/RunTaskJobTest.php @@ -2,6 +2,7 @@ namespace App\Tests\Integration\Jobs\Schedule; +use App\Enums\ServerState; use Carbon\Carbon; use Carbon\CarbonImmutable; use GuzzleHttp\Psr7\Request; @@ -151,7 +152,7 @@ class RunTaskJobTest extends IntegrationTestCase public function testTaskIsNotRunIfServerIsSuspended(): void { $server = $this->createServerModel([ - 'status' => Server::STATUS_SUSPENDED, + 'status' => ServerState::Suspended, ]); $schedule = Schedule::factory()->for($server)->create([ diff --git a/tests/Integration/Services/Servers/SuspensionServiceTest.php b/tests/Integration/Services/Servers/SuspensionServiceTest.php index 423e2534f..1af1d6c45 100644 --- a/tests/Integration/Services/Servers/SuspensionServiceTest.php +++ b/tests/Integration/Services/Servers/SuspensionServiceTest.php @@ -2,8 +2,8 @@ namespace App\Tests\Integration\Services\Servers; +use App\Enums\ServerState; use Mockery\MockInterface; -use App\Models\Server; use App\Services\Servers\SuspensionService; use App\Tests\Integration\IntegrationTestCase; use App\Repositories\Daemon\DaemonServerRepository; @@ -47,7 +47,7 @@ class SuspensionServiceTest extends IntegrationTestCase $server->refresh(); $this->assertFalse($server->isSuspended()); - $server->update(['status' => Server::STATUS_SUSPENDED]); + $server->update(['status' => ServerState::Suspended]); $this->getService()->toggle($server); $server->refresh();