|
|
|
#!/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 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};
|
|
|
|
|
|
|
|
$anvil->Log->variables({
|
|
|
|
source => $THIS_FILE,
|
|
|
|
line => __LINE__,
|
|
|
|
level => 2,
|
|
|
|
list => {
|
|
|
|
host_membership_status => Dumper($anvil->data->{hosts}{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
|
|
|
|
#{}
|
|
|
|
|
|
|
|
# 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";
|