Correctly pass along startup variables for a server; closes #2255

This commit is contained in:
Dane Everitt 2020-08-25 19:11:25 -07:00
parent 9d95c5ab32
commit d58fd72bf5
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 22 additions and 65 deletions

View file

@ -65,18 +65,6 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter
*/ */
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server; public function getPrimaryAllocation(Server $server, bool $refresh = false): Server;
/**
* Return all of the server variables possible and default to the variable
* default if there is no value defined for the specific server requested.
*
* @param int $id
* @param bool $returnAsObject
* @return array|object
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getVariablesWithValues(int $id, bool $returnAsObject = false);
/** /**
* Return enough data to be used for the creation of a server via the daemon. * Return enough data to be used for the creation of a server via the daemon.
* *

View file

@ -9,6 +9,7 @@ use Pterodactyl\Models\Server;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\MountRepository;
@ -56,6 +57,11 @@ class ServerViewController extends Controller
*/ */
private $nodeRepository; private $nodeRepository;
/**
* @var \Pterodactyl\Services\Servers\EnvironmentService
*/
private $environmentService;
/** /**
* ServerViewController constructor. * ServerViewController constructor.
* *
@ -66,6 +72,7 @@ class ServerViewController extends Controller
* @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository * @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository * @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
*/ */
public function __construct( public function __construct(
Factory $view, Factory $view,
@ -74,7 +81,8 @@ class ServerViewController extends Controller
MountRepository $mountRepository, MountRepository $mountRepository,
NestRepository $nestRepository, NestRepository $nestRepository,
NodeRepository $nodeRepository, NodeRepository $nodeRepository,
ServerRepository $repository ServerRepository $repository,
EnvironmentService $environmentService
) { ) {
$this->view = $view; $this->view = $view;
$this->databaseHostRepository = $databaseHostRepository; $this->databaseHostRepository = $databaseHostRepository;
@ -83,6 +91,7 @@ class ServerViewController extends Controller
$this->nestRepository = $nestRepository; $this->nestRepository = $nestRepository;
$this->nodeRepository = $nodeRepository; $this->nodeRepository = $nodeRepository;
$this->repository = $repository; $this->repository = $repository;
$this->environmentService = $environmentService;
} }
/** /**
@ -138,12 +147,12 @@ class ServerViewController extends Controller
*/ */
public function startup(Request $request, Server $server) public function startup(Request $request, Server $server)
{ {
$parameters = $this->repository->getVariablesWithValues($server->id, true);
$nests = $this->nestRepository->getWithEggs(); $nests = $this->nestRepository->getWithEggs();
$variables = $this->environmentService->handle($server);
$this->plainInject([ $this->plainInject([
'server' => $server, 'server' => $server,
'server_variables' => $parameters->data, 'server_variables' => $variables,
'nests' => $nests->map(function (Nest $item) { 'nests' => $nests->map(function (Nest $item) {
return array_merge($item->toArray(), [ return array_merge($item->toArray(), [
'eggs' => $item->eggs->keyBy('id')->toArray(), 'eggs' => $item->eggs->keyBy('id')->toArray(),

View file

@ -131,45 +131,6 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
return $server; return $server;
} }
/**
* Return all of the server variables possible and default to the variable
* default if there is no value defined for the specific server requested.
*
* @param int $id
* @param bool $returnAsObject
* @return array|object
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getVariablesWithValues(int $id, bool $returnAsObject = false)
{
$this->getBuilder()
->with('variables', 'egg.variables')
->findOrFail($id);
try {
$instance = $this->getBuilder()->with('variables', 'egg.variables')->find($id, $this->getColumns());
} catch (ModelNotFoundException $exception) {
throw new RecordNotFoundException;
}
$data = [];
$instance->getRelation('egg')->getRelation('variables')->each(function ($item) use (&$data, $instance) {
$display = $instance->getRelation('variables')->where('variable_id', $item->id)->pluck('variable_value')->first();
$data[$item->env_variable] = $display ?? $item->default_value;
});
if ($returnAsObject) {
return (object) [
'data' => $data,
'server' => $instance,
];
}
return $data;
}
/** /**
* Return enough data to be used for the creation of a server via the daemon. * Return enough data to be used for the creation of a server via the daemon.
* *

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -63,35 +64,33 @@ class EnvironmentService
* *
* @param \Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @return array * @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle(Server $server): array public function handle(Server $server): array
{ {
$variables = $this->repository->getVariablesWithValues($server->id); $variables = $server->variables->toBase()->mapWithKeys(function (EggVariable $variable) {
return [$variable->env_variable => $variable->server_value ?? $variable->default_value];
});
// Process environment variables defined in this file. This is done first // Process environment variables defined in this file. This is done first
// in order to allow run-time and config defined variables to take // in order to allow run-time and config defined variables to take
// priority over built-in values. // priority over built-in values.
foreach ($this->getEnvironmentMappings() as $key => $object) { foreach ($this->getEnvironmentMappings() as $key => $object) {
$variables[$key] = object_get($server, $object); $variables->put($key, object_get($server, $object));
} }
// Process variables set in the configuration file. // Process variables set in the configuration file.
foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) { foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) {
if (is_callable($object)) { $variables->put(
$variables[$key] = call_user_func($object, $server); $key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
} else { );
$variables[$key] = object_get($server, $object);
}
} }
// Process dynamically included environment variables. // Process dynamically included environment variables.
foreach ($this->additional as $key => $closure) { foreach ($this->additional as $key => $closure) {
$variables[$key] = call_user_func($closure, $server); $variables->put($key, call_user_func($closure, $server));
} }
return $variables; return $variables->toArray();
} }
/** /**