forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application Soft Delete (librenms#15270)
* add the softdeletes migrations for applications * add working migration file * add deleted_at to db schema.yaml for applications * update includes/html/forms/application-update.inc.php to work with softdeletes * update includes/html/pages/device/edit/apps.inc.php for softdelete * update includes/discovery/applications.inc.php to work with softdelete * minor updates to application-update.inc.php for disabling * style cleanup * set discovered when running discovery * update application tests to include deleted_at * add deleted_at to a missed test * a few more tweaks for opensips * add a missing deleted_at for linux_suricata_extract-v1 * fix fillable for Application model * massive cleanup of the application update widget thingy * improve the code for discovery and using Laravel * add a missing line to app/Models/Application * add a missing include to app/Models/Application.php * record includes for Application model * remove apps from the applications table when a device is deleted * revert to using upcert and where for discovery to fix CI * make discovered fillable and set it when running discovery... convert back to firstOrNew * clean up application discovery a bit and use observer * style fix * spelling fix... disablaed -> disabled * rever removal to just use where * cleanup app removal on delete * add restored to ModuleModelObserver * delete -> forcedelete fix * apply the suggested changes * use murrants other suggestion * style fix
- Loading branch information
Showing
55 changed files
with
269 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
database/migrations/2023_08_30_105156_add_applications_soft_deleted.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddApplicationsSoftDeleted extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('applications', function (Blueprint $table) { | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('applications', function (Blueprint $table) { | ||
$table->dropColumn('deleted_at'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
* @author Tony Murray <[email protected]> | ||
*/ | ||
|
||
use App\Models\Application; | ||
use App\Observers\ModuleModelObserver; | ||
use LibreNMS\Config; | ||
|
||
echo "\nApplications: "; | ||
|
@@ -56,7 +58,7 @@ | |
|
||
// Generate a list of enabled apps and a list of all discovered apps from the db | ||
[$enabled_apps, $discovered_apps] = array_reduce(dbFetchRows( | ||
'SELECT `app_type`,`discovered` FROM `applications` WHERE `device_id`=? ORDER BY `app_type`', | ||
'SELECT `app_type`,`discovered` FROM `applications` WHERE `device_id`=? AND deleted_at IS NULL ORDER BY `app_type`', | ||
[$device['device_id']] | ||
), function ($result, $app) { | ||
$result[0][] = $app['app_type']; | ||
|
@@ -67,46 +69,34 @@ | |
return $result; | ||
}, [[], []]); | ||
|
||
// enable observer for printing changes | ||
ModuleModelObserver::observe(\App\Models\Application::class); | ||
|
||
// Enable applications | ||
$current_apps = []; | ||
foreach ($results as $extend => $result) { | ||
if (isset($applications[$extend])) { | ||
$app = $applications[$extend]; | ||
$current_apps[] = $app; | ||
|
||
if (in_array($app, $enabled_apps)) { | ||
echo '.'; | ||
} else { | ||
dbInsert([ | ||
'device_id' => $device['device_id'], | ||
'app_type' => $app, | ||
'discovered' => 1, | ||
'app_status' => '', | ||
'app_instance' => '', | ||
], 'applications'); | ||
|
||
echo '+'; | ||
if (! in_array($app, $enabled_apps)) { | ||
$app_obj = Application::withTrashed()->firstOrNew(['device_id' => $device['device_id'], 'app_type' => $app]); | ||
if ($app_obj->trashed()) { | ||
$app_obj->restore(); | ||
} | ||
$app_obj->discovered = 1; | ||
$app_obj->save(); | ||
log_event("Application enabled by discovery: $app", $device, 'application', 1); | ||
} | ||
} | ||
} | ||
|
||
// remove non-existing apps | ||
$apps_to_remove = array_diff($discovered_apps, $current_apps); | ||
$num = count($apps_to_remove); | ||
if ($num > 0) { | ||
echo str_repeat('-', $num); | ||
$vars = $apps_to_remove; | ||
array_unshift($vars, $device['device_id']); | ||
dbDelete( | ||
'applications', | ||
'`device_id`=? AND `app_type` IN ' . dbGenPlaceholders($num), | ||
$vars | ||
); | ||
foreach ($apps_to_remove as $app) { | ||
log_event("Application disabled by discovery: $app", $device, 'application', 3); | ||
} | ||
} | ||
DeviceCache::getPrimary()->applications()->whereIn('app_type', $apps_to_remove)->get()->each(function (Application $app) { | ||
$app->delete(); | ||
\App\Models\Eventlog::log("Application disabled by discovery: $app->app_type", DeviceCache::getPrimary(), 'application', \LibreNMS\Enum\Severity::Notice); | ||
}); | ||
|
||
// clean application_metrics | ||
dbDeleteOrphans('application_metrics', ['applications.app_id']); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
* @copyright 2017 Tony Murray | ||
* @author Tony Murray <[email protected]> | ||
*/ | ||
use App\Models\Application; | ||
|
||
if (! Auth::user()->hasGlobalAdmin()) { | ||
$status = ['status' => 1, 'message' => 'You need to be admin']; | ||
} else { | ||
|
@@ -32,21 +34,24 @@ | |
$status = ['status' => 1, 'message' => 'Error with data']; | ||
} else { | ||
$status = ['status' => 1, 'message' => 'Database update failed']; | ||
$app = Application::withTrashed()->firstOrNew(['device_id' => $device_id, 'app_type' => $app]); | ||
if ($_POST['state'] == 'true') { | ||
$update = [ | ||
'device_id' => $device_id, | ||
'app_type' => $app, | ||
'app_status' => '', | ||
'app_instance' => '', | ||
]; | ||
if (dbInsert($update, 'applications')) { | ||
if ($app->trashed()) { | ||
$app->restore(); | ||
} | ||
if ($app->save()) { | ||
log_event("Application enabled by user: $app", $device_id, 'application', 1); | ||
$status = ['status' => 0, 'message' => 'Application enabled']; | ||
} else { | ||
$status = ['status' => 1, 'message' => 'Database update for enabling the application failed']; | ||
} | ||
} else { | ||
if (dbDelete('applications', '`device_id`=? AND `app_type`=?', [$device_id, $app])) { | ||
$app->delete(); | ||
if ($app->save()) { | ||
log_event("Application disabled by user: $app", $device_id, 'application', 3); | ||
$status = ['status' => 0, 'message' => 'Application disabled']; | ||
} else { | ||
$status = ['status' => 1, 'message' => 'Database update for disabling the application failed']; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.