Files

117 lines
4.3 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Services\Servers;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Collection;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use UnexpectedValueException;
class ServerConfigurationStructureService
{
2026-10-05 18:10:18 -04:00
public function __construct(private readonly EnvironmentService $environment) {}
/**
2026-10-12 00:21:31 -04:00
* The server configuration Wings reads. Its shape is a contract with Wings, so it must not
* change.
*
2026-10-11 19:54:55 -04:00
* @return array<string, mixed>
*/
2026-10-12 00:21:31 -04:00
public function forServer(Server $server): array
{
2026-10-12 00:21:31 -04:00
$server->loadMissing(['egg', 'allocation', 'allocations', 'mounts']);
$egg = $server->egg ?? throw new UnexpectedValueException('The server does not have an egg relationship.');
$allocation = $server->allocation ?? throw new UnexpectedValueException('The server does not have a primary allocation.');
2026-10-05 18:10:18 -04:00
2019-11-16 13:33:01 -08:00
return [
'uuid' => $server->uuid,
'meta' => [
'name' => $server->name,
'description' => $server->description,
],
'suspended' => $server->isSuspended(),
2026-10-12 00:21:31 -04:00
'environment' => $this->environment->forServer($server),
'invocation' => $server->startup,
'skip_egg_scripts' => $server->skip_scripts,
2019-11-16 13:33:01 -08:00
'build' => [
'memory_limit' => $server->memory,
2019-11-16 13:33:01 -08:00
'swap' => $server->swap,
'io_weight' => $server->io,
'cpu_limit' => $server->cpu,
2020-03-29 16:55:06 -04:00
'threads' => $server->threads,
'disk_space' => $server->disk,
'oom_disabled' => $server->oom_disabled,
2019-11-16 13:33:01 -08:00
],
'container' => [
'image' => $server->image,
'requires_rebuild' => false,
],
'allocations' => [
2026-10-05 18:10:18 -04:00
'force_outgoing_ip' => $egg->force_outgoing_ip,
2019-11-16 13:33:01 -08:00
'default' => [
2026-10-05 18:10:18 -04:00
'ip' => $allocation->ip,
'port' => $allocation->port,
2019-11-16 13:33:01 -08:00
],
'mappings' => $server->getAllocationMappings(),
2019-11-16 13:33:01 -08:00
],
2026-10-05 18:10:18 -04:00
'mounts' => $server->mounts->map(fn (Mount $mount): array => [
'source' => $mount->source,
'target' => $mount->target,
'read_only' => $mount->read_only,
])->values()->all(),
'egg' => [
2026-10-05 18:10:18 -04:00
'id' => $egg->uuid,
'file_denylist' => $egg->inherit_file_denylist,
2021-01-23 12:09:16 -08:00
],
2019-11-16 13:33:01 -08:00
];
}
/**
2026-10-12 00:21:31 -04:00
* The structure the old daemon read, which egg configuration placeholders such as
* {{server.build.default.port}} still resolve against.
2019-11-16 13:33:01 -08:00
*
2026-10-11 19:54:55 -04:00
* @return array<string, mixed>
2019-11-16 13:33:01 -08:00
*/
2026-10-12 00:21:31 -04:00
public function legacyForServer(Server $server): array
2019-11-16 13:33:01 -08:00
{
2026-10-12 00:21:31 -04:00
$server->loadMissing(['egg', 'allocation', 'allocations']);
$egg = $server->egg ?? throw new UnexpectedValueException('The server does not have an egg relationship.');
$allocation = $server->allocation ?? throw new UnexpectedValueException('The server does not have a primary allocation.');
return [
'uuid' => $server->uuid,
'build' => [
'default' => [
2026-10-05 18:10:18 -04:00
'ip' => $allocation->ip,
'port' => $allocation->port,
],
2026-10-05 18:10:18 -04:00
'ports' => $server->allocations
->groupBy('ip')
->map(fn (Collection $allocations): array => $allocations
->map(fn (Allocation $allocation): int => $allocation->port)
->values()
->all())
->all(),
2026-10-12 00:21:31 -04:00
'env' => $this->environment->forServer($server),
'oom_disabled' => $server->oom_disabled,
2021-01-23 12:09:16 -08:00
'memory' => (int) $server->memory,
'swap' => (int) $server->swap,
'io' => (int) $server->io,
'cpu' => (int) $server->cpu,
2020-03-29 14:41:55 -04:00
'threads' => $server->threads,
2021-01-23 12:09:16 -08:00
'disk' => (int) $server->disk,
'image' => $server->image,
],
'service' => [
2026-10-05 18:10:18 -04:00
'egg' => $egg->uuid,
'skip_scripts' => $server->skip_scripts,
],
'rebuild' => false,
'suspended' => $server->isSuspended() ? 1 : 0,
];
}
}