Update server listing and associated logic to pull from the panel dynamiacally
This commit is contained in:
parent
952dff854e
commit
fb9c106448
26 changed files with 384 additions and 239 deletions
|
@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Api\Application;
|
|||
use Illuminate\Http\Request;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Container\Container;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
|
||||
|
@ -30,7 +31,10 @@ abstract class ApplicationApiController extends Controller
|
|||
Container::getInstance()->call([$this, 'loadDependencies']);
|
||||
|
||||
// Parse all of the includes to use on this request.
|
||||
$includes = collect(explode(',', $this->request->input('include', '')))->map(function ($value) {
|
||||
$input = $this->request->input('include', []);
|
||||
$input = is_array($input) ? $input : explode(',', $input);
|
||||
|
||||
$includes = (new Collection($input))->map(function ($value) {
|
||||
return trim($value);
|
||||
})->filter()->toArray();
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ class ClientController extends ClientApiController
|
|||
break;
|
||||
}
|
||||
|
||||
$servers = $this->repository->
|
||||
$servers = $this->repository
|
||||
->setSearchTerm($request->input('query'))
|
||||
->filterUserAccessServers(
|
||||
$request->user(), $filter, config('pterodactyl.paginate.frontend.servers')
|
||||
|
|
|
@ -3,21 +3,45 @@
|
|||
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Repositories\Wings\WingsServerRepository;
|
||||
use Pterodactyl\Transformers\Api\Client\StatsTransformer;
|
||||
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
|
||||
|
||||
class ResourceUtilizationController extends ClientApiController
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Wings\WingsServerRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* ResourceUtilizationController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Repositories\Wings\WingsServerRepository $repository
|
||||
*/
|
||||
public function __construct(WingsServerRepository $repository)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current resource utilization for a server.
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request
|
||||
* @return array
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function index(GetServerRequest $request): array
|
||||
public function __invoke(GetServerRequest $request): array
|
||||
{
|
||||
return $this->fractal->item($request->getModel(Server::class))
|
||||
$stats = $this->repository
|
||||
->setServer($request->getModel(Server::class))
|
||||
->getDetails();
|
||||
|
||||
return $this->fractal->item($stats)
|
||||
->transformWith($this->getTransformer(StatsTransformer::class))
|
||||
->toArray();
|
||||
}
|
||||
|
|
|
@ -4,27 +4,11 @@ namespace Pterodactyl\Http\Controllers\Base;
|
|||
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\User;
|
||||
use Illuminate\Http\Response;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
|
||||
class IndexController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
protected $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
||||
*/
|
||||
protected $keyProviderService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
|
@ -33,17 +17,10 @@ class IndexController extends Controller
|
|||
/**
|
||||
* IndexController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
DaemonKeyProviderService $keyProviderService,
|
||||
DaemonServerRepositoryInterface $daemonRepository,
|
||||
ServerRepositoryInterface $repository
|
||||
) {
|
||||
$this->daemonRepository = $daemonRepository;
|
||||
$this->keyProviderService = $keyProviderService;
|
||||
public function __construct(ServerRepositoryInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
|
@ -61,34 +38,4 @@ class IndexController extends Controller
|
|||
|
||||
return view('templates/base.core', ['servers' => $servers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status of the server in a JSON response used for populating active status list.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param string $uuid
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function status(Request $request, $uuid)
|
||||
{
|
||||
$server = $this->repository->findFirstWhere([['uuidShort', '=', $uuid]]);
|
||||
$token = $this->keyProviderService->handle($server, $request->user());
|
||||
|
||||
if (! $server->installed) {
|
||||
return response()->json(['status' => 20]);
|
||||
} elseif ($server->suspended) {
|
||||
return response()->json(['status' => 30]);
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->daemonRepository->setServer($server)->setToken($token)->details();
|
||||
} catch (ConnectException $exception) {
|
||||
throw new HttpException(Response::HTTP_GATEWAY_TIMEOUT, $exception->getMessage());
|
||||
} catch (RequestException $exception) {
|
||||
throw new HttpException(500, $exception->getMessage());
|
||||
}
|
||||
|
||||
return response()->json(json_decode($response->getBody()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,50 @@ use Znck\Eloquent\Traits\BelongsToThrough;
|
|||
use Sofa\Eloquence\Contracts\CleansAttributes;
|
||||
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string|null $external_id
|
||||
* @property string $uuid
|
||||
* @property string $uuidShort
|
||||
* @property int $node_id
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property bool $skip_scripts
|
||||
* @property bool $suspended
|
||||
* @property int $owner_id
|
||||
* @property int $memory
|
||||
* @property int $swap
|
||||
* @property int $disk
|
||||
* @property int $io
|
||||
* @property int $cpu
|
||||
* @property bool $oom_disabled
|
||||
* @property int $allocation_id
|
||||
* @property int $nest_id
|
||||
* @property int $egg_id
|
||||
* @property int|null $pack_id
|
||||
* @property string $startup
|
||||
* @property string $image
|
||||
* @property int $installed
|
||||
* @property int $allocation_limit
|
||||
* @property int $database_limit
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*
|
||||
* @property \Pterodactyl\Models\User $user
|
||||
* @property \Pterodactyl\Models\User[]|\Illuminate\Support\Collection $subusers
|
||||
* @property \Pterodactyl\Models\Allocation $allocation
|
||||
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Support\Collection $allocations
|
||||
* @property \Pterodactyl\Models\Pack|null $pack
|
||||
* @property \Pterodactyl\Models\Node $node
|
||||
* @property \Pterodactyl\Models\Nest $nest
|
||||
* @property \Pterodactyl\Models\Egg $egg
|
||||
* @property \Pterodactyl\Models\EggVariable[]|\Illuminate\Support\Collection $variables
|
||||
* @property \Pterodactyl\Models\Schedule[]|\Illuminate\Support\Collection $schedule
|
||||
* @property \Pterodactyl\Models\Database[]|\Illuminate\Support\Collection $databases
|
||||
* @property \Pterodactyl\Models\Location $location
|
||||
* @property \Pterodactyl\Models\DaemonKey $key
|
||||
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Support\Collection $keys
|
||||
*/
|
||||
class Server extends Model implements CleansAttributes, ValidableContract
|
||||
{
|
||||
use BelongsToThrough, Eloquence, Notifiable, Validable;
|
||||
|
|
|
@ -51,7 +51,7 @@ use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
|||
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
|
||||
use Pterodactyl\Repositories\Daemon\ServerRepository as DaemonServerRepository;
|
||||
use Pterodactyl\Repositories\Wings\WingsServerRepository as DaemonServerRepository;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
|
||||
class RepositoryServiceProvider extends ServiceProvider
|
||||
|
|
28
app/Repositories/Wings/WingsServerRepository.php
Normal file
28
app/Repositories/Wings/WingsServerRepository.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Repositories\Wings;
|
||||
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
|
||||
class WingsServerRepository extends BaseWingsRepository
|
||||
{
|
||||
/**
|
||||
* Returns details about a server from the Daemon instance.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function getDetails(): array
|
||||
{
|
||||
try {
|
||||
$response = $this->getHttpClient()->get(
|
||||
sprintf('/api/servers/%s', $this->getServer()->uuid)
|
||||
);
|
||||
} catch (TransferException $exception) {
|
||||
throw new DaemonConnectionException($exception);
|
||||
}
|
||||
|
||||
return json_decode($response->getBody()->__toString(), true);
|
||||
}
|
||||
}
|
|
@ -2,28 +2,10 @@
|
|||
|
||||
namespace Pterodactyl\Transformers\Api\Client;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class StatsTransformer extends BaseClientTransformer
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Perform dependency injection.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $repository
|
||||
*/
|
||||
public function handle(ServerRepositoryInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -36,60 +18,21 @@ class StatsTransformer extends BaseClientTransformer
|
|||
* Transform stats from the daemon into a result set that can be used in
|
||||
* the client API.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Server $model
|
||||
* @param array $data
|
||||
* @return array
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function transform(Server $model)
|
||||
public function transform(array $data)
|
||||
{
|
||||
try {
|
||||
$stats = $this->repository->setServer($model)->details();
|
||||
} catch (RequestException $exception) {
|
||||
throw new DaemonConnectionException($exception);
|
||||
}
|
||||
|
||||
$object = json_decode($stats->getBody()->getContents());
|
||||
|
||||
return [
|
||||
'state' => $this->transformState(object_get($object, 'status', 0)),
|
||||
'memory' => [
|
||||
'current' => round(object_get($object, 'proc.memory.total', 0) / 1024 / 1024),
|
||||
'limit' => floatval($model->memory),
|
||||
'current_state' => Arr::get($data, 'state', 'stopped'),
|
||||
'is_suspended' => Arr::get($data, 'suspended', false),
|
||||
'resources' => [
|
||||
'memory_bytes' => Arr::get($data, 'resources.memory_bytes', 0),
|
||||
'cpu_absolute' => Arr::get($data, 'resources.cpu_absolute', 0),
|
||||
'disk_bytes' => Arr::get($data, 'resources.disk_bytes', 0),
|
||||
'network_rx_bytes' => Arr::get($data, 'resources.network.rx_bytes', 0),
|
||||
'network_tx_bytes' => Arr::get($data, 'resources.network.tx_bytes', 0),
|
||||
],
|
||||
'cpu' => [
|
||||
'current' => object_get($object, 'proc.cpu.total', 0),
|
||||
'cores' => object_get($object, 'proc.cpu.cores', []),
|
||||
'limit' => floatval($model->cpu),
|
||||
],
|
||||
'disk' => [
|
||||
'current' => round(object_get($object, 'proc.disk.used', 0)),
|
||||
'limit' => floatval($model->disk),
|
||||
'io' => $model->io,
|
||||
],
|
||||
'installed' => $model->installed === 1,
|
||||
'suspended' => (bool) $model->suspended,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the state returned by the daemon into a human readable string.
|
||||
*
|
||||
* @param int $state
|
||||
* @return string
|
||||
*/
|
||||
private function transformState(int $state): string
|
||||
{
|
||||
switch ($state) {
|
||||
case 1:
|
||||
return 'on';
|
||||
case 2:
|
||||
return 'starting';
|
||||
case 3:
|
||||
return 'stopping';
|
||||
case 0:
|
||||
default:
|
||||
return 'off';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue