|
|
|
#!/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 is_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 $join_cluster = $parameters->{join_cluster};
|
|
|
|
|
|
|
|
# Determine whether host is already a cluster member.
|
|
|
|
my $is_already_member = is_active_member({ host_uuid => $host_uuid });
|
|
|
|
|
|
|
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => {
|
|
|
|
is_already_member => $is_already_member
|
|
|
|
} });
|
|
|
|
}
|
|
|
|
|
|
|
|
# Re-adds a host to its anvil cluster.
|
|
|
|
#
|
|
|
|
# Note: not to be confused wtih adding a new host the an anvil.
|
|
|
|
#sub join_cluster
|
|
|
|
#{}
|
|
|
|
|
|
|
|
# Removes a host from its anvil cluster.
|
|
|
|
#
|
|
|
|
# Note: does not permanently remove the host; can be re-added.
|
|
|
|
#sub leave_cluster
|
|
|
|
#{}
|
|
|
|
|
|
|
|
$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} : "";
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
if ($host_uuid)
|
|
|
|
{
|
|
|
|
if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid})
|
|
|
|
{
|
|
|
|
alter_host_membership({ host_uuid => $host_uuid, join_cluster => $is_member });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$anvil->Log->entry({
|
|
|
|
source => $THIS_FILE,
|
|
|
|
line => __LINE__,
|
|
|
|
level => 0,
|
|
|
|
'print' => 1,
|
|
|
|
priority => "err",
|
|
|
|
key => "error_0160",
|
|
|
|
variables => { name => "host UUID", uuid => $host_uuid }
|
|
|
|
});
|
|
|
|
$anvil->nice_exit({ exit_code => 1 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print JSON->new->utf8->encode($response_body)."\n";
|