Skip to content

Commit 8f00445

Browse files
committed
Update server creation data logic
1 parent 2848d18 commit 8f00445

File tree

6 files changed

+177
-76
lines changed

6 files changed

+177
-76
lines changed

app/Http/Controllers/Api/Application/Servers/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public function index(GetServersRequest $request): array
7272
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest $request
7373
* @return \Illuminate\Http\JsonResponse
7474
*
75+
* @throws \Throwable
7576
* @throws \Illuminate\Validation\ValidationException
7677
* @throws \Pterodactyl\Exceptions\DisplayException
77-
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
7878
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
7979
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
8080
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException

app/Models/Allocation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
namespace Pterodactyl\Models;
44

5+
/**
6+
* @property int $id
7+
* @property int $node_id
8+
* @property string $ip
9+
* @property string|null $ip_alias
10+
* @property int $port
11+
* @property int|null $server_id
12+
* @property \Carbon\Carbon|null $created_at
13+
* @property \Carbon\Carbon|null $updated_at
14+
*
15+
* @property string $alias
16+
* @property bool $has_alias
17+
*
18+
* @property \Pterodactyl\Models\Server|null $server
19+
* @property \Pterodactyl\Models\Node $node
20+
*/
521
class Allocation extends Validable
622
{
723
/**

app/Repositories/Wings/DaemonServerRepository.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,26 @@ public function getDetails(): array
2929

3030
return json_decode($response->getBody()->__toString(), true);
3131
}
32+
33+
/**
34+
* Creates a new server on the Wings daemon.
35+
*
36+
* @param array $data
37+
*
38+
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
39+
*/
40+
public function create(array $data): void
41+
{
42+
Assert::isInstanceOf($this->server, Server::class);
43+
44+
try {
45+
$this->getHttpClient()->post(
46+
'/api/servers', [
47+
'json' => $data,
48+
]
49+
);
50+
} catch (TransferException $exception) {
51+
throw new DaemonConnectionException($exception);
52+
}
53+
}
3254
}

app/Services/Eggs/EggConfigurationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function replacePlaceholders(Server $server, object $configs)
107107
{
108108
// Get the legacy configuration structure for the server so that we
109109
// can property map the egg placeholders to values.
110-
$structure = $this->configurationStructureService->handle($server);
110+
$structure = $this->configurationStructureService->handle($server, true);
111111

112112
foreach ($configs as $file => $data) {
113113
foreach ($data->find ?? [] as &$value) {

app/Services/Servers/ServerConfigurationStructureService.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,76 @@ public function __construct(
4747
* daemon, if you modify the structure eggs will break unexpectedly.
4848
*
4949
* @param \Pterodactyl\Models\Server $server
50+
* @param bool $legacy
5051
* @return array
5152
*
5253
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5354
*/
54-
public function handle(Server $server): array
55+
public function handle(Server $server, bool $legacy = false): array
5556
{
5657
$server->loadMissing(self::REQUIRED_RELATIONS);
5758

59+
return $legacy ?
60+
$this->returnLegacyFormat($server)
61+
: $this->returnCurrentFormat($server);
62+
}
63+
64+
/**
65+
* Returns the new data format used for the Wings daemon.
66+
*
67+
* @param \Pterodactyl\Models\Server $server
68+
* @return array
69+
*
70+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
71+
*/
72+
protected function returnCurrentFormat(Server $server)
73+
{
74+
return [
75+
'uuid' => $server->uuid,
76+
'suspended' => $server->suspended,
77+
'environment' => $this->environment->handle($server),
78+
'build' => [
79+
'oom_disabled' => $server->oom_disabled,
80+
'memory' => $server->memory,
81+
'swap' => $server->swap,
82+
'io' => $server->io,
83+
'cpu' => $server->cpu,
84+
'disk' => $server->disk,
85+
],
86+
'service' => [
87+
'egg' => $server->egg->uuid,
88+
'pack' => $server->pack ? $server->pack->uuid : null,
89+
'skip_scripts' => $server->skip_scripts,
90+
],
91+
'container' => [
92+
'image' => $server->image,
93+
'requires_rebuild' => false,
94+
],
95+
'allocations' => [
96+
'default' => [
97+
'ip' => $server->allocation->ip,
98+
'port' => $server->allocation->port,
99+
],
100+
'mappings' => [
101+
$server->allocations->groupBy('ip')->map(function ($item) {
102+
return $item->pluck('port');
103+
})->toArray(),
104+
],
105+
],
106+
];
107+
}
108+
109+
/**
110+
* Returns the legacy server data format to continue support for old egg configurations
111+
* that have not yet been updated.
112+
*
113+
* @param \Pterodactyl\Models\Server $server
114+
* @return array
115+
*
116+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
117+
*/
118+
protected function returnLegacyFormat(Server $server)
119+
{
58120
return [
59121
'uuid' => $server->uuid,
60122
'build' => [

0 commit comments

Comments
 (0)