anvil/tools/anvil-maintenance-mode
Digimer 2c23c6beba * Improved infinite loop handling in Log->entry, but broke the Striker UI in the process. To be fixed next...
* Added a 'test' parameter to Log->entry, Storage->make_directory and Words->key to help debug in places that Log->x may not be usable.
* Converted many $anvil->Log->x calls to print if $test to help prevent recursive loops, but not all fixed yet.
* Added the new 'host_keys' database table to the schema for a possible new feature of removing passwords in favour if machines adding peers' public keys to their authorized_hosts file.
* Cleaned up the opening calls to $anvil->Tools->new() in most tools.
* Cleaned up some variables in tools/anvil-update-states after reading their values from files (clean trailing newlines).

Signed-off-by: Digimer <digimer@alteeve.ca>
2019-01-18 03:19:36 -05:00

101 lines
2.9 KiB
Perl
Executable File

#!/usr/bin/perl
#
# This set, clear and report if the host is in mainentance mode or not.
#
# Examples;
# - Enable - anvil-maintenance-mode --set 1
# - Disable - anvil-maintenance-mode --set 0
# - Report - anvil-maintenance-mode
#
# Exit codes;
# 0 = Normal exit.
# 1 = No database connections available.
use strict;
use warnings;
use Anvil::Tools;
# Disable buffering
$| = 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->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }});
# Read switches
$anvil->data->{switches}{set} = "";
$anvil->Get->switches;
# Connect to DBs.
$anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
# No databases, exit.
print $anvil->Words->string({key => "error_0003"})."\n";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => 0, key => "error_0003"});
$anvil->nice_exit({exit_code => 1});
}
my $maintenance_mode = $anvil->System->maintenance_mode({debug => 3});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { maintenance_mode => $maintenance_mode }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { "switches::set" => $anvil->data->{switches}{set} }});
if ($anvil->data->{switches}{set} eq "1")
{
# Enable
if (not $maintenance_mode)
{
$maintenance_mode = $anvil->System->maintenance_mode({debug => 3, set => 1});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { maintenance_mode => $maintenance_mode }});
print $anvil->Words->string({key => "message_0041"})."\n";
}
else
{
# Was already set, do nothing
print $anvil->Words->string({key => "message_0042"})."\n";
}
}
elsif ($anvil->data->{switches}{set} eq "0")
{
# Disabled
if ($maintenance_mode)
{
$maintenance_mode = $anvil->System->maintenance_mode({debug => 3, set => 0});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { maintenance_mode => $maintenance_mode }});
print $anvil->Words->string({key => "message_0043"})."\n";
}
else
{
# Was already disabled, do nothing
print $anvil->Words->string({key => "message_0044"})."\n";
}
}
elsif ($anvil->data->{switches}{set})
{
# Bad call
print $anvil->Words->string({key => "message_0045", variables => { program => $THIS_FILE }})."\n";
}
# Get the current state
if ($maintenance_mode)
{
# Report that we're in mainteance mode
print $anvil->Words->string({key => "message_0046"})."\n";
}
else
{
# Report that we're not.
print $anvil->Words->string({key => "message_0047"})."\n";
}
# We're done
$anvil->nice_exit({exit_code => 0});