2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes;
|
|
|
|
|
|
|
|
use App\Models\Egg;
|
|
|
|
use App\Models\Nest;
|
|
|
|
use App\Models\Node;
|
|
|
|
use App\Models\Server;
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
class Pterodactyl
|
|
|
|
{
|
2021-06-06 10:11:35 +00:00
|
|
|
/**
|
|
|
|
* @return PendingRequest
|
|
|
|
*/
|
|
|
|
public static function client()
|
|
|
|
{
|
|
|
|
return Http::withHeaders([
|
|
|
|
'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
|
|
|
|
'Content-type' => 'application/json',
|
|
|
|
'Accept' => 'Application/vnd.pterodactyl.v1+json',
|
|
|
|
])->baseUrl(env('PTERODACTYL_URL') . '/api');
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:53:58 +00:00
|
|
|
//TODO: Extend error handling (maybe logger for more errors when debugging)
|
2021-06-06 10:11:35 +00:00
|
|
|
/**
|
|
|
|
* Get user by pterodactyl id
|
|
|
|
* @param int $pterodactylId
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-06-09 18:53:58 +00:00
|
|
|
public function getUser(int $pterodactylId){
|
2021-06-06 10:11:35 +00:00
|
|
|
$response = self::client()->get("/application/users/{$pterodactylId}");
|
2021-06-09 18:53:58 +00:00
|
|
|
if ($response->failed()) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-06-06 10:11:35 +00:00
|
|
|
return $response->json()['attributes'];
|
|
|
|
}
|
|
|
|
|
2021-06-05 09:26:32 +00:00
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @return array|mixed|null
|
2021-06-06 10:11:35 +00:00
|
|
|
* @throws Exception
|
2021-06-05 09:26:32 +00:00
|
|
|
*/
|
|
|
|
public static function getFreeAllocations(Node $node)
|
|
|
|
{
|
|
|
|
$response = self::getAllocations($node);
|
|
|
|
$freeAllocations = [];
|
|
|
|
|
2021-06-15 15:39:43 +00:00
|
|
|
if(isset($response['data'])){
|
|
|
|
if (!empty($response['data'])) {
|
|
|
|
foreach ($response['data'] as $allocation) {
|
|
|
|
if (!$allocation['attributes']['assigned']) array_push($freeAllocations, $allocation);
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $freeAllocations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getNests()
|
|
|
|
{
|
|
|
|
$response = self::client()->get('/application/nests');
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response->json()['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Nest $nest
|
|
|
|
* @return mixed
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getEggs(Nest $nest)
|
|
|
|
{
|
|
|
|
$response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables");
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response->json()['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getNodes()
|
|
|
|
{
|
|
|
|
$response = self::client()->get('/application/nodes');
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response->json()['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getLocations()
|
|
|
|
{
|
|
|
|
$response = self::client()->get('/application/locations');
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response->json()['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function getFreeAllocationId(Node $node)
|
|
|
|
{
|
2021-06-30 21:53:28 +00:00
|
|
|
|
|
|
|
return self::getFreeAllocations($node)[0]['attributes']['id'] ?? null;
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getAllocations(Node $node)
|
|
|
|
{
|
|
|
|
$response = self::client()->get("/application/nodes/{$node->id}/allocations");
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $route
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function url(string $route): string
|
|
|
|
{
|
|
|
|
return env('PTERODACTYL_URL') . $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Server $server
|
|
|
|
* @param Egg $egg
|
2021-06-30 21:53:28 +00:00
|
|
|
* @param int $allocationId
|
2021-06-05 09:26:32 +00:00
|
|
|
* @return Response
|
|
|
|
*/
|
2021-06-30 21:53:28 +00:00
|
|
|
public static function createServer(Server $server, Egg $egg, int $allocationId)
|
2021-06-05 09:26:32 +00:00
|
|
|
{
|
|
|
|
return self::client()->post("/application/servers", [
|
2021-06-06 10:11:35 +00:00
|
|
|
"name" => $server->name,
|
|
|
|
"external_id" => $server->id,
|
|
|
|
"user" => $server->user->pterodactyl_id,
|
|
|
|
"egg" => $egg->id,
|
|
|
|
"docker_image" => $egg->docker_image,
|
|
|
|
"startup" => $egg->startup,
|
|
|
|
"environment" => $egg->getEnvironmentVariables(),
|
|
|
|
"limits" => [
|
2021-06-05 09:26:32 +00:00
|
|
|
"memory" => $server->product->memory,
|
2021-06-06 10:11:35 +00:00
|
|
|
"swap" => $server->product->swap,
|
|
|
|
"disk" => $server->product->disk,
|
|
|
|
"io" => $server->product->io,
|
|
|
|
"cpu" => $server->product->cpu
|
2021-06-05 09:26:32 +00:00
|
|
|
],
|
|
|
|
"feature_limits" => [
|
2021-06-06 10:11:35 +00:00
|
|
|
"databases" => $server->product->databases,
|
|
|
|
"backups" => $server->product->backups,
|
2021-06-05 09:26:32 +00:00
|
|
|
"allocations" => 1
|
|
|
|
],
|
2021-06-06 10:11:35 +00:00
|
|
|
"allocation" => [
|
2021-06-30 21:53:28 +00:00
|
|
|
"default" => $allocationId
|
2021-06-05 09:26:32 +00:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-06-06 10:11:35 +00:00
|
|
|
public static function suspendServer(Server $server)
|
|
|
|
{
|
2021-06-05 09:26:32 +00:00
|
|
|
$response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2021-06-06 10:11:35 +00:00
|
|
|
public static function unSuspendServer(Server $server)
|
|
|
|
{
|
2021-06-05 09:26:32 +00:00
|
|
|
$response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
|
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Exception
|
|
|
|
*/
|
|
|
|
private static function getException(): Exception
|
|
|
|
{
|
|
|
|
return new Exception('Request Failed, is pterodactyl set-up correctly?');
|
|
|
|
}
|
|
|
|
}
|