Merge pull request #102 from ControlPanel-gg/server_creation
Server creation
This commit is contained in:
commit
42c4cea7ec
|
@ -113,7 +113,8 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public static function getFreeAllocationId(Node $node)
|
public static function getFreeAllocationId(Node $node)
|
||||||
{
|
{
|
||||||
return self::getFreeAllocations($node)[0]['attributes']['id'];
|
|
||||||
|
return self::getFreeAllocations($node)[0]['attributes']['id'] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -141,10 +142,10 @@ class Pterodactyl
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param Egg $egg
|
* @param Egg $egg
|
||||||
* @param Node $node
|
* @param int $allocationId
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function createServer(Server $server, Egg $egg, Node $node)
|
public static function createServer(Server $server, Egg $egg, int $allocationId)
|
||||||
{
|
{
|
||||||
return self::client()->post("/application/servers", [
|
return self::client()->post("/application/servers", [
|
||||||
"name" => $server->name,
|
"name" => $server->name,
|
||||||
|
@ -167,7 +168,7 @@ class Pterodactyl
|
||||||
"allocations" => 1
|
"allocations" => 1
|
||||||
],
|
],
|
||||||
"allocation" => [
|
"allocation" => [
|
||||||
"default" => Pterodactyl::getFreeAllocationId($node)
|
"default" => $allocationId
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ use App\Models\Product;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use App\Notifications\ServerCreationError;
|
use App\Notifications\ServerCreationError;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Illuminate\Http\Client\Response;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
@ -44,6 +45,8 @@ class ServerController extends Controller
|
||||||
/** Store a newly created resource in storage. */
|
/** Store a newly created resource in storage. */
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
|
if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
"name" => "required|max:191",
|
"name" => "required|max:191",
|
||||||
"description" => "nullable|max:191",
|
"description" => "nullable|max:191",
|
||||||
|
@ -52,18 +55,18 @@ class ServerController extends Controller
|
||||||
"product_id" => "required|exists:products,id",
|
"product_id" => "required|exists:products,id",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
|
//get required resources
|
||||||
|
|
||||||
//create server
|
|
||||||
$egg = Egg::findOrFail($request->input('egg_id'));
|
$egg = Egg::findOrFail($request->input('egg_id'));
|
||||||
$server = Auth::user()->servers()->create($request->all());
|
|
||||||
$node = Node::findOrFail($request->input('node_id'));
|
$node = Node::findOrFail($request->input('node_id'));
|
||||||
|
$server = Auth::user()->servers()->create($request->all());
|
||||||
|
|
||||||
|
//get free allocation ID
|
||||||
|
$allocationId = Pterodactyl::getFreeAllocationId($node);
|
||||||
|
if (!$allocationId) return $this->noAllocationsError($server);
|
||||||
|
|
||||||
//create server on pterodactyl
|
//create server on pterodactyl
|
||||||
$response = Pterodactyl::createServer($server, $egg, $node);
|
$response = Pterodactyl::createServer($server, $egg, $allocationId);
|
||||||
|
if ($response->failed()) return $this->serverCreationFailed($response, $server);
|
||||||
if (is_null($response)) return $this->serverCreationFailed($server);
|
|
||||||
if ($response->failed()) return $this->serverCreationFailed($server);
|
|
||||||
|
|
||||||
//update server with pterodactyl_id
|
//update server with pterodactyl_id
|
||||||
$server->update([
|
$server->update([
|
||||||
|
@ -74,16 +77,6 @@ class ServerController extends Controller
|
||||||
return redirect()->route('servers.index')->with('success', 'server created');
|
return redirect()->route('servers.index')->with('success', 'server created');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Quick Fix */
|
|
||||||
private function serverCreationFailed(Server $server)
|
|
||||||
{
|
|
||||||
$server->delete();
|
|
||||||
|
|
||||||
Auth::user()->notify(new ServerCreationError($server));
|
|
||||||
return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return null|RedirectResponse
|
* @return null|RedirectResponse
|
||||||
*/
|
*/
|
||||||
|
@ -121,4 +114,31 @@ class ServerController extends Controller
|
||||||
return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource');
|
return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return redirect with error
|
||||||
|
* @param Server $server
|
||||||
|
* @return RedirectResponse
|
||||||
|
*/
|
||||||
|
private function noAllocationsError(Server $server)
|
||||||
|
{
|
||||||
|
$server->delete();
|
||||||
|
|
||||||
|
Auth::user()->notify(new ServerCreationError($server));
|
||||||
|
return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return redirect with error
|
||||||
|
* @param Response $response
|
||||||
|
* @param Server $server
|
||||||
|
* @return RedirectResponse
|
||||||
|
*/
|
||||||
|
private function serverCreationFailed(Response $response , Server $server)
|
||||||
|
{
|
||||||
|
$server->delete();
|
||||||
|
|
||||||
|
return redirect()->route('servers.index')->with('error', json_encode($response->json()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue