Skip to content

Commit 723b608

Browse files
committed
Implement node deletion properly, fixes pterodactyl#173
1 parent 0e89ecb commit 723b608

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,22 @@ public function postAllocations(Request $request, $id)
253253

254254
public function deleteNode(Request $request, $id)
255255
{
256-
$node = Models\Node::findOrFail($id);
257-
$servers = Models\Server::where('node', $id)->count();
258-
if ($servers > 0) {
259-
Alert::danger('You cannot delete a node with servers currently attached to it.')->flash();
260-
return redirect()->route('admin.nodes.view', [
261-
'id' => $id,
262-
'tab' => 'tab_delete'
263-
]);
256+
try {
257+
$repo = new NodeRepository;
258+
$repo->delete($id);
259+
Alert::success('Successfully deleted the requested node from the panel.')->flash();
260+
return redirect()->route('admin.nodes');
261+
} catch (DisplayException $e) {
262+
Alert::danger($e->getMessage())->flash();
263+
} catch (\Exception $e) {
264+
Log::error($e);
265+
Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
264266
}
265267

266-
$node->delete();
267-
Alert::success('Node successfully deleted.')->flash();
268-
return redirect()->route('admin.nodes');
269-
268+
return redirect()->route('admin.nodes.view', [
269+
'id' => $id,
270+
'tab' => 'tab_delete'
271+
]);
270272
}
271273

272274
}

app/Repositories/NodeRepository.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,30 @@ public function addAllocations($id, array $allocations)
229229

230230
public function delete($id)
231231
{
232-
// @TODO: add logic;
233-
return true;
232+
$node = Models\Node::findOrFail($id);
233+
if (Models\Server::where('node', $id)->count() > 0) {
234+
throw new DisplayException('You cannot delete a node with servers currently attached to it.');
235+
}
236+
237+
DB::beginTransaction();
238+
239+
try {
240+
// Unlink Database Servers
241+
Models\DatabaseServer::where('linked_node', $node->id)->update([
242+
'linked_node' => null,
243+
]);
244+
245+
// Delete Allocations
246+
Models\Allocation::where('node', $node->id)->delete();
247+
248+
// Delete Node
249+
$node->delete();
250+
251+
DB::commit();
252+
} catch (\Exception $ex) {
253+
DB::rollback();
254+
throw $ex;
255+
}
234256
}
235257

236258
}

0 commit comments

Comments
 (0)