$rules['external_id'], 'name' => $rules['name'], 'description' => $rules['description'], 'owner_id' => $rules['owner_id'], 'egg_id' => ['required', 'exists:eggs,id'], 'docker_image' => $rules['image'], 'startup' => $rules['startup'], 'environment' => ['present', 'array'], 'skip_scripts' => $rules['skip_scripts'], 'oom_disabled' => ['sometimes', 'boolean'], 'memory' => $rules['memory'], 'swap' => $rules['swap'], 'disk' => $rules['disk'], 'io' => $rules['io'], 'threads' => $rules['threads'], 'cpu' => $rules['cpu'], 'database_limit' => ['present', 'nullable', 'integer', 'min:0'], 'allocation_limit' => $rules['allocation_limit'], 'backup_limit' => ['present', 'nullable', 'integer', 'min:0'], // Allocations are chosen by hand unless a deployment object picks them. 'primary_allocation_id' => ['exclude_with:deploy', ...$rules['allocation_id']], 'secondary_allocations_ids.*' => ['exclude_with:deploy', ...$rules['allocation_additional.*']], 'deploy' => ['sometimes', 'required', 'array'], 'deploy.locations' => ['present_with:deploy', 'array', 'list'], 'deploy.locations.*' => ['required', Rule::anyOf([['integer:strict'], ['string']]), 'integer', 'min:1'], 'deploy.dedicated_ip' => ['required_with:deploy', 'boolean'], 'deploy.port_range' => ['present_with:deploy', 'array', 'list'], 'deploy.port_range.*' => ['string'], // Deploy tags (D): the reservations this deployment opts into. Each may // be given as a tag id or a slug; existence is checked in after() // so the message can name the offending value. 'deploy.tags' => ['array'], 'deploy.tags.*' => ['string'], 'start_on_completion' => $rules['start_on_completion'], 'ignore_tags' => ['sometimes', 'boolean'], ]; } /** * The validated input under the action's field names: docker_image is its image, and * primary_allocation_id and secondary_allocations_ids its allocation_id and * allocation_additional. The deployment goes to the action as getDeploymentObject(). * * @return array */ public function actionInput(): array { $input = $this->inputWithExtensions(); $input['image'] = $input['docker_image'] ?? null; $input['allocation_id'] = $input['primary_allocation_id'] ?? null; $input['allocation_additional'] = $input['secondary_allocations_ids'] ?? null; unset($input['docker_image'], $input['primary_allocation_id'], $input['secondary_allocations_ids'], $input['deploy'], $input['ignore_tags']); return $input; } /** * @return list */ public function after(): array { return [$this->validateDeployTagsExist(...), $this->validateAllocationAcceptsEgg(...)]; } /** Build a deployment object from the deploy payload, or null when absent. */ public function getDeploymentObject(): ?DeploymentObject { if (($this->input('deploy')) === null) { return null; } $object = new DeploymentObject; $object->setDedicated($this->boolean('deploy.dedicated_ip')); $object->setLocations(array_map(fn (int|string $index): int => $this->integer("deploy.locations.{$index}"), array_keys($this->array('deploy.locations')))); $object->setPorts(array_map(fn (int|string $index): string => $this->string("deploy.port_range.{$index}")->toString(), array_keys($this->array('deploy.port_range')))); $object->setTags($this->resolveDeployTagSlugs()); return $object; } /** * {@inheritdoc} */ protected function extensionFieldsModel(): Model|string { return Server::class; } /** * A deployment picks a node that accepts the egg itself, so only a chosen allocation is * checked. */ private function validateAllocationAcceptsEgg(Validator $validator): void { if ($validator->errors()->isNotEmpty() || $this->has('deploy')) { return; } $validated = $validator->safe(); $node = Allocation::query()->findOrFail($validated->integer('primary_allocation_id'))->node; $this->validateNodeAcceptsEgg($validator, 'primary_allocation_id', $node, Egg::query()->findOrFail($validated->integer('egg_id'))); } }