More server management via the API
This commit is contained in:
parent
3724559468
commit
17544481b5
|
@ -3,6 +3,7 @@
|
||||||
namespace Pterodactyl\Http\Controllers\Api\Application;
|
namespace Pterodactyl\Http\Controllers\Api\Application;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Container\Container;
|
use Illuminate\Container\Container;
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
|
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
|
||||||
|
@ -21,17 +22,13 @@ abstract class ApplicationApiController extends Controller
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ApplicationApiController constructor.
|
* ApplicationApiController constructor.
|
||||||
*
|
|
||||||
* @param \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal $fractal
|
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
*/
|
*/
|
||||||
public function __construct(Fractal $fractal, Request $request)
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->fractal = $fractal;
|
Container::getInstance()->call([$this, 'loadDependencies']);
|
||||||
$this->request = $request;
|
|
||||||
|
|
||||||
// Parse all of the includes to use on this request.
|
// Parse all of the includes to use on this request.
|
||||||
$includes = collect(explode(',', $request->input('include', '')))->map(function ($value) {
|
$includes = collect(explode(',', $this->request->input('include', '')))->map(function ($value) {
|
||||||
return trim($value);
|
return trim($value);
|
||||||
})->filter()->toArray();
|
})->filter()->toArray();
|
||||||
|
|
||||||
|
@ -39,6 +36,19 @@ abstract class ApplicationApiController extends Controller
|
||||||
$this->fractal->limitRecursion(2);
|
$this->fractal->limitRecursion(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform dependency injection of certain classes needed for core functionality
|
||||||
|
* without littering the constructors of classes that extend this abstract.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal $fractal
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
*/
|
||||||
|
public function loadDependencies(Fractal $fractal, Request $request)
|
||||||
|
{
|
||||||
|
$this->fractal = $fractal;
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an instance of an application transformer.
|
* Return an instance of an application transformer.
|
||||||
*
|
*
|
||||||
|
@ -53,4 +63,14 @@ abstract class ApplicationApiController extends Controller
|
||||||
|
|
||||||
return $transformer;
|
return $transformer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a HTTP/204 response for the API.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
protected function returnNoContent(): Response
|
||||||
|
{
|
||||||
|
return new Response('', Response::HTTP_NO_CONTENT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,22 @@
|
||||||
|
|
||||||
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Response;
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
|
use Pterodactyl\Services\Servers\ServerDeletionService;
|
||||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||||
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
||||||
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;
|
|
||||||
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
|
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
|
||||||
|
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
|
||||||
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
||||||
|
|
||||||
class ServerController extends ApplicationApiController
|
class ServerController extends ApplicationApiController
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Services\Servers\ServerDeletionService
|
||||||
|
*/
|
||||||
|
private $deletionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||||
*/
|
*/
|
||||||
|
@ -21,14 +26,14 @@ class ServerController extends ApplicationApiController
|
||||||
/**
|
/**
|
||||||
* ServerController constructor.
|
* ServerController constructor.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal $fractal
|
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||||
*/
|
*/
|
||||||
public function __construct(Fractal $fractal, Request $request, ServerRepositoryInterface $repository)
|
public function __construct(ServerDeletionService $deletionService, ServerRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
parent::__construct($fractal, $request);
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->deletionService = $deletionService;
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,14 +55,30 @@ class ServerController extends ApplicationApiController
|
||||||
/**
|
/**
|
||||||
* Show a single server transformed for the application API.
|
* Show a single server transformed for the application API.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest $request
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
* @param \Pterodactyl\Models\Server $server
|
* @param \Pterodactyl\Models\Server $server
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function view(GetServerRequest $request, Server $server): array
|
public function view(ServerWriteRequest $request, Server $server): array
|
||||||
{
|
{
|
||||||
return $this->fractal->item($server)
|
return $this->fractal->item($server)
|
||||||
->transformWith($this->getTransformer(ServerTransformer::class))
|
->transformWith($this->getTransformer(ServerTransformer::class))
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @param string $force
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*
|
||||||
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
|
*/
|
||||||
|
public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response
|
||||||
|
{
|
||||||
|
$this->deletionService->withForce($force === 'force')->handle($server);
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,115 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
|
use Pterodactyl\Services\Servers\SuspensionService;
|
||||||
|
use Pterodactyl\Services\Servers\ReinstallServerService;
|
||||||
|
use Pterodactyl\Services\Servers\ContainerRebuildService;
|
||||||
|
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
|
||||||
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
||||||
|
|
||||||
|
class ServerManagementController extends ApplicationApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Services\Servers\ContainerRebuildService
|
||||||
|
*/
|
||||||
|
private $rebuildService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Services\Servers\ReinstallServerService
|
||||||
|
*/
|
||||||
|
private $reinstallServerService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Services\Servers\SuspensionService
|
||||||
|
*/
|
||||||
|
private $suspensionService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SuspensionController constructor.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Services\Servers\ContainerRebuildService $rebuildService
|
||||||
|
* @param \Pterodactyl\Services\Servers\ReinstallServerService $reinstallServerService
|
||||||
|
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
|
||||||
|
*/
|
||||||
|
public function __construct(ContainerRebuildService $rebuildService, ReinstallServerService $reinstallServerService, SuspensionService $suspensionService)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->rebuildService = $rebuildService;
|
||||||
|
$this->reinstallServerService = $reinstallServerService;
|
||||||
|
$this->suspensionService = $suspensionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suspend a server on the Panel.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*
|
||||||
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
|
*/
|
||||||
|
public function suspend(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
$this->suspensionService->toggle($server, SuspensionService::ACTION_SUSPEND);
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsuspend a server on the Panel.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*
|
||||||
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
|
*/
|
||||||
|
public function unsuspend(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
$this->suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark a server as needing to be reinstalled.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*
|
||||||
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
|
*/
|
||||||
|
public function reinstall(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
$this->reinstallServerService->reinstall($server);
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark a server as needing its container rebuilt the next time it is started.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*
|
||||||
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||||
|
*/
|
||||||
|
public function rebuild(ServerWriteRequest $request, Server $server): Response
|
||||||
|
{
|
||||||
|
$this->rebuildService->handle($server);
|
||||||
|
|
||||||
|
return $this->returnNoContent();
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ use Pterodactyl\Models\Server;
|
||||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||||
|
|
||||||
class GetServerRequest extends ApplicationApiRequest
|
class ServerWriteRequest extends ApplicationApiRequest
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
|
@ -16,7 +16,7 @@ class GetServerRequest extends ApplicationApiRequest
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $permission = AdminAcl::READ;
|
protected $permission = AdminAcl::WRITE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the requested server exists on the Panel.
|
* Determine if the requested server exists on the Panel.
|
|
@ -19,6 +19,9 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS
|
||||||
|
|
||||||
class SuspensionService
|
class SuspensionService
|
||||||
{
|
{
|
||||||
|
const ACTION_SUSPEND = 'suspend';
|
||||||
|
const ACTION_UNSUSPEND = 'unsuspend';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||||
*/
|
*/
|
||||||
|
@ -70,29 +73,29 @@ class SuspensionService
|
||||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
*/
|
*/
|
||||||
public function toggle($server, $action = 'suspend')
|
public function toggle($server, $action = self::ACTION_SUSPEND)
|
||||||
{
|
{
|
||||||
if (! $server instanceof Server) {
|
if (! $server instanceof Server) {
|
||||||
$server = $this->repository->find($server);
|
$server = $this->repository->find($server);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! in_array($action, ['suspend', 'unsuspend'])) {
|
if (! in_array($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND])) {
|
||||||
throw new \InvalidArgumentException(sprintf(
|
throw new \InvalidArgumentException(sprintf(
|
||||||
'Action must be either suspend or unsuspend, %s passed.',
|
'Action must be either ' . self::ACTION_SUSPEND . ' or ' . self::ACTION_UNSUSPEND . ', %s passed.',
|
||||||
$action
|
$action
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$action === 'suspend' && $server->suspended ||
|
$action === self::ACTION_SUSPEND && $server->suspended ||
|
||||||
$action === 'unsuspend' && ! $server->suspended
|
$action === self::ACTION_UNSUSPEND && ! $server->suspended
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->database->beginTransaction();
|
$this->database->beginTransaction();
|
||||||
$this->repository->withoutFreshModel()->update($server->id, [
|
$this->repository->withoutFreshModel()->update($server->id, [
|
||||||
'suspended' => $action === 'suspend',
|
'suspended' => $action === self::ACTION_SUSPEND,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -96,5 +96,13 @@ Route::group(['prefix' => '/servers'], function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::get('/', 'Servers\ServerController@index')->name('api.application.servers');
|
Route::get('/', 'Servers\ServerController@index')->name('api.application.servers');
|
||||||
Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers');
|
Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers.view');
|
||||||
|
|
||||||
|
Route::post('/{server}/suspend', 'Servers\ServerManagementController@suspend')->name('api.application.servers.suspend');
|
||||||
|
Route::post('/{server}/unsuspend', 'Servers\ServerManagementController@unsuspend')->name('api.application.servers.unsuspend');
|
||||||
|
Route::post('/{server}/reinstall', 'Servers\ServerManagementController@reinstall')->name('api.application.servers.reinstall');
|
||||||
|
Route::post('/{server}/rebuild', 'Servers\ServerManagementController@rebuild')->name('api.application.servers.rebuild');
|
||||||
|
|
||||||
|
Route::delete('/{server}', 'Servers\ServerController@delete');
|
||||||
|
Route::delete('/{server}/{force?}', 'Servers\ServerController@delete');
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue