2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Requests\Api\Admin\Servers;
|
|
|
|
|
|
|
|
|
|
use Closure;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use Illuminate\Validation\Validator;
|
2026-10-11 08:45:00 -04:00
|
|
|
use Pterodactyl\Actions\Servers\CreateServer;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Enum\Permissions;
|
|
|
|
|
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
|
|
|
|
|
use Pterodactyl\Http\Requests\Concerns\FiltersDeployTags;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
|
2026-10-12 02:35:44 -04:00
|
|
|
use Pterodactyl\Http\Requests\Concerns\ValidatesNodeAcceptsEgg;
|
|
|
|
|
use Pterodactyl\Models\Allocation;
|
|
|
|
|
use Pterodactyl\Models\Egg;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\Objects\DeploymentObject;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Pterodactyl\Models\Server;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
class StoreServerRequest extends AdminApiRequest
|
|
|
|
|
{
|
|
|
|
|
use FiltersDeployTags;
|
2026-10-11 08:45:00 -04:00
|
|
|
use ValidatesExtensionFields {
|
|
|
|
|
actionInput as private inputWithExtensions;
|
|
|
|
|
}
|
2026-10-12 02:35:44 -04:00
|
|
|
use ValidatesNodeAcceptsEgg;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
public function permissions(): array
|
|
|
|
|
{
|
|
|
|
|
return [Permissions::AdminServersCreate];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-11 08:45:00 -04:00
|
|
|
* Validation rules for creating a server, under the admin API's field names. The fields
|
|
|
|
|
* it checks differently from the action keep their own rules: it requires the environment
|
|
|
|
|
* and the database and backup limits, refuses a null OOM flag, and checks only that the
|
|
|
|
|
* egg exists, so a missing egg reports one error.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @return ValidationRules
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2026-10-11 08:45:00 -04:00
|
|
|
$rules = CreateServer::rules();
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'external_id' => $rules['external_id'],
|
|
|
|
|
'name' => $rules['name'],
|
2026-10-11 08:45:00 -04:00
|
|
|
'description' => $rules['description'],
|
2026-10-05 18:10:18 -04:00
|
|
|
'owner_id' => $rules['owner_id'],
|
2026-10-11 08:45:00 -04:00
|
|
|
'egg_id' => ['required', 'exists:eggs,id'],
|
2026-10-05 18:10:18 -04:00
|
|
|
'docker_image' => $rules['image'],
|
|
|
|
|
'startup' => $rules['startup'],
|
|
|
|
|
'environment' => ['present', 'array'],
|
2026-10-11 08:45:00 -04:00
|
|
|
'skip_scripts' => $rules['skip_scripts'],
|
2026-10-05 18:10:18 -04:00
|
|
|
'oom_disabled' => ['sometimes', 'boolean'],
|
|
|
|
|
|
|
|
|
|
'memory' => $rules['memory'],
|
|
|
|
|
'swap' => $rules['swap'],
|
|
|
|
|
'disk' => $rules['disk'],
|
|
|
|
|
'io' => $rules['io'],
|
|
|
|
|
'threads' => $rules['threads'],
|
|
|
|
|
'cpu' => $rules['cpu'],
|
|
|
|
|
|
2026-10-11 08:45:00 -04:00
|
|
|
'database_limit' => ['present', 'nullable', 'integer', 'min:0'],
|
2026-10-05 18:10:18 -04:00
|
|
|
'allocation_limit' => $rules['allocation_limit'],
|
2026-10-11 08:45:00 -04:00
|
|
|
'backup_limit' => ['present', 'nullable', 'integer', 'min:0'],
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
// Allocations are chosen by hand unless a deployment object picks them.
|
2026-10-11 08:45:00 -04:00
|
|
|
'primary_allocation_id' => ['exclude_with:deploy', ...$rules['allocation_id']],
|
|
|
|
|
'secondary_allocations_ids.*' => ['exclude_with:deploy', ...$rules['allocation_additional.*']],
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
'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'],
|
|
|
|
|
|
2026-10-11 08:45:00 -04:00
|
|
|
'start_on_completion' => $rules['start_on_completion'],
|
2026-10-12 02:35:44 -04:00
|
|
|
'ignore_tags' => ['sometimes', 'boolean'],
|
2026-10-05 18:10:18 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-11 08:45:00 -04:00
|
|
|
* 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().
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
2026-10-11 19:54:55 -04:00
|
|
|
* @return array<string, mixed>
|
2026-10-05 18:10:18 -04:00
|
|
|
*/
|
2026-10-11 08:45:00 -04:00
|
|
|
public function actionInput(): array
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
2026-10-11 08:45:00 -04:00
|
|
|
$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;
|
2026-10-12 02:35:44 -04:00
|
|
|
unset($input['docker_image'], $input['primary_allocation_id'], $input['secondary_allocations_ids'], $input['deploy'], $input['ignore_tags']);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-11 08:45:00 -04:00
|
|
|
return $input;
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return list<Closure(Validator): void>
|
|
|
|
|
*/
|
|
|
|
|
public function after(): array
|
|
|
|
|
{
|
2026-10-12 02:35:44 -04:00
|
|
|
return [$this->validateDeployTagsExist(...), $this->validateAllocationAcceptsEgg(...)];
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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'));
|
2026-10-11 16:17:55 -04:00
|
|
|
$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'))));
|
2026-10-05 18:10:18 -04:00
|
|
|
$object->setTags($this->resolveDeployTagSlugs());
|
|
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
|
}
|
2026-10-09 06:26:45 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function extensionFieldsModel(): Model|string
|
|
|
|
|
{
|
|
|
|
|
return Server::class;
|
|
|
|
|
}
|
2026-10-12 02:35:44 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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')));
|
|
|
|
|
}
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|