From 30f58382bcfdf5df5ae7b2aa919a3440459bcc95 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 27 Sep 2016 21:01:46 -0400 Subject: [PATCH] Add support for automatic node assignment --- CHANGELOG.md | 15 ++- app/Repositories/ServerRepository.php | 96 +++++++++----- app/Services/DeploymentService.php | 137 ++++++++++++++++++++ resources/views/admin/servers/new.blade.php | 28 +++- 4 files changed, 237 insertions(+), 39 deletions(-) create mode 100644 app/Services/DeploymentService.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 83738aed..d4d52f97 100644 --- a/CHANGELOG.md +++ b/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) diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index ea83ec5d..549941b7 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -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; } diff --git a/app/Services/DeploymentService.php b/app/Services/DeploymentService.php new file mode 100644 index 00000000..957b8049 --- /dev/null +++ b/app/Services/DeploymentService.php @@ -0,0 +1,137 @@ + + * + * 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); + } + } +} diff --git a/resources/views/admin/servers/new.blade.php b/resources/views/admin/servers/new.blade.php index b89eb484..52da62df 100644 --- a/resources/views/admin/servers/new.blade.php +++ b/resources/views/admin/servers/new.blade.php @@ -65,7 +65,7 @@

The location in which this server will be deployed.

-
@@ -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) {