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.
196 lines
5.8 KiB
196 lines
5.8 KiB
#!/usr/bin/perl |
|
# |
|
# This manages power on the host. It can set that a reboot is or is no longer required. It can also reboot or |
|
# power off the machine. |
|
# |
|
# Examples; |
|
# - Mark that a reboot is required - anvil-manage-power --reboot-needed 1 |
|
# - Clear that a reboot is needed - anvil-manage-power --reboot-needed 0 |
|
# - Report whether a reboot is needed or not - anvil-manage-power |
|
# - Reboot the system - anvil-manage-power --reboot [-y] |
|
# - Power the system off - anvil-manage-power --poweroff [-y] |
|
# |
|
# Exit codes; |
|
# 0 = Normal exit. |
|
# 1 = No database connections available. |
|
# |
|
# TODO: Don't reboot or power off until all external users are done with the database on this system (if |
|
# applicable) |
|
# |
|
|
|
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({log_level => 2, log_secure => 1}); |
|
|
|
$anvil->Storage->read_config({file => "/etc/anvil/anvil.conf"}); |
|
|
|
# Read switches |
|
$anvil->data->{switches}{'poweroff'} = ""; |
|
$anvil->data->{switches}{'power-off'} = ""; |
|
$anvil->data->{switches}{'reboot'} = ""; |
|
$anvil->data->{switches}{'y'} = ""; |
|
$anvil->data->{switches}{'yes'} = ""; |
|
$anvil->data->{switches}{'reboot-needed'} = ""; |
|
$anvil->data->{switches}{'job-uuid'} = ""; |
|
$anvil->Get->switches; |
|
|
|
if ($anvil->data->{switches}{'power-off'}) |
|
{ |
|
$anvil->data->{switches}{'poweroff'} = 1; |
|
} |
|
if ($anvil->data->{switches}{'yes'}) |
|
{ |
|
$anvil->data->{switches}{'y'} = 1; |
|
} |
|
|
|
# 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}); |
|
} |
|
|
|
# Are we being asked to reboot or power off? |
|
if ($anvil->data->{switches}{'reboot'}) |
|
{ |
|
# Did the user confirm? |
|
if ($anvil->data->{switches}{'reboot'} =~ /^y/i) |
|
{ |
|
do_poweroff($anvil, "reboot"); |
|
} |
|
else |
|
{ |
|
# Not yet, ask to confirm. |
|
print $anvil->Words->string({key => "message_0059"})." "; |
|
my $answer = <STDIN>; |
|
chomp($answer); |
|
if ($answer =~ /^y/i) |
|
{ |
|
do_poweroff($anvil, "reboot"); |
|
} |
|
else |
|
{ |
|
# Abort and exit. |
|
print $anvil->Words->string({key => "message_0061"})."\n"; |
|
$anvil->nice_exit({exit_code => 0}); |
|
} |
|
} |
|
} |
|
if ($anvil->data->{switches}{'poweroff'}) |
|
{ |
|
# Did the user confirm? |
|
if ($anvil->data->{switches}{'poweroff'} =~ /^y/i) |
|
{ |
|
do_poweroff($anvil, "poweroff"); |
|
} |
|
else |
|
{ |
|
# Not yet, ask to confirm. |
|
print $anvil->Words->string({key => "message_0060"})." "; |
|
my $answer = <STDIN>; |
|
chomp($answer); |
|
if ($answer =~ /^y/i) |
|
{ |
|
do_poweroff($anvil, "poweroff"); |
|
} |
|
else |
|
{ |
|
# Abort and exit. |
|
print $anvil->Words->string({key => "message_0061"})."\n"; |
|
$anvil->nice_exit({exit_code => 0}); |
|
} |
|
} |
|
} |
|
|
|
my $reboot_needed = $anvil->System->reboot_needed({debug => 2}); |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }}); |
|
|
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::reboot-needed" => $anvil->data->{switches}{'reboot-needed'} }}); |
|
if ($anvil->data->{switches}{'reboot-needed'} eq "1") |
|
{ |
|
# Enable |
|
if (not $reboot_needed) |
|
{ |
|
$reboot_needed = $anvil->System->reboot_needed({set => 1}); |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }}); |
|
print $anvil->Words->string({key => "message_0048"})."\n"; |
|
} |
|
else |
|
{ |
|
# Was already set, do nothing |
|
print $anvil->Words->string({key => "message_0049"})."\n"; |
|
} |
|
} |
|
elsif ($anvil->data->{switches}{'reboot-needed'} eq "0") |
|
{ |
|
# Disabled |
|
if ($reboot_needed) |
|
{ |
|
$reboot_needed = $anvil->System->reboot_needed({set => 0}); |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }}); |
|
print $anvil->Words->string({key => "message_0050"})."\n"; |
|
} |
|
else |
|
{ |
|
# Was already disabled, do nothing |
|
print $anvil->Words->string({key => "message_0051"})."\n"; |
|
} |
|
} |
|
elsif ($anvil->data->{switches}{'reboot-needed'}) |
|
{ |
|
# Bad call |
|
print $anvil->Words->string({key => "message_0052", variables => { program => $THIS_FILE }})."\n"; |
|
} |
|
|
|
# Get the current state |
|
if ($reboot_needed) |
|
{ |
|
# Report that we're in mainteance mode |
|
print $anvil->Words->string({key => "message_0053"})."\n"; |
|
} |
|
else |
|
{ |
|
# Report that we're not. |
|
print $anvil->Words->string({key => "message_0054"})."\n"; |
|
} |
|
|
|
# We're done |
|
$anvil->nice_exit({exit_code => 0}); |
|
|
|
|
|
############################################################################################################# |
|
# Private functions. # |
|
############################################################################################################# |
|
|
|
# This does a reboot, clearing the flag indicating a reboot is required in the process. |
|
sub do_poweroff |
|
{ |
|
my ($anvil, $task) = @_; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { task => $task }}); |
|
|
|
my $say_task = $task eq "poweroff" ? "message_0062" : "message_0063"; |
|
print $anvil->Words->string({key => $say_task})."\n"; |
|
|
|
# Clear the "reboot needed" flag. |
|
$reboot_needed = $anvil->System->reboot_needed({set => 0}); |
|
|
|
# Set the progress to '50%' (anvil-daemon will set it to 100% when it starts post-boot). |
|
|
|
$anvil->nice_exit({exit_code => 0}); |
|
}
|
|
|