Delete device from CLI with new "delete" option to netdisco-do

This commit is contained in:
Oliver Gorwits
2015-02-04 13:54:00 +00:00
parent af7f36774a
commit aabc65b87a
6 changed files with 68 additions and 17 deletions

View File

@@ -3,6 +3,7 @@
[NEW FEATURES]
* [#171] Log files now rotate at 10MB up to seven times
* Delete device from CLI with new "delete" option to netdisco-do
[ENHANCEMENTS]

View File

@@ -113,8 +113,9 @@ unless ($action) {
}
use App::Netdisco::Util::SNMP ();
use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Util::Device qw/get_device delete_device/;
use NetAddr::IP::Lite ':lower';
use Scalar::Util 'blessed';
sub show {
my $ip = NetAddr::IP::Lite->new($device)
@@ -137,6 +138,22 @@ unless ($action) {
Data::Printer::p($i->$extra);
return ('done', "Showed $extra response from $device.");
}
sub delete {
my $ip = NetAddr::IP::Lite->new($device)
or return ('error', "Bad host or IP: $device");
my $dev = get_device($ip->addr);
unless (blessed $dev and $dev->in_storage) {
return ('error', "Don't know device: $device");
}
$extra ||= ''; my $archive = 0;
if ($extra =~ m/^(\d),(.+)/) {
($archive, $extra) = ($1, $2);
}
delete_device($dev->ip, $archive, $extra);
return ('done', "Deleted device $device.");
}
}
my $worker = MyWorker->new();
@@ -204,6 +221,12 @@ Run a macsuck on the device (specified with C<-d>).
Run an arpnip on the device (specified with C<-d>).
=head2 delete
Delete a device (specified with C<-d>). Pass a log message for the action in
the C<-e> parameter. Optionally request for associated nodes to be archived
(rather than deleted) by prefixing the C<-e> parameter with "C<1,>".
=head2 nbtstat
Run an nbtstat on the node (specified with C<-d>).

View File

@@ -40,6 +40,8 @@ __PACKAGE__->add_columns(
},
);
__PACKAGE__->set_primary_key("id");
=head1 ADDITIONAL COLUMNS
=head2 creation_stamp

View File

@@ -11,15 +11,16 @@ use base 'DBIx::Class::Core';
__PACKAGE__->table("device_port_wireless");
__PACKAGE__->add_columns(
"ip",
{ data_type => "inet", is_nullable => 1 },
{ data_type => "inet", is_nullable => 0 },
"port",
{ data_type => "text", is_nullable => 1 },
{ data_type => "text", is_nullable => 0 },
"channel",
{ data_type => "integer", is_nullable => 1 },
"power",
{ data_type => "integer", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("port", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:T5GmnCj/9BB7meiGZ3xN7g

View File

@@ -8,6 +8,7 @@ use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
get_device
delete_device
check_device_no
check_device_only
is_discoverable
@@ -61,6 +62,39 @@ sub get_device {
->find_or_new({ip => $ip});
}
=head2 delete_device( $ip, $archive? )
Given an IP address, deletes the device from Netdisco, including all related
data such as logs and nodes. If the C<$archive> parameter is true, then nodes
will be maintained in an archive state.
Returns true if the transaction completes, else returns false.
=cut
sub delete_device {
my ($ip, $archive, $log) = @_;
my $device = get_device($ip) or return 0;
return 0 if not $device->in_storage;
my $happy = 0;
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('UserLog')->create({
username => session('logged_in_user'),
userip => scalar eval {request->remote_address},
event => "Delete device ". $device->ip ." ($ip)",
details => $log,
});
# will delete everything related too...
schema('netdisco')->resultset('Device')
->search({ ip => $device->ip })->delete({archive_nodes => $archive});
$happy = 1;
});
return $happy;
}
=head2 check_device_no( $ip, $setting_name )
Given the IP address of a device, returns true if the configuration setting

View File

@@ -6,6 +6,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::JobQueue 'jq_insert';
use App::Netdisco::Util::Device 'delete_device';
sub add_job {
my ($action, $device, $subaction) = @_;
@@ -48,20 +49,9 @@ ajax '/ajax/control/admin/delete' => require_role admin => sub {
send_error('Bad device', 400)
if ! $device or $device->addr eq '0.0.0.0';
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('UserLog')->create({
username => session('logged_in_user'),
userip => request->remote_address,
event => "Delete device ". $device->addr,
details => param('log'),
});
my $device = schema('netdisco')->resultset('Device')
->search({ip => param('device')});
# will delete everything related too...
$device->delete({archive_nodes => param('archive')});
});
return delete_device(
$device->addr, param('archive'), param('log'),
);
};
get '/admin/*' => require_role admin => sub {