|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Accepts a HTTP PUT request to power ON or OFF a specified 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();
|
|
|
|
|
|
|
|
$anvil->Log->level({ set => 2 });
|
|
|
|
|
|
|
|
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 set_anvil_power
|
|
|
|
{
|
|
|
|
# Expects the first element of @_ (argument array) to be a hash.
|
|
|
|
my $parameters = shift;
|
|
|
|
my $anvil_uuid = $parameters->{anvil_uuid};
|
|
|
|
my $on = $parameters->{on};
|
|
|
|
|
|
|
|
# Get all mandatory hosts inside the anvil identified by the given UUID.
|
|
|
|
my @host_uuids = (
|
|
|
|
$anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid},
|
|
|
|
$anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Check for DR host outside of the loop to avoid duplicating checks.
|
|
|
|
if (length($anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}))
|
|
|
|
{
|
|
|
|
push(@host_uuids, $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid});
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (@host_uuids)
|
|
|
|
{
|
|
|
|
set_host_power({ host_uuid => $_, on => $on, stop_servers => 1 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set_host_power
|
|
|
|
{
|
|
|
|
# Expects the first element of @_ (argument array) to be a hash.
|
|
|
|
my $parameters = shift;
|
|
|
|
my $host_uuid = $parameters->{host_uuid};
|
|
|
|
my $on = $parameters->{on};
|
|
|
|
my $stop_servers = exists $parameters->{stop_servers} ? "--stop-servers" : "";
|
|
|
|
|
|
|
|
# Check the host's status before trying to power it up/down.
|
|
|
|
my $is_host_on = $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_status} eq "online" ? 1 : 0;
|
|
|
|
|
|
|
|
$anvil->Log->variables({
|
|
|
|
source => $THIS_FILE,
|
|
|
|
line => __LINE__,
|
|
|
|
level => 2,
|
|
|
|
list => {
|
|
|
|
host_uuid => $host_uuid,
|
|
|
|
is_host_on => $is_host_on,
|
|
|
|
on => $on
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
# When host is ON and request is to power OFF.
|
|
|
|
#
|
|
|
|
# Power OFF should invoke anvil-safe-stop on a host.
|
|
|
|
if ($is_host_on && not $on)
|
|
|
|
{
|
|
|
|
$anvil->Database->insert_or_update_jobs({
|
|
|
|
job_command => $anvil->data->{path}{exe}{'anvil-safe-stop'}." --power-off ".$stop_servers,
|
|
|
|
job_host_uuid => $host_uuid,
|
|
|
|
job_description => "job_0333",
|
|
|
|
job_name => "cgi-bin::set_power::off",
|
|
|
|
job_progress => 0,
|
|
|
|
job_title => "job_0332"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
# When host is OFF and request is to power ON.
|
|
|
|
#
|
|
|
|
# Power ON should invoke striker-boot-machine on this striker.
|
|
|
|
elsif (not $is_host_on && $on)
|
|
|
|
{
|
|
|
|
$anvil->Database->insert_or_update_jobs({
|
|
|
|
job_command => $anvil->data->{path}{directories}{tools}."/striker-boot-machine --host-uuid ".$host_uuid,
|
|
|
|
job_description => "job_0335",
|
|
|
|
job_name => "cgi-bin::set_power::on",
|
|
|
|
job_progress => 0,
|
|
|
|
job_title => "job_0334"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set_server_power
|
|
|
|
{
|
|
|
|
my $parameters = shift;
|
|
|
|
my $server_uuid = $parameters->{server_uuid};
|
|
|
|
my $on = $parameters->{on};
|
|
|
|
|
|
|
|
my $query = "
|
|
|
|
SELECT
|
|
|
|
server_anvil_uuid
|
|
|
|
FROM
|
|
|
|
servers
|
|
|
|
WHERE
|
|
|
|
server_uuid = ".$anvil->Database->quote($server_uuid)."
|
|
|
|
;";
|
|
|
|
my $results = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__ });
|
|
|
|
my $count = @{$results};
|
|
|
|
|
|
|
|
if ($count == 1)
|
|
|
|
{
|
|
|
|
my $row = $results->[0];
|
|
|
|
my $anvil_uuid = $row->[0];
|
|
|
|
my $host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid};
|
|
|
|
|
|
|
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_uuid => $anvil_uuid, host_uuid => $host_uuid, server_uuid => $server_uuid } });
|
|
|
|
|
|
|
|
# if ($on)
|
|
|
|
# {
|
|
|
|
# $anvil->Database->insert_or_update_jobs({
|
|
|
|
# job_command => $anvil->data->{path}{exe}{'anvil-boot-server'}." --server-uuid ".$server_uuid,
|
|
|
|
# job_host_uuid => $host_uuid,
|
|
|
|
# job_description => "",
|
|
|
|
# job_name => "cgi-bin::set_power::server::on",
|
|
|
|
# job_progress => 0,
|
|
|
|
# job_title => ""
|
|
|
|
# });
|
|
|
|
# }
|
|
|
|
# else
|
|
|
|
# {
|
|
|
|
# $anvil->Database->insert_or_update_jobs({
|
|
|
|
# job_command => $anvil->data->{path}{exe}{'anvil-shutdown-server'}." --server-uuid ".$server_uuid,
|
|
|
|
# job_host_uuid => $host_uuid,
|
|
|
|
# job_description => "",
|
|
|
|
# job_name => "cgi-bin::set_power::server::off",
|
|
|
|
# job_progress => 0,
|
|
|
|
# job_title => ""
|
|
|
|
# });
|
|
|
|
# }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$anvil->Get->switches;
|
|
|
|
|
|
|
|
$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});
|
|
|
|
}
|
|
|
|
|
|
|
|
my $cookie_problem = $anvil->Account->read_cookies();
|
|
|
|
|
|
|
|
# Don't do anything data-related if the user is not logged in.
|
|
|
|
if ($cookie_problem)
|
|
|
|
{
|
|
|
|
$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0307" });
|
|
|
|
$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 = {};
|
|
|
|
|
|
|
|
# Decode request body (JSON string) to a hash.
|
|
|
|
#
|
|
|
|
# Note: requests made with the PUT method won't have query params.
|
|
|
|
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 => $_ }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
my $anvil_uuid = exists $request_body->{anvil_uuid} ? $request_body->{anvil_uuid} : $anvil->data->{switches}{'anvil-uuid'};
|
|
|
|
my $host_uuid = exists $request_body->{host_uuid} ? $request_body->{host_uuid} : $anvil->data->{switches}{'host-uuid'};
|
|
|
|
my $server_uuid_list = exists $request_body->{server_uuid_list} ? $request_body->{server_uuid_list} : [ $anvil->data->{switches}{'server-uuid'} ];
|
|
|
|
my $is_on = exists $request_body->{is_on} ? $request_body->{is_on} : $anvil->data->{switches}{'is-on'};
|
|
|
|
my $anvil_uuid_variable_name = "anvil UUID";
|
|
|
|
my $host_uuid_variable_name = "host UUID";
|
|
|
|
my $server_uuid_list_variable_name = "server UUID list";
|
|
|
|
|
|
|
|
$anvil->Log->variables({
|
|
|
|
source => $THIS_FILE,
|
|
|
|
line => __LINE__,
|
|
|
|
level => 2,
|
|
|
|
list => {
|
|
|
|
anvil_uuid => $anvil_uuid,
|
|
|
|
host_uuid => $host_uuid,
|
|
|
|
server_uuid_list => $server_uuid_list,
|
|
|
|
is_on => $is_on
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($anvil_uuid)
|
|
|
|
{
|
|
|
|
if (exists $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid})
|
|
|
|
{
|
|
|
|
set_anvil_power({ anvil_uuid => $anvil_uuid, on => $is_on });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handle_invalid_uuid({ name => $anvil_uuid_variable_name, uuid => $anvil_uuid });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($host_uuid)
|
|
|
|
{
|
|
|
|
if (exists $anvil->data->{hosts}{host_uuid}{$host_uuid})
|
|
|
|
{
|
|
|
|
set_host_power({ host_uuid => $host_uuid, on => $is_on });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handle_invalid_uuid({ name => $host_uuid_variable_name, uuid => $host_uuid });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($server_uuid_list)
|
|
|
|
{
|
|
|
|
foreach (@{$server_uuid_list})
|
|
|
|
{
|
|
|
|
my $server_uuid = $_;
|
|
|
|
|
|
|
|
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { server_uuid => $server_uuid } });
|
|
|
|
|
|
|
|
set_server_power({ server_uuid => $server_uuid, on => $is_on });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print JSON->new->utf8->encode($response_body)."\n";
|