diff --git a/Netdisco/Changes b/Netdisco/Changes index 5d0e36ea..8a3ab2a0 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -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] diff --git a/Netdisco/bin/netdisco-do b/Netdisco/bin/netdisco-do index 724892b6..2084b4e5 100755 --- a/Netdisco/bin/netdisco-do +++ b/Netdisco/bin/netdisco-do @@ -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>). diff --git a/Netdisco/lib/App/Netdisco/DB/Result/DevicePortLog.pm b/Netdisco/lib/App/Netdisco/DB/Result/DevicePortLog.pm index 2c9d1f7b..9f8043e2 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/DevicePortLog.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/DevicePortLog.pm @@ -40,6 +40,8 @@ __PACKAGE__->add_columns( }, ); +__PACKAGE__->set_primary_key("id"); + =head1 ADDITIONAL COLUMNS =head2 creation_stamp diff --git a/Netdisco/lib/App/Netdisco/DB/Result/DevicePortWireless.pm b/Netdisco/lib/App/Netdisco/DB/Result/DevicePortWireless.pm index 11e2656a..83eb3ce4 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/DevicePortWireless.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/DevicePortWireless.pm @@ -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 diff --git a/Netdisco/lib/App/Netdisco/Util/Device.pm b/Netdisco/lib/App/Netdisco/Util/Device.pm index 6076c529..a5677516 100644 --- a/Netdisco/lib/App/Netdisco/Util/Device.pm +++ b/Netdisco/lib/App/Netdisco/Util/Device.pm @@ -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 diff --git a/Netdisco/lib/App/Netdisco/Web/AdminTask.pm b/Netdisco/lib/App/Netdisco/Web/AdminTask.pm index e0197a69..5aab8a73 100644 --- a/Netdisco/lib/App/Netdisco/Web/AdminTask.pm +++ b/Netdisco/lib/App/Netdisco/Web/AdminTask.pm @@ -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 {