From 8f823d3b8650f4aec3e3c49993966bb33dfdb6f6 Mon Sep 17 00:00:00 2001 From: Digimer Date: Mon, 28 Dec 2020 21:59:32 -0500 Subject: [PATCH] * Switched out the static list of core table to use the array generated by Database->get_tables_from_schema(). * Fixed bugs around creating and filtering storage groups. Signed-off-by: Digimer --- Anvil/Tools.pm | 40 ++------------------ Anvil/Tools/Database.pm | 41 ++++++++++++++++++--- Anvil/Tools/Get.pm | 30 ++++++--------- Anvil/Tools/Log.pm | 2 +- scancore-agents/scan-server/scan-server.xml | 2 +- share/anvil.sql | 14 +++---- tools/test.pl | 5 ++- 7 files changed, 63 insertions(+), 71 deletions(-) diff --git a/Anvil/Tools.pm b/Anvil/Tools.pm index 31671829..ae736b4f 100644 --- a/Anvil/Tools.pm +++ b/Anvil/Tools.pm @@ -267,6 +267,9 @@ sub new # Read in any command line switches. $anvil->Get->switches({debug => $debug}); + # Populate the core_tables array reference from the main anvil.sql file. + $anvil->data->{sys}{database}{core_tables} = $anvil->Database->get_tables_from_schema({schema_file => $anvil->data->{path}{sql}{'anvil.sql'}}); + # Read in the local Anvil! version. #... @@ -862,43 +865,6 @@ sub _set_defaults trigger => 100000, }, connections => 0, - ### WARNING: The order the tables are resync'ed is important! Any table that has a - ### foreign key needs to resync *AFTER* the tables with the primary keys. - # NOTE: Check that this list is complete with; - # grep 'CREATE TABLE' share/anvil.sql | grep -v history. | awk '{print $3}' - core_tables => [ - "hosts", # Always has to be first. - "health", - "power", - "temperature", - "ssh_keys", - "users", - "host_variable", - "sessions", # Has to come after users and hosts - "anvils", - "alerts", - "recipients", - "notifications", - "mail_servers", - "variables", - "jobs", - "bridges", - "bonds", - "network_interfaces", - "ip_addresses", - "files", - "file_locations", - "servers", - "definitions", - "oui", - "mac_to_ip", - "updated", - "alert_sent", - "states", - "manifests", - "fences", - "upses", - ], failed_connection_log_level => 1, local_lock_active => 0, local_uuid => "", diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index 854684fc..62e726d7 100644 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -10973,14 +10973,13 @@ AND else { my $vg_group_number = 0; - my $storage_group_name = ""; until ($storage_group_name) { $vg_group_number++; my $test_name = $anvil->Words->string({debug => $debug, key => "striker_0280", variables => { number => $vg_group_number }}); my $query = " SELECT - COUNT(*) + storage_group_uuid FROM storage_groups WHERE @@ -10988,9 +10987,41 @@ WHERE AND storage_group_name = ".$anvil->Database->quote($test_name)." ;"; - my $count = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { count => $count }}); + 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) + { + # Are there any members of this group? If not, we'll use it. + my $storage_group_uuid = $results->[0]->[0]; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { storage_group_uuid => $storage_group_uuid }}); + + my $query = " +SELECT + COUNT(*) +FROM + storage_group_members +WHERE + storage_group_member_storage_group_uuid = ".$anvil->Database->quote($storage_group_uuid)." +;"; + 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) + { + # No members yet, and it's an auto-generated + $storage_group_name = $test_name; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { storage_group_name => $storage_group_name }}); + } + } if (not $count) { # We can use this name. @@ -11221,7 +11252,7 @@ INSERT INTO ".$anvil->Database->quote($storage_group_member_host_uuid).", ".$anvil->Database->quote($storage_group_member_vg_uuid).", ".$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__}); } diff --git a/Anvil/Tools/Get.pm b/Anvil/Tools/Get.pm index 8daea0a4..9220572e 100644 --- a/Anvil/Tools/Get.pm +++ b/Anvil/Tools/Get.pm @@ -332,6 +332,9 @@ sub available_resources $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Get->available_resources()" }}); my $anvil_uuid = defined $parameter->{anvil_uuid} ? $parameter->{anvil_uuid} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + anvil_uuid=> $anvil_uuid, + }}); if (not $anvil_uuid) { @@ -377,21 +380,6 @@ WHERE dr1_host_uuid => $dr1_host_uuid, }}); - # Make sure scancore has run. We do this by looking for the 'scan_lvm_vg_groups' table. - undef $query; - undef $count; - $query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename='scan_lvm_vg_groups' AND schemaname='public';"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $count = $anvil->Database->query({debug => $debug, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { count => $count }}); - - if ($count < 1) - { - # Not ready yes. - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "warning_0070", variables => { anvil_name => $anvil_name }}); - return('!!error!!'); - } - # Load hosts, Network bridge, and Storages group data $anvil->Database->get_hosts({debug => $debug}); $anvil->Database->get_bridges({debug => $debug}); @@ -470,14 +458,16 @@ ORDER BY $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_uuid} = $row->[0]; $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_name} = $row->[1]; $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_extent_size} = $row->[2]; - $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_free} = $row->[3]; - $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_internal_uuid} = $row->[4]; + $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_size} = $row->[3]; + $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_free} = $row->[4]; + $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_internal_uuid} = $row->[5]; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "ungrouped_vg_count::${this_is}" => $anvil->data->{ungrouped_vg_count}{$this_is}, "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::count" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{count}, "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_uuid" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_uuid}, "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_name" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_name}, "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_extent_size" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_extent_size}." (".$anvil->Convert->bytes_to_human_readable({'bytes' => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_extent_size}}).")", + "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_size" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_size}." (".$anvil->Convert->bytes_to_human_readable({'bytes' => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_size}}).")", "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_free" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_free}." (".$anvil->Convert->bytes_to_human_readable({'bytes' => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_free}}).")", "ungrouped_vgs::${scan_lvm_vg_size}::host_uuid::${host_uuid}::vg_internal_uuid" => $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_internal_uuid}, }}); @@ -493,7 +483,7 @@ ORDER BY if (($count == 2) or ($count == 3)) { # Create the volume group ... group. First we need a group number - my $storage_group_uuid = $anvil->Database->create_storage_group({ + my $storage_group_uuid = $anvil->Database->insert_or_update_storage_groups({ debug => $debug, storage_group_anvil_uuid => $anvil_uuid, }); @@ -507,7 +497,9 @@ ORDER BY $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { this_is => $this_is }}); my $storage_group_member_vg_uuid = $anvil->data->{ungrouped_vgs}{$scan_lvm_vg_size}{host_uuid}{$host_uuid}{vg_internal_uuid}; - my $storage_group_member_uuid = $anvil->Database->insert_or_update_storage_group_members({ + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { storage_group_member_vg_uuid => $storage_group_member_vg_uuid }}); + + my $storage_group_member_uuid = $anvil->Database->insert_or_update_storage_group_members({ debug => $debug, storage_group_member_storage_group_uuid => $storage_group_uuid, storage_group_member_host_uuid => $host_uuid, diff --git a/Anvil/Tools/Log.pm b/Anvil/Tools/Log.pm index 609dbc71..a217fef6 100644 --- a/Anvil/Tools/Log.pm +++ b/Anvil/Tools/Log.pm @@ -984,7 +984,7 @@ sub _adjust_log_level { $anvil->Log->secure({set => 1}); } - if ($anvil->data->{switches}{'log-db'}) + if (($anvil->data->{switches}{'log-db'}) or ($anvil->data->{switches}{'log-db-transactions'})) { $anvil->data->{sys}{database}{log_transactions} = 1; } diff --git a/scancore-agents/scan-server/scan-server.xml b/scancore-agents/scan-server/scan-server.xml index faddbd2b..f5687e08 100644 --- a/scancore-agents/scan-server/scan-server.xml +++ b/scancore-agents/scan-server/scan-server.xml @@ -59,7 +59,7 @@ The definition for the server: [#!variable!server!#] was edited outside of the A ==[ Database ]======== #!variable!db_difference!# ==[ New Definition ]== -#!variable!new_difference!# +#!variable!new_definition!# ====================== The name of the server: [#!variable!old_name!#] has changed to: [#!variable!new_name!#] diff --git a/share/anvil.sql b/share/anvil.sql index 1a4b30c0..3a655954 100644 --- a/share/anvil.sql +++ b/share/anvil.sql @@ -1694,22 +1694,22 @@ CREATE TRIGGER trigger_storage_groups -- This links specific VGs with a give storage group CREATE TABLE storage_group_members ( storage_group_member_uuid uuid not null primary key, - storage_group_member_storage_group_uuid uuid + storage_group_member_storage_group_uuid uuid not null, storage_group_member_host_uuid uuid not null, -- The host this VG comes from. storage_group_member_vg_uuid text not null, -- This is the VG's internal "uuid". It's not a valid UUID string format, but it's what LVM calls a 'uuid'. modified_date timestamp with time zone not null, - FOREIGN KEY(storage_group_member_storage_group_uuid) REFERENCES storage_groups(storage_group_uuid) - FOREIGN KEY(storage_group_member_anvil_uuid) REFERENCES anvils(anvil_uuid) + FOREIGN KEY(storage_group_member_storage_group_uuid) REFERENCES storage_groups(storage_group_uuid), + FOREIGN KEY(storage_group_member_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE storage_group_members OWNER TO admin; CREATE TABLE history.storage_group_members ( history_id bigserial, - storage_group_member_uuid uuid, - storage_group_member_storage_group_uuid uuid - storage_group_member_host_uuid uuid, - storage_group_member_vg_uuid text, + storage_group_member_uuid uuid, + storage_group_member_storage_group_uuid uuid, + storage_group_member_host_uuid uuid, + storage_group_member_vg_uuid text, modified_date timestamp with time zone not null ); ALTER TABLE history.storage_group_members OWNER TO admin; diff --git a/tools/test.pl b/tools/test.pl index 3efccc5b..47fe98dc 100755 --- a/tools/test.pl +++ b/tools/test.pl @@ -26,7 +26,10 @@ $anvil->Get->switches; $anvil->Database->connect; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0132"}); -$anvil->Words->load_agent_strings({debug => 2}); +$anvil->Get->available_resources({ + debug => 2, + anvil_uuid => '2ac4dbcb-25d2-44b2-ae07-59707b0551ca', +}); $anvil->nice_exit({exit_code => 0});