From 0191f93bad1ca9491a7a0b9b114264d561c86bf2 Mon Sep 17 00:00:00 2001 From: Digimer Date: Wed, 22 Aug 2018 17:57:50 -0400 Subject: [PATCH] * Created tools/anvil-maintenance-mode to enable/disable maintenance mode from the command line. * Fixed a bug in System->maintenance_mode where disabling it wouldn't work. Signed-off-by: Digimer --- Anvil/Tools/System.pm | 5 +- share/words.xml | 11 ++++ tools/anvil-maintenance-mode | 101 +++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100755 tools/anvil-maintenance-mode diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index 67589638..619339ee 100755 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -898,8 +898,9 @@ sub maintenance_mode my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $set = defined $parameter->{set} ? $parameter->{set} : ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { set => $set }}); - if ($set) + if (($set) or ($set eq "0")) { ### TODO: stop other systems from using this database. # Am I enabling or disabling? @@ -907,6 +908,7 @@ sub maintenance_mode { # Enabling $anvil->Database->insert_or_update_variables({ + debug => $debug, variable_name => "maintenance_mode", variable_value => "1", variable_default => "0", @@ -921,6 +923,7 @@ sub maintenance_mode { # Disabling $anvil->Database->insert_or_update_variables({ + debug => $debug, variable_name => "maintenance_mode", variable_value => "0", variable_default => "0", diff --git a/share/words.xml b/share/words.xml index a37625f5..5e33bfc0 100644 --- a/share/words.xml +++ b/share/words.xml @@ -66,6 +66,17 @@ NOTE: You must update the password of any other system using this host's Updates finished. Verifying now. System update complete! The kernel was updated, so a reboot is required. System update complete! A reboot is not required. + This system has been placed into maintenance mode. + This system was already in maintenance mode, nothing changed. + This system has been removed from maintenance mode. + This system was not in maintenance mode, nothing changed. + Bad call. Usage: +Set maintenance mode: #!variable!program!# --set 1 +Clear maintenance mode: #!variable!program!# --set 0 +Report maintenance mode: #!variable!program!# + + This system is in maintenance mode. + This system is NOT in maintenance mode. Starting: [#!variable!program!#]. diff --git a/tools/anvil-maintenance-mode b/tools/anvil-maintenance-mode new file mode 100755 index 00000000..e4da0002 --- /dev/null +++ b/tools/anvil-maintenance-mode @@ -0,0 +1,101 @@ +#!/usr/bin/perl +# +# This set, clears or reports 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({log_level => 2, log_secure => 1}); + +$anvil->Storage->read_config({file => "/etc/anvil/anvil.conf"}); + +# 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 => 2, list => { maintenance_mode => $maintenance_mode }}); + +$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, 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});