Skip to content

Commit fd9f1a6

Browse files
committed
Implement node view, cleanup other files.
Still in progress, need to do a lot of controller cleanup first and add node deletion as well.
1 parent 6c7fff1 commit fd9f1a6

File tree

16 files changed

+1335
-149
lines changed

16 files changed

+1335
-149
lines changed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 164 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Alert;
3030
use Carbon;
3131
use Validator;
32+
use Javascript;
3233
use Pterodactyl\Models;
3334
use Illuminate\Http\Request;
3435
use Pterodactyl\Exceptions\DisplayException;
@@ -55,9 +56,13 @@ public function getScript(Request $request, $id)
5556

5657
public function getIndex(Request $request)
5758
{
58-
return view('admin.nodes.index', [
59-
'nodes' => Models\Node::with('location')->withCount('servers')->paginate(20),
60-
]);
59+
$nodes = Models\Node::with('location')->withCount('servers');
60+
61+
if (! is_null($request->input('query'))) {
62+
$nodes->search($request->input('query'));
63+
}
64+
65+
return view('admin.nodes.index', ['nodes' => $nodes->paginate(25)]);
6166
}
6267

6368
public function getNew(Request $request)
@@ -117,6 +122,105 @@ public function getView(Request $request, $id)
117122
]);
118123
}
119124

125+
/**
126+
* Shows the index overview page for a specific node.
127+
*
128+
* @param Request $request
129+
* @param int $id The ID of the node to display information for.
130+
*
131+
* @return \Illuminate\View\View
132+
*/
133+
public function viewIndex(Request $request, $id)
134+
{
135+
$node = Models\Node::with('location')->withCount('servers')->findOrFail($id);
136+
$stats = collect(
137+
Models\Server::select(
138+
DB::raw('SUM(memory) as memory, SUM(disk) as disk')
139+
)->where('node_id', $node->id)->first()
140+
)->mapWithKeys(function ($item, $key) use ($node) {
141+
$percent = ($item / $node->{$key}) * 100;
142+
143+
return [$key => [
144+
'value' => $item,
145+
'percent' => $percent,
146+
'css' => ($percent <= 75) ? 'green' : (($percent > 90) ? 'red' : 'yellow'),
147+
]];
148+
})->toArray();
149+
150+
return view('admin.nodes.view.index', ['node' => $node, 'stats' => $stats]);
151+
}
152+
153+
/**
154+
* Shows the settings page for a specific node.
155+
*
156+
* @param Request $request
157+
* @param int $id The ID of the node to display information for.
158+
*
159+
* @return \Illuminate\View\View
160+
*/
161+
public function viewSettings(Request $request, $id)
162+
{
163+
return view('admin.nodes.view.settings', [
164+
'node' => Models\Node::findOrFail($id),
165+
'locations' => Models\Location::all(),
166+
]);
167+
}
168+
169+
/**
170+
* Shows the configuration page for a specific node.
171+
*
172+
* @param Request $request
173+
* @param int $id The ID of the node to display information for.
174+
*
175+
* @return \Illuminate\View\View
176+
*/
177+
public function viewConfiguration(Request $request, $id)
178+
{
179+
return view('admin.nodes.view.configuration', [
180+
'node' => Models\Node::findOrFail($id),
181+
]);
182+
}
183+
184+
/**
185+
* Shows the allocation page for a specific node.
186+
*
187+
* @param Request $request
188+
* @param int $id The ID of the node to display information for.
189+
*
190+
* @return \Illuminate\View\View
191+
*/
192+
public function viewAllocation(Request $request, $id)
193+
{
194+
$node = Models\Node::findOrFail($id);
195+
$node->setRelation('allocations', $node->allocations()->orderBy('ip', 'asc')->orderBy('port', 'asc')->with('server')->paginate(50));
196+
197+
Javascript::put([
198+
'node' => collect($node)->only(['id']),
199+
]);
200+
201+
return view('admin.nodes.view.allocation', ['node' => $node]);
202+
}
203+
204+
/**
205+
* Shows the server listing page for a specific node.
206+
*
207+
* @param Request $request
208+
* @param int $id The ID of the node to display information for.
209+
*
210+
* @return \Illuminate\View\View
211+
*/
212+
public function viewServers(Request $request, $id)
213+
{
214+
$node = Models\Node::with('servers.user', 'servers.service', 'servers.allocations')->findOrFail($id);
215+
Javascript::put([
216+
'node' => collect($node->makeVisible('daemonSecret'))->only(['scheme', 'fqdn', 'daemonListen', 'daemonSecret']),
217+
]);
218+
219+
return view('admin.nodes.view.servers', [
220+
'node' => $node,
221+
]);
222+
}
223+
120224
public function postView(Request $request, $id)
121225
{
122226
try {
@@ -149,10 +253,18 @@ public function postView(Request $request, $id)
149253
])->withInput();
150254
}
151255

