Set canonical IP failed on synthesized cols (and was wrong anyway) (closes #35)

This commit is contained in:
Oliver Gorwits
2013-10-16 21:00:10 +01:00
parent 8946cf291b
commit 57cc2e029f
3 changed files with 25 additions and 23 deletions

View File

@@ -4,6 +4,7 @@
* Update Print media CSS to handle new UI components * Update Print media CSS to handle new UI components
* Deadlock in Discover over access to the device_ports table * Deadlock in Discover over access to the device_ports table
* Set canonical IP failed on synthesized cols (and was wrong anyway) (closes #35)
2.018000 - 2013-10-08 2.018000 - 2013-10-08

View File

@@ -12,6 +12,7 @@ use Try::Tiny;
use base 'Exporter'; use base 'Exporter';
our @EXPORT = (); our @EXPORT = ();
our @EXPORT_OK = qw/ our @EXPORT_OK = qw/
set_canonical_ip
store_device store_interfaces store_wireless store_device store_interfaces store_wireless
store_vlans store_power store_modules store_vlans store_power store_modules
store_neighbors discover_new_neighbors store_neighbors discover_new_neighbors
@@ -48,9 +49,6 @@ sub store_device {
my $interfaces = $snmp->interfaces; my $interfaces = $snmp->interfaces;
my $ip_netmask = $snmp->ip_netmask; my $ip_netmask = $snmp->ip_netmask;
# find root IP
_set_canonical_ip($device, $snmp);
my $hostname = hostname_from_ip($device->ip); my $hostname = hostname_from_ip($device->ip);
$device->dns($hostname) if $hostname; $device->dns($hostname) if $hostname;
my $localnet = NetAddr::IP::Lite->new('127.0.0.0/8'); my $localnet = NetAddr::IP::Lite->new('127.0.0.0/8');
@@ -118,7 +116,7 @@ sub store_device {
}); });
} }
sub _set_canonical_ip { sub set_canonical_ip {
my ($device, $snmp) = @_; my ($device, $snmp) = @_;
my $oldip = $device->ip; my $oldip = $device->ip;
@@ -131,34 +129,36 @@ sub _set_canonical_ip {
# remove old device and aliases # remove old device and aliases
schema('netdisco')->txn_do(sub { schema('netdisco')->txn_do(sub {
my $gone = $device->device_ips->delete; my $copy = schema('netdisco')->resultset('Device')
debug sprintf ' [%s] device - removed %s aliases', ->find({ip => $oldip});
$oldip, $gone;
# our special delete which is more efficient # our special delete which is more efficient
schema('netdisco')->resultset('Device') schema('netdisco')->resultset('Device')
->search({ ip => $device->ip })->delete({keep_nodes => 1}); ->search({ ip => $device->ip })->delete({keep_nodes => 1});
# a new row object from the old one
$device = schema('netdisco')->resultset('Device')
->new({ $device->get_columns });
debug sprintf ' [%s] device - deleted self', $oldip; debug sprintf ' [%s] device - deleted self', $oldip;
$device = schema('netdisco')->resultset('Device')
->create({ $copy->get_columns, ip => $newip })
if defined $copy;
# make nodes follow device
schema('netdisco')->resultset('Node')
->search({switch => $oldip})
->update({switch => $newip});
}); });
$device->ip($newip);
} }
}
# either root_ip is changed or unchanged, but it exists else {
return; my $revname = ipv4_from_hostname($snmp->name);
if (setting('reverse_sysname') and $revname) {
debug sprintf ' [%s] device - changing root IP to revname %s',
$oldip, $revname;
$device->ip($revname);
}
} }
my $revname = ipv4_from_hostname($snmp->name); # either root_ip is changed or unchanged, but it exists
if (setting('reverse_sysname') and $revname) { return $device;
debug sprintf ' [%s] device - changing root IP to revname %s',
$oldip, $revname;
$device->ip($revname);
}
} }
=head2 store_interfaces( $device, $snmp ) =head2 store_interfaces( $device, $snmp )

View File

@@ -71,6 +71,7 @@ sub discover {
return job_error("discover failed: could not SNMP connect to $host"); return job_error("discover failed: could not SNMP connect to $host");
} }
$device = set_canonical_ip($device, $snmp);
store_device($device, $snmp); store_device($device, $snmp);
store_interfaces($device, $snmp); store_interfaces($device, $snmp);
store_wireless($device, $snmp); store_wireless($device, $snmp);