* Fixed a bug in Database->insert_or_update_bonds() that caused unneeded UPDATE.

* Fixed anvil-update-states to record the bond UUID for interfaces already in bonds.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 7 years ago
parent d600a635d4
commit 76725e9160
  1. 10
      Anvil/Tools/Database.pm
  2. 240
      tools/anvil-update-states

@ -1874,11 +1874,11 @@ WHERE
my $old_bond_primary_slave = $row->[4]; my $old_bond_primary_slave = $row->[4];
my $old_bond_primary_reselect = $row->[5]; my $old_bond_primary_reselect = $row->[5];
my $old_bond_active_slave = $row->[6]; my $old_bond_active_slave = $row->[6];
my $old_bond_mii_polling_interval = $row->[8]; my $old_bond_mii_polling_interval = $row->[7];
my $old_bond_up_delay = $row->[9]; my $old_bond_up_delay = $row->[8];
my $old_bond_down_delay = $row->[10]; my $old_bond_down_delay = $row->[9];
my $old_bond_mac_address = $row->[11]; my $old_bond_mac_address = $row->[10];
my $old_bond_operational = $row->[12]; my $old_bond_operational = $row->[11];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
old_bond_host_uuid => $old_bond_host_uuid, old_bond_host_uuid => $old_bond_host_uuid,
old_bond_name => $old_bond_name, old_bond_name => $old_bond_name,

@ -91,6 +91,7 @@ sub report_network
my $mii_polling_interval = ""; my $mii_polling_interval = "";
my $up_delay = ""; my $up_delay = "";
my $down_delay = ""; my $down_delay = "";
my $bond_master = "";
if (exists $anvil->data->{sys}{network}{interface}{$interface}) if (exists $anvil->data->{sys}{network}{interface}{$interface})
{ {
@ -158,9 +159,20 @@ sub report_network
type => $type, type => $type,
}}); }});
} }
elsif (-e $full_path."/master")
{
# No, but it's slaved to one.
my $target = readlink($full_path."/master");
$bond_master = ($target =~ /^.*\/(.*)$/)[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
target => $target,
bond_master => $bond_master,
}});
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
active_slave => $active_slave, active_slave => $active_slave,
bond_master => $bond_master,
bond_mode => $bond_mode, bond_mode => $bond_mode,
down_delay => $down_delay, down_delay => $down_delay,
duplex => $duplex, duplex => $duplex,
@ -212,6 +224,7 @@ sub report_network
$anvil->data->{network}{interfaces}{by_name}{$interface} = { $anvil->data->{network}{interfaces}{by_name}{$interface} = {
active_slave => $active_slave, active_slave => $active_slave,
bond_mode => $bond_mode, bond_mode => $bond_mode,
bond_master => $bond_master,
down_delay => $down_delay, down_delay => $down_delay,
duplex => $duplex, duplex => $duplex,
ip_address => $ip_address, ip_address => $ip_address,
@ -231,6 +244,7 @@ sub report_network
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"network::interfaces::by_name::${interface}::active_slave" => $anvil->data->{network}{interfaces}{by_name}{$interface}{active_slave}, "network::interfaces::by_name::${interface}::active_slave" => $anvil->data->{network}{interfaces}{by_name}{$interface}{active_slave},
"network::interfaces::by_name::${interface}::bond_mode" => $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_mode}, "network::interfaces::by_name::${interface}::bond_mode" => $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_mode},
"network::interfaces::by_name::${interface}::bond_master" => $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_master},
"network::interfaces::by_name::${interface}::down_delay" => $anvil->data->{network}{interfaces}{by_name}{$interface}{down_delay}, "network::interfaces::by_name::${interface}::down_delay" => $anvil->data->{network}{interfaces}{by_name}{$interface}{down_delay},
"network::interfaces::by_name::${interface}::duplex" => $anvil->data->{network}{interfaces}{by_name}{$interface}{duplex}, "network::interfaces::by_name::${interface}::duplex" => $anvil->data->{network}{interfaces}{by_name}{$interface}{duplex},
"network::interfaces::by_name::${interface}::ip_address" => $anvil->data->{network}{interfaces}{by_name}{$interface}{ip_address}, "network::interfaces::by_name::${interface}::ip_address" => $anvil->data->{network}{interfaces}{by_name}{$interface}{ip_address},
@ -251,10 +265,15 @@ sub report_network
} }
closedir(DIRECTORY); closedir(DIRECTORY);
# We need to record bonds first so that their UUIDs are available when recording interfaces.
foreach my $processing ("bond", "interface")
{
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { processing => $processing }});
foreach my $interface (sort {$a cmp $b} keys %{$anvil->data->{network}{interfaces}{by_name}}) foreach my $interface (sort {$a cmp $b} keys %{$anvil->data->{network}{interfaces}{by_name}})
{ {
my $active_slave = $anvil->data->{network}{interfaces}{by_name}{$interface}{active_slave}; my $active_slave = $anvil->data->{network}{interfaces}{by_name}{$interface}{active_slave};
my $bond_mode = $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_mode}; my $bond_mode = $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_mode};
my $bond_master = $anvil->data->{network}{interfaces}{by_name}{$interface}{bond_master};
my $down_delay = $anvil->data->{network}{interfaces}{by_name}{$interface}{down_delay}; my $down_delay = $anvil->data->{network}{interfaces}{by_name}{$interface}{down_delay};
my $duplex = $anvil->data->{network}{interfaces}{by_name}{$interface}{duplex}; my $duplex = $anvil->data->{network}{interfaces}{by_name}{$interface}{duplex};
my $ip_address = $anvil->data->{network}{interfaces}{by_name}{$interface}{ip_address}; my $ip_address = $anvil->data->{network}{interfaces}{by_name}{$interface}{ip_address};
@ -276,6 +295,7 @@ sub report_network
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
active_slave => $active_slave, active_slave => $active_slave,
bond_mode => $bond_mode, bond_mode => $bond_mode,
bond_master => $bond_master,
default_gateway => $default_gateway, default_gateway => $default_gateway,
down_delay => $down_delay, down_delay => $down_delay,
dns => $dns, dns => $dns,
@ -297,31 +317,37 @@ sub report_network
up_delay => $up_delay, up_delay => $up_delay,
}}); }});
if ($type eq "interface") if (($type eq $processing) && ($type eq "bond"))
{ {
my $network_interface_uuid = $anvil->Database->insert_or_update_network_interfaces({ my $bond_uuid = $anvil->Database->insert_or_update_bonds({
debug => 2, debug => 2,
file => $THIS_FILE, file => $THIS_FILE,
line => __LINE__, line => __LINE__,
network_interface_name => $interface, bond_name => $interface,
network_interface_duplex => $duplex, bond_mode => $bond_mode,
network_interface_link_state => $link_state, bond_mtu => $mtu,
network_interface_operational => $operational, bond_link_state => $link_state,
network_interface_mac_address => $mac_address, bond_operational => $operational,
network_interface_medium => $media, bond_mac_address => $mac_address,
network_interface_mtu => $mtu, bond_primary_slave => $primary_slave,
network_interface_speed => $speed, bond_primary_reselect => $primary_reselect,
bond_active_slave => $active_slave,
bond_mii_polling_interval => $mii_polling_interval,
bond_up_delay => $up_delay,
bond_down_delay => $down_delay,
}); });
$anvil->data->{bond_by_name}{$interface} = $bond_uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "bond_by_name::${interface}" => $anvil->data->{bond_by_name}{$interface} }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { network_interface_uuid => $network_interface_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { bond_uuid => $bond_uuid }});
if (($network_interface_uuid) && ($ip_address)) if (($bond_uuid) && ($ip_address))
{ {
my $ip_address_uuid = $anvil->Database->insert_or_update_ip_addresses({ my $ip_address_uuid = $anvil->Database->insert_or_update_ip_addresses({
debug => 2, debug => 2,
file => $THIS_FILE, file => $THIS_FILE,
line => __LINE__, line => __LINE__,
ip_address_on_type => $type, ip_address_on_type => $type,
ip_address_on_uuid => $network_interface_uuid, ip_address_on_uuid => $bond_uuid,
ip_address_address => $ip_address, ip_address_address => $ip_address,
ip_address_subnet_mask => $subnet_mask, ip_address_subnet_mask => $subnet_mask,
ip_address_gateway => $gateway, ip_address_gateway => $gateway,
@ -330,35 +356,40 @@ sub report_network
}); });
} }
} }
elsif ($type eq "bond")
if (($type eq $processing) && ($type eq "interface"))
{ {
my $bond_uuid = $anvil->Database->insert_or_update_bonds({ my $say_bond_uuid = "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "bond_by_name::${bond_master}" => $anvil->data->{bond_by_name}{$bond_master} }});
if (($bond_master) && ($anvil->data->{bond_by_name}{$bond_master}))
{
$say_bond_uuid = $anvil->data->{bond_by_name}{$bond_master};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { say_bond_uuid => $say_bond_uuid }});
}
my $network_interface_uuid = $anvil->Database->insert_or_update_network_interfaces({
debug => 2, debug => 2,
file => $THIS_FILE, file => $THIS_FILE,
line => __LINE__, line => __LINE__,
bond_name => $interface, network_interface_bond_uuid => $say_bond_uuid,
bond_mode => $bond_mode, network_interface_name => $interface,
bond_mtu => $mtu, network_interface_duplex => $duplex,
bond_link_state => $link_state, network_interface_link_state => $link_state,
bond_operational => $operational, network_interface_operational => $operational,
bond_mac_address => $mac_address, network_interface_mac_address => $mac_address,
bond_primary_slave => $primary_slave, network_interface_medium => $media,
bond_primary_reselect => $primary_reselect, network_interface_mtu => $mtu,
bond_active_slave => $active_slave, network_interface_speed => $speed,
bond_mii_polling_interval => $mii_polling_interval,
bond_up_delay => $up_delay,
bond_down_delay => $down_delay,
}); });
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { bond_uuid => $bond_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { network_interface_uuid => $network_interface_uuid }});
if (($bond_uuid) && ($ip_address)) if (($network_interface_uuid) && ($ip_address))
{ {
my $ip_address_uuid = $anvil->Database->insert_or_update_ip_addresses({ my $ip_address_uuid = $anvil->Database->insert_or_update_ip_addresses({
debug => 2, debug => 2,
file => $THIS_FILE, file => $THIS_FILE,
line => __LINE__, line => __LINE__,
ip_address_on_type => $type, ip_address_on_type => $type,
ip_address_on_uuid => $bond_uuid, ip_address_on_uuid => $network_interface_uuid,
ip_address_address => $ip_address, ip_address_address => $ip_address,
ip_address_subnet_mask => $subnet_mask, ip_address_subnet_mask => $subnet_mask,
ip_address_gateway => $gateway, ip_address_gateway => $gateway,
@ -368,11 +399,14 @@ sub report_network
} }
} }
} }
}
# I need to read back in the interface and bridge data and splice it together before I write out the # I need to read back in the interface and bridge data and splice it together before I write out the
# XML and JSON files. # XML and JSON files.
my $ip_address = {};
my $query = " my $query = "
SELECT SELECT
ip_address_uuid,
ip_address_on_type, ip_address_on_type,
ip_address_on_uuid, ip_address_on_uuid,
ip_address_address, ip_address_address,
@ -394,24 +428,28 @@ WHERE
}}); }});
foreach my $row (@{$results}) foreach my $row (@{$results})
{ {
my $ip_address_on_type = $row->[0]; my $ip_address_uuid = $row->[0];
my $ip_address_on_uuid = $row->[1]; $ip_address->{$ip_address_uuid} = {
my $ip_address_address = $row->[2]; ip_address_on_type => $row->[1],
my $ip_address_subnet_mask = $row->[3]; ip_address_on_uuid => $row->[2],
my $ip_address_gateway = $row->[4]; ip_address_address => $row->[3],
my $ip_address_default_gateway = $row->[5]; ip_address_subnet_mask => $row->[4],
my $ip_address_dns = $row->[6]; ip_address_gateway => $row->[5],
ip_address_default_gateway => $row->[6],
ip_address_dns => $row->[7],
};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
ip_address_on_type => $ip_address_on_type, "ip_address->{$ip_address_uuid}{ip_address_on_type}" => $ip_address->{$ip_address_uuid}{ip_address_on_type},
ip_address_on_uuid => $ip_address_on_uuid, "ip_address->{$ip_address_uuid}{ip_address_on_uuid}" => $ip_address->{$ip_address_uuid}{ip_address_on_uuid},
ip_address_address => $ip_address_address, "ip_address->{$ip_address_uuid}{ip_address_address}" => $ip_address->{$ip_address_uuid}{ip_address_address},
ip_address_subnet_mask => $ip_address_subnet_mask, "ip_address->{$ip_address_uuid}{ip_address_subnet_mask}" => $ip_address->{$ip_address_uuid}{ip_address_subnet_mask},
ip_address_gateway => $ip_address_gateway, "ip_address->{$ip_address_uuid}{ip_address_gateway}" => $ip_address->{$ip_address_uuid}{ip_address_gateway},
ip_address_default_gateway => $ip_address_default_gateway, "ip_address->{$ip_address_uuid}{ip_address_default_gateway}" => $ip_address->{$ip_address_uuid}{ip_address_default_gateway},
ip_address_dns => $ip_address_dns, "ip_address->{$ip_address_uuid}{ip_address_dns}" => $ip_address->{$ip_address_uuid}{ip_address_dns},
}}); }});
} }
my $bonds = {};
$query = " $query = "
SELECT SELECT
bond_uuid, bond_uuid,
@ -441,30 +479,65 @@ WHERE
foreach my $row (@{$results}) foreach my $row (@{$results})
{ {
my $bond_uuid = $row->[0]; my $bond_uuid = $row->[0];
my $bond_name = $row->[1]; $bonds->{$bond_uuid} = {
my $bond_mode = $row->[2]; bond_name => $row->[1],
my $bond_mtu = $row->[3]; bond_mode => $row->[2],
my $bond_primary_slave = $row->[4]; bond_mtu => $row->[3],
my $bond_primary_reselect = $row->[5]; bond_primary_slave => $row->[4],
my $bond_active_slave = $row->[6]; bond_primary_reselect => $row->[5],
my $bond_mii_polling_interval = $row->[7]; bond_active_slave => $row->[6],
my $bond_up_delay = $row->[8]; bond_mii_polling_interval => $row->[7],
my $bond_down_delay = $row->[9]; bond_up_delay => $row->[8],
my $bond_mac_address = $row->[10]; bond_down_delay => $row->[9],
my $bond_operational = $row->[11]; bond_mac_address => $row->[10],
bond_operational => $row->[11],
};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
bond_uuid => $bond_uuid, "bonds->{$bond_uuid}{bond_name}" => $bonds->{$bond_uuid}{bond_name},
bond_name => $bond_name, "bonds->{$bond_uuid}{bond_mode}" => $bonds->{$bond_uuid}{bond_mode},
bond_mode => $bond_mode, "bonds->{$bond_uuid}{bond_mtu}" => $bonds->{$bond_uuid}{bond_mtu},
bond_mtu => $bond_mtu, "bonds->{$bond_uuid}{bond_primary_slave}" => $bonds->{$bond_uuid}{bond_primary_slave},
bond_primary_slave => $bond_primary_slave, "bonds->{$bond_uuid}{bond_primary_reselect}" => $bonds->{$bond_uuid}{bond_primary_reselect},
bond_primary_reselect => $bond_primary_reselect, "bonds->{$bond_uuid}{bond_active_slave}" => $bonds->{$bond_uuid}{bond_active_slave},
bond_active_slave => $bond_active_slave, "bonds->{$bond_uuid}{bond_mii_polling_interval}" => $bonds->{$bond_uuid}{bond_mii_polling_interval},
bond_mii_polling_interval => $bond_mii_polling_interval, "bonds->{$bond_uuid}{bond_up_delay}" => $bonds->{$bond_uuid}{bond_up_delay},
bond_up_delay => $bond_up_delay, "bonds->{$bond_uuid}{bond_down_delay}" => $bonds->{$bond_uuid}{bond_down_delay},
bond_down_delay => $bond_down_delay, "bonds->{$bond_uuid}{bond_mac_address}" => $bonds->{$bond_uuid}{bond_mac_address},
bond_mac_address => $bond_mac_address, "bonds->{$bond_uuid}{bond_operational}" => $bonds->{$bond_uuid}{bond_operational},
bond_operational => $bond_operational, }});
}
my $bridges = {};
$query = "
SELECT
bridge_uuid,
bridge_name,
bridge_id,
bridge_stp_enabled
FROM
bridges
WHERE
bridge_host_uuid = ".$anvil->data->{sys}{use_db_fh}->quote($anvil->Get->host_uuid)."
;";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0124", variables => { query => $query }});
$results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
$count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
results => $results,
count => $count,
}});
foreach my $row (@{$results})
{
my $bridge_uuid = $row->[0];
$bridges->{$bridge_uuid} = {
bridge_name => $row->[1],
bridge_id => $row->[2],
bridge_stp_enabled => $row->[3],
};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"bridges->{$bridge_uuid}{bridge_name}" => $bridges->{$bridge_uuid}{bridge_name},
"bridges->{$bridge_uuid}{bridge_id}" => $bridges->{$bridge_uuid}{bridge_id},
"bridges->{$bridge_uuid}{bridge_stp_enabled}" => $bridges->{$bridge_uuid}{bridge_stp_enabled},
}}); }});
} }
@ -497,6 +570,9 @@ ORDER BY
}}); }});
# The order will track the order the interfaces were last modified, which is a way to determine when # The order will track the order the interfaces were last modified, which is a way to determine when
# they came up. # they came up.
my $network_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$network_xml .= "<network>\n";
my $network_json = "{\"networks\":[\n";
my $order = 1; my $order = 1;
foreach my $row (@{$results}) foreach my $row (@{$results})
{ {
@ -524,15 +600,32 @@ ORDER BY
network_interface_bridge_uuid => $network_interface_bridge_uuid, network_interface_bridge_uuid => $network_interface_bridge_uuid,
order => $order, order => $order,
}}); }});
my $say_bond = "";
if (($network_interface_bond_uuid) && ($network_interface_bond_uuid ne 'NULL'))
{
$say_bond = $bonds->{$network_interface_bond_uuid}{bond_name};
}
my $say_bridge = "";
if (($network_interface_bridge_uuid) && ($network_interface_bridge_uuid ne 'NULL'))
{
$say_bridge = $bridges->{$network_interface_bridge_uuid}{bridge_name};
}
$network_json .= " { \"name\":\"$network_interface_name\", \"mac\":\"$network_interface_mac_address\", \"link\":\"$network_interface_link_state\", \"speed\":\"$network_interface_speed\", \"mtu\":\"$network_interface_mtu\", \"duplex\":\"$network_interface_duplex\", \"state\":\"$network_interface_operational\", \"media\":\"$network_interface_medium\", \"bond\":\"$say_bond\", \"bridge\":\"$say_bridge\", \"order\":\"$order\" },\n";
$network_xml .= " <interface name=\"$network_interface_name\" mac=\"$network_interface_mac_address\" link=\"$network_interface_link_state\" speed=\"$network_interface_speed\" mtu=\"$network_interface_mtu\" duplex=\"$network_interface_duplex\" state=\"$network_interface_operational\" media=\"$network_interface_medium\" bond=\"$say_bond\" bridge=\"$say_bridge\" order=\"$order\" />\n";
$order++; $order++;
} }
$network_json =~ s/,$//s;
$network_json .= "]}\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { network_json => $network_json }});
$network_xml .= "</network>\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { network_xml => $network_xml }});
die; die;
# Write out the XML file and JSON file. # Write out the XML file and JSON file.
my $network_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$network_xml .= "<network>\n";
my $network_json = "{\"networks\":[\n";
$query = " $query = "
SELECT SELECT
network_interface_mac_address, network_interface_mac_address,
@ -589,13 +682,6 @@ ORDER BY
$order++; $order++;
} }
$network_json =~ s/,$//s;
$network_json .= "]}\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_json => $network_json }});
$network_xml .= "</network>\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_xml => $network_xml }});
### TODO: Set the 'status/network.json' name into 'anvil.conf' ### TODO: Set the 'status/network.json' name into 'anvil.conf'
# Write the JSON file. # Write the JSON file.
my $output_json = $anvil->data->{path}{directories}{html}."/status/network.json"; my $output_json = $anvil->data->{path}{directories}{html}."/status/network.json";

Loading…
Cancel
Save