152-
public function deallocateSingle(Request $request, $node, $allocation)
256+
/**
257+
* Removes a single allocation from a node.
258+
*
259+
* @param Request $request
260+
* @param integer $node
261+
* @param integer $allocation [description]
262+
* @return mixed
263+
*/
264+
public function allocationRemoveSingle(Request $request, $node, $allocation)
153265
{
154266
$query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
155-
if ((int) $query === 0) {
267+
if ($query < 1) {
156268
return response()->json([
157269
'error' => 'Unable to find an allocation matching those details to delete.',
158270
], 400);
@@ -161,33 +273,40 @@ public function deallocateSingle(Request $request, $node, $allocation)
161273
return response('', 204);
162274
}
163275

164-
public function deallocateBlock(Request $request, $node)
276+
/**
277+
* Remove all allocations for a specific IP at once on a node.
278+
*
279+
* @param Request $request
280+
* @param integer $node
281+
* @return mixed
282+
*/
283+
public function allocationRemoveBlock(Request $request, $node)
165284
{
166285
$query = Models\Allocation::where('node_id', $node)->whereNull('server_id')->where('ip', $request->input('ip'))->delete();
167-
if ((int) $query === 0) {
286+
if ($query < 1) {
168287
Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash();
169-
170-
return redirect()->route('admin.nodes.view', [
171-
'id' => $node,
172-
'tab' => 'tab_allocations',
173-
]);
288+
} else {
289+
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
174290
}
175-
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash();
176291

177-
return redirect()->route('admin.nodes.view', [
178-
'id' => $node,
179-
'tab' => 'tab_allocation',
180-
]);
292+
return redirect()->route('admin.nodes.view.allocation', $node);
181293
}
182294

183-
public function setAlias(Request $request, $node)
295+
/**
296+
* Sets an alias for a specific allocation on a node.
297+
*
298+
* @param Request $request
299+
* @param integer $node
300+
* @return mixed
301+
*/
302+
public function allocationSetAlias(Request $request, $node)
184303
{
185-
if (! $request->input('allocation')) {
304+
if (! $request->input('allocation_id')) {
186305
return response('Missing required parameters.', 422);
187306
}
188307

189308
try {
190-
$update = Models\Allocation::findOrFail($request->input('allocation'));
309+
$update = Models\Allocation::findOrFail($request->input('allocation_id'));
191310
$update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
192311
$update->save();
193312

@@ -197,60 +316,37 @@ public function setAlias(Request $request, $node)
197316
}
198317
}
199318

200-
public function getAllocationsJson(Request $request, $id)
201-
{
202-
$allocations = Models\Allocation::select('ip')->where('node_id', $id)->groupBy('ip')->get();
203-
204-
return response()->json($allocations);
205-
}
206-
207-
public function postAllocations(Request $request, $id)
319+
/**
320+
* Creates new allocations on a node.
321+
*
322+
* @param Request $request
323+
* @param integer $node
324+
* @return \Illuminate\Http\RedirectResponse
325+
*/
326+
public function createAllocation(Request $request, $node)
208327
{
209-
$validator = Validator::make($request->all(), [
210-
'allocate_ip.*' => 'required|string',
211-
'allocate_port.*' => 'required',
212-
]);
328+
$repo = new NodeRepository;
213329

214-
if ($validator->fails()) {
215-
return redirect()->route('admin.nodes.view', [
216-
'id' => $id,
217-
'tab' => 'tab_allocation',
218-
])->withErrors($validator->errors())->withInput();
330+
try {
331+
$repo->addAllocations($node, $request->intersect(['allocation_ip', 'allocation_alias', 'allocation_ports']));
332+
Alert::success('Successfully added new allocations!')->flash();
333+
} catch (DisplayValidationException $ex) {
334+
return redirect()->route('admin.nodes.view.allocation', $node)->withErrors(json_decode($ex->getMessage()))->withInput();
335+
} catch (DisplayException $ex) {
336+
Alert::danger($ex->getMessage())->flash();
337+
} catch (\Exception $ex) {
338+
Log::error($ex);
339+
Alert::danger('An unhandled exception occured while attempting to add allocations this node. This error has been logged.')->flash();
219340
}
220341

221-
$processedData = [];
222-
foreach ($request->input('allocate_ip') as $ip) {
223-
if (! array_key_exists($ip, $processedData)) {
224-
$processedData[$ip] = [];
225-
}
226-
}
342+
return redirect()->route('admin.nodes.view.allocation', $node);
343+
}
227344

228-
foreach ($request->input('allocate_port') as $portid => $ports) {
229-
if (array_key_exists($portid, $request->input('allocate_ip'))) {
230-
$json = json_decode($ports);
231-
if (json_last_error() === 0 && ! empty($json)) {
232-
foreach ($json as &$parsed) {
233-
array_push($processedData[$request->input('allocate_ip')[$portid]], $parsed->value);
234-
}
235-
}
236-
}
237-
}
345+
public function getAllocationsJson(Request $request, $id)
346+
{
347+
$allocations = Models\Allocation::select('ip')->where('node_id', $id)->groupBy('ip')->get();
238348

239-
try {
240-
$node = new NodeRepository;
241-
$node->addAllocations($id, $processedData);
242-
Alert::success('Successfully added new allocations to this node.')->flash();
243-
} catch (DisplayException $e) {
244-
Alert::danger($e->getMessage())->flash();
245-
} catch (\Exception $e) {
246-
Log::error($e);
247-
Alert::danger('An unhandled exception occured while attempting to add allocations this node. Please try again.')->flash();
248-
} finally {
249-
return redirect()->route('admin.nodes.view', [
250-
'id' => $id,
251-
'tab' => 'tab_allocation',
252-
]);
253-
}
349+
return response()->json($allocations);
254350
}
255351

256352
public function deleteNode(Request $request, $id)

app/Http/Routes/AdminRoutes.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,27 +232,53 @@ public function map(Router $router)
232232
'uses' => 'Admin\NodesController@postNew',
233233
]);
234234

235-
// View Node
236-
$router->get('/view/{id}', [
235+
$router->get('/view/{id}/do/index', [
237236
'as' => 'admin.nodes.view',
238-
'uses' => 'Admin\NodesController@getView',
237+
'uses' => 'Admin\NodesController@viewIndex',
239238
]);
240239

241-
$router->post('/view/{id}', [
242-
'uses' => 'Admin\NodesController@postView',
240+
$router->get('/view/{id}/do/settings', [
241+
'as' => 'admin.nodes.view.settings',
242+
'uses' => 'Admin\NodesController@viewSettings',
243+
]);
244+
245+
$router->get('/view/{id}/do/configuration', [
246+
'as' => 'admin.nodes.view.configuration',
247+
'uses' => 'Admin\NodesController@viewConfiguration',
248+
]);
249+
250+
$router->get('/view/{id}/do/allocation', [
251+
'as' => 'admin.nodes.view.allocation',
252+
'uses' => 'Admin\NodesController@viewAllocation',
253+
]);
254+
255+
$router->post('/view/{id}/do/allocation', [
256+
'uses' => 'Admin\NodesController@createAllocation',
257+
]);
258+
259+
$router->get('/view/{id}/do/servers', [
260+
'as' => 'admin.nodes.view.servers',
261+
'uses' => 'Admin\NodesController@viewServers',
262+
]);
263+
264+
$router->get('/view/{id}/do/delete', [
265+
'as' => 'admin.nodes.view.delete',
266+
'uses' => 'Admin\NodesController@viewDelete',
243267
]);
244268

245-
$router->delete('/view/{id}/deallocate/single/{allocation}', [
246-
'uses' => 'Admin\NodesController@deallocateSingle',
269+
$router->delete('/view/{id}/do/allocation/remove/{allocation}', [
270+
'as' => 'admin.nodes.view.allocation.removeSingle',
271+
'uses' => 'Admin\NodesController@allocationRemoveSingle',
247272
]);
248273

249-
$router->post('/view/{id}/deallocate/block', [
250-
'uses' => 'Admin\NodesController@deallocateBlock',
274+
$router->post('/view/{id}/do/allocation/remove', [
275+
'as' => 'admin.nodes.view.allocation.removeBlock',
276+
'uses' => 'Admin\NodesController@allocationRemoveBlock',
251277
]);
252278

253-
$router->post('/view/{id}/alias', [
254-
'as' => 'admin.nodes.alias',
255-
'uses' => 'Admin\NodesController@setAlias',
279+
$router->post('/view/{id}/do/allocation/alias', [
280+
'as' => 'admin.nodes.view.allocation.setAlias',
281+
'uses' => 'Admin\NodesController@allocationSetAlias',
256282
]);
257283

258284
$router->get('/view/{id}/allocations.json', [

app/Models/Node.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
use GuzzleHttp\Client;
2828
use Illuminate\Database\Eloquent\Model;
2929
use Illuminate\Notifications\Notifiable;
30+
use Nicolaslopezj\Searchable\SearchableTrait;
3031

3132
class Node extends Model
3233
{
33-
use Notifiable;
34+
use Notifiable, SearchableTrait;
3435

3536
/**
3637
* The table associated with the model.
@@ -74,6 +75,18 @@ class Node extends Model
7475
'daemonSFTP', 'daemonListen',
7576
];
7677

78+
protected $searchable = [
79+
'columns' => [
80+
'nodes.name' => 10,
81+
'nodes.fqdn' => 8,
82+
'locations.short' => 4,
83+
'locations.long' => 4,
84+
],
85+
'joins' => [
86+
'locations' => ['locations.id', 'nodes.location_id'],
87+
],
88+
];
89+
7790
/**
7891
* Return an instance of the Guzzle client for this specific node.
7992
*

0 commit comments

Comments
 (0)