Refactor obscure service names to be clearer

This commit is contained in:
Dane Everitt 2017-08-27 15:10:51 -05:00
parent a91d84ecfe
commit 67ac36f5ce
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
25 changed files with 96 additions and 341 deletions

View file

@ -26,7 +26,7 @@ namespace Pterodactyl\Services\Nodes;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
class CreationService
class NodeCreationService
{
const DAEMON_SECRET_LENGTH = 18;

View file

@ -30,7 +30,7 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class DeletionService
class NodeDeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface

View file

@ -31,7 +31,7 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class UpdateService
class NodeUpdateService
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface
@ -74,6 +74,7 @@ class UpdateService
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($node, array $data)
{
@ -82,7 +83,7 @@ class UpdateService
}
if (! is_null(array_get($data, 'reset_secret'))) {
$data['daemonSecret'] = bin2hex(random_bytes(CreationService::DAEMON_SECRET_LENGTH));
$data['daemonSecret'] = bin2hex(random_bytes(NodeCreationService::DAEMON_SECRET_LENGTH));
unset($data['reset_secret']);
}

View file

@ -1,249 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 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\Node;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Location;
use Pterodactyl\Exceptions\AutoDeploymentException;
class DeploymentService
{
/**
* Eloquent model representing the allocation to use.
*
* @var \Pterodactyl\Models\Allocation
*/
protected $allocation;
/**
* Amount of disk to be used by the server.
*
* @var int
*/
protected $disk;
/**
* Amount of memory to be used by the sever.
*
* @var int
*/
protected $memory;
/**
* Eloquent model representing the location to use.
*
* @var \Pterodactyl\Models\Location
*/
protected $location;
/**
* Eloquent model representing the node to use.
*
* @var \Pterodactyl\Models\Node
*/
protected $node;
/**
* Set the location to use when auto-deploying.
*
* @param int|\Pterodactyl\Models\Location $location
*/
public function setLocation($location)
{
$this->location = ($location instanceof Location) ? $location : Location::with('nodes')->findOrFail($location);
if (! $this->location->relationLoaded('nodes')) {
$this->location->load('nodes');
}
if (count($this->location->nodes) < 1) {
throw new AutoDeploymentException('The location provided does not contain any nodes and cannot be used.');
}
return $this;
}
/**
* Set the node to use when auto-deploying.
*
* @param int|\Pterodactyl\Models\Node $node
*/
public function setNode($node)
{
$this->node = ($node instanceof Node) ? $node : Node::findOrFail($node);
if (! $this->node->relationLoaded('allocations')) {
$this->node->load('allocations');
}
$this->setLocation($this->node->location);
return $this;
}
/**
* Set the amount of disk space to be used by the new server.
*
* @param int $disk
*/
public function setDisk(int $disk)
{
$this->disk = $disk;
return $this;
}
/**
* Set the amount of memory to be used by the new server.
*
* @param int $memory
*/
public function setMemory(int $memory)
{
$this->memory = $memory;
return $this;
}
/**
* Return a random location model.
*
* @param array $exclude
*/
protected function findLocation(array $exclude = [])
{
$location = Location::with('nodes')->whereNotIn('id', $exclude)->inRandomOrder()->first();
if (! $location) {
throw new AutoDeploymentException('Unable to locate a suitable location to select a node from.');
}
if (count($location->nodes) < 1) {
return $this->findLocation(array_merge($exclude, [$location->id]));
}
$this->setLocation($location);
}
/**
* Return a model instance of a random node.
*/
protected function findNode(array $exclude = [])
{
if (! $this->location) {
$this->setLocation($this->findLocation());
}
$select = $this->location->nodes->whereNotIn('id', $exclude);
if (count($select) < 1) {
throw new AutoDeploymentException('Unable to find a suitable node within the assigned location with enough space.');
}
// Check usage, select new node if necessary
$this->setNode($select->random());
if (! $this->checkNodeUsage()) {
return $this->findNode(array_merge($exclude, [$this->node()->id]));
}
}
/**
* Checks that a node's allocation limits will not be passed
* with the assigned limits.
*
* @return bool
*/
protected function checkNodeUsage()
{
if (! $this->disk && ! $this->memory) {
return true;
}
$totals = Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $this->node()->id)->first();
if ($this->memory) {
$limit = ($this->node()->memory * (1 + ($this->node()->memory_overallocate / 100)));
if (($totals->memory + $this->memory) > $limit) {
return false;
}
}
if ($this->disk) {
$limit = ($this->node()->disk * (1 + ($this->node()->disk_overallocate / 100)));
if (($totals->disk + $this->disk) > $limit) {
return false;
}
}
return true;
}
/**
* Return the assigned node for this auto-deployment.
*
* @return \Pterodactyl\Models\Node
*/
public function node()
{
return $this->node;
}
/**
* Return the assigned location for this auto-deployment.
*
* @return \Pterodactyl\Models\Location
*/
public function location()
{
return $this->location;
}
/**
* Return the assigned location for this auto-deployment.
*
* @return \Pterodactyl\Models\Allocation
*/
public function allocation()
{
return $this->allocation;
}
/**
* Select and return the node to be used by the auto-deployment system.
*/
public function select()
{
if (! $this->node) {
$this->findNode();
}
// Set the Allocation
$this->allocation = $this->node()->allocations->where('server_id', null)->random();
if (! $this->allocation) {
throw new AutoDeploymentException('Unable to find a suitable allocation to assign to this server.');
}
}
}

View file

@ -32,7 +32,7 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class ReinstallService
class ReinstallServerService
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface

View file

@ -36,7 +36,7 @@ use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class CreationService
class ServerCreationService
{
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface

View file

@ -34,7 +34,7 @@ use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class DeletionService
class ServerDeletionService
{
/**
* @var \Illuminate\Database\ConnectionInterface

View file

@ -29,7 +29,7 @@ use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Users\CreationService;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
@ -67,7 +67,7 @@ class SubuserCreationService
protected $serverRepository;
/**
* @var \Pterodactyl\Services\Users\CreationService
* @var \Pterodactyl\Services\Users\UserCreationService
*/
protected $userCreationService;
@ -83,7 +83,7 @@ class SubuserCreationService
public function __construct(
ConnectionInterface $connection,
CreationService $userCreationService,
UserCreationService $userCreationService,
DaemonServerRepositoryInterface $daemonRepository,
PermissionCreationService $permissionService,
ServerRepositoryInterface $serverRepository,

View file

@ -32,7 +32,7 @@ use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class CreationService
class UserCreationService
{
/**
* @var \Illuminate\Foundation\Application

View file

@ -30,7 +30,7 @@ use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class DeletionService
class UserDeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface

View file

@ -27,7 +27,7 @@ namespace Pterodactyl\Services\Users;
use Illuminate\Contracts\Hashing\Hasher;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UpdateService
class UserUpdateService
{
/**
* @var \Illuminate\Contracts\Hashing\Hasher