Merge pull request #574 from ok236449/development-2

Resource checks on server creation and upgrade
This commit is contained in:
Dennis 2022-08-24 20:11:32 +02:00 committed by GitHub
commit 1c389c85c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 7 deletions

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Classes\Pterodactyl;
use App\Models\Egg; use App\Models\Egg;
use App\Models\Location; use App\Models\Location;
use App\Models\Node; use App\Models\Node;
@ -55,6 +56,10 @@ class ProductController extends Controller
public function getLocationsBasedOnEgg(Request $request, Egg $egg) public function getLocationsBasedOnEgg(Request $request, Egg $egg)
{ {
$nodes = $this->getNodesBasedOnEgg($request, $egg); $nodes = $this->getNodesBasedOnEgg($request, $egg);
foreach($nodes as $key => $node){
$pteroNode = Pterodactyl::getNode($node->id);
if($pteroNode['allocated_resources']['memory']>=($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)||$pteroNode['allocated_resources']['disk']>=($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)) $nodes->forget($key);
}
$locations = collect(); $locations = collect();
//locations //locations
@ -87,7 +92,7 @@ class ProductController extends Controller
{ {
if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400'); if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400');
return Product::query() $products = Product::query()
->where('disabled', '=', false) ->where('disabled', '=', false)
->whereHas('nodes', function (Builder $builder) use ($node) { ->whereHas('nodes', function (Builder $builder) use ($node) {
$builder->where('id', '=', $node->id); $builder->where('id', '=', $node->id);
@ -96,5 +101,12 @@ class ProductController extends Controller
$builder->where('id', '=', $egg->id); $builder->where('id', '=', $egg->id);
}) })
->get(); ->get();
$pteroNode = Pterodactyl::getNode($node->id);
foreach($products as $key => $product){
if($product->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true;
}
return $products;
} }
} }

View file

@ -241,6 +241,9 @@ class ServerController extends Controller
$serverRelationships = $serverAttributes['relationships']; $serverRelationships = $serverAttributes['relationships'];
$serverLocationAttributes = $serverRelationships['location']['attributes']; $serverLocationAttributes = $serverRelationships['location']['attributes'];
//Get current product
$currentProduct = Product::where('id', $server->product_id)->first();
//Set server infos //Set server infos
$server->location = $serverLocationAttributes['long'] ? $server->location = $serverLocationAttributes['long'] ?
$serverLocationAttributes['long'] : $serverLocationAttributes['long'] :
@ -249,11 +252,19 @@ class ServerController extends Controller
$server->node = $serverRelationships['node']['attributes']['name']; $server->node = $serverRelationships['node']['attributes']['name'];
$server->name = $serverAttributes['name']; $server->name = $serverAttributes['name'];
$server->egg = $serverRelationships['egg']['attributes']['name']; $server->egg = $serverRelationships['egg']['attributes']['name'];
$products = Product::orderBy("created_at")->get();
$pteroNode = Pterodactyl::getNode($serverRelationships['node']['attributes']['id']);
$products = Product::orderBy("created_at")
->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node
$builder->where('id', '=', $serverRelationships['node']['attributes']['id']);
})
->get();
// Set the each product eggs array to just contain the eggs name // Set the each product eggs array to just contain the eggs name
foreach ($products as $product) { foreach ($products as $product) {
$product->eggs = $product->eggs->pluck('name')->toArray(); $product->eggs = $product->eggs->pluck('name')->toArray();
if($product->memory-$currentProduct->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk-$currentProduct->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true;
} }
return view('servers.settings')->with([ return view('servers.settings')->with([

View file

@ -218,10 +218,10 @@
</div> </div>
</div> </div>
<button type="submit" x-model="selectedProduct" name="product" <button type="submit" x-model="selectedProduct" name="product"
:disabled="product.minimum_credits > user.credits" :disabled="product.minimum_credits > user.credits||product.doesNotFit == true"
:class="product.minimum_credits > user.credits ? 'disabled' : ''" :class="product.minimum_credits > user.credits ? 'disabled' : ''"
class="btn btn-primary btn-block mt-2" @click="setProduct(product.id)" class="btn btn-primary btn-block mt-2" @click="setProduct(product.id)"
x-text=" product.minimum_credits > user.credits ? '{{ __('Not enough') }} {{ CREDITS_DISPLAY_NAME }}!' : '{{ __('Create server') }}'"> x-text=" product.doesNotFit == true? '{{ __('Server can´t fit on this node') }}' : (product.minimum_credits > user.credits ? '{{ __('Not enough') }} {{ CREDITS_DISPLAY_NAME }}!' : '{{ __('Create server') }}')">
</button> </button>
</div> </div>
</div> </div>

View file

@ -255,13 +255,13 @@
<option value="">{{__("Select the product")}}</option> <option value="">{{__("Select the product")}}</option>
@foreach($products as $product) @foreach($products as $product)
@if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false) @if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false)
<option value="{{ $product->id }}">{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->minimum_credits!=-1) / <option value="{{ $product->id }}" @if($product->doesNotFit)disabled @endif>{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->doesNotFit)] {{__('Server can´t fit on this node')}} @else @if($product->minimum_credits!=-1) /
{{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ]</option> {{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ] @endif</option>
@endif @endif
@endforeach @endforeach
</select> </select>
<br> {{__("Once the Upgrade button is pressed, we will automatically deduct the amount for the first hour according to the new product from your credits")}}. <br> <br> {{__("Once the Upgrade button is pressed, we will automatically deduct the amount for the first hour according to the new product from your credits")}}. <br>
<br> {{_("Server will be automatically restarted once upgraded")}} <br> {{__("Server will be automatically restarted once upgraded")}}
</div> </div>
<div class="modal-footer card-body"> <div class="modal-footer card-body">
<button type="submit" class="btn btn-primary upgrade-once" style="width: 100%"><strong>{{__("Change Product")}}</strong></button> <button type="submit" class="btn btn-primary upgrade-once" style="width: 100%"><strong>{{__("Change Product")}}</strong></button>