Merge pull request #102 from ControlPanel-gg/server_creation

Server creation
This commit is contained in:
AVMG 2021-07-01 13:11:32 +02:00 committed by GitHub
commit 42c4cea7ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 23 deletions

View file

@ -113,7 +113,8 @@ class Pterodactyl
*/
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 Egg $egg
* @param Node $node
* @param int $allocationId
* @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", [
"name" => $server->name,
@ -167,7 +168,7 @@ class Pterodactyl
"allocations" => 1
],
"allocation" => [
"default" => Pterodactyl::getFreeAllocationId($node)
"default" => $allocationId
]
]);
}

View file

@ -12,6 +12,7 @@ use App\Models\Product;
use App\Models\Server;
use App\Notifications\ServerCreationError;
use Exception;
use Illuminate\Http\Client\Response;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@ -44,6 +45,8 @@ class ServerController extends Controller
/** Store a newly created resource in storage. */
public function store(Request $request)
{
if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
$request->validate([
"name" => "required|max:191",
"description" => "nullable|max:191",
@ -52,18 +55,18 @@ class ServerController extends Controller
"product_id" => "required|exists:products,id",
]);
if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
//create server
//get required resources
$egg = Egg::findOrFail($request->input('egg_id'));
$server = Auth::user()->servers()->create($request->all());
$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
$response = Pterodactyl::createServer($server, $egg, $node);
if (is_null($response)) return $this->serverCreationFailed($server);
if ($response->failed()) return $this->serverCreationFailed($server);
$response = Pterodactyl::createServer($server, $egg, $allocationId);
if ($response->failed()) return $this->serverCreationFailed($response, $server);
//update server with pterodactyl_id
$server->update([
@ -74,16 +77,6 @@ class ServerController extends Controller
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
*/
@ -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 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()));
}
}

View file

@ -104,7 +104,7 @@ Route::middleware('auth')->group(function () {
Route::resource('configurations', ConfigurationController::class);
Route::resource('configurations', ConfigurationController::class);
Route::patch('settings/update/icons', [SettingsController::class , 'updateIcons'])->name('settings.update.icons');
Route::patch('settings/update/icons', [SettingsController::class, 'updateIcons'])->name('settings.update.icons');
Route::resource('settings', SettingsController::class)->only('index');
Route::get('usefullinks/datatable', [UsefulLinkController::class, 'datatable'])->name('usefullinks.datatable');