Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/ServerConfigurationStructureService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Pterodactyl\Models\Mount;
6
use Pterodactyl\Models\Server;
7
8
class ServerConfigurationStructureService
9
{
10
/**
11
* ServerConfigurationStructureService constructor.
12
*/
13
public function __construct(private EnvironmentService $environment)
14
{
15
}
16
17
/**
18
* Return a configuration array for a specific server when passed a server model.
19
*
20
* DO NOT MODIFY THIS FUNCTION. This powers legacy code handling for the new Wings
21
* daemon, if you modify the structure eggs will break unexpectedly.
22
*/
23
public function handle(Server $server, array $override = [], bool $legacy = false): array
24
{
25
$clone = $server;
26
// If any overrides have been set on this call make sure to update them on the
27
// cloned instance so that the configuration generated uses them.
28
if (!empty($override)) {
29
$clone = $server->fresh();
30
foreach ($override as $key => $value) {
31
$clone->setAttribute($key, $value);
32
}
33
}
34
35
return $legacy
36
? $this->returnLegacyFormat($clone)
37
: $this->returnCurrentFormat($clone);
38
}
39
40
/**
41
* Returns the new data format used for the Wings daemon.
42
*/
43
protected function returnCurrentFormat(Server $server): array
44
{
45
return [
46
'uuid' => $server->uuid,
47
'meta' => [
48
'name' => $server->name,
49
'description' => $server->description,
50
],
51
'suspended' => $server->isSuspended(),
52
'environment' => $this->environment->handle($server),
53
'invocation' => $server->startup,
54
'skip_egg_scripts' => $server->skip_scripts,
55
'build' => [
56
'memory_limit' => $server->memory,
57
'swap' => $server->swap,
58
'io_weight' => $server->io,
59
'cpu_limit' => $server->cpu,
60
'threads' => $server->threads,
61
'disk_space' => $server->disk,
62
'oom_disabled' => $server->oom_disabled,
63
],
64
'container' => [
65
'image' => $server->image,
66
// This field is deprecated — use the value in the "build" block.
67
//
68
// TODO: remove this key in V2.
69
'oom_disabled' => $server->oom_disabled,
70
'requires_rebuild' => false,
71
],
72
'allocations' => [
73
'force_outgoing_ip' => $server->egg->force_outgoing_ip,
74
'default' => [
75
'ip' => $server->allocation->ip,
76
'port' => $server->allocation->port,
77
],
78
'mappings' => $server->getAllocationMappings(),
79
],
80
'mounts' => $server->mounts->map(function (Mount $mount) {
81
return [
82
'source' => $mount->source,
83
'target' => $mount->target,
84
'read_only' => $mount->read_only,
85
];
86
}),
87
'egg' => [
88
'id' => $server->egg->uuid,
89
'file_denylist' => $server->egg->inherit_file_denylist,
90
],
91
];
92
}
93
94
/**
95
* Returns the legacy server data format to continue support for old egg configurations
96
* that have not yet been updated.
97
*
98
* @deprecated
99
*/
100
protected function returnLegacyFormat(Server $server): array
101
{
102
return [
103
'uuid' => $server->uuid,
104
'build' => [
105
'default' => [
106
'ip' => $server->allocation->ip,
107
'port' => $server->allocation->port,
108
],
109
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
110
return $item->pluck('port');
111
})->toArray(),
112
'env' => $this->environment->handle($server),
113
'oom_disabled' => $server->oom_disabled,
114
'memory' => (int) $server->memory,
115
'swap' => (int) $server->swap,
116
'io' => (int) $server->io,
117
'cpu' => (int) $server->cpu,
118
'threads' => $server->threads,
119
'disk' => (int) $server->disk,
120
'image' => $server->image,
121
],
122
'service' => [
123
'egg' => $server->egg->uuid,
124
'skip_scripts' => $server->skip_scripts,
125
],
126
'rebuild' => false,
127
'suspended' => $server->isSuspended() ? 1 : 0,
128
];
129
}
130
}
131
132