#380 attempt to resolve port name when it is given wrong

This commit is contained in:
Oliver Gorwits
2018-04-07 23:17:23 +01:00
parent f7ca094e26
commit f4a143612b

View File

@@ -238,6 +238,7 @@ sub store_neighbors {
# OK, remote IP seems sane
$remote_ip = $r_netaddr->addr;
my $peer_device = get_device($remote_ip);
# what we came here to do.... discover the neighbor
debug sprintf ' [%s] neigh - %s with ID [%s] on %s',
@@ -248,6 +249,24 @@ sub store_neighbors {
if (defined $remote_port) {
# clean weird characters
$remote_port =~ s/[^\d\s\/\.,()\w:-]+//gi;
# attempt to resolve port name when it is given wrong
# https://github.com/netdisco/netdisco/issues/380
if ($peer_device and $peer_device->in_storage) {
my $peer_port = schema('netdisco')->resultset('DevicePort')->single({
ip => $peer_device->ip,
port => [
{'=', $remote_port},
{'-ilike', normalize_port($remote_port)},
{'-ilike', ('%'.quotemeta($remote_port))},
],
});
if ($peer_port and ($peer_port->port ne $remote_port)) {
info sprintf ' [%s] neigh - changing remote port on %s from %s to %s',
$device->ip, $port, $remote_port, $peer_port->port;
$remote_port = $peer_port->port;
}
}
}
else {
info sprintf ' [%s] neigh - no remote port found for port %s at %s',
@@ -266,22 +285,18 @@ sub store_neighbors {
# update master of our aggregate to be a neighbor of
# the master on our peer device (a lot of iffs to get there...).
# & cannot use ->neighbor prefetch because this is the port insert!
if (defined $portrow->slave_of) {
my $peer_device = get_device($remote_ip);
if ($peer_device and $peer_device->in_storage and defined $portrow->slave_of) {
my $master = schema('netdisco')->resultset('DevicePort')->single({
ip => $device->ip,
port => $portrow->slave_of
});
if ($peer_device and $peer_device->in_storage and $master
and not ($portrow->is_master or defined $master->slave_of)) {
my $peer_port = schema('netdisco')->resultset('DevicePort')->single({
ip => $peer_device->ip,
port => $portrow->remote_port,
});
my $peer_port = schema('netdisco')->resultset('DevicePort')->single({
ip => $peer_device->ip,
port => $portrow->remote_port,
});
if ($master and not ($portrow->is_master or defined $master->slave_of)) {
$master->update({
remote_ip => ($peer_device->ip || $remote_ip),
remote_port => ($peer_port ? $peer_port->slave_of : undef ),
@@ -296,6 +311,14 @@ sub store_neighbors {
return @to_discover;
}
sub normalize_port {
my $port = shift or return '';
my ($start, $end) = ('', '');
if ($port =~ m/^([a-z]{2})/i) { $start = $1 }
($end = $port) =~ s/^\D+//;
return (quotemeta($start) .'%'. quotemeta($end));
}
# take data from the topology table and update remote_ip and remote_port
# in the devices table. only use root_ips and skip any bad topo entries.
sub set_manual_topology {