Skip to content

Commit d58fd72

Browse files
committed
Correctly pass along startup variables for a server; closes pterodactyl#2255
1 parent 9d95c5a commit d58fd72

File tree

4 files changed

+22
-65
lines changed

4 files changed

+22
-65
lines changed

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ public function findWithVariables(int $id): Server;
6565
*/
6666
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server;
6767

68-
/**
69-
* Return all of the server variables possible and default to the variable
70-
* default if there is no value defined for the specific server requested.
71-
*
72-
* @param int $id
73-
* @param bool $returnAsObject
74-
* @return array|object
75-
*
76-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
77-
*/
78-
public function getVariablesWithValues(int $id, bool $returnAsObject = false);
79-
8068
/**
8169
* Return enough data to be used for the creation of a server via the daemon.
8270
*

app/Http/Controllers/Admin/Servers/ServerViewController.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\View\Factory;
1010
use Pterodactyl\Exceptions\DisplayException;
1111
use Pterodactyl\Http\Controllers\Controller;
12+
use Pterodactyl\Services\Servers\EnvironmentService;
1213
use Pterodactyl\Repositories\Eloquent\NestRepository;
1314
use Pterodactyl\Repositories\Eloquent\NodeRepository;
1415
use Pterodactyl\Repositories\Eloquent\MountRepository;
@@ -56,6 +57,11 @@ class ServerViewController extends Controller
5657
*/
5758
private $nodeRepository;
5859

60+
/**
61+
* @var \Pterodactyl\Services\Servers\EnvironmentService
62+
*/
63+
private $environmentService;
64+
5965
/**
6066
* ServerViewController constructor.
6167
*
@@ -66,6 +72,7 @@ class ServerViewController extends Controller
6672
* @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository
6773
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
6874
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
75+
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
6976
*/
7077
public function __construct(
7178
Factory $view,
@@ -74,7 +81,8 @@ public function __construct(
7481
MountRepository $mountRepository,
7582
NestRepository $nestRepository,
7683
NodeRepository $nodeRepository,
77-
ServerRepository $repository
84+
ServerRepository $repository,
85+
EnvironmentService $environmentService
7886
) {
7987
$this->view = $view;
8088
$this->databaseHostRepository = $databaseHostRepository;
@@ -83,6 +91,7 @@ public function __construct(
8391
$this->nestRepository = $nestRepository;
8492
$this->nodeRepository = $nodeRepository;
8593
$this->repository = $repository;
94+
$this->environmentService = $environmentService;
8695
}
8796

8897
/**
@@ -138,12 +147,12 @@ public function build(Request $request, Server $server)
138147
*/
139148
public function startup(Request $request, Server $server)
140149
{
141-
$parameters = $this->repository->getVariablesWithValues($server->id, true);
142150
$nests = $this->nestRepository->getWithEggs();
151+
$variables = $this->environmentService->handle($server);
143152

144153
$this->plainInject([
145154
'server' => $server,
146-
'server_variables' => $parameters->data,
155+
'server_variables' => $variables,
147156
'nests' => $nests->map(function (Nest $item) {
148157
return array_merge($item->toArray(), [
149158
'eggs' => $item->eggs->keyBy('id')->toArray(),

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -131,45 +131,6 @@ public function getPrimaryAllocation(Server $server, bool $refresh = false): Ser
131131
return $server;
132132
}
133133

134-
/**
135-
* Return all of the server variables possible and default to the variable
136-
* default if there is no value defined for the specific server requested.
137-
*
138-
* @param int $id
139-
* @param bool $returnAsObject
140-
* @return array|object
141-
*
142-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
143-
*/
144-
public function getVariablesWithValues(int $id, bool $returnAsObject = false)
145-
{
146-
$this->getBuilder()
147-
->with('variables', 'egg.variables')
148-
->findOrFail($id);
149-
150-
try {
151-
$instance = $this->getBuilder()->with('variables', 'egg.variables')->find($id, $this->getColumns());
152-
} catch (ModelNotFoundException $exception) {
153-
throw new RecordNotFoundException;
154-
}
155-
156-
$data = [];
157-
$instance->getRelation('egg')->getRelation('variables')->each(function ($item) use (&$data, $instance) {
158-
$display = $instance->getRelation('variables')->where('variable_id', $item->id)->pluck('variable_value')->first();
159-
160-
$data[$item->env_variable] = $display ?? $item->default_value;
161-
});
162-
163-
if ($returnAsObject) {
164-
return (object) [
165-
'data' => $data,
166-
'server' => $instance,
167-
];
168-
}
169-
170-
return $data;
171-
}
172-
173134
/**
174135
* Return enough data to be used for the creation of a server via the daemon.
175136
*

app/Services/Servers/EnvironmentService.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Services\Servers;
44

55
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Models\EggVariable;
67
use Illuminate\Contracts\Config\Repository as ConfigRepository;
78
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
89

@@ -63,35 +64,33 @@ public function getEnvironmentKeys(): array
6364
*
6465
* @param \Pterodactyl\Models\Server $server
6566
* @return array
66-
*
67-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6867
*/
6968
public function handle(Server $server): array
7069
{
71-
$variables = $this->repository->getVariablesWithValues($server->id);
70+
$variables = $server->variables->toBase()->mapWithKeys(function (EggVariable $variable) {
71+
return [$variable->env_variable => $variable->server_value ?? $variable->default_value];
72+
});
7273

7374
// Process environment variables defined in this file. This is done first
7475
// in order to allow run-time and config defined variables to take
7576
// priority over built-in values.
7677
foreach ($this->getEnvironmentMappings() as $key => $object) {
77-
$variables[$key] = object_get($server, $object);
78+
$variables->put($key, object_get($server, $object));
7879
}
7980

8081
// Process variables set in the configuration file.
8182
foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) {
82-
if (is_callable($object)) {
83-
$variables[$key] = call_user_func($object, $server);
84-
} else {
85-
$variables[$key] = object_get($server, $object);
86-
}
83+
$variables->put(
84+
$key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
85+
);
8786
}
8887

8988
// Process dynamically included environment variables.
9089
foreach ($this->additional as $key => $closure) {
91-
$variables[$key] = call_user_func($closure, $server);
90+
$variables->put($key, call_user_func($closure, $server));
9291
}
9392

94-
return $variables;
93+
return $variables->toArray();
9594
}
9695

9796
/**

0 commit comments

Comments
 (0)