* Removed the 'bridge_interfaces' table and related database methods. This was found to be duplicate, given that 'network_interfaces' has references to parent bonds and bridges.

* Renamed 'bridges' -> 'bridge_mac' to 'bridge_mac_address' to be consstent with other MAC address column names.
* Finished Network->load_interfces().
* Updated anvil-update-states to check for interfaces under bridges that are missing their 'network_interface_bridge_uuid' reference UUID.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 5 years ago
parent 628f7faa45
commit ea27f88bcc
  1. 1
      Anvil/Tools.pm
  2. 259
      Anvil/Tools/Database.pm
  3. 268
      Anvil/Tools/Network.pm
  4. 65
      share/anvil.sql
  5. 45
      tools/anvil-update-states

@ -865,7 +865,6 @@ sub _set_defaults
"network_interfaces",
"bonds",
"bridges",
"bridge_interfaces",
"ip_addresses",
"files",
"file_locations",

@ -28,7 +28,6 @@ my $THIS_FILE = "Database.pm";
# get_jobs
# get_local_uuid
# initialize
# insert_or_update_bridge_interfaces
# insert_or_update_bridges
# insert_or_update_bonds
# insert_or_update_file_locations
@ -2073,236 +2072,6 @@ sub initialize
return($success);
};
=head2 insert_or_update_bridge_interfaces
This updates (or inserts) a record in the 'bridge_interfaces' table. The C<< bridge_interface_uuid >> referencing the database row will be returned.
If there is an error, an empty string is returned.
Parameters;
=head3 uuid (optional)
If set, only the corresponding database will be written to.
=head3 file (optional)
If set, this is the file name logged as the source of any INSERTs or UPDATEs.
=head3 line (optional)
If set, this is the file line number logged as the source of any INSERTs or UPDATEs.
=head3 bridge_interface_uuid (optional)
If not passed, a check will be made to see if an existing entry is found for C<< bridge_interface_bridge_uuid >> and C<< bridge_interface_network_interface_uuid >>. If found, that entry will be updated. If not found, a new record will be inserted.
=head3 bridge_interface_host_uuid (optional)
This is the host that the IP address is on. If not passed, the local C<< sys::host_uuid >> will be used (indicating it is a local IP address).
=head3 bridge_interface_bridge_uuid (required)
This is the C<< bridges -> bridge_uuid >> of the bridge that this interface is connected to.
=head3 bridge_interface_network_interface_uuid (required)
This is the C<< network_interfaces -> network_interface_uuid >> if the interface connected to the specified bridge.
=head3 bridge_interface_note (optional)
When this is set to C<< DELETED >>, the interface will be flagged as removed by the system
=cut
sub insert_or_update_bridge_interfaces
{
my $self = shift;
my $parameter = shift;
my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_bridge_interfaces()" }});
my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : "";
my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $line = defined $parameter->{line} ? $parameter->{line} : "";
my $bridge_interface_uuid = defined $parameter->{bridge_interface_uuid} ? $parameter->{bridge_interface_uuid} : "";
my $bridge_interface_host_uuid = defined $parameter->{bridge_interface_host_uuid} ? $parameter->{bridge_interface_host_uuid} : $anvil->data->{sys}{host_uuid};
my $bridge_interface_bridge_uuid = defined $parameter->{bridge_interface_bridge_uuid} ? $parameter->{bridge_interface_bridge_uuid} : "";
my $bridge_interface_network_interface_uuid = defined $parameter->{bridge_interface_network_interface_uuid} ? $parameter->{bridge_interface_network_interface_uuid} : "";
my $bridge_interface_note = defined $parameter->{bridge_interface_note} ? $parameter->{bridge_interface_note} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
uuid => $uuid,
file => $file,
line => $line,
bridge_interface_uuid => $bridge_interface_uuid,
bridge_interface_host_uuid => $bridge_interface_host_uuid,
bridge_interface_bridge_uuid => $bridge_interface_bridge_uuid,
bridge_interface_network_interface_uuid => $bridge_interface_network_interface_uuid,
bridge_interface_note => $bridge_interface_note,
}});
if (not $bridge_interface_bridge_uuid)
{
# Throw an error and exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bridge_interfaces()", parameter => "bridge_interface_bridge_uuid" }});
return("");
}
if (not $bridge_interface_network_interface_uuid)
{
# Throw an error and exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_bridge_interfaces()", parameter => "bridge_interface_network_interface_uuid" }});
return("");
}
# If we don't have a UUID, see if we can find one for the given bridge_interface server name.
if (not $bridge_interface_uuid)
{
my $query = "
SELECT
bridge_interface_uuid
FROM
bridge_interfaces
WHERE
bridge_interface_bridge_uuid = ".$anvil->Database->quote($bridge_interface_bridge_uuid)."
AND
bridge_interface_network_interface_uuid = ".$anvil->Database->quote($bridge_interface_network_interface_uuid)."
;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if ($count)
{
$bridge_interface_uuid = $results->[0]->[0];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bridge_interface_uuid => $bridge_interface_uuid }});
}
}
# If I still don't have an bridge_interface_uuid, we're INSERT'ing .
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bridge_interface_uuid => $bridge_interface_uuid }});
if (not $bridge_interface_uuid)
{
# It's possible that this is called before the host is recorded in the database. So to be
# safe, we'll return without doing anything if there is no host_uuid in the database.
my $hosts = $anvil->Database->get_hosts();
my $found = 0;
foreach my $hash_ref (@{$hosts})
{
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"hash_ref->{host_uuid}" => $hash_ref->{host_uuid},
"sys::host_uuid" => $anvil->data->{sys}{host_uuid},
}});
if ($hash_ref->{host_uuid} eq $anvil->data->{sys}{host_uuid})
{
$found = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { found => $found }});
}
}
if (not $found)
{
# We're out.
return("");
}
# INSERT
$bridge_interface_uuid = $anvil->Get->uuid();
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { bridge_interface_uuid => $bridge_interface_uuid }});
my $query = "
INSERT INTO
bridge_interfaces
(
bridge_interface_uuid,
bridge_interface_host_uuid,
bridge_interface_bridge_uuid,
bridge_interface_network_interface_uuid,
bridge_interface_note,
modified_date
) VALUES (
".$anvil->Database->quote($bridge_interface_uuid).",
".$anvil->Database->quote($bridge_interface_host_uuid).",
".$anvil->Database->quote($bridge_interface_bridge_uuid).",
".$anvil->Database->quote($bridge_interface_network_interface_uuid).",
".$anvil->Database->quote($bridge_interface_note).",
".$anvil->Database->quote($anvil->data->{sys}{database}{timestamp})."
);
";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
else
{
# Query the rest of the values and see if anything changed.
my $query = "
SELECT
bridge_interface_host_uuid,
bridge_interface_bridge_uuid,
bridge_interface_network_interface_uuid,
bridge_interface_note
FROM
bridge_interfaces
WHERE
bridge_interface_uuid = ".$anvil->Database->quote($bridge_interface_uuid)."
;";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
results => $results,
count => $count,
}});
if (not $count)
{
# I have a bridge_interface_uuid but no matching record. Probably an error.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0216", variables => { uuid_name => "bridge_interface_uuid", uuid => $bridge_interface_uuid }});
return("");
}
foreach my $row (@{$results})
{
my $old_bridge_interface_host_uuid = $row->[0];
my $old_bridge_interface_bridge_uuid = $row->[1];
my $old_bridge_interface_network_interface_uuid = $row->[2];
my $old_bridge_interface_note = $row->[3];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
old_bridge_interface_host_uuid => $old_bridge_interface_host_uuid,
old_bridge_interface_bridge_uuid => $old_bridge_interface_bridge_uuid,
old_bridge_interface_network_interface_uuid => $old_bridge_interface_network_interface_uuid,
old_bridge_interface_note => $old_bridge_interface_note,
}});
# Anything change?
if (($old_bridge_interface_host_uuid ne $bridge_interface_host_uuid) or
($old_bridge_interface_bridge_uuid ne $bridge_interface_bridge_uuid) or
($old_bridge_interface_network_interface_uuid ne $bridge_interface_network_interface_uuid) or
($old_bridge_interface_note ne $bridge_interface_note))
{
# Something changed, save.
my $query = "
UPDATE
bridge_interfaces
SET
bridge_interface_host_uuid = ".$anvil->Database->quote($bridge_interface_host_uuid).",
bridge_interface_bridge_uuid = ".$anvil->Database->quote($bridge_interface_bridge_uuid).",
bridge_interface_network_interface_uuid = ".$anvil->Database->quote($bridge_interface_network_interface_uuid).",
bridge_interface_note = ".$anvil->Database->quote($bridge_interface_note).",
modified_date = ".$anvil->Database->quote($anvil->data->{sys}{database}{timestamp})."
WHERE
bridge_interface_uuid = ".$anvil->Database->quote($bridge_interface_uuid)."
";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
$anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
}
}
}
return($bridge_interface_uuid);
}
=head2 insert_or_update_bridges
This updates (or inserts) a record in the 'bridges' table. The C<< bridge_uuid >> referencing the database row will be returned.
@ -2339,7 +2108,7 @@ This is the bridge's device name.
This is the unique identifier for the bridge.
=head2 bridge_mac (optional)
=head2 bridge_mac_address (optional)
This is the MAC address of the bridge.
@ -2367,7 +2136,7 @@ sub insert_or_update_bridges
my $bridge_host_uuid = defined $parameter->{bridge_host_uuid} ? $parameter->{bridge_host_uuid} : $anvil->data->{sys}{host_uuid};
my $bridge_name = defined $parameter->{bridge_name} ? $parameter->{bridge_name} : "";
my $bridge_id = defined $parameter->{bridge_id} ? $parameter->{bridge_id} : "";
my $bridge_mac = defined $parameter->{bridge_mac} ? $parameter->{bridge_mac} : "";
my $bridge_mac_address = defined $parameter->{bridge_mac_address} ? $parameter->{bridge_mac_address} : "";
my $bridge_mtu = defined $parameter->{bridge_mtu} ? $parameter->{bridge_mtu} : "";
my $bridge_stp_enabled = defined $parameter->{bridge_stp_enabled} ? $parameter->{bridge_stp_enabled} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
@ -2378,7 +2147,7 @@ sub insert_or_update_bridges
bridge_host_uuid => $bridge_host_uuid,
bridge_name => $bridge_name,
bridge_id => $bridge_id,
bridge_mac => $bridge_mac,
bridge_mac_address => $bridge_mac_address,
bridge_mtu => $bridge_mtu,
bridge_stp_enabled => $bridge_stp_enabled,
}});
@ -2456,7 +2225,7 @@ INSERT INTO
bridge_host_uuid,
bridge_name,
bridge_id,
bridge_mac,
bridge_mac_address,
bridge_mtu,
bridge_stp_enabled,
modified_date
@ -2465,7 +2234,7 @@ INSERT INTO
".$anvil->Database->quote($bridge_host_uuid).",
".$anvil->Database->quote($bridge_name).",
".$anvil->Database->quote($bridge_id).",
".$anvil->Database->quote($bridge_mac).",
".$anvil->Database->quote($bridge_mac_address).",
".$anvil->Database->quote($bridge_mtu).",
".$anvil->Database->quote($bridge_stp_enabled).",
".$anvil->Database->quote($anvil->data->{sys}{database}{timestamp})."
@ -2482,7 +2251,7 @@ SELECT
bridge_host_uuid,
bridge_name,
bridge_id,
bridge_mac,
bridge_mac_address,
bridge_mtu,
bridge_stp_enabled
FROM
@ -2509,24 +2278,24 @@ WHERE
my $old_bridge_host_uuid = $row->[0];
my $old_bridge_name = $row->[1];
my $old_bridge_id = $row->[2];
my $old_bridge_mac = $row->[3];
my $old_bridge_mac_address = $row->[3];
my $old_bridge_mtu = $row->[4];
my $old_bridge_stp_enabled = $row->[5];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
old_bridge_host_uuid => $old_bridge_host_uuid,
old_bridge_name => $old_bridge_name,
old_bridge_id => $old_bridge_id,
old_bridge_mac => $old_bridge_mac,
old_bridge_mac_address => $old_bridge_mac_address,
old_bridge_mtu => $old_bridge_mtu,
old_bridge_stp_enabled => $old_bridge_stp_enabled,
}});
# Anything change?
if (($old_bridge_host_uuid ne $bridge_host_uuid) or
($old_bridge_name ne $bridge_name) or
($old_bridge_id ne $bridge_id) or
($old_bridge_mac ne $bridge_mac) or
($old_bridge_mtu ne $bridge_mtu) or
if (($old_bridge_host_uuid ne $bridge_host_uuid) or
($old_bridge_name ne $bridge_name) or
($old_bridge_id ne $bridge_id) or
($old_bridge_mac_address ne $bridge_mac_address) or
($old_bridge_mtu ne $bridge_mtu) or
($old_bridge_stp_enabled ne $bridge_stp_enabled))
{
# Something changed, save.
@ -2537,7 +2306,7 @@ SET
bridge_host_uuid = ".$anvil->Database->quote($bridge_host_uuid).",
bridge_name = ".$anvil->Database->quote($bridge_name).",
bridge_id = ".$anvil->Database->quote($bridge_id).",
bridge_mac = ".$anvil->Database->quote($bridge_mac).",
bridge_mac_address = ".$anvil->Database->quote($bridge_mac_address).",
bridge_mtu = ".$anvil->Database->quote($bridge_mtu).",
bridge_stp_enabled = ".$anvil->Database->quote($bridge_stp_enabled).",
modified_date = ".$anvil->Database->quote($anvil->data->{sys}{database}{timestamp})."

@ -785,7 +785,273 @@ sub load_interfces
delete $anvil->data->{network}{$host};
}
my $query = "";
# Now load bond info
my $query = "
SELECT
bond_uuid,
bond_name,
bond_mode,
bond_mtu,
bond_primary_slave,
bond_primary_reselect,
bond_active_slave,
bond_mii_polling_interval,
bond_up_delay,
bond_down_delay,
bond_mac_address,
bond_operational
FROM
bonds WHERE bond_mode != 'DELETED'
AND
bond_host_uuid = ".$anvil->Database->quote($host_uuid)."
;";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0124", variables => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
results => $results,
count => $count,
}});
foreach my $row (@{$results})
{
my $bond_uuid = defined $row->[0] ? $row->[0] : "";
my $bond_name = defined $row->[1] ? $row->[1] : "";
my $bond_mode = defined $row->[2] ? $row->[2] : "";
my $bond_mtu = defined $row->[3] ? $row->[3] : "";
my $bond_primary_slave = defined $row->[4] ? $row->[4] : "";
my $bond_primary_reselect = defined $row->[5] ? $row->[5] : "";
my $bond_active_slave = defined $row->[6] ? $row->[6] : "";
my $bond_mii_polling_interval = defined $row->[7] ? $row->[7] : "";
my $bond_up_delay = defined $row->[8] ? $row->[8] : "";
my $bond_down_delay = defined $row->[9] ? $row->[9] : "";
my $bond_mac_address = defined $row->[10] ? $row->[10] : "";
my $bond_operational = defined $row->[11] ? $row->[11] : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
bond_uuid => $bond_uuid,
bond_name => $bond_name,
bond_mode => $bond_mode,
bond_mtu => $bond_mtu,
bond_primary_slave => $bond_primary_slave,
bond_primary_reselect => $bond_primary_reselect,
bond_active_slave => $bond_active_slave,
bond_mii_polling_interval => $bond_mii_polling_interval,
bond_up_delay => $bond_up_delay,
bond_down_delay => $bond_down_delay,
bond_mac_address => $bond_mac_address,
bond_operational => $bond_operational,
}});
# Record the bond_uuid -> name
$anvil->data->{network}{$host}{bond_uuid}{$bond_uuid}{name} = $bond_name;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"network::${host}::bond_uuid::${bond_uuid}::name" => $anvil->data->{network}{$host}{bond_uuid}{$bond_uuid}{name},
}});
# We'll initially load empty strings for what would be the IP information. Any interface with IPs will be populated when we call
$anvil->data->{network}{$host}{interface}{$bond_name}{uuid} = $bond_uuid;
$anvil->data->{network}{$host}{interface}{$bond_name}{mode} = $bond_mode;
$anvil->data->{network}{$host}{interface}{$bond_name}{mtu} = $bond_mtu;
$anvil->data->{network}{$host}{interface}{$bond_name}{primary_slave} = $bond_primary_slave;
$anvil->data->{network}{$host}{interface}{$bond_name}{primary_reselect} = $bond_primary_reselect;
$anvil->data->{network}{$host}{interface}{$bond_name}{active_slave} = $bond_active_slave;
$anvil->data->{network}{$host}{interface}{$bond_name}{mii_polling_interval} = $bond_mii_polling_interval;
$anvil->data->{network}{$host}{interface}{$bond_name}{up_delay} = $bond_up_delay;
$anvil->data->{network}{$host}{interface}{$bond_name}{down_delay} = $bond_down_delay;
$anvil->data->{network}{$host}{interface}{$bond_name}{mac_address} = $bond_mac_address;
$anvil->data->{network}{$host}{interface}{$bond_name}{operational} = $bond_operational;
$anvil->data->{network}{$host}{interface}{$bond_name}{type} = "bond";
$anvil->data->{network}{$host}{interface}{$bond_name}{interfaces} = [];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"network::${host}::interface::${bond_name}::uuid" => $anvil->data->{network}{$host}{interface}{$bond_name}{uuid},
"network::${host}::interface::${bond_name}::mode" => $anvil->data->{network}{$host}{interface}{$bond_name}{mode},
"network::${host}::interface::${bond_name}::mtu" => $anvil->data->{network}{$host}{interface}{$bond_name}{mtu},
"network::${host}::interface::${bond_name}::primary_slave" => $anvil->data->{network}{$host}{interface}{$bond_name}{primary_slave},
"network::${host}::interface::${bond_name}::primary_reselect" => $anvil->data->{network}{$host}{interface}{$bond_name}{primary_reselect},
"network::${host}::interface::${bond_name}::active_slave" => $anvil->data->{network}{$host}{interface}{$bond_name}{active_slave},
"network::${host}::interface::${bond_name}::mii_polling_interval" => $anvil->data->{network}{$host}{interface}{$bond_name}{mii_polling_interval},
"network::${host}::interface::${bond_name}::up_delay" => $anvil->data->{network}{$host}{interface}{$bond_name}{up_delay},
"network::${host}::interface::${bond_name}::down_delay" => $anvil->data->{network}{$host}{interface}{$bond_name}{down_delay},
"network::${host}::interface::${bond_name}::mac_address" => $anvil->data->{network}{$host}{interface}{$bond_name}{mac_address},
"network::${host}::interface::${bond_name}::operational" => $anvil->data->{network}{$host}{interface}{$bond_name}{operational},
"network::${host}::interface::${bond_name}::type" => $anvil->data->{network}{$host}{interface}{$bond_name}{type},
}});
}
# Now load bridge info
$query = "
SELECT
bridge_uuid,
bridge_name,
bridge_id,
bridge_mac_address,
bridge_mtu,
bridge_stp_enabled
FROM
bridges
WHERE
bridge_id != 'DELETED'
AND
bridge_host_uuid = ".$anvil->Database->quote($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 = defined $row->[0] ? $row->[0] : "";
my $bridge_name = defined $row->[1] ? $row->[1] : "";
my $bridge_id = defined $row->[2] ? $row->[2] : "";
my $bridge_mac_address = defined $row->[3] ? $row->[3] : "";
my $bridge_mtu = defined $row->[4] ? $row->[4] : "";
my $bridge_stp_enabled = defined $row->[5] ? $row->[5] : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
bridge_uuid => $bridge_uuid,
bridge_name => $bridge_name,
bridge_id => $bridge_id,
bridge_mac_address => $bridge_mac_address,
bridge_mtu => $bridge_mtu,
bridge_stp_enabled => $bridge_stp_enabled,
}});
# Record the bridge_uuid -> name
$anvil->data->{network}{$host}{bridge_uuid}{$bridge_uuid}{name} = $bridge_name;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"network::${host}::bridge_uuid::${bridge_uuid}::name" => $anvil->data->{network}{$host}{bridge_uuid}{$bridge_uuid}{name},
}});
# We'll initially load empty strings for what would be the IP information. Any interface with IPs will be populated when we call
$anvil->data->{network}{$host}{interface}{$bridge_name}{uuid} = $bridge_uuid;
$anvil->data->{network}{$host}{interface}{$bridge_name}{id} = $bridge_id;
$anvil->data->{network}{$host}{interface}{$bridge_name}{mac} = $bridge_mac_address;
$anvil->data->{network}{$host}{interface}{$bridge_name}{mtu} = $bridge_mtu;
$anvil->data->{network}{$host}{interface}{$bridge_name}{stp_enabled} = $bridge_mtu;
$anvil->data->{network}{$host}{interface}{$bridge_name}{type} = "bridge";
$anvil->data->{network}{$host}{interface}{$bridge_name}{interfaces} = [];
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"network::${host}::interface::${bridge_name}::uuid" => $anvil->data->{network}{$host}{interface}{$bridge_name}{uuid},
"network::${host}::interface::${bridge_name}::id" => $anvil->data->{network}{$host}{interface}{$bridge_name}{id},
"network::${host}::interface::${bridge_name}::mac" => $anvil->data->{network}{$host}{interface}{$bridge_name}{mac},
"network::${host}::interface::${bridge_name}::mtu" => $anvil->data->{network}{$host}{interface}{$bridge_name}{mtu},
"network::${host}::interface::${bridge_name}::stp_enabled" => $anvil->data->{network}{$host}{interface}{$bridge_name}{stp_enabled},
"network::${host}::interface::${bridge_name}::type" => $anvil->data->{network}{$host}{interface}{$bridge_name}{type},
}});
}
# The order will allow us to show the order in which the interfaces were changed, which the user can
# use to track interfaces as they unplug and plug cables back in.
my $order = 1;
$query = "
SELECT
network_interface_uuid,
network_interface_mac_address,
network_interface_name,
network_interface_speed,
network_interface_mtu,
network_interface_link_state,
network_interface_operational,
network_interface_duplex,
network_interface_medium,
network_interface_bond_uuid,
network_interface_bridge_uuid
FROM
network_interfaces
WHERE
network_interface_operational != 'DELETED'
AND
network_interface_host_uuid = ".$anvil->Database->quote($host_uuid)."
ORDER BY
modified_date DESC
;";
$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 $network_interface_uuid = defined $row->[0] ? $row->[0] : "";
my $network_interface_mac_address = defined $row->[1] ? $row->[1] : "";
my $network_interface_name = defined $row->[2] ? $row->[2] : "";
my $network_interface_speed = defined $row->[3] ? $row->[3] : "";
my $network_interface_mtu = defined $row->[4] ? $row->[4] : "";
my $network_interface_link_state = defined $row->[5] ? $row->[5] : "";
my $network_interface_operational = defined $row->[6] ? $row->[6] : "";
my $network_interface_duplex = defined $row->[7] ? $row->[7] : "";
my $network_interface_medium = defined $row->[8] ? $row->[8] : "";
my $network_interface_bond_uuid = defined $row->[9] ? $row->[9] : "";
my $network_interface_bridge_uuid = defined $row->[10] ? $row->[10] : "";
my $bond_name = "";
my $bridge_name = "";
if (($network_interface_bond_uuid) && (defined $anvil->data->{network}{$host}{bond_uuid}{$network_interface_bond_uuid}{name}))
{
$bond_name = $anvil->data->{network}{$host}{bond_uuid}{$network_interface_bond_uuid}{name};
push @{$anvil->data->{network}{$host}{interface}{$bond_name}{interfaces}}, $network_interface_name;
}
if (($network_interface_bridge_uuid) && (defined $anvil->data->{network}{$host}{bridge_uuid}{$network_interface_bridge_uuid}{name}))
{
$bridge_name = $anvil->data->{network}{$host}{bridge_uuid}{$network_interface_bridge_uuid}{name};
push @{$anvil->data->{network}{$host}{interface}{$bridge_name}{interfaces}}, $network_interface_name;
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
network_interface_uuid => $network_interface_uuid,
network_interface_mac_address => $network_interface_mac_address,
network_interface_name => $network_interface_name,
network_interface_speed => $network_interface_speed,
network_interface_mtu => $network_interface_mtu,
network_interface_link_state => $network_interface_link_state,
network_interface_operational => $network_interface_operational,
network_interface_duplex => $network_interface_duplex,
network_interface_medium => $network_interface_medium,
network_interface_bond_uuid => $network_interface_bond_uuid,
network_interface_bridge_uuid => $network_interface_bridge_uuid,
bond_name => $bond_name,
}});
# We'll initially load empty strings for what would be the IP information. Any interface with IPs will be populated when we call
$anvil->data->{network}{$host}{interface}{$network_interface_name}{uuid} = $network_interface_uuid;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{mac_address} = $network_interface_mac_address;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{speed} = $network_interface_speed;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{mtu} = $network_interface_mtu;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{link_state} = $network_interface_link_state;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{operational} = $network_interface_operational;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{duplex} = $network_interface_duplex;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{medium} = $network_interface_medium;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{bond_uuid} = $network_interface_bond_uuid;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{bond_name} = $bond_name;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{bridge_uuid} = $network_interface_bridge_uuid;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{bridge_name} = $bridge_name;
$anvil->data->{network}{$host}{interface}{$network_interface_name}{type} = "interface";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"network::${host}::interface::${network_interface_name}::uuid" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{uuid},
"network::${host}::interface::${network_interface_name}::mac_address" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{mac_address},
"network::${host}::interface::${network_interface_name}::speed" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{speed},
"network::${host}::interface::${network_interface_name}::mtu" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{mtu},
"network::${host}::interface::${network_interface_name}::link_state" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{link_state},
"network::${host}::interface::${network_interface_name}::operational" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{operational},
"network::${host}::interface::${network_interface_name}::duplex" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{duplex},
"network::${host}::interface::${network_interface_name}::medium" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{medium},
"network::${host}::interface::${network_interface_name}::bond_uuid" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{bond_uuid},
"network::${host}::interface::${network_interface_name}::bond_name" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{bond_name},
"network::${host}::interface::${network_interface_name}::bridge_uuid" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{bridge_uuid},
"network::${host}::interface::${network_interface_name}::bridge_name" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{bridge_name},
"network::${host}::interface::${network_interface_name}::type" => $anvil->data->{network}{$host}{interface}{$network_interface_name}{type},
}});
}
# Load the IPs
$anvil->Network->load_ips({
debug => $debug,
host_uuid => $host_uuid,
host => $host,
clear => 0,
});
return(0);
}

