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:
@@ -77,7 +77,7 @@ Returns true if the transaction completes, else returns false.
|
|||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub delete_device {
|
sub delete_device {
|
||||||
my ($ip, $archive, $log) = @_;
|
my ($ip, $archive) = @_;
|
||||||
my $device = get_device($ip) or return 0;
|
my $device = get_device($ip) or return 0;
|
||||||
return 0 if not $device->in_storage;
|
return 0 if not $device->in_storage;
|
||||||
|
|
||||||
@@ -87,13 +87,6 @@ sub delete_device {
|
|||||||
schema(vars->{'tenant'})->resultset('Device')
|
schema(vars->{'tenant'})->resultset('Device')
|
||||||
->search({ ip => $device->ip })->delete({archive_nodes => $archive});
|
->search({ ip => $device->ip })->delete({archive_nodes => $archive});
|
||||||
|
|
||||||
schema(vars->{'tenant'})->resultset('UserLog')->create({
|
|
||||||
username => session('logged_in_user'),
|
|
||||||
userip => scalar eval {request->remote_address},
|
|
||||||
event => (sprintf "Delete device %s", $device->ip),
|
|
||||||
details => $log,
|
|
||||||
});
|
|
||||||
|
|
||||||
$happy = 1;
|
$happy = 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ use Dancer::Plugin::Auth::Extensible;
|
|||||||
|
|
||||||
use NetAddr::IP qw/:rfc3021 :lower/;
|
use NetAddr::IP qw/:rfc3021 :lower/;
|
||||||
use App::Netdisco::JobQueue 'jq_insert';
|
use App::Netdisco::JobQueue 'jq_insert';
|
||||||
use App::Netdisco::Util::Device qw/delete_device renumber_device/;
|
use App::Netdisco::Util::Device qw/renumber_device/;
|
||||||
|
|
||||||
sub add_job {
|
sub add_job {
|
||||||
my ($action, $device, $subaction) = @_;
|
my ($action, $device, $extra, $port) = @_;
|
||||||
|
|
||||||
my $net = NetAddr::IP->new($device);
|
my $net = NetAddr::IP->new($device);
|
||||||
return if
|
return if
|
||||||
@@ -18,15 +18,29 @@ sub add_job {
|
|||||||
|
|
||||||
my @hostlist = $device ? ($net->hostenum) : (undef);
|
my @hostlist = $device ? ($net->hostenum) : (undef);
|
||||||
|
|
||||||
jq_insert([map {{
|
my $happy = jq_insert([map {{
|
||||||
($_ ? (device => $_->addr) : ()),
|
|
||||||
action => $action,
|
action => $action,
|
||||||
($subaction ? (subaction => $subaction) : ()),
|
($_ ? (device => $_->addr) : ()),
|
||||||
|
($port ? (port => $port) : ()),
|
||||||
|
($extra ? (extra => $extra) : ()),
|
||||||
username => session('logged_in_user'),
|
username => session('logged_in_user'),
|
||||||
userip => request->remote_address,
|
userip => scalar eval {request->remote_address},
|
||||||
}} @hostlist]);
|
}} @hostlist]);
|
||||||
|
|
||||||
true;
|
foreach my $h (@hostlist) {
|
||||||
|
next unless defined $h;
|
||||||
|
my $msg = ($happy ? "Queued job to $action device \%s"
|
||||||
|
: "Failed to queue job to $action device \%s");
|
||||||
|
|
||||||
|
schema(vars->{'tenant'})->resultset('UserLog')->create({
|
||||||
|
username => session('logged_in_user'),
|
||||||
|
userip => scalar eval {request->remote_address},
|
||||||
|
event => (sprintf $msg, $h->addr),
|
||||||
|
details => ($extra || 'no user log supplied'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $happy;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach my $action (@{ setting('job_prio')->{high} },
|
foreach my $action (@{ setting('job_prio')->{high} },
|
||||||
@@ -35,12 +49,12 @@ foreach my $action (@{ setting('job_prio')->{high} },
|
|||||||
next if $action and $action =~ m/^hook::/; # skip hooks
|
next if $action and $action =~ m/^hook::/; # skip hooks
|
||||||
|
|
||||||
ajax "/ajax/control/admin/$action" => require_role admin => sub {
|
ajax "/ajax/control/admin/$action" => require_role admin => sub {
|
||||||
add_job($action, param('device'), param('extra'))
|
add_job($action, param('device'), param('extra'), param('port'))
|
||||||
or send_error('Bad device', 400);
|
or send_error('Bad device', 400);
|
||||||
};
|
};
|
||||||
|
|
||||||
post "/admin/$action" => require_role admin => sub {
|
post "/admin/$action" => require_role admin => sub {
|
||||||
add_job($action, param('device'), param('extra'))
|
add_job($action, param('device'), param('extra'), param('port'))
|
||||||
? redirect uri_for('/admin/jobqueue')->path
|
? redirect uri_for('/admin/jobqueue')->path
|
||||||
: redirect uri_for('/')->path;
|
: redirect uri_for('/')->path;
|
||||||
};
|
};
|
||||||
@@ -61,18 +75,6 @@ ajax qr{/ajax/control/admin/(?:\w+/)?renumber} => require_role setting('defanged
|
|||||||
return renumber_device( $device->addr, $newip->addr );
|
return renumber_device( $device->addr, $newip->addr );
|
||||||
};
|
};
|
||||||
|
|
||||||
ajax qr{/ajax/control/admin/(?:\w+/)?delete} => require_role setting('defanged_admin') => sub {
|
|
||||||
send_error('Missing device', 400) unless param('device');
|
|
||||||
|
|
||||||
my $device = NetAddr::IP->new(param('device'));
|
|
||||||
send_error('Bad device', 400)
|
|
||||||
if ! $device or $device->addr eq '0.0.0.0';
|
|
||||||
|
|
||||||
return delete_device(
|
|
||||||
$device->addr, param('archive'), param('log'),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
ajax "/ajax/control/admin/snapshot_req" => require_role admin => sub {
|
ajax "/ajax/control/admin/snapshot_req" => require_role admin => sub {
|
||||||
my $device = NetAddr::IP->new(param('device'));
|
my $device = NetAddr::IP->new(param('device'));
|
||||||
send_error('Bad device', 400)
|
send_error('Bad device', 400)
|
||||||
|
|||||||
@@ -14,11 +14,21 @@ register_worker({ phase => 'check' }, sub {
|
|||||||
|
|
||||||
register_worker({ phase => 'main' }, sub {
|
register_worker({ phase => 'main' }, sub {
|
||||||
my ($job, $workerconf) = @_;
|
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);
|
$port = ($port ? 1 : 0);
|
||||||
delete_device($device, $port, $extra);
|
my $happy = delete_device($device, $port);
|
||||||
return Status->done("Deleted device: $device");
|
|
||||||
|
if ($happy) {
|
||||||
|
return Status->done("Deleted device: $device")
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Status->error("Failed to delete device: $device")
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
true;
|
true;
|
||||||
|
|||||||
31
lib/App/Netdisco/Worker/Plugin/Delete/Hooks.pm
Normal file
31
lib/App/Netdisco/Worker/Plugin/Delete/Hooks.pm
Normal 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;
|
||||||
@@ -5,6 +5,7 @@ use App::Netdisco::Worker::Plugin;
|
|||||||
use aliased 'App::Netdisco::Worker::Status';
|
use aliased 'App::Netdisco::Worker::Status';
|
||||||
|
|
||||||
use Dancer::Plugin::DBIC 'schema';
|
use Dancer::Plugin::DBIC 'schema';
|
||||||
|
use App::Netdisco::JobQueue 'jq_insert';
|
||||||
use App::Netdisco::Util::Statistics 'update_stats';
|
use App::Netdisco::Util::Statistics 'update_stats';
|
||||||
use App::Netdisco::DB::ExplicitLocking ':modes';
|
use App::Netdisco::DB::ExplicitLocking ':modes';
|
||||||
|
|
||||||
@@ -13,11 +14,16 @@ register_worker({ phase => 'main' }, sub {
|
|||||||
|
|
||||||
if (setting('expire_devices') and setting('expire_devices') > 0) {
|
if (setting('expire_devices') and setting('expire_devices') > 0) {
|
||||||
schema('netdisco')->txn_do(sub {
|
schema('netdisco')->txn_do(sub {
|
||||||
schema('netdisco')->resultset('Device')->search({
|
my @hostlist = schema('netdisco')->resultset('Device')->search({
|
||||||
-or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }],
|
-not_bool => 'is_pseudo',
|
||||||
last_discover => \[q/< (LOCALTIMESTAMP - ?::interval)/,
|
last_discover => \[q/< (LOCALTIMESTAMP - ?::interval)/,
|
||||||
(setting('expire_devices') * 86400)],
|
(setting('expire_devices') * 86400)],
|
||||||
})->delete();
|
})->get_column('ip')->all;
|
||||||
|
|
||||||
|
my $happy = jq_insert([map {{
|
||||||
|
device => $_,
|
||||||
|
action => 'delete',
|
||||||
|
}} @hostlist]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -472,6 +472,7 @@ job_prio:
|
|||||||
- power
|
- power
|
||||||
- snapshot
|
- snapshot
|
||||||
- vlan
|
- vlan
|
||||||
|
- delete
|
||||||
normal:
|
normal:
|
||||||
- arpnip
|
- arpnip
|
||||||
- arpwalk
|
- arpwalk
|
||||||
@@ -493,6 +494,7 @@ worker_plugins:
|
|||||||
- 'Arpwalk'
|
- 'Arpwalk'
|
||||||
- 'Contact'
|
- 'Contact'
|
||||||
- 'Delete'
|
- 'Delete'
|
||||||
|
- 'Delete::Hooks'
|
||||||
- 'Discover'
|
- 'Discover'
|
||||||
- 'Discover::CanonicalIP'
|
- 'Discover::CanonicalIP'
|
||||||
- 'Discover::Entities'
|
- 'Discover::Entities'
|
||||||
|
|||||||
@@ -51,9 +51,9 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<textarea id="nd_devdel-log" class="input-block-level" rows="2" data-form="delete"
|
<textarea id="nd_devdel-log" class="input-block-level" rows="2" data-form="delete"
|
||||||
placeholder="Enter a log message" name="log"></textarea>
|
placeholder="Enter a log message" name="extra"></textarea>
|
||||||
<label class="checkbox" style="display: block">
|
<label class="checkbox" style="display: block">
|
||||||
<input id="nd_devdel-archive" type="checkbox" data-form="delete" name="archive">
|
<input id="nd_devdel-archive" type="checkbox" data-form="delete" name="port">
|
||||||
<h4 class="nd_unbolden">Archive Nodes</h4>
|
<h4 class="nd_unbolden">Archive Nodes</h4>
|
||||||
</label>
|
</label>
|
||||||
<input type="hidden" data-form="delete" value="[% row.ip | html_entity %]" name="device"/>
|
<input type="hidden" data-form="delete" value="[% row.ip | html_entity %]" name="device"/>
|
||||||
|
|||||||
@@ -243,9 +243,9 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<textarea id="nd_devdel-log" class="input-block-level" rows="2" data-form="delete"
|
<textarea id="nd_devdel-log" class="input-block-level" rows="2" data-form="delete"
|
||||||
placeholder="Enter a log message" name="log"></textarea>
|
placeholder="Enter a log message" name="extra"></textarea>
|
||||||
<label class="checkbox">
|
<label class="checkbox">
|
||||||
<input id="nd_devdel-archive" type="checkbox" data-form="delete" name="archive">
|
<input id="nd_devdel-archive" type="checkbox" data-form="delete" name="port">
|
||||||
<h4 class="nd_unbolden">Archive Nodes</h4>
|
<h4 class="nd_unbolden">Archive Nodes</h4>
|
||||||
</label>
|
</label>
|
||||||
<input type="hidden" data-form="delete" value="[% d.ip | html_entity %]" name="device"/>
|
<input type="hidden" data-form="delete" value="[% d.ip | html_entity %]" name="device"/>
|
||||||
|
|||||||
@@ -176,7 +176,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
toastr.success('Deleted device '+ tr.data('for-device'));
|
toastr.success('Queued job to delete '+ tr.data('for-device'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// skip any error reporting for now
|
// skip any error reporting for now
|
||||||
|
|||||||
Reference in New Issue
Block a user