Update panel to send correct service option configuration to the daemon.

This commit is contained in:
Dane Everitt 2017-10-05 23:09:43 -05:00
parent 1a5a1f82e7
commit 38075c6b9f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 282 additions and 83 deletions

View file

@ -0,0 +1,83 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ServerConfigurationStructureService
{
const REQUIRED_RELATIONS = ['allocation', 'allocations', 'pack', 'option'];
/**
* @var \Pterodactyl\Services\Servers\EnvironmentService
*/
protected $environment;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* ServerConfigurationStructureService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Services\Servers\EnvironmentService $environment
*/
public function __construct(
ServerRepositoryInterface $repository,
EnvironmentService $environment
) {
$this->repository = $repository;
$this->environment = $environment;
}
/**
* @param int|\Pterodactyl\Models\Server $server
* @return array
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($server): array
{
if (! $server instanceof Server || array_diff(self::REQUIRED_RELATIONS, $server->getRelations())) {
$server = $this->repository->getDataForCreation(is_digit($server) ? $server : $server->id);
}
return [
'uuid' => $server->uuid,
'user' => $server->username,
'build' => [
'default' => [
'ip' => $server->allocation->ip,
'port' => $server->allocation->port,
],
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray(),
'env' => $this->environment->process($server),
'memory' => (int) $server->memory,
'swap' => (int) $server->swap,
'io' => (int) $server->io,
'cpu' => (int) $server->cpu,
'disk' => (int) $server->disk,
'image' => $server->image,
],
'keys' => [],
'service' => [
'option' => $server->option->uuid,
'pack' => object_get($server, 'pack.uuid'),
'skip_scripts' => $server->skip_scripts,
],
'rebuild' => false,
'suspended' => (int) $server->suspended,
];
}
}

View file

@ -29,6 +29,11 @@ class ServerCreationService
*/
protected $allocationRepository;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
protected $configurationStructureService;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
@ -81,6 +86,7 @@ class ServerCreationService
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
@ -93,6 +99,7 @@ class ServerCreationService
DaemonServerRepositoryInterface $daemonServerRepository,
DatabaseManager $database,
NodeRepositoryInterface $nodeRepository,
ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository,
ServerVariableRepositoryInterface $serverVariableRepository,
UserRepositoryInterface $userRepository,
@ -102,6 +109,7 @@ class ServerCreationService
) {
$this->allocationRepository = $allocationRepository;
$this->daemonServerRepository = $daemonServerRepository;
$this->configurationStructureService = $configurationStructureService;
$this->database = $database;
$this->nodeRepository = $nodeRepository;
$this->repository = $repository;
@ -175,10 +183,11 @@ class ServerCreationService
}
$this->serverVariableRepository->insert($records);
$structure = $this->configurationStructureService->handle($server->id);
// Create the server on the daemon & commit it to the database.
try {
$this->daemonServerRepository->setNode($server->node_id)->create($server->id);
$this->daemonServerRepository->setNode($server->node_id)->create($structure, ['start_on_completion' => (bool) $data['start_on_completion']]);
$this->database->commit();
} catch (RequestException $exception) {
$response = $exception->getResponse();

View file

@ -0,0 +1,51 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Services\Options;
use Pterodactyl\Models\ServiceOption;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
class OptionConfigurationFileService
{
protected $repository;
/**
* OptionConfigurationFileService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $repository
*/
public function __construct(ServiceOptionRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Return a service configuration file to be used by the daemon.
*
* @param int|\Pterodactyl\Models\ServiceOption $option
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($option): array
{
if (! $option instanceof ServiceOption) {
$option = $this->repository->getWithCopyAttributes($option);
}
return [
'startup' => json_decode($option->inherit_config_startup),
'stop' => $option->inherit_config_stop,
'configs' => json_decode($option->inherit_config_files),
'log' => json_decode($option->inherit_config_logs),
'query' => 'none',
];
}
}