From 6916b89638d31ab7c27e3a9695f9c48e7e1778ba Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 13 Jun 2024 15:48:36 +0200 Subject: [PATCH] Fill startup & image from egg if missing in request (#384) --- .../Api/Application/Servers/StoreServerRequest.php | 4 ++-- .../Application/Servers/UpdateServerStartupRequest.php | 4 ++-- app/Services/Servers/ServerCreationService.php | 8 ++++++++ app/Services/Servers/StartupModificationService.php | 4 ++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php index 263b7c540..86ac941fd 100644 --- a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php +++ b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php @@ -28,8 +28,8 @@ class StoreServerRequest extends ApplicationApiRequest 'description' => array_merge(['nullable'], $rules['description']), 'user' => $rules['owner_id'], 'egg' => $rules['egg_id'], - 'docker_image' => $rules['image'], - 'startup' => $rules['startup'], + 'docker_image' => 'sometimes|string', + 'startup' => 'sometimes|string', 'environment' => 'present|array', 'skip_scripts' => 'sometimes|boolean', 'oom_killer' => 'sometimes|boolean', diff --git a/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php b/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php index 11e15977d..47b74f886 100644 --- a/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php +++ b/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php @@ -20,10 +20,10 @@ class UpdateServerStartupRequest extends ApplicationApiRequest $data = Server::getRulesForUpdate($this->parameter('server', Server::class)); return [ - 'startup' => $data['startup'], + 'startup' => 'sometimes|string', 'environment' => 'present|array', 'egg' => $data['egg_id'], - 'image' => $data['image'], + 'image' => 'sometimes|string', 'skip_scripts' => 'present|boolean', ]; } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index d555f7876..7fd3e8b67 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -17,6 +17,7 @@ use App\Repositories\Daemon\DaemonServerRepository; use App\Services\Deployment\FindViableNodesService; use App\Services\Deployment\AllocationSelectionService; use App\Exceptions\Http\Connection\DaemonConnectionException; +use App\Models\Egg; class ServerCreationService { @@ -50,6 +51,13 @@ class ServerCreationService $data['oom_killer'] = !$data['oom_disabled']; } + /** @var Egg $egg */ + $egg = Egg::query()->findOrFail($data['egg_id']); + + // Fill missing fields from egg + $data['image'] = $data['image'] ?? collect($egg->docker_images)->first(); + $data['startup'] = $data['startup'] ?? $egg->startup; + // If a deployment object has been passed we need to get the allocation // that the server should use, and assign the node from that allocation. if ($deployment instanceof DeploymentObject) { diff --git a/app/Services/Servers/StartupModificationService.php b/app/Services/Servers/StartupModificationService.php index 25f14d39d..c8eb95cf6 100644 --- a/app/Services/Servers/StartupModificationService.php +++ b/app/Services/Servers/StartupModificationService.php @@ -76,6 +76,10 @@ class StartupModificationService $server = $server->forceFill([ 'egg_id' => $egg->id, ]); + + // Fill missing fields from egg + $data['docker_image'] = $data['docker_image'] ?? collect($egg->docker_images)->first(); + $data['startup'] = $data['startup'] ?? $egg->startup; } $server->fill([