Implement node view, cleanup other files.

Still in progress, need to do a lot of controller cleanup first and add
node deletion as well.
This commit is contained in:
Dane Everitt 2017-03-03 17:30:39 -05:00
parent 6c7fff1de0
commit fd9f1a68eb
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
16 changed files with 1335 additions and 149 deletions

View file

@ -29,6 +29,7 @@ use Log;
use Alert;
use Carbon;
use Validator;
use Javascript;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Exceptions\DisplayException;
@ -55,9 +56,13 @@ class NodesController extends Controller
public function getIndex(Request $request)
{
return view('admin.nodes.index', [
'nodes' => Models\Node::with('location')->withCount('servers')->paginate(20),
]);
$nodes = Models\Node::with('location')->withCount('servers');
if (! is_null($request->input('query'))) {
$nodes->search($request->input('query'));
}
return view('admin.nodes.index', ['nodes' => $nodes->paginate(25)]);
}
public function getNew(Request $request)
@ -117,6 +122,105 @@ class NodesController extends Controller
]);
}
/**
* Shows the index overview page for a specific node.
*
* @param Request $request
* @param int $id The ID of the node to display information for.
*
* @return \Illuminate\View\View
*/
public function viewIndex(Request $request, $id)
{
$node = Models\Node::with('location')->withCount('servers')->findOrFail($id);
$stats = collect(
Models\Server::select(
DB::raw('SUM(memory) as memory, SUM(disk) as disk')
)->where('node_id', $node->id)->first()
)->mapWithKeys(function ($item, $key) use ($node) {
$percent = ($item / $node->{$key}) * 100;
return [$key => [
'value' => $item,
'percent' => $percent,
'css' => ($percent <= 75) ? 'green' : (($percent > 90) ? 'red' : 'yellow'),
]];
})->toArray();
return view('admin.nodes.view.index', ['node' => $node, 'stats' => $stats]);
}
/**
* Shows the settings page for a specific node.
*
* @param Request $request
* @param int $id The ID of the node to display information for.
*
* @return \Illuminate\View\View
*/
public function viewSettings(Request $request, $id)
{
return view('admin.nodes.view.settings', [
'node' => Models\Node::findOrFail($id),
'locations' => Models\Location::all(),
]);
}
/**
* Shows the configuration page for a specific node.
*
* @param Request $request
* @param int $id The ID of the node to display information for.
*
* @return \Illuminate\View\View
*/
public function viewConfiguration(Request $request, $id)
{
return view('admin.nodes.view.configuration', [
'node' => Models\Node::findOrFail($id),
]);
}
/**
* Shows the allocation page for a specific node.
*
* @param Request $request
* @param int $id The ID of the node to display information for.
*
* @return \Illuminate\View\View
*/
public function viewAllocation(Request $request, $id)
{
$node = Models\Node::findOrFail($id);
$node->setRelation('allocations', $node->allocations()->orderBy('ip', 'asc')->orderBy('port', 'asc')->with('server')->paginate(50));
Javascript::put([
'node' => collect($node)->only(['id']),
]);
return view('admin.nodes.view.allocation', ['node' => $node]);
}
/**
* Shows the server listing page for a specific node.
*
* @param Request $request
* @param int $id The ID of the node to display information for.
*
* @return \Illuminate\View\View
*/
public function viewServers(Request $request, $id)
{
$node = Models\Node::with('servers.user', 'servers.service', 'servers.allocations')->findOrFail($id);
Javascript::put([
'node' => collect($node->makeVisible('daemonSecret'))->only(['scheme', 'fqdn', 'daemonListen', 'daemonSecret']),
]);
return view('admin.nodes.view.servers', [
'node' => $node,
]);
}
public function postView(Request $request, $id)
{
try {
@ -149,10 +253,18 @@ class NodesController extends Controller
])->withInput();
}
public function deallocateSingle(Request $request, $node, $allocation)
/**
* Removes a single allocation from a node.
*
* @param Request $request
* @param integer $node
* @param integer $allocation [description]
* @return mixed
*/
public function allocationRemoveSingle(Request $request, $node, $allocation)
{
$query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
if ((int) $query === 0) {
if ($query < 1) {
return response()->json([
'error' => 'Unable to find an allocation matching those details to delete.',
], 400);
@ -161,33 +273,40 @@ class NodesController extends Controller
return response('', 204);
}
public function deallocateBlock(Request $request, $node)
/**
* Remove all allocations for a specific IP at once on a node.
*
* @param Request $request
* @param integer $node
* @return mixed
*/
public function allocationRemoveBlock(Request $request, $node)
{
$query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('ip', $request->input('ip'))->delete();
if ((int) $query === 0) {
if ($query < 1) {
Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash();
return redirect()->route('admin.nodes.view', [
'id' => $node,
'tab' => 'tab_allocations',
]);
} else {
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
}
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
return redirect()->route('admin.nodes.view', [
'id' => $node,
'tab' => 'tab_allocation',
]);
return redirect()->route('admin.nodes.view.allocation', $node);
}
public function setAlias(Request $request, $node)
/**
* Sets an alias for a specific allocation on a node.
*
* @param Request $request
* @param integer $node
* @return mixed
*/
public function allocationSetAlias(Request $request, $node)
{
if (! $request->input('allocation')) {
if (! $request->input('allocation_id')) {
return response('Missing required parameters.', 422);
}
try {
$update = Models\Allocation::findOrFail($request->input('allocation'));
$update = Models\Allocation::findOrFail($request->input('allocation_id'));
$update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
$update->save();
@ -197,6 +316,32 @@ class NodesController extends Controller
}
}
/**
* Creates new allocations on a node.
*
* @param Request $request
* @param integer $node
* @return \Illuminate\Http\RedirectResponse
*/
public function createAllocation(Request $request, $node)
{
$repo = new NodeRepository;
try {
$repo->addAllocations($node, $request->intersect(['allocation_ip', 'allocation_alias', 'allocation_ports']));
Alert::success('Successfully added new allocations!')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.nodes.view.allocation', $node)->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attempting to add allocations this node. This error has been logged.')->flash();
}
return redirect()->route('admin.nodes.view.allocation', $node);
}
public function getAllocationsJson(Request $request, $id)
{
$allocations = Models\Allocation::select('ip')->where('node_id', $id)->groupBy('ip')->get();
@ -204,55 +349,6 @@ class NodesController extends Controller
return response()->json($allocations);
}
public function postAllocations(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'allocate_ip.*' => 'required|string',
'allocate_port.*' => 'required',
]);
if ($validator->fails()) {
return redirect()->route('admin.nodes.view', [
'id' => $id,
'tab' => 'tab_allocation',
])->withErrors($validator->errors())->withInput();
}
$processedData = [];
foreach ($request->input('allocate_ip') as $ip) {
if (! array_key_exists($ip, $processedData)) {
$processedData[$ip] = [];
}
}
foreach ($request->input('allocate_port') as $portid => $ports) {
if (array_key_exists($portid, $request->input('allocate_ip'))) {
$json = json_decode($ports);
if (json_last_error() === 0 && ! empty($json)) {
foreach ($json as &$parsed) {
array_push($processedData[$request->input('allocate_ip')[$portid]], $parsed->value);
}
}
}
}
try {
$node = new NodeRepository;
$node->addAllocations($id, $processedData);
Alert::success('Successfully added new allocations to this node.')->flash();
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
Alert::danger('An unhandled exception occured while attempting to add allocations this node. Please try again.')->flash();
} finally {
return redirect()->route('admin.nodes.view', [
'id' => $id,
'tab' => 'tab_allocation',
]);
}
}
public function deleteNode(Request $request, $id)
{
try {

View file

@ -232,27 +232,53 @@ class AdminRoutes
'uses' => 'Admin\NodesController@postNew',
]);
// View Node
$router->get('/view/{id}', [
$router->get('/view/{id}/do/index', [
'as' => 'admin.nodes.view',
'uses' => 'Admin\NodesController@getView',
'uses' => 'Admin\NodesController@viewIndex',
]);
$router->post('/view/{id}', [
'uses' => 'Admin\NodesController@postView',
$router->get('/view/{id}/do/settings', [
'as' => 'admin.nodes.view.settings',
'uses' => 'Admin\NodesController@viewSettings',
]);
$router->delete('/view/{id}/deallocate/single/{allocation}', [
'uses' => 'Admin\NodesController@deallocateSingle',
$router->get('/view/{id}/do/configuration', [
'as' => 'admin.nodes.view.configuration',
'uses' => 'Admin\NodesController@viewConfiguration',
]);
$router->post('/view/{id}/deallocate/block', [
'uses' => 'Admin\NodesController@deallocateBlock',
$router->get('/view/{id}/do/allocation', [
'as' => 'admin.nodes.view.allocation',
'uses' => 'Admin\NodesController@viewAllocation',
]);
$router->post('/view/{id}/alias', [
'as' => 'admin.nodes.alias',
'uses' => 'Admin\NodesController@setAlias',
$router->post('/view/{id}/do/allocation', [
'uses' => 'Admin\NodesController@createAllocation',
]);
$router->get('/view/{id}/do/servers', [
'as' => 'admin.nodes.view.servers',
'uses' => 'Admin\NodesController@viewServers',
]);
$router->get('/view/{id}/do/delete', [
'as' => 'admin.nodes.view.delete',
'uses' => 'Admin\NodesController@viewDelete',
]);
$router->delete('/view/{id}/do/allocation/remove/{allocation}', [
'as' => 'admin.nodes.view.allocation.removeSingle',
'uses' => 'Admin\NodesController@allocationRemoveSingle',
]);
$router->post('/view/{id}/do/allocation/remove', [
'as' => 'admin.nodes.view.allocation.removeBlock',
'uses' => 'Admin\NodesController@allocationRemoveBlock',
]);
$router->post('/view/{id}/do/allocation/alias', [
'as' => 'admin.nodes.view.allocation.setAlias',
'uses' => 'Admin\NodesController@allocationSetAlias',
]);
$router->get('/view/{id}/allocations.json', [

View file

@ -27,10 +27,11 @@ namespace Pterodactyl\Models;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Nicolaslopezj\Searchable\SearchableTrait;
class Node extends Model
{
use Notifiable;
use Notifiable, SearchableTrait;
/**
* The table associated with the model.
@ -74,6 +75,18 @@ class Node extends Model
'daemonSFTP', 'daemonListen',
];
protected $searchable = [
'columns' => [
'nodes.name' => 10,
'nodes.fqdn' => 8,
'locations.short' => 4,
'locations.long' => 4,
],
'joins' => [
'locations' => ['locations.id', 'nodes.location_id'],
],
];
/**
* Return an instance of the Guzzle client for this specific node.
*

View file

@ -177,80 +177,70 @@ class NodeRepository
}
}
public function addAllocations($id, array $allocations)
/**
* Adds allocations to a provided node.
* @param integer $id
* @param array $data
*/
public function addAllocations($id, array $data)
{
$node = Models\Node::findOrFail($id);
DB::beginTransaction();
$validator = Validator::make($data, [
'allocation_ip' => 'required|string',
'allocation_alias' => 'sometimes|required|string|max:255',
'allocation_ports' => 'required|array',
]);
try {
foreach ($allocations as $rawIP => $ports) {
try {
$setAlias = null;
$parsedIP = Network::parse($rawIP);
} catch (\Exception $ex) {
try {
$setAlias = $rawIP;
$parsedIP = Network::parse(gethostbyname($rawIP));
} catch (\Exception $ex) {
throw $ex;
if ($validator->fails()) {
throw new DisplayValidationException($validator->errors());
}
$explode = explode('/', $data['allocation_ip']);
if (count($explode) !== 1) {
if (! ctype_digit($explode[1]) || ($explode[1] > 32 || $explode[1] < 25)) {
throw new DisplayException('CIDR notation only allows masks between /32 and /25.');
}
}
DB::transaction(function () use ($parsed, $node, $data) {
foreach(Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
foreach ($data['allocation_ports'] as $port) {
// Determine if this is a valid single port, or a valid port range.
if (! ctype_digit($port) && ! preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for <code>' . $port . '</code> is invalid and cannot be processed.');
}
}
foreach ($parsedIP as $ip) {
foreach ($ports as $port) {
if (! is_int($port) && ! preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
$block = range($matches[1], $matches[2]);
if (count($block) > 1000) {
throw new DisplayException('Adding more than 1000 ports at once is not supported. Please use a smaller port range.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
$portBlock = range($matches[1], $matches[2]);
if (count($portBlock) > 2000) {
throw new DisplayException('Adding more than 2000 ports at once is not currently supported. Please consider using a smaller port range.');
}
foreach ($portBlock as $assignPort) {
$alloc = Models\Allocation::firstOrNew([
'node_id' => $node->id,
'ip' => $ip,
'port' => $assignPort,
]);
if (! $alloc->exists) {
$alloc->fill([
'node_id' => $node->id,
'ip' => $ip,
'port' => $assignPort,
'ip_alias' => $setAlias,
'server_id' => null,
]);
$alloc->save();
}
}
} else {
$alloc = Models\Allocation::firstOrNew([
foreach ($block as $unit) {
// Insert into Database
Models\Allocation::firstOrCreate([
'node_id' => $node->id,
'ip' => $ip,
'port' => $port,
'port' => $unit,
'ip_alias' => isset($data['allocation_alias']) ? $data['allocation_alias'] : null,
'server_id' => null,
]);
if (! $alloc->exists) {
$alloc->fill([
'node_id' => $node->id,
'ip' => $ip,
'port' => $port,
'ip_alias' => $setAlias,
'server_id' => null,
]);
$alloc->save();
}
}
} else {
// Insert into Database
Models\Allocation::firstOrCreate([
'node_id' => $node->id,
'ip' => $ip,
'port' => $port,
'ip_alias' => isset($data['allocation_alias']) ? $data['allocation_alias'] : null,
'server_id' => null,
]);
}
}
}
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
});
}
public function delete($id)