2017-10-06 04:09:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2020-09-13 16:59:54 +00:00
|
|
|
use Pterodactyl\Models\Mount;
|
2017-10-06 04:09:43 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
|
|
|
|
class ServerConfigurationStructureService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* ServerConfigurationStructureService constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private EnvironmentService $environment)
|
2020-10-06 04:54:29 +00:00
|
|
|
{
|
2017-10-06 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Return a configuration array for a specific server when passed a server model.
|
|
|
|
*
|
2019-09-22 22:30:53 +00:00
|
|
|
* DO NOT MODIFY THIS FUNCTION. This powers legacy code handling for the new Wings
|
|
|
|
* daemon, if you modify the structure eggs will break unexpectedly.
|
2017-10-06 04:09:43 +00:00
|
|
|
*/
|
2020-12-24 18:10:40 +00:00
|
|
|
public function handle(Server $server, array $override = [], bool $legacy = false): array
|
2017-10-06 04:09:43 +00:00
|
|
|
{
|
2020-12-24 18:10:40 +00:00
|
|
|
$clone = $server;
|
|
|
|
// If any overrides have been set on this call make sure to update them on the
|
|
|
|
// cloned instance so that the configuration generated uses them.
|
|
|
|
if (!empty($override)) {
|
|
|
|
$clone = $server->fresh();
|
|
|
|
foreach ($override as $key => $value) {
|
|
|
|
$clone->setAttribute($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $legacy
|
|
|
|
? $this->returnLegacyFormat($clone)
|
|
|
|
: $this->returnCurrentFormat($clone);
|
2019-11-16 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the new data format used for the Wings daemon.
|
|
|
|
*/
|
2021-01-23 21:59:52 +00:00
|
|
|
protected function returnCurrentFormat(Server $server): array
|
2019-11-16 21:33:01 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'uuid' => $server->uuid,
|
2022-07-24 21:15:24 +00:00
|
|
|
'meta' => [
|
|
|
|
'name' => $server->name,
|
|
|
|
'description' => $server->description,
|
|
|
|
],
|
2021-01-17 23:51:56 +00:00
|
|
|
'suspended' => $server->isSuspended(),
|
2019-11-16 21:33:01 +00:00
|
|
|
'environment' => $this->environment->handle($server),
|
2022-12-15 00:05:46 +00:00
|
|
|
'invocation' => !is_null($server->startup) ? $server->startup : $server->egg->startup,
|
2020-08-30 16:40:19 +00:00
|
|
|
'skip_egg_scripts' => $server->skip_scripts,
|
2019-11-16 21:33:01 +00:00
|
|
|
'build' => [
|
2019-12-22 05:01:38 +00:00
|
|
|
'memory_limit' => $server->memory,
|
2019-11-16 21:33:01 +00:00
|
|
|
'swap' => $server->swap,
|
2019-12-22 05:01:38 +00:00
|
|
|
'io_weight' => $server->io,
|
|
|
|
'cpu_limit' => $server->cpu,
|
2020-03-29 20:55:06 +00:00
|
|
|
'threads' => $server->threads,
|
2019-12-22 05:01:38 +00:00
|
|
|
'disk_space' => $server->disk,
|
2023-01-17 19:11:31 +00:00
|
|
|
// TODO: remove oom_disabled and use oom_killer, this requires a Wings update.
|
|
|
|
'oom_disabled' => !$server->oom_killer,
|
|
|
|
'oom_killer' => $server->oom_killer,
|
2019-11-16 21:33:01 +00:00
|
|
|
],
|
|
|
|
'container' => [
|
|
|
|
'image' => $server->image,
|
|
|
|
],
|
|
|
|
'allocations' => [
|
|
|
|
'default' => [
|
|
|
|
'ip' => $server->allocation->ip,
|
|
|
|
'port' => $server->allocation->port,
|
|
|
|
],
|
2022-12-15 00:05:46 +00:00
|
|
|
'force_outgoing_ip' => $server->egg->force_outgoing_ip,
|
2019-11-25 04:40:29 +00:00
|
|
|
'mappings' => $server->getAllocationMappings(),
|
2019-11-16 21:33:01 +00:00
|
|
|
],
|
2020-09-13 16:59:54 +00:00
|
|
|
'mounts' => $server->mounts->map(function (Mount $mount) {
|
|
|
|
return [
|
|
|
|
'source' => $mount->source,
|
|
|
|
'target' => $mount->target,
|
|
|
|
'read_only' => $mount->read_only,
|
|
|
|
];
|
|
|
|
}),
|
2021-01-11 01:02:14 +00:00
|
|
|
'egg' => [
|
|
|
|
'id' => $server->egg->uuid,
|
2021-01-27 05:08:53 +00:00
|
|
|
'file_denylist' => $server->egg->inherit_file_denylist,
|
2021-01-23 20:09:16 +00:00
|
|
|
],
|
2019-11-16 21:33:01 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the legacy server data format to continue support for old egg configurations
|
|
|
|
* that have not yet been updated.
|
|
|
|
*
|
2020-10-31 18:14:28 +00:00
|
|
|
* @deprecated
|
2019-11-16 21:33:01 +00:00
|
|
|
*/
|
2021-01-23 21:59:52 +00:00
|
|
|
protected function returnLegacyFormat(Server $server): array
|
2019-11-16 21:33:01 +00:00
|
|
|
{
|
2017-10-06 04:09:43 +00:00
|
|
|
return [
|
|
|
|
'uuid' => $server->uuid,
|
|
|
|
'build' => [
|
|
|
|
'default' => [
|
|
|
|
'ip' => $server->allocation->ip,
|
|
|
|
'port' => $server->allocation->port,
|
|
|
|
],
|
|
|
|
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
|
|
|
|
return $item->pluck('port');
|
|
|
|
})->toArray(),
|
2017-10-27 04:49:54 +00:00
|
|
|
'env' => $this->environment->handle($server),
|
2023-01-17 19:11:31 +00:00
|
|
|
'oom_disabled' => !$server->oom_killer,
|
2021-01-23 20:09:16 +00:00
|
|
|
'memory' => (int) $server->memory,
|
|
|
|
'swap' => (int) $server->swap,
|
|
|
|
'io' => (int) $server->io,
|
|
|
|
'cpu' => (int) $server->cpu,
|
2020-03-29 18:41:55 +00:00
|
|
|
'threads' => $server->threads,
|
2021-01-23 20:09:16 +00:00
|
|
|
'disk' => (int) $server->disk,
|
2017-10-06 04:09:43 +00:00
|
|
|
'image' => $server->image,
|
|
|
|
],
|
|
|
|
'service' => [
|
2017-10-09 02:36:22 +00:00
|
|
|
'egg' => $server->egg->uuid,
|
2017-10-06 04:09:43 +00:00
|
|
|
'skip_scripts' => $server->skip_scripts,
|
|
|
|
],
|
|
|
|
'rebuild' => false,
|
2021-01-17 23:51:56 +00:00
|
|
|
'suspended' => $server->isSuspended() ? 1 : 0,
|
2017-10-06 04:09:43 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|