delete hook (#1032)

* make log_message optional in delete_device

* add hooks support to delete job

* make delete job high prio

* web delete now queues job instead of inline delete

* move web logging into web package and remove userlog from device delete helper

* submit delete job for expire device instead of inline delete

* fixes to get web submit form for delete device to work

* enable delete hook functionality
This commit is contained in:
Oliver Gorwits
2023-04-30 22:58:42 +01:00
committed by GitHub
parent d338c2d15a
commit 949aeb9eea
9 changed files with 84 additions and 40 deletions

View File

@@ -14,11 +14,21 @@ register_worker({ phase => 'check' }, sub {
register_worker({ phase => 'main' }, sub {
my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
my ($device, $port) = map {$job->$_} qw/device port/;
# support for Hooks
vars->{'hook_data'} = { $device->get_columns };
delete vars->{'hook_data'}->{'snmp_comm'}; # for privacy
$port = ($port ? 1 : 0);
delete_device($device, $port, $extra);
return Status->done("Deleted device: $device");
my $happy = delete_device($device, $port);
if ($happy) {
return Status->done("Deleted device: $device")
}
else {
return Status->error("Failed to delete device: $device")
}
});
true;

View File

@@ -0,0 +1,31 @@
package App::Netdisco::Worker::Plugin::Delete::Hooks;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Worker;
use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/;
register_worker({ phase => 'late' }, sub {
my ($job, $workerconf) = @_;
my $count = 0;
foreach my $conf (@{ setting('hooks') }) {
my $no = ($conf->{'filter'}->{'no'} || []);
my $only = ($conf->{'filter'}->{'only'} || []);
next if check_acl_no( $job->device, $no );
next unless check_acl_only( $job->device, $only);
if ($conf->{'event'} eq 'delete') {
$count += queue_hook('delete', $conf);
debug sprintf ' [%s] hooks - %s queued', 'delete', $job->device;
}
}
return Status
->info(sprintf ' [%s] hooks - %d queued', $job->device, $count);
});
true;

View File

@@ -5,6 +5,7 @@ use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::JobQueue 'jq_insert';
use App::Netdisco::Util::Statistics 'update_stats';
use App::Netdisco::DB::ExplicitLocking ':modes';
@@ -13,11 +14,16 @@ register_worker({ phase => 'main' }, sub {
if (setting('expire_devices') and setting('expire_devices') > 0) {
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('Device')->search({
-or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }],
my @hostlist = schema('netdisco')->resultset('Device')->search({
-not_bool => 'is_pseudo',
last_discover => \[q/< (LOCALTIMESTAMP - ?::interval)/,
(setting('expire_devices') * 86400)],
})->delete();
})->get_column('ip')->all;
my $happy = jq_insert([map {{
device => $_,
action => 'delete',
}} @hostlist]);
});
}