You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
381 lines
15 KiB
381 lines
15 KiB
#!/usr/bin/perl |
|
# |
|
# This is a tool that purges hosts or Anvil! systems from the ScanCore databases. |
|
# |
|
|
|
use strict; |
|
use warnings; |
|
use Anvil::Tools; |
|
use Data::Dumper; |
|
|
|
$| = 1; |
|
|
|
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0]; |
|
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0]; |
|
if (($running_directory =~ /^\./) && ($ENV{PWD})) |
|
{ |
|
$running_directory =~ s/^\./$ENV{PWD}/; |
|
} |
|
|
|
my $anvil = Anvil::Tools->new(); |
|
|
|
|
|
$anvil->Database->connect({check_for_resync => 1}); |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); |
|
if (not $anvil->data->{sys}{database}{connections}) |
|
{ |
|
# No databases, exit. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
|
|
$anvil->data->{switches}{'anvil'} = ""; |
|
$anvil->data->{switches}{'host'} = ""; |
|
$anvil->data->{switches}{'y'} = ""; |
|
$anvil->data->{switches}{'yes'} = ""; |
|
$anvil->Get->switches(); |
|
if ((not $anvil->data->{switches}{'anvil'}) && (not $anvil->data->{switches}{'host'})) |
|
{ |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0240"}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
"switches::anvil" => $anvil->data->{switches}{'anvil'}, |
|
"switches::host" => $anvil->data->{switches}{'host'}, |
|
"switches::y" => $anvil->data->{switches}{'y'}, |
|
"switches::yes" => $anvil->data->{switches}{'yes'}, |
|
}}); |
|
|
|
$anvil->data->{purge}{anvil_uuid} = ""; |
|
$anvil->data->{purge}{anvil_name} = ""; |
|
$anvil->data->{purge}{host_uuid} = ""; |
|
$anvil->data->{purge}{host_name} = ""; |
|
$anvil->data->{purge}{hosts} = []; |
|
$anvil->Database->get_anvils(); |
|
$anvil->Database->get_hosts({include_deleted => 1}); |
|
my $vacuum = 0; |
|
if ($anvil->data->{switches}{'anvil'}) |
|
{ |
|
my $anvil_name = ""; |
|
my $anvil_uuid = ""; |
|
if ($anvil->Validate->uuid({uuid => $anvil->data->{switches}{'anvil'}})) |
|
{ |
|
$anvil_uuid = $anvil->data->{switches}{'anvil'}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_uuid => $anvil_uuid }}); |
|
|
|
# Convert it to an Anvil! name. |
|
$anvil->data->{purge}{anvil_uuid} = $anvil_uuid; |
|
$anvil->data->{purge}{anvil_name} = exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid} ? $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name} : ""; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
"purge::anvil_uuid" => $anvil->data->{purge}{anvil_uuid}, |
|
"purge::anvil_name" => $anvil->data->{purge}{anvil_name}, |
|
}}); |
|
|
|
if (not $anvil->data->{purge}{anvil_name}) |
|
{ |
|
# Bad anvil_uuid |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "error_0302", priority => "err", variables => { anvil_uuid => $anvil->data->{switches}{'anvil'} }}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
} |
|
else |
|
{ |
|
# Look for the name. |
|
$anvil_name = $anvil->data->{switches}{'anvil'}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_name => $anvil_name }}); |
|
|
|
$anvil->data->{purge}{anvil_name} = $anvil_name; |
|
$anvil->data->{purge}{anvil_uuid} = exists $anvil->data->{anvils}{anvil_name}{$anvil_name} ? $anvil->data->{anvils}{anvil_name}{$anvil_name}{anvil_uuid} : ""; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
"purge::anvil_uuid" => $anvil->data->{purge}{anvil_uuid}, |
|
"purge::anvil_name" => $anvil->data->{purge}{anvil_name}, |
|
}}); |
|
|
|
if (not $anvil->data->{purge}{anvil_uuid}) |
|
{ |
|
# Bad anvil name. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "error_0302", priority => "err", variables => { anvil_uuid => $anvil->data->{switches}{'anvil'} }}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
} |
|
|
|
# Load the list of hosts. |
|
$anvil_uuid = $anvil->data->{purge}{anvil_uuid}; |
|
push @{$anvil->data->{purge}{hosts}}, $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; |
|
push @{$anvil->data->{purge}{hosts}}, $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}; |
|
if ($anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}) |
|
{ |
|
push @{$anvil->data->{purge}{hosts}}, $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}; |
|
} |
|
} |
|
elsif($anvil->data->{switches}{'host'}) |
|
{ |
|
my $host_uuid = ""; |
|
my $host_name = ""; |
|
if ($anvil->Validate->uuid({uuid => $anvil->data->{switches}{'host'}})) |
|
{ |
|
$host_uuid = $anvil->data->{switches}{'host'}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { host_uuid => $host_uuid }}); |
|
|
|
# Convert it to an Anvil! name. |
|
$anvil->data->{purge}{host_uuid} = $host_uuid; |
|
$anvil->data->{purge}{host_name} = exists $anvil->data->{hosts}{host_uuid}{$host_uuid} ? $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_name} : ""; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
"purge::host_uuid" => $anvil->data->{purge}{host_uuid}, |
|
"purge::host_name" => $anvil->data->{purge}{host_name}, |
|
}}); |
|
|
|
if (not $anvil->data->{purge}{host_name}) |
|
{ |
|
# Bad host_uuid |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "error_0303", priority => "err", variables => { host_uuid => $anvil->data->{switches}{'host'} }}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
} |
|
else |
|
{ |
|
# Look for the name. |
|
$host_name = $anvil->data->{switches}{'host'}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { host_name => $host_name }}); |
|
|
|
$anvil->data->{purge}{host_name} = $host_name; |
|
$anvil->data->{purge}{host_uuid} = $anvil->Get->host_uuid_from_name({host_name => $host_name});; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
"purge::host_uuid" => $anvil->data->{purge}{host_uuid}, |
|
"purge::host_name" => $anvil->data->{purge}{host_name}, |
|
}}); |
|
|
|
if (not $anvil->data->{purge}{host_uuid}) |
|
{ |
|
# Bad anvil name. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "error_0303", priority => "err", variables => { host_uuid => $anvil->data->{switches}{'host'} }}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
} |
|
push @{$anvil->data->{purge}{hosts}}, $anvil->data->{purge}{host_uuid}; |
|
} |
|
else |
|
{ |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => ""}); |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
|
|
# Ask to confirm. |
|
if (($anvil->data->{switches}{'y'}) or ($anvil->data->{switches}{'yes'})) |
|
{ |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0241"}); |
|
} |
|
else |
|
{ |
|
if ($anvil->data->{purge}{anvil_name}) |
|
{ |
|
# Show the Anvil! and member hosts. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0243", variables => { |
|
anvil_name => $anvil->data->{purge}{anvil_name}, |
|
anvil_uuid => $anvil->data->{purge}{anvil_uuid}, |
|
}}); |
|
|
|
my $anvil_uuid = $anvil->data->{purge}{anvil_uuid}; |
|
my $anvil_name = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name}; |
|
my $node1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; |
|
my $node2_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
anvil_uuid => $anvil_uuid, |
|
anvil_name => $anvil_name, |
|
node1_host_uuid => $node1_host_uuid, |
|
}}); |
|
|
|
# Node 1; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0244", variables => { |
|
host_name => $anvil->data->{hosts}{host_uuid}{$node1_host_uuid}{host_name}, |
|
host_uuid => $node1_host_uuid, |
|
}}); |
|
|
|
# Node 2; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0244", variables => { |
|
host_name => $anvil->data->{hosts}{host_uuid}{$node2_host_uuid}{host_name}, |
|
host_uuid => $node2_host_uuid, |
|
}}); |
|
|
|
# DR, if set. |
|
if ($anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}) |
|
{ |
|
my $dr1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0244", variables => { |
|
host_name => $anvil->data->{hosts}{host_uuid}{$dr1_host_uuid}{host_name}, |
|
host_uuid => $dr1_host_uuid, |
|
}}); |
|
} |
|
} |
|
else |
|
{ |
|
# Ask the user to confirm the host deletion. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0242", variables => { |
|
host_name => $anvil->data->{purge}{host_name}, |
|
host_uuid => $anvil->data->{purge}{host_uuid}, |
|
}}); |
|
} |
|
print $anvil->Words->string({key => "message_0021"})." "; |
|
|
|
chomp(my $answer = <STDIN>); |
|
if ((lc($answer) ne "y") && (lc($answer) ne "yes")) |
|
{ |
|
print "\n".$anvil->Words->string({key => "message_0022"})."\n\n"; |
|
$anvil->nice_exit({exit_code => 1}); |
|
} |
|
} |
|
|
|
# List all database tables in reverse order with X_host_uuid tables |
|
$anvil->Database->find_host_uuid_columns({debug => 3, search_column => "host_uuid", main_table => "hosts"}); |
|
|
|
# For each host |
|
foreach my $host_uuid (@{$anvil->data->{purge}{hosts}}) |
|
{ |
|
my $host_name = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_name}; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0245", variables => { |
|
host_name => $host_name, |
|
host_uuid => $host_uuid, |
|
}}); |
|
|
|
my $queries = []; |
|
foreach my $hash_ref (@{$anvil->data->{sys}{database}{uuid_tables}}) |
|
{ |
|
my $table = $hash_ref->{table}; |
|
my $host_uuid_column = $hash_ref->{host_uuid_column}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
's1:table' => $table, |
|
's2:host_uuid_column' => $host_uuid_column, |
|
}}); |
|
|
|
if (($table eq "hosts") && ($anvil->data->{purge}{anvil_uuid})) |
|
{ |
|
# Remove this host from the Anvil! |
|
next if not $anvil->data->{purge}{anvil_uuid}; |
|
my $anvil_uuid = $anvil->data->{purge}{anvil_uuid}; |
|
my $host_key = ""; |
|
if ($host_uuid eq $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}) |
|
{ |
|
$host_key = "anvil_node1_host_uuid"; |
|
} |
|
elsif ($host_uuid eq $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}) |
|
{ |
|
$host_key = "anvil_node2_host_uuid"; |
|
} |
|
elsif ($host_uuid eq $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}) |
|
{ |
|
$host_key = "anvil_dr1_host_uuid"; |
|
} |
|
if ($host_key) |
|
{ |
|
my $query = " |
|
UPDATE |
|
anvils |
|
SET |
|
".$host_key." = NULL, |
|
modified_date = ".$anvil->Database->quote($anvil->Database->refresh_timestamp)." |
|
WHERE |
|
anvil_uuid = ".$anvil->Database->quote($anvil_uuid)." |
|
;"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
} |
|
|
|
# Just delete the record normally. |
|
if ($anvil->data->{sys}{database}{history_table}{$table}) |
|
{ |
|
my $query = "DELETE FROM history.".$table." WHERE ".$host_uuid_column." = ".$anvil->Database->quote($host_uuid).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
|
|
my $query = "DELETE FROM ".$table." WHERE ".$host_uuid_column." = ".$anvil->Database->quote($host_uuid).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
|
|
# Commit. |
|
$anvil->Database->write({query => $queries, source => $THIS_FILE, line => __LINE__}); |
|
|
|
$vacuum = 1; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { vacuum => $vacuum }}); |
|
} |
|
|
|
# If we're deleting an Anvil!, clear it out now. |
|
if ($anvil->data->{purge}{anvil_uuid}) |
|
{ |
|
# List all database tables in reverse order with X_host_uuid tables |
|
$anvil->Database->find_host_uuid_columns({search_column => "anvil_uuid", main_table => "anvils"}); |
|
|
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0246", variables => { |
|
anvil_name => $anvil->data->{purge}{anvil_name}, |
|
anvil_uuid => $anvil->data->{purge}{anvil_uuid}, |
|
}}); |
|
|
|
my $queries = []; |
|
foreach my $hash_ref (@{$anvil->data->{sys}{database}{uuid_tables}}) |
|
{ |
|
my $table = $hash_ref->{table}; |
|
my $host_uuid_column = $hash_ref->{host_uuid_column}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
's1:table' => $table, |
|
's2:host_uuid_column' => $host_uuid_column, |
|
}}); |
|
|
|
# If the table is 'servers', we need to pull up the server_uuids and delete their definition |
|
# file entries. |
|
if ($table eq "servers") |
|
{ |
|
my $query = "SELECT server_uuid FROM servers WHERE server_anvil_uuid = ".$anvil->Database->quote($anvil->data->{purge}{anvil_uuid}).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 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 => 2, list => { |
|
results => $results, |
|
count => $count, |
|
}}); |
|
foreach my $row (@{$results}) |
|
{ |
|
my $server_uuid = $row->[0]; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { server_uuid => $server_uuid }}); |
|
|
|
my $query = "DELETE FROM history.server_definitions WHERE server_definition_server_uuid = ".$anvil->Database->quote($server_uuid).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
|
|
$query = "DELETE FROM server_definitions WHERE server_definition_server_uuid = ".$anvil->Database->quote($server_uuid).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
|
|
} |
|
|
|
if ($anvil->data->{sys}{database}{history_table}{$table}) |
|
{ |
|
my $query = "DELETE FROM history.".$table." WHERE ".$host_uuid_column." = ".$anvil->Database->quote($anvil->data->{purge}{anvil_uuid}).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
|
|
my $query = "DELETE FROM ".$table." WHERE ".$host_uuid_column." = ".$anvil->Database->quote($anvil->data->{purge}{anvil_uuid}).";"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
push @{$queries}, $query; |
|
} |
|
|
|
# Commit. |
|
$anvil->Database->write({query => $queries, source => $THIS_FILE, line => __LINE__}); |
|
|
|
$vacuum = 1; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { vacuum => $vacuum }}); |
|
} |
|
|
|
# Vacuum the database |
|
if ($vacuum) |
|
{ |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0458"}); |
|
my $query = "VACUUM FULL;"; |
|
$anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); |
|
} |
|
|
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0025"}); |
|
$anvil->nice_exit({exit_code => 0});
|
|
|