forked from Alex/Pterodactyl-Panel
Add support for automatic node assignment
This commit is contained in:
parent
09b0a3da27
commit
30f58382bc
15
CHANGELOG.md
15
CHANGELOG.md
@ -3,7 +3,18 @@ This file is a running track of new features and fixes to each version of the pa
|
||||
|
||||
This project follows [Semantic Versioning](http://semver.org) guidelines.
|
||||
|
||||
## v0.4.1
|
||||
## v0.5.0 (Bodacious Boreopterus) [Unreleased]
|
||||
|
||||
### Added
|
||||
* Support for creating server without having to assign a node and allocation manually. Simply select the checkbox or pass `auto_deploy=true` to the API to auto-select a node and allocation given a location.
|
||||
|
||||
### Changed
|
||||
### Fixed
|
||||
### Deprecated
|
||||
### Removed
|
||||
### Security
|
||||
|
||||
## v0.4.1 (Articulate Aerotitan)
|
||||
|
||||
### Changed
|
||||
* Overallocate fields are now auto-filled with a value of `0`
|
||||
@ -13,7 +24,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
|
||||
* Server link in navbar directed to 404 link (PR by [@Randomfish132](https://github.com/Randomfish132))
|
||||
* Composer fails to finish ([#92](https://github.com/Pterodactyl/Panel/issues/92), PR by [@schrej](https://github.com/schrej), thanks [@parkervcp](https://github.com/parkervcp))
|
||||
|
||||
## v0.4.0
|
||||
## v0.4.0 (Arty Aerodactylus)
|
||||
|
||||
### Added
|
||||
* Task scheduler supporting customized CRON syntax or dropdown selected options. (currently only support command and power options)
|
||||
|
@ -31,6 +31,7 @@ use Log;
|
||||
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Services\UuidService;
|
||||
use Pterodactyl\Services\DeploymentService;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\AccountNotFoundException;
|
||||
@ -88,23 +89,37 @@ class ServerRepository
|
||||
|
||||
// Validate Fields
|
||||
$validator = Validator::make($data, [
|
||||
'owner' => 'required|email|exists:users,email',
|
||||
'node' => 'required|numeric|min:1|exists:nodes,id',
|
||||
'owner' => 'bail|required|email|exists:users,email',
|
||||
'name' => 'required|regex:/^([\w -]{4,35})$/',
|
||||
'memory' => 'required|numeric|min:0',
|
||||
'swap' => 'required|numeric|min:-1',
|
||||
'io' => 'required|numeric|min:10|max:1000',
|
||||
'cpu' => 'required|numeric|min:0',
|
||||
'disk' => 'required|numeric|min:0',
|
||||
'allocation' => 'numeric|exists:allocations,id|required_without:ip,port',
|
||||
'ip' => 'required_without:allocation|ip',
|
||||
'port' => 'required_without:allocation|numeric|min:1|max:65535',
|
||||
'service' => 'required|numeric|min:1|exists:services,id',
|
||||
'option' => 'required|numeric|min:1|exists:service_options,id',
|
||||
'service' => 'bail|required|numeric|min:1|exists:services,id',
|
||||
'option' => 'bail|required|numeric|min:1|exists:service_options,id',
|
||||
'startup' => 'string',
|
||||
'custom_image_name' => 'required_if:use_custom_image,on',
|
||||
'auto_deploy' => 'sometimes|boolean'
|
||||
]);
|
||||
|
||||
$validator->sometimes('node', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) {
|
||||
return !($input->auto_deploy);
|
||||
});
|
||||
|
||||
$validator->sometimes('ip', 'required|ip', function ($input) {
|
||||
return (!$input->auto_deploy && !$input->allocation);
|
||||
});
|
||||
|
||||
$validator->sometimes('port', 'required|numeric|min:1|max:65535', function ($input) {
|
||||
return (!$input->auto_deploy && !$input->allocation);
|
||||
});
|
||||
|
||||
$validator->sometimes('allocation', 'numeric|exists:allocations,id', function ($input) {
|
||||
return !($input->auto_deploy || ($input->port && $input->ip));
|
||||
});
|
||||
|
||||
|
||||
// Run validator, throw catchable and displayable exception if it fails.
|
||||
// Exception includes a JSON result of failed validation rules.
|
||||
if ($validator->fails()) {
|
||||
@ -114,15 +129,27 @@ class ServerRepository
|
||||
// Get the User ID; user exists since we passed the 'exists:users,email' part of the validation
|
||||
$user = Models\User::select('id')->where('email', $data['owner'])->first();
|
||||
|
||||
// Get Node Information
|
||||
$node = Models\Node::getByID($data['node']);
|
||||
$autoDeployed = false;
|
||||
if (isset($data['auto_deploy']) && in_array($data['auto_deploy'], [true, 1, "1"])) {
|
||||
// This is an auto-deployment situation
|
||||
// Ignore any other passed node data
|
||||
unset($data['node'], $data['ip'], $data['port'], $data['allocation']);
|
||||
|
||||
$autoDeployed = true;
|
||||
$node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location']);
|
||||
$allocation = DeploymentService::randomAllocation($node->id);
|
||||
} else {
|
||||
$node = Models\Node::getByID($data['node']);
|
||||
}
|
||||
|
||||
// Verify IP & Port are a.) free and b.) assigned to the node.
|
||||
// We know the node exists because of 'exists:nodes,id' in the validation
|
||||
if (!isset($data['allocation'])) {
|
||||
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||
} else {
|
||||
$allocation = Models\Allocation::where('id' , $data['allocation'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||
if (!$autoDeployed) {
|
||||
if (!isset($data['allocation'])) {
|
||||
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||
} else {
|
||||
$allocation = Models\Allocation::where('id' , $data['allocation'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||
}
|
||||
}
|
||||
|
||||
// Something failed in the query, either that combo doesn't exist, or it is in use.
|
||||
@ -176,28 +203,29 @@ class ServerRepository
|
||||
}
|
||||
|
||||
// Check Overallocation
|
||||
if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) {
|
||||
if (!$autoDeployed) {
|
||||
if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) {
|
||||
|
||||
$totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first();
|
||||
$totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first();
|
||||
|
||||
// Check memory limits
|
||||
if (is_numeric($node->memory_overallocate)) {
|
||||
$newMemory = $totals->memory + $data['memory'];
|
||||
$memoryLimit = ($node->memory * (1 + ($node->memory_overallocate / 100)));
|
||||
if($newMemory > $memoryLimit) {
|
||||
throw new DisplayException('The amount of memory allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->memory_overallocate + 100) . '% of its assigned ' . $node->memory . 'Mb of memory (' . $memoryLimit . 'Mb) of which ' . (($totals->memory / $node->memory) * 100) . '% (' . $totals->memory . 'Mb) is in use already. By allocating this server the node would be at ' . (($newMemory / $node->memory) * 100) . '% (' . $newMemory . 'Mb) usage.');
|
||||
// Check memory limits
|
||||
if (is_numeric($node->memory_overallocate)) {
|
||||
$newMemory = $totals->memory + $data['memory'];
|
||||
$memoryLimit = ($node->memory * (1 + ($node->memory_overallocate / 100)));
|
||||
if($newMemory > $memoryLimit) {
|
||||
throw new DisplayException('The amount of memory allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->memory_overallocate + 100) . '% of its assigned ' . $node->memory . 'Mb of memory (' . $memoryLimit . 'Mb) of which ' . (($totals->memory / $node->memory) * 100) . '% (' . $totals->memory . 'Mb) is in use already. By allocating this server the node would be at ' . (($newMemory / $node->memory) * 100) . '% (' . $newMemory . 'Mb) usage.');
|
||||
}
|
||||
}
|
||||
|
||||
// Check Disk Limits
|
||||
if (is_numeric($node->disk_overallocate)) {
|
||||
$newDisk = $totals->disk + $data['disk'];
|
||||
$diskLimit = ($node->disk * (1 + ($node->disk_overallocate / 100)));
|
||||
if($newDisk > $diskLimit) {
|
||||
throw new DisplayException('The amount of disk allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->disk_overallocate + 100) . '% of its assigned ' . $node->disk . 'Mb of disk (' . $diskLimit . 'Mb) of which ' . (($totals->disk / $node->disk) * 100) . '% (' . $totals->disk . 'Mb) is in use already. By allocating this server the node would be at ' . (($newDisk / $node->disk) * 100) . '% (' . $newDisk . 'Mb) usage.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check Disk Limits
|
||||
if (is_numeric($node->disk_overallocate)) {
|
||||
$newDisk = $totals->disk + $data['disk'];
|
||||
$diskLimit = ($node->disk * (1 + ($node->disk_overallocate / 100)));
|
||||
if($newDisk > $diskLimit) {
|
||||
throw new DisplayException('The amount of disk allocated to this server would put the node over its allocation limits. This node is allowed ' . ($node->disk_overallocate + 100) . '% of its assigned ' . $node->disk . 'Mb of disk (' . $diskLimit . 'Mb) of which ' . (($totals->disk / $node->disk) * 100) . '% (' . $totals->disk . 'Mb) is in use already. By allocating this server the node would be at ' . (($newDisk / $node->disk) * 100) . '% (' . $newDisk . 'Mb) usage.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
@ -211,7 +239,7 @@ class ServerRepository
|
||||
$server->fill([
|
||||
'uuid' => $generatedUuid,
|
||||
'uuidShort' => $uuid->generateShort('servers', 'uuidShort', $generatedUuid),
|
||||
'node' => $data['node'],
|
||||
'node' => $node->id,
|
||||
'name' => $data['name'],
|
||||
'suspended' => 0,
|
||||
'owner' => $user->id,
|
||||
@ -226,7 +254,8 @@ class ServerRepository
|
||||
'option' => $data['option'],
|
||||
'startup' => $data['startup'],
|
||||
'daemonSecret' => $uuid->generate('servers', 'daemonSecret'),
|
||||
'username' => $this->generateSFTPUsername($data['name'])
|
||||
'username' => $this->generateSFTPUsername($data['name']),
|
||||
'sftp_password' => Crypt::encrypt('not set')
|
||||
]);
|
||||
$server->save();
|
||||
|
||||
@ -292,7 +321,6 @@ class ServerRepository
|
||||
throw new DisplayException('There was an error while attempting to connect to the daemon to add this server.', $ex);
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollBack();
|
||||
Log:error($ex);
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
|
137
app/Services/DeploymentService.php
Normal file
137
app/Services/DeploymentService.php
Normal file
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
namespace Pterodactyl\Services;
|
||||
|
||||
use DB;
|
||||
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class DeploymentService
|
||||
{
|
||||
|
||||
public function __constructor()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a random location model. DO NOT USE.
|
||||
* @return \Pterodactyl\Models\Node
|
||||
*
|
||||
* @TODO Actually make this smarter. If we're selecting a random location
|
||||
* but then it has no nodes we should probably continue attempting all locations
|
||||
* until we hit one.
|
||||
*
|
||||
* Currently you should just pick a location and go from there.
|
||||
*/
|
||||
public static function randomLocation()
|
||||
{
|
||||
return Models\Location::inRandomOrder()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a model instance of a random node.
|
||||
* @param int $location
|
||||
* @param array $not
|
||||
* @return \Pterodactyl\Models\Node
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public static function randomNode($location, array $not = [])
|
||||
{
|
||||
$useLocation = Models\Location::findOrFail($location);
|
||||
$node = Models\Node::where('location', $useLocation->id)->where('public', 1)->whereNotIn('id', $not)->inRandomOrder()->first();
|
||||
|
||||
if (!$node) {
|
||||
throw new DisplayException("Unable to find a node in location {$useLocation->short} (id: {$useLocation->id}) that is available and has space.");
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects a random node ensuring it does not put the node
|
||||
* over allocation limits.
|
||||
* @param int $memory
|
||||
* @param int $disk
|
||||
* @param int $location
|
||||
* @return \Pterodactyl\Models\Node;
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public static function smartRandomNode($memory, $disk, $location = null) {
|
||||
$node = self::randomNode($location);
|
||||
$notIn = [];
|
||||
do {
|
||||
$return = self::checkNodeAllocation($node, $memory, $disk);
|
||||
if (!$return) {
|
||||
$notIn = array_merge($notIn, [
|
||||
$node->id
|
||||
]);
|
||||
$node = self::randomNode($location, $notIn);
|
||||
}
|
||||
} while (!$return);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random allocation for a node.
|
||||
* @param int $node
|
||||
* @return \Models\Pterodactyl\Allocation;
|
||||
*/
|
||||
public static function randomAllocation($node)
|
||||
{
|
||||
return Models\Allocation::where('node', $node)->whereNull('assigned_to')->inRandomOrder()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a node's allocation limits will not be passed with the given information.
|
||||
* @param \Pterodactyl\Models\Node $node
|
||||
* @param int $memory
|
||||
* @param int $disk
|
||||
* @return bool Returns true if this information would not put the node over it's limit.
|
||||
*/
|
||||
protected static function checkNodeAllocation(Models\Node $node, $memory, $disk)
|
||||
{
|
||||
if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) {
|
||||
$totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first();
|
||||
|
||||
// Check memory limits
|
||||
if (is_numeric($node->memory_overallocate)) {
|
||||
$limit = ($node->memory * (1 + ($node->memory_overallocate / 100)));
|
||||
$memoryLimitReached = (($totals->memory + $memory) > $limit);
|
||||
}
|
||||
|
||||
// Check Disk Limits
|
||||
if (is_numeric($node->disk_overallocate)) {
|
||||
$limit = ($node->disk * (1 + ($node->disk_overallocate / 100)));
|
||||
$diskLimitReached = (($totals->disk + $disk) > $limit);
|
||||
}
|
||||
|
||||
return (!$diskLimitReached && !$memoryLimitReached);
|
||||
}
|
||||
}
|
||||
}
|
@ -65,7 +65,7 @@
|
||||
<p class="text-muted"><small>The location in which this server will be deployed.</small></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-6 hidden">
|
||||
<div class="form-group col-md-6 hidden" id="allocationNode">
|
||||
<label for="node" class="control-label">Server Node</label>
|
||||
<div>
|
||||
<select name="node" id="getNode" class="form-control">
|
||||
@ -76,7 +76,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="form-group col-md-6 hidden">
|
||||
<div class="form-group col-md-6 hidden" id="allocationIP">
|
||||
<label for="ip" class="control-label">Server IP</label>
|
||||
<div>
|
||||
<select name="ip" id="getIP" class="form-control">
|
||||
@ -85,7 +85,7 @@
|
||||
<p class="text-muted"><small>Select the main IP that this server will be listening on. You can assign additional open IPs and ports below.</small></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-6 hidden">
|
||||
<div class="form-group col-md-6 hidden" id="allocationPort">
|
||||
<label for="port" class="control-label">Server Port</label>
|
||||
<div>
|
||||
<select name="port" id="getPort" class="form-control"></select>
|
||||
@ -93,6 +93,17 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-12 fuelux">
|
||||
<hr style="margin-top: 10px;"/>
|
||||
<div class="checkbox highlight" style="margin: 0;">
|
||||
<label class="checkbox-custom highlight" data-initialize="checkbox">
|
||||
<input class="sr-only" name="auto_deploy" type="checkbox" @if(isset($oldInput['auto_deploy']))checked="checked"@endif value="1"> <strong>Enable Automatic Deployment</strong>
|
||||
<p class="text-muted"><small>Check this box if you want the panel to automatically select a node and allocation for this server in the given location.</small><p>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="well">
|
||||
@ -341,6 +352,17 @@ $(document).ready(function () {
|
||||
|
||||
});
|
||||
|
||||
$('input[name="auto_deploy"]').change(function () {
|
||||
if ($(this).is(':checked')) {
|
||||
$('#allocationPort, #allocationIP, #allocationNode').addClass('hidden');
|
||||
} else {
|
||||
currentLocation = null;
|
||||
$('#getLocation').trigger('change', function (e) {
|
||||
alert('triggered');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$('#getService').on('change', function (event) {
|
||||
|
||||
if ($('#getService').val() === '' || $('#getService').val() === currentService) {
|
||||
|
Loading…
Reference in New Issue
Block a user