Finalize server management API
This commit is contained in:
parent
1be7805481
commit
c599112021
6 changed files with 163 additions and 17 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Pterodactyl\Exceptions\Http\Connection;
|
namespace Pterodactyl\Exceptions\Http\Connection;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Pterodactyl\Exceptions\DisplayException;
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ class DaemonConnectionException extends DisplayException
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
private $statusCode = 500;
|
private $statusCode = Response::HTTP_GATEWAY_TIMEOUT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throw a displayable exception caused by a daemon connection error.
|
* Throw a displayable exception caused by a daemon connection error.
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
||||||
|
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
|
use Pterodactyl\Services\Servers\StartupModificationService;
|
||||||
|
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
||||||
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
||||||
|
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerStartupRequest;
|
||||||
|
|
||||||
|
class StartupController extends ApplicationApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Services\Servers\StartupModificationService
|
||||||
|
*/
|
||||||
|
private $modificationService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* StartupController constructor.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Services\Servers\StartupModificationService $modificationService
|
||||||
|
*/
|
||||||
|
public function __construct(StartupModificationService $modificationService)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->modificationService = $modificationService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the startup and environment settings for a specific server.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerStartupRequest $request
|
||||||
|
* @return array
|
||||||
|
*
|
||||||
|
* @throws \Illuminate\Validation\ValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
|
*/
|
||||||
|
public function index(UpdateServerStartupRequest $request): array
|
||||||
|
{
|
||||||
|
$server = $this->modificationService->handle($request->getModel(Server::class), $request->validated());
|
||||||
|
|
||||||
|
return $this->fractal->item($server)
|
||||||
|
->transformWith($this->getTransformer(ServerTransformer::class))
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
|
||||||
|
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||||
|
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||||
|
|
||||||
|
class UpdateServerStartupRequest extends ApplicationApiRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $resource = AdminAcl::RESOURCE_SERVERS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $permission = AdminAcl::WRITE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validation rules to run the input aganist.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
$data = Server::getUpdateRulesForId($this->getModel(Server::class)->id);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'startup' => $data['startup'],
|
||||||
|
'environment' => 'present|array',
|
||||||
|
'egg' => $data['egg_id'],
|
||||||
|
'pack' => $data['pack_id'],
|
||||||
|
'image' => $data['image'],
|
||||||
|
'skip_scripts' => 'present|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the validated data in a format that is expected by the service.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function validated()
|
||||||
|
{
|
||||||
|
$data = parent::validated();
|
||||||
|
|
||||||
|
return collect($data)->only(['startup', 'environment', 'skip_scripts'])->merge([
|
||||||
|
'egg_id' => array_get($data, 'egg'),
|
||||||
|
'pack_id' => array_get($data, 'pack'),
|
||||||
|
'docker_image' => array_get($data, 'image'),
|
||||||
|
])->toArray();
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ use Pterodactyl\Models\Server;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Pterodactyl\Traits\Services\HasUserLevels;
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
||||||
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||||
|
@ -26,6 +27,11 @@ class StartupModificationService
|
||||||
*/
|
*/
|
||||||
private $connection;
|
private $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
||||||
|
*/
|
||||||
|
private $eggRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
||||||
*/
|
*/
|
||||||
|
@ -51,6 +57,7 @@ class StartupModificationService
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
||||||
|
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
|
||||||
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
|
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
|
||||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||||
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
||||||
|
@ -59,6 +66,7 @@ class StartupModificationService
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ConnectionInterface $connection,
|
ConnectionInterface $connection,
|
||||||
DaemonServerRepositoryInterface $daemonServerRepository,
|
DaemonServerRepositoryInterface $daemonServerRepository,
|
||||||
|
EggRepositoryInterface $eggRepository,
|
||||||
EnvironmentService $environmentService,
|
EnvironmentService $environmentService,
|
||||||
ServerRepositoryInterface $repository,
|
ServerRepositoryInterface $repository,
|
||||||
ServerVariableRepositoryInterface $serverVariableRepository,
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
||||||
|
@ -66,6 +74,7 @@ class StartupModificationService
|
||||||
) {
|
) {
|
||||||
$this->daemonServerRepository = $daemonServerRepository;
|
$this->daemonServerRepository = $daemonServerRepository;
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
|
$this->eggRepository = $eggRepository;
|
||||||
$this->environmentService = $environmentService;
|
$this->environmentService = $environmentService;
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->serverVariableRepository = $serverVariableRepository;
|
$this->serverVariableRepository = $serverVariableRepository;
|
||||||
|
@ -77,13 +86,14 @@ class StartupModificationService
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Models\Server $server
|
* @param \Pterodactyl\Models\Server $server
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
* @return \Pterodactyl\Models\Server
|
||||||
*
|
*
|
||||||
* @throws \Illuminate\Validation\ValidationException
|
* @throws \Illuminate\Validation\ValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
||||||
*/
|
*/
|
||||||
public function handle(Server $server, array $data)
|
public function handle(Server $server, array $data): Server
|
||||||
{
|
{
|
||||||
$this->connection->beginTransaction();
|
$this->connection->beginTransaction();
|
||||||
if (! is_null(array_get($data, 'environment'))) {
|
if (! is_null(array_get($data, 'environment'))) {
|
||||||
|
@ -119,6 +129,8 @@ class StartupModificationService
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection->commit();
|
$this->connection->commit();
|
||||||
|
|
||||||
|
return $server;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,13 +145,22 @@ class StartupModificationService
|
||||||
*/
|
*/
|
||||||
private function updateAdministrativeSettings(array $data, Server &$server, array &$daemonData)
|
private function updateAdministrativeSettings(array $data, Server &$server, array &$daemonData)
|
||||||
{
|
{
|
||||||
|
if (
|
||||||
|
is_digit(array_get($data, 'egg_id'))
|
||||||
|
&& $data['egg_id'] != $server->egg_id
|
||||||
|
&& is_null(array_get($data, 'nest_id'))
|
||||||
|
) {
|
||||||
|
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find($data['egg_id']);
|
||||||
|
$data['nest_id'] = $egg->nest_id;
|
||||||
|
}
|
||||||
|
|
||||||
$server = $this->repository->update($server->id, [
|
$server = $this->repository->update($server->id, [
|
||||||
'installed' => 0,
|
'installed' => 0,
|
||||||
'startup' => array_get($data, 'startup', $server->startup),
|
'startup' => array_get($data, 'startup', $server->startup),
|
||||||
'nest_id' => array_get($data, 'nest_id', $server->nest_id),
|
'nest_id' => array_get($data, 'nest_id', $server->nest_id),
|
||||||
'egg_id' => array_get($data, 'egg_id', $server->egg_id),
|
'egg_id' => array_get($data, 'egg_id', $server->egg_id),
|
||||||
'pack_id' => array_get($data, 'pack_id', $server->pack_id) > 0 ? array_get($data, 'pack_id', $server->pack_id) : null,
|
'pack_id' => array_get($data, 'pack_id', $server->pack_id) > 0 ? array_get($data, 'pack_id', $server->pack_id) : null,
|
||||||
'skip_scripts' => isset($data['skip_scripts']),
|
'skip_scripts' => array_get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
|
||||||
'image' => array_get($data, 'docker_image', $server->image),
|
'image' => array_get($data, 'docker_image', $server->image),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -147,7 +168,7 @@ class StartupModificationService
|
||||||
'build' => ['image' => $server->image],
|
'build' => ['image' => $server->image],
|
||||||
'service' => array_merge(
|
'service' => array_merge(
|
||||||
$this->repository->getDaemonServiceData($server, true),
|
$this->repository->getDaemonServiceData($server, true),
|
||||||
['skip_scripts' => isset($data['skip_scripts'])]
|
['skip_scripts' => $server->skip_scripts]
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,7 @@ Route::group(['prefix' => '/servers'], function () {
|
||||||
|
|
||||||
Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details');
|
Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details');
|
||||||
Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');
|
Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');
|
||||||
|
Route::patch('/{server}/startup', 'Servers\StartupController@index')->name('api.application.servers.startup');
|
||||||
|
|
||||||
Route::post('/', 'Servers\ServerController@store');
|
Route::post('/', 'Servers\ServerController@store');
|
||||||
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend');
|
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend');
|
||||||
|
|
|
@ -1,22 +1,17 @@
|
||||||
<?php
|
<?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 Tests\Unit\Services\Servers;
|
namespace Tests\Unit\Services\Servers;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use Pterodactyl\Models\Egg;
|
||||||
use Pterodactyl\Models\User;
|
use Pterodactyl\Models\User;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Pterodactyl\Services\Servers\EnvironmentService;
|
use Pterodactyl\Services\Servers\EnvironmentService;
|
||||||
use Pterodactyl\Services\Servers\VariableValidatorService;
|
use Pterodactyl\Services\Servers\VariableValidatorService;
|
||||||
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||||
use Pterodactyl\Services\Servers\StartupModificationService;
|
use Pterodactyl\Services\Servers\StartupModificationService;
|
||||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||||
|
@ -34,6 +29,11 @@ class StartupModificationServiceTest extends TestCase
|
||||||
*/
|
*/
|
||||||
private $connection;
|
private $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $eggRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Services\Servers\EnvironmentService|\Mockery\Mock
|
* @var \Pterodactyl\Services\Servers\EnvironmentService|\Mockery\Mock
|
||||||
*/
|
*/
|
||||||
|
@ -63,6 +63,7 @@ class StartupModificationServiceTest extends TestCase
|
||||||
|
|
||||||
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
|
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
|
||||||
$this->connection = m::mock(ConnectionInterface::class);
|
$this->connection = m::mock(ConnectionInterface::class);
|
||||||
|
$this->eggRepository = m::mock(EggRepositoryInterface::class);
|
||||||
$this->environmentService = m::mock(EnvironmentService::class);
|
$this->environmentService = m::mock(EnvironmentService::class);
|
||||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||||
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
||||||
|
@ -96,8 +97,10 @@ class StartupModificationServiceTest extends TestCase
|
||||||
|
|
||||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||||
|
|
||||||
$this->getService()->handle($model, ['egg_id' => 123, 'environment' => ['test' => 'abcd1234']]);
|
$response = $this->getService()->handle($model, ['egg_id' => 123, 'environment' => ['test' => 'abcd1234']]);
|
||||||
$this->assertTrue(true);
|
|
||||||
|
$this->assertInstanceOf(Server::class, $response);
|
||||||
|
$this->assertSame($model, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,6 +113,11 @@ class StartupModificationServiceTest extends TestCase
|
||||||
'image' => 'docker:image',
|
'image' => 'docker:image',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$eggModel = factory(Egg::class)->make([
|
||||||
|
'id' => 456,
|
||||||
|
'nest_id' => 12345,
|
||||||
|
]);
|
||||||
|
|
||||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||||
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
||||||
$this->validatorService->shouldReceive('handle')->with(456, ['test' => 'abcd1234'])->once()->andReturn(
|
$this->validatorService->shouldReceive('handle')->with(456, ['test' => 'abcd1234'])->once()->andReturn(
|
||||||
|
@ -122,9 +130,12 @@ class StartupModificationServiceTest extends TestCase
|
||||||
'variable_id' => 1,
|
'variable_id' => 1,
|
||||||
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
|
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
|
||||||
|
|
||||||
|
$this->eggRepository->shouldReceive('setColumns->find')->once()->with($eggModel->id)->andReturn($eggModel);
|
||||||
|
|
||||||
$this->repository->shouldReceive('update')->with($model->id, m::subset([
|
$this->repository->shouldReceive('update')->with($model->id, m::subset([
|
||||||
'installed' => 0,
|
'installed' => 0,
|
||||||
'egg_id' => 456,
|
'nest_id' => $eggModel->nest_id,
|
||||||
|
'egg_id' => $eggModel->id,
|
||||||
'pack_id' => 789,
|
'pack_id' => 789,
|
||||||
'image' => 'docker:image',
|
'image' => 'docker:image',
|
||||||
]))->once()->andReturn($model);
|
]))->once()->andReturn($model);
|
||||||
|
@ -152,8 +163,15 @@ class StartupModificationServiceTest extends TestCase
|
||||||
|
|
||||||
$service = $this->getService();
|
$service = $this->getService();
|
||||||
$service->setUserLevel(User::USER_LEVEL_ADMIN);
|
$service->setUserLevel(User::USER_LEVEL_ADMIN);
|
||||||
$service->handle($model, ['docker_image' => 'docker:image', 'egg_id' => 456, 'pack_id' => 789, 'environment' => ['test' => 'abcd1234']]);
|
$response = $service->handle($model, [
|
||||||
$this->assertTrue(true);
|
'docker_image' => 'docker:image',
|
||||||
|
'egg_id' => $eggModel->id,
|
||||||
|
'pack_id' => 789,
|
||||||
|
'environment' => ['test' => 'abcd1234'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Server::class, $response);
|
||||||
|
$this->assertSame($model, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,6 +184,7 @@ class StartupModificationServiceTest extends TestCase
|
||||||
return new StartupModificationService(
|
return new StartupModificationService(
|
||||||
$this->connection,
|
$this->connection,
|
||||||
$this->daemonServerRepository,
|
$this->daemonServerRepository,
|
||||||
|
$this->eggRepository,
|
||||||
$this->environmentService,
|
$this->environmentService,
|
||||||
$this->repository,
|
$this->repository,
|
||||||
$this->serverVariableRepository,
|
$this->serverVariableRepository,
|
||||||
|
|
Loading…
Reference in a new issue