@ -982,7 +982,7 @@ CREATE TABLE bridges (
bridge_host_uuid uuid not null,
bridge_name text not null,
bridge_id text not null,
bridge_mac text not null,
bridge_mac_address text not null,
bridge_mtu text not null,
bridge_stp_enabled text not null, -- 0 = disabled, 1 = kernel STP, 2 = user STP
modified_date timestamp with time zone not null,
@ -997,7 +997,7 @@ CREATE TABLE history.bridges (
bridge_host_uuid uuid,
bridge_name text,
bridge_id text,
bridge_mac text,
bridge_mac_address text,
bridge_mtu text,
bridge_stp_enabled text,
modified_date timestamp with time zone not null
@ -1015,7 +1015,7 @@ BEGIN
bridge_host_uuid,
bridge_name,
bridge_id,
bridge_mac,
bridge_mac_address,
bridge_mtu,
bridge_stp_enabled,
modified_date)
@ -1024,7 +1024,7 @@ BEGIN
history_bridges.bridge_host_uuid,
history_bridges.bridge_name,
history_bridges.bridge_id,
history_bridges.bridge_mac,
history_bridges.bridge_mac_address,
history_bridges.bridge_mtu,
history_bridges.bridge_stp_enabled,
history_bridges.modified_date);
@ -1039,63 +1039,6 @@ CREATE TRIGGER trigger_bridges
FOR EACH ROW EXECUTE PROCEDURE history_bridges();
-- This records which interfaces are connect to which bridges
CREATE TABLE bridge_interfaces (
bridge_interface_uuid uuid not null primary key,
bridge_interface_host_uuid uuid not null,
bridge_interface_bridge_uuid uuid not null,
bridge_interface_network_interface_uuid uuid not null,
bridge_interface_note text not null, -- Will have 'DELETED' when removed, or the server name the device connects to otherwise.
modified_date timestamp with time zone not null,
FOREIGN KEY(bridge_interface_host_uuid) REFERENCES hosts(host_uuid),
FOREIGN KEY(bridge_interface_bridge_uuid) REFERENCES bridges(bridge_uuid),
FOREIGN KEY(bridge_interface_network_interface_uuid) REFERENCES network_interfaces(network_interface_uuid)
);
ALTER TABLE bridge_interfaces OWNER TO admin;
CREATE TABLE history.bridge_interfaces (
history_id bigserial,
bridge_interface_uuid uuid,
bridge_interface_host_uuid uuid,
bridge_interface_bridge_uuid uuid,
bridge_interface_network_interface_uuid uuid,
bridge_interface_note text,
modified_date timestamp with time zone not null
);
ALTER TABLE history.bridge_interfaces OWNER TO admin;
CREATE FUNCTION history_bridge_interfaces() RETURNS trigger
AS $$
DECLARE
history_bridge_interfaces RECORD;
BEGIN
SELECT INTO history_bridge_interfaces * FROM bridge_interfaces WHERE bridge_interface_uuid = new.bridge_interface_uuid;
INSERT INTO history.bridge_interfaces
(bridge_interface_uuid,
bridge_interface_host_uuid,
bridge_interface_bridge_uuid,
bridge_interface_network_interface_uuid,
bridge_interface_note,
modified_date)
VALUES
(history_bridge_interfaces.bridge_interface_uuid,
history_bridge_interfaces.bridge_interface_host_uuid,
history_bridge_interfaces.bridge_interface_bridge_uuid,
history_bridge_interfaces.bridge_interface_network_interface_uuid,
history_bridge_interfaces.bridge_interface_note,
history_bridge_interfaces.modified_date);
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
ALTER FUNCTION history_bridge_interfaces() OWNER TO admin;
CREATE TRIGGER trigger_bridge_interfaces
AFTER INSERT OR UPDATE ON bridge_interfaces
FOR EACH ROW EXECUTE PROCEDURE history_bridge_interfaces();
-- This stores information about network ip addresss.
CREATE TABLE ip_addresses (
ip_address_uuid uuid not null primary key,

@ -499,7 +499,7 @@ sub update_network
line => __LINE__,
bridge_name => $interface,
bridge_id => $bridge_id,
bridge_mac => $mac_address,
bridge_mac_address => $mac_address,
bridge_mtu => $mtu,
bridge_stp_enabled => $bridge_stp_enabled,
});
@ -604,7 +604,7 @@ SELECT
bridge_uuid,
bridge_name,
bridge_id,
bridge_mac,
bridge_mac_address,
bridge_mtu,
bridge_stp_enabled
FROM
@ -626,20 +626,20 @@ AND
my $bridge_uuid = $row->[0];
my $bridge_name = $row->[1];
my $bridge_id = $row->[2];
my $bridge_mac = $row->[3];
my $bridge_mac_address = $row->[3];
my $bridge_mtu = $row->[4];
my $bridge_stp_enabled = $row->[5];
$anvil->data->{bridges}{$bridge_uuid} = {
bridge_name => $bridge_name,
bridge_id => $bridge_id,
bridge_mac => $bridge_mac,
bridge_mac_address => $bridge_mac_address,
bridge_mtu => $bridge_mtu,
bridge_stp_enabled => $bridge_stp_enabled,
};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
"bridges::${bridge_uuid}::bridge_name" => $anvil->data->{bridges}{$bridge_uuid}{bridge_name},
"bridges::${bridge_uuid}::bridge_id" => $anvil->data->{bridges}{$bridge_uuid}{bridge_id},
"bridges::${bridge_uuid}::bridge_mac" => $anvil->data->{bridges}{$bridge_uuid}{bridge_mac},
"bridges::${bridge_uuid}::bridge_mac_address" => $anvil->data->{bridges}{$bridge_uuid}{bridge_mac_address},
"bridges::${bridge_uuid}::bridge_mtu" => $anvil->data->{bridges}{$bridge_uuid}{bridge_mtu},
"bridges::${bridge_uuid}::bridge_stp_enabled" => $anvil->data->{bridges}{$bridge_uuid}{bridge_stp_enabled},
}});
@ -681,7 +681,6 @@ ORDER BY
$results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
$count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
query => $query,
results => $results,
count => $count,
}});
@ -837,7 +836,7 @@ WHERE
$network_xml .= " <ip address=\"$ip_address_address\" on=\"$say_on\" subnet=\"$ip_address_subnet_mask\" gateway=\"$ip_address_gateway\" default_gateway=\"$ip_address_default_gateway\" dns=\"$ip_address_dns\" />\n";
}
# Now record the interfaces on bridges.
# Now verify that the interface is connected to this bridge
$anvil->Network->bridge_info({debug => 3});
foreach my $bridge_name (sort {$a cmp $b} keys %{$anvil->data->{bridge}{'local'}})
{
@ -855,16 +854,32 @@ WHERE
interface_uuid => $interface_uuid,
}});
my $bridge_interfaces_uuid = $anvil->Database->insert_or_update_bridge_interfaces({
debug => 3,
file => $THIS_FILE,
line => __LINE__,
bridge_interface_bridge_uuid => $bridge_uuid,
bridge_interface_network_interface_uuid => $interface_uuid,
});
# Read the interface_bridge_uuid and, if it doesn't match, update it.
my $query = "SELECT network_interface_bridge_uuid FROM network_interfaces WHERE network_interface_uuid = ".$anvil->Database->quote($interface_uuid).";";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0124", variables => { query => $query }});
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});
my $count = @{$results};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
bridge_interfaces_uuid => $bridge_interfaces_uuid,
results => $results,
count => $count,
}});
my $interface_bridge_uuid = defined $results->[0]->[0] ? $results->[0]->[0] : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { interface_bridge_uuid => $interface_bridge_uuid }});
if (($bridge_uuid) && ($interface_bridge_uuid ne $bridge_uuid))
{
# Update it.
my $query = "
UPDATE
network_interfaces
SET
network_interface_bridge_uuid = ".$anvil->Database->quote($bridge_uuid).",
modified_date = ".$anvil->Database->quote($anvil->data->{sys}{database}{timestamp})."
WHERE
network_interface_uuid = ".$anvil->Database->quote($interface_uuid)."
;" ;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }});
$anvil->Database->write({debug => 3, query => $query, source => $THIS_FILE, line => __LINE__});
}
}
}

Loading…
Cancel
Save