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.
217 lines
5.8 KiB
217 lines
5.8 KiB
#!/usr/bin/perl |
|
# |
|
# Accepts a HTTP PUT request to set the cluster membership of a host. |
|
# |
|
|
|
use strict; |
|
use warnings; |
|
use Anvil::Tools; |
|
use Data::Dumper; |
|
use JSON |
|
|
|
$| = 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(); |
|
|
|
sub handle_invalid_uuid |
|
{ |
|
my $parameters = shift; |
|
my $name = $parameters->{name}; |
|
my $uuid = $parameters->{uuid}; |
|
|
|
$anvil->Log->entry({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 0, |
|
'print' => 1, |
|
priority => "err", |
|
key => "error_0160", |
|
variables => { name => $name, uuid => $uuid } |
|
}); |
|
$anvil->nice_exit({ exit_code => 1 }); |
|
} |
|
|
|
sub get_active_member |
|
{ |
|
my $parameters = shift; |
|
my $host_uuid = $parameters->{host_uuid}; |
|
|
|
my $query = " |
|
SELECT |
|
scan_cluster_node_in_ccm, |
|
scan_cluster_node_crmd_member, |
|
scan_cluster_node_cluster_member |
|
FROM |
|
scan_cluster_nodes |
|
WHERE |
|
scan_cluster_node_host_uuid = ".$anvil->Database->quote($host_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 |
|
} }); |
|
|
|
if ($count == 1) |
|
{ |
|
my $row = $results->[0]; |
|
my $is_in_ccm = $row->[0]; |
|
my $is_crmd_member = $row->[1]; |
|
my $is_cluster_member = $row->[2]; |
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
is_in_ccm => $is_in_ccm, |
|
is_crmd_member => $is_crmd_member, |
|
is_cluster_member => $is_cluster_member |
|
} }); |
|
|
|
return $is_in_ccm && $is_crmd_member && $is_cluster_member; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
sub alter_host_membership |
|
{ |
|
# Expects the first of positional parameters to be a hash. |
|
my $parameters = shift; |
|
my $host_uuid = $parameters->{host_uuid}; |
|
my $is_join_cluster = $parameters->{is_join_cluster}; |
|
|
|
if ($is_join_cluster) |
|
{ |
|
join_cluster({ host_uuid => $host_uuid }); |
|
} |
|
else |
|
{ |
|
leave_cluster({ host_uuid => $host_uuid }); |
|
} |
|
} |
|
|
|
# Re-adds a host to its anvil cluster. |
|
# |
|
# Note: not to be confused wtih adding a new host the an anvil. |
|
sub join_cluster |
|
{ |
|
my $parameters = shift; |
|
my $host_uuid = $parameters->{host_uuid}; |
|
|
|
# Determine whether host is already a cluster member. |
|
my $is_active_member = get_active_member({ host_uuid => $host_uuid }); |
|
# Find out whether the host is online before trying to make it join the cluster. |
|
my $is_online = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_status} eq "online" ? 1 : 0; |
|
|
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
is_active_member => $is_active_member, |
|
is_online => $is_online |
|
} }); |
|
|
|
if (not $is_active_member && $is_online) |
|
{ |
|
$anvil->Database->insert_or_update_jobs({ |
|
job_command => $anvil->data->{path}{exe}{'anvil-safe-start'}, |
|
job_description => "job_0337", |
|
job_name => "cgi-bin::set_membership::join", |
|
job_progress => 0, |
|
job_title => "job_0336" |
|
}); |
|
} |
|
} |
|
|
|
# Removes a host from its anvil cluster. |
|
# |
|
# Note: does not permanently remove the host; can be re-added. |
|
sub leave_cluster |
|
{ |
|
my $parameters = shift; |
|
my $host_uuid = $parameters->{host_uuid}; |
|
|
|
# Determine whether host is already a cluster member. |
|
my $is_active_member = get_active_member({ host_uuid => $host_uuid }); |
|
|
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
is_active_member => $is_active_member |
|
} }); |
|
|
|
if ($is_active_member) |
|
{ |
|
$anvil->Database->insert_or_update_jobs({ |
|
job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}, |
|
job_description => "job_0339", |
|
job_name => "cgi-bin::set_membership::leave", |
|
job_progress => 0, |
|
job_title => "job_0338" |
|
}); |
|
} |
|
} |
|
|
|
$anvil->Get->switches; |
|
|
|
# TODO: remove when done; temporarily set log level to 2 |
|
$anvil->Log->level({ set => 2 }); |
|
|
|
$anvil->Database->connect; |
|
$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}); |
|
} |
|
|
|
# Read in any CGI variables, if needed. |
|
$anvil->Get->cgi(); |
|
|
|
$anvil->Database->get_hosts(); |
|
$anvil->Database->get_anvils(); |
|
|
|
print $anvil->Template->get({file => "shared.html", name => "json_headers", show_name => 0})."\n"; |
|
|
|
my $response_body = {}; |
|
|
|
my $request_body; |
|
|
|
my $is_decode_json_success = eval { |
|
$request_body = decode_json($anvil->data->{cgi}{PUTDATA}{value}); |
|
}; |
|
|
|
if (not $is_decode_json_success) |
|
{ |
|
$anvil->Log->entry({ |
|
source => $THIS_FILE, |
|
line => __LINE__, |
|
level => 0, |
|
'print' => 1, |
|
priority => "err", |
|
key => "error_0304", |
|
variables => { request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, json_decode_error => $_ } |
|
}); |
|
$anvil->nice_exit({ exit_code => 1 }); |
|
} |
|
|
|
my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'}; |
|
# Defaults to join; will check whether host is already part of its anvil cluster. |
|
my $is_member = exists $request_body->{is_member} ? $request_body->{is_member} : 1; |
|
my $host_uuid_variable_name = "host UUID"; |
|
|
|
if ($host_uuid) |
|
{ |
|
if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid}) |
|
{ |
|
alter_host_membership({ host_uuid => $host_uuid, is_join_cluster => $is_member }); |
|
} |
|
else |
|
{ |
|
handle_invalid_uuid({ name => $host_uuid_variable_name, uuid => $host_uuid }); |
|
} |
|
} |
|
|
|
print JSON->new->utf8->encode($response_body)."\n";
|
|
|