Untested code to perform an update of server build settings

This commit is contained in:
Dane Everitt 2019-11-24 15:08:54 -08:00
parent c17f9ba8a9
commit 547e8840e2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 306 additions and 141 deletions

View file

@ -6,10 +6,10 @@ use Pterodactyl\Models\Node;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Daemon\ConfigurationRepository;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class NodeUpdateService
{
@ -32,12 +32,12 @@ class NodeUpdateService
* UpdateService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface $configurationRepository
* @param \Pterodactyl\Repositories\Daemon\ConfigurationRepository $configurationRepository
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
*/
public function __construct(
ConnectionInterface $connection,
ConfigurationRepositoryInterface $configurationRepository,
ConfigurationRepository $configurationRepository,
NodeRepositoryInterface $repository
) {
$this->connection = $connection;
@ -58,6 +58,8 @@ class NodeUpdateService
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function handle(Node $node, array $data, bool $resetToken = false)
{

View file

@ -6,11 +6,11 @@ use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class BuildModificationService
{
@ -25,7 +25,7 @@ class BuildModificationService
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
@ -39,13 +39,13 @@ class BuildModificationService
*
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/
public function __construct(
AllocationRepositoryInterface $allocationRepository,
ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository,
DaemonServerRepository $daemonServerRepository,
ServerRepositoryInterface $repository
) {
$this->allocationRepository = $allocationRepository;
@ -67,23 +67,22 @@ class BuildModificationService
*/
public function handle(Server $server, array $data)
{
$build = [];
$this->connection->beginTransaction();
$this->processAllocations($server, $data);
if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) {
try {
$allocation = $this->allocationRepository->findFirstWhere([
$this->allocationRepository->findFirstWhere([
['id', '=', $data['allocation_id']],
['server_id', '=', $server->id],
]);
} catch (RecordNotFoundException $ex) {
throw new DisplayException(trans('admin/server.exceptions.default_allocation_not_found'));
}
$build['default'] = ['ip' => $allocation->ip, 'port' => $allocation->port];
}
/** @var \Pterodactyl\Models\Server $server */
$server = $this->repository->withFreshModel()->update($server->id, [
'oom_disabled' => array_get($data, 'oom_disabled'),
'memory' => array_get($data, 'memory'),
@ -96,20 +95,28 @@ class BuildModificationService
'allocation_limit' => array_get($data, 'allocation_limit'),
]);
$allocations = $this->allocationRepository->findWhere([['server_id', '=', $server->id]]);
$build['oom_disabled'] = $server->oom_disabled;
$build['memory'] = (int) $server->memory;
$build['swap'] = (int) $server->swap;
$build['io'] = (int) $server->io;
$build['cpu'] = (int) $server->cpu;
$build['disk'] = (int) $server->disk;
$build['ports|overwrite'] = $allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray();
$updateData = [
'allocations' => [
'default' => [
'ip' => $server->allocation->ip,
'port' => $server->allocation->port,
],
'mappings' => [$server->getAllocationMappings()],
],
'build' => [
'memory' => $server->memory,
'swap' => $server->swap,
'io' => $server->io,
'cpu' => $server->cpu,
'disk' => $server->disk,
],
'container' => [
'oom_disabled' => $server->oom_disabled,
],
];
try {
$this->daemonServerRepository->setServer($server)->update(['build' => $build]);
$this->daemonServerRepository->setServer($server)->update($updateData);
$this->connection->commit();
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);

View file

@ -76,7 +76,6 @@ class ServerConfigurationStructureService
'suspended' => (bool) $server->suspended,
'environment' => $this->environment->handle($server),
'build' => [
'oom_disabled' => $server->oom_disabled,
'memory' => $server->memory,
'swap' => $server->swap,
'io' => $server->io,
@ -90,6 +89,7 @@ class ServerConfigurationStructureService
],
'container' => [
'image' => $server->image,
'oom_disabled' => $server->oom_disabled,
'requires_rebuild' => false,
],
'allocations' => [
@ -97,11 +97,7 @@ class ServerConfigurationStructureService
'ip' => $server->allocation->ip,
'port' => $server->allocation->port,
],
'mappings' => [
$server->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray(),
],
'mappings' => [$server->getAllocationMappings()],
],
];
}