Compare commits

...

1 commit

Author SHA1 Message Date
Lance Pioch
bd863d0ac4 Move logic into model 2022-10-20 19:43:11 -04:00
6 changed files with 50 additions and 88 deletions

View file

@ -9,7 +9,6 @@ use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService;
use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\Factory as ViewFactory;
use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
@ -33,7 +32,6 @@ class ServerViewController extends Controller
private NestRepository $nestRepository, private NestRepository $nestRepository,
private NodeRepository $nodeRepository, private NodeRepository $nodeRepository,
private ServerRepository $repository, private ServerRepository $repository,
private EnvironmentService $environmentService,
private ViewFactory $view private ViewFactory $view
) { ) {
} }
@ -76,7 +74,7 @@ class ServerViewController extends Controller
public function startup(Request $request, Server $server): View public function startup(Request $request, Server $server): View
{ {
$nests = $this->nestRepository->getWithEggs(); $nests = $this->nestRepository->getWithEggs();
$variables = $this->environmentService->handle($server); $variables = $server->getEnvironment();
$this->plainInject([ $this->plainInject([
'server' => $server, 'server' => $server,

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Http\Controllers\Api\Remote;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EggInstallController extends Controller class EggInstallController extends Controller
@ -13,7 +12,7 @@ class EggInstallController extends Controller
/** /**
* EggInstallController constructor. * EggInstallController constructor.
*/ */
public function __construct(private EnvironmentService $environment, private ServerRepositoryInterface $repository) public function __construct(private ServerRepositoryInterface $repository)
{ {
} }
@ -45,7 +44,7 @@ class EggInstallController extends Controller
'container' => $egg->copy_script_container, 'container' => $egg->copy_script_container,
'entry' => $egg->copy_script_entry, 'entry' => $egg->copy_script_entry,
], ],
'env' => $this->environment->handle($server), 'env' => $server->getEnvironment(),
]); ]);
} }
} }

View file

@ -378,4 +378,45 @@ class Server extends Model
throw new ServerStateConflictException($this); throw new ServerStateConflictException($this);
} }
} }
/**
* Take all the environment variables configured for this server and return
* them in an easy to process format.
*/
public function getEnvironment()
{
$mappings = [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
$variables = $this->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
// in order to allow run-time and config defined variables to take
// priority over built-in values.
foreach ($mappings as $key => $object) {
$variables->put($key, object_get($this, $object));
}
// Process variables set in the configuration file.
foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
$variables->put(
$key,
is_callable($object) ? call_user_func($object, $this) : object_get($this, $object)
);
}
$this->additional ??= [];
// Process dynamically included environment variables.
foreach ($this->additional as $key => $closure) {
$variables->put($key, call_user_func($closure, $this));
}
return $variables->toArray();
}
} }

View file

@ -1,73 +0,0 @@
<?php
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\EggVariable;
class EnvironmentService
{
private array $additional = [];
/**
* Dynamically configure additional environment variables to be assigned
* with a specific server.
*/
public function setEnvironmentKey(string $key, callable $closure): void
{
$this->additional[$key] = $closure;
}
/**
* Return the dynamically added additional keys.
*/
public function getEnvironmentKeys(): array
{
return $this->additional;
}
/**
* Take all of the environment variables configured for this server and return
* them in an easy to process format.
*/
public function handle(Server $server): array
{
$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
// in order to allow run-time and config defined variables to take
// priority over built-in values.
foreach ($this->getEnvironmentMappings() as $key => $object) {
$variables->put($key, object_get($server, $object));
}
// Process variables set in the configuration file.
foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
$variables->put(
$key,
is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
);
}
// Process dynamically included environment variables.
foreach ($this->additional as $key => $closure) {
$variables->put($key, call_user_func($closure, $server));
}
return $variables->toArray();
}
/**
* Return a mapping of Panel default environment variables.
*/
private function getEnvironmentMappings(): array
{
return [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
}
}

View file

@ -10,7 +10,7 @@ class ServerConfigurationStructureService
/** /**
* ServerConfigurationStructureService constructor. * ServerConfigurationStructureService constructor.
*/ */
public function __construct(private EnvironmentService $environment) public function __construct()
{ {
} }
@ -49,7 +49,7 @@ class ServerConfigurationStructureService
'description' => $server->description, 'description' => $server->description,
], ],
'suspended' => $server->isSuspended(), 'suspended' => $server->isSuspended(),
'environment' => $this->environment->handle($server), 'environment' => $server->getEnvironment(),
'invocation' => $server->startup, 'invocation' => $server->startup,
'skip_egg_scripts' => $server->skip_scripts, 'skip_egg_scripts' => $server->skip_scripts,
'build' => [ 'build' => [
@ -109,7 +109,7 @@ class ServerConfigurationStructureService
'ports' => $server->allocations->groupBy('ip')->map(function ($item) { 'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port'); return $item->pluck('port');
})->toArray(), })->toArray(),
'env' => $this->environment->handle($server), 'env' => $server->getEnvironment(),
'oom_disabled' => $server->oom_disabled, 'oom_disabled' => $server->oom_disabled,
'memory' => (int) $server->memory, 'memory' => (int) $server->memory,
'swap' => (int) $server->swap, 'swap' => (int) $server->swap,

View file

@ -7,12 +7,9 @@ use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection; use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource; use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl; use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Services\Servers\EnvironmentService;
class ServerTransformer extends BaseTransformer class ServerTransformer extends BaseTransformer
{ {
private EnvironmentService $environmentService;
/** /**
* List of resources that can be included. * List of resources that can be included.
*/ */
@ -32,9 +29,9 @@ class ServerTransformer extends BaseTransformer
/** /**
* Perform dependency injection. * Perform dependency injection.
*/ */
public function handle(EnvironmentService $environmentService) public function handle()
{ {
$this->environmentService = $environmentService;
} }
/** /**
@ -84,7 +81,7 @@ class ServerTransformer extends BaseTransformer
'image' => $server->image, 'image' => $server->image,
// This field is deprecated, please use "status". // This field is deprecated, please use "status".
'installed' => $server->isInstalled() ? 1 : 0, 'installed' => $server->isInstalled() ? 1 : 0,
'environment' => $this->environmentService->handle($server), 'environment' => $server->getEnvironment(),
], ],
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at), $server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at), $server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),