Skip to content

Commit

Permalink
Merge pull request #312 from cpanel/mysql-cover
Browse files Browse the repository at this point in the history
Mysql cover
  • Loading branch information
toddr authored Sep 7, 2023
2 parents a822473 + d6c9902 commit 8378b0f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
19 changes: 16 additions & 3 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -3028,8 +3028,9 @@ EOS

use File::Copy ();

# use Log::Log4perl qw(:easy);
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
use Elevate::Notify ();

# use Elevate::Components::Base();
our @ISA;
Expand Down Expand Up @@ -3087,6 +3088,7 @@ EOS
if ( !$enabled ) {
INFO("MySQL is not enabled. This will cause the MySQL upgrade tool to fail. Temporarily enabling it to ensure the upgrade succeeds.");
Cpanel::SafeRun::Simple::saferunnoerror(qw{/usr/local/cpanel/bin/whmapi1 configureservice service=mysql enabled=1});

}

my $out = Cpanel::SafeRun::Simple::saferunnoerror( qw{/usr/local/cpanel/bin/whmapi1 start_background_mysql_upgrade}, "version=$mysql_version" );
Expand All @@ -3102,6 +3104,7 @@ EOS

my $c = 0;

my $wait_more = 30;
while (1) {
$c = ( $c + 1 ) % 10;
$out = Cpanel::SafeRun::Simple::saferunnoerror( qw{/usr/local/cpanel/bin/whmapi1 background_mysql_upgrade_status }, "upgrade_id=$id" );
Expand All @@ -3120,6 +3123,12 @@ EOS
if ( $out =~ m{\sstate:\s*(\w+)} ) {
$status = $1;
}

if ( $status ne 'success' && --$wait_more > 0 ) {
sleep 1;
next;
}

last;
}

Expand All @@ -3130,9 +3139,13 @@ EOS
INFO("MySQL $mysql_version restored");
}
else {
FATAL("Failed to restore MySQL $mysql_version: upgrade $id status '$status'");
my $msg = "Failed to restore MySQL $mysql_version: upgrade $id status '$status'";

FATAL($msg);
FATAL("$out");
die 'Failed to restore MySQL';

Elevate::Notify::add_final_notification($msg);
return;
}
}
else {
Expand Down
35 changes: 25 additions & 10 deletions lib/Elevate/Components/MySQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Capture and reinstall MySQL packages.

use cPstrict;

use File::Copy ();
use Log::Log4perl qw(:easy);
use File::Copy ();
use Log::Log4perl qw(:easy);
use Elevate::Notify ();

use parent qw{Elevate::Components::Base};

my $cnf_file = '/etc/my.cnf';

sub pre_leapp ($self) {

$self->run_once("_cleanup_mysql_packages");
Expand Down Expand Up @@ -62,7 +64,7 @@ sub _cleanup_mysql_packages ($self) {
sub _reinstall_mysql_packages {

my $mysql_version = cpev::read_stage_file( 'mysql-version', '' ) or return;
my $enabled = cpev::read_stage_file( 'mysql-enabled', '' ) or return;
my $enabled = cpev::read_stage_file( 'mysql-enabled', '' ) or return;

INFO("Restoring MySQL $mysql_version");

Expand All @@ -73,9 +75,10 @@ sub _reinstall_mysql_packages {
INFO("Restoring $cnf_file.rpmsave_pre_elevate to $cnf_file...");
File::Copy::copy( "$cnf_file.rpmsave_pre_elevate", $cnf_file ) or WARN("Couldn't restore $cnf_file.rpmsave: $!");

if( !$enabled ) {
if ( !$enabled ) {
INFO("MySQL is not enabled. This will cause the MySQL upgrade tool to fail. Temporarily enabling it to ensure the upgrade succeeds.");
Cpanel::SafeRun::Simple::saferunnoerror( qw{/usr/local/cpanel/bin/whmapi1 configureservice service=mysql enabled=1} );
Cpanel::SafeRun::Simple::saferunnoerror(qw{/usr/local/cpanel/bin/whmapi1 configureservice service=mysql enabled=1});

# Pray it goes ok, as what exactly do you want me to do if this reports failure? May as well just move forward in this case without checking.
}

Expand All @@ -92,10 +95,11 @@ sub _reinstall_mysql_packages {

my $c = 0;

my $wait_more = 30;
while (1) {
$c = ( $c + 1 ) % 10;
$out = Cpanel::SafeRun::Simple::saferunnoerror( qw{/usr/local/cpanel/bin/whmapi1 background_mysql_upgrade_status }, "upgrade_id=$id" );
if($?) {
if ($?) {
last if !$enabled;
die qq[Failed to restore MySQL $mysql_version: cannot check upgrade_id=$id];
}
Expand All @@ -110,19 +114,30 @@ sub _reinstall_mysql_packages {
if ( $out =~ m{\sstate:\s*(\w+)} ) {
$status = $1;
}

# we cannot trust the whmapi1 call (race condition) CPANEL-43253
if ( $status ne 'success' && --$wait_more > 0 ) {
sleep 1;
next;
}

last;
}

print "\n" if $c; # clear the last "." from above
Cpanel::SafeRun::Simple::saferunnoerror( qw{/usr/local/cpanel/bin/whmapi1 configureservice service=mysql enabled=0} ) if !$enabled;
print "\n" if $c; # clear the last "." from above
Cpanel::SafeRun::Simple::saferunnoerror(qw{/usr/local/cpanel/bin/whmapi1 configureservice service=mysql enabled=0}) if !$enabled;

if ( $status eq 'success' ) {
INFO("MySQL $mysql_version restored");
}
else {
FATAL("Failed to restore MySQL $mysql_version: upgrade $id status '$status'");
my $msg = "Failed to restore MySQL $mysql_version: upgrade $id status '$status'";

FATAL($msg);
FATAL("$out");
die 'Failed to restore MySQL';

Elevate::Notify::add_final_notification($msg);
return;
}
}
else {
Expand Down

0 comments on commit 8378b0f

Please sign in to comment.