Skip to content

Commit

Permalink
Move up the recording of the active remote MySQL profile
Browse files Browse the repository at this point in the history
case RE-34: Fixes an issue where it was not recorded when the
`try` block actually succeeded. I also golfed a bit where I could.

TODO: Fix test

Changelog: Fix minor bug in original RE-34 fix where it would not always
  record the active profile.
  • Loading branch information
troglodyne committed Jul 25, 2024
1 parent 5ef8705 commit 0353a8c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 60 deletions.
55 changes: 25 additions & 30 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -5470,65 +5470,60 @@ EOS
}

sub post_leapp ($self) {
return unless -e MYSQL_PROFILE_FILE;

if ( -e MYSQL_PROFILE_FILE ) {
my $original_profile = File::Slurper::read_text(MYSQL_PROFILE_FILE) // 'localhost';
INFO(qq{Reactivating "$original_profile" MySQL profile});
my $original_profile = File::Slurper::read_text(MYSQL_PROFILE_FILE) // 'localhost';
INFO(qq{Reactivating "$original_profile" MySQL profile});

my $output = $self->ssystem_capture_output( '/usr/local/cpanel/scripts/manage_mysql_profiles', '--activate', "$original_profile" );
my $stdout = join qq{\n}, @{ $output->{'stdout'} };

unless ( $stdout =~ m{MySQL profile activation done} ) {
die <<~"EOS";
Unable to reactivate the original remote MySQL profile "$original_profile":
my $output = $self->ssystem_capture_output( '/usr/local/cpanel/scripts/manage_mysql_profiles', '--activate', "$original_profile" );
my $stdout = join qq{\n}, @{ $output->{'stdout'} };

$stdout
unless ( $stdout =~ m{MySQL profile activation done} ) {
die <<~"EOS";
Unable to reactivate the original remote MySQL profile "$original_profile":
Please resolve the reported problems then run this script again with:
$stdout
/scripts/elevate-cpanel --continue
Please resolve the reported problems then run this script again with:
EOS
}
/scripts/elevate-cpanel --continue
unlink MYSQL_PROFILE_FILE;
EOS
}

unlink MYSQL_PROFILE_FILE or WARN( "Could not delete " . MYSQL_PROFILE_FILE . ": $!" );

return;
}

sub _ensure_localhost_mysql_profile_is_active ( $self, $should_create_localhost_profile ) {

if ( Cpanel::MysqlUtils::MyCnf::Basic::is_local_mysql() ) {
return;
}
return if Cpanel::MysqlUtils::MyCnf::Basic::is_local_mysql();

my $profile_manager = Cpanel::MysqlUtils::RemoteMySQL::ProfileManager->new();

my $profile = $profile_manager->get_active_profile('dont_die') || 'localhost';
if ($should_create_localhost_profile) {
INFO( "Saving the currently active MySQL Profile ($profile) to " . MYSQL_PROFILE_FILE );
File::Slurper::write_text( MYSQL_PROFILE_FILE, $profile );
}

try {
$profile_manager->validate_profile('localhost');
$self->_activate_localhost_profile($profile_manager);
}

catch {
if ($should_create_localhost_profile) {
INFO("Attempting to create new localhost MySQL profile...");
$self->_create_new_localhost_profile($profile_manager);
$self->_ensure_localhost_mysql_profile_is_active(0);
}
else {
die "Unable to generate/enable localhost MySQL profile: $_\n";
}
die "Unable to generate/enable localhost MySQL profile: $_\n" unless $should_create_localhost_profile;
INFO("Attempting to create new localhost MySQL profile...");
$self->_create_new_localhost_profile($profile_manager);
$self->_ensure_localhost_mysql_profile_is_active(0);
};

return;
}

sub _create_new_localhost_profile ( $self, $profile_manager ) {

my $active_profile = $profile_manager->get_active_profile('dont_die');
File::Slurper::write_text( MYSQL_PROFILE_FILE, $active_profile );

my $password = Cpanel::PasswdStrength::Generate::generate_password( 16, no_othersymbols => 1 );

try {
Expand Down
58 changes: 28 additions & 30 deletions lib/Elevate/Components/DatabaseUpgrade.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,46 @@ sub pre_leapp ($self) {
}

sub post_leapp ($self) {
return unless -e MYSQL_PROFILE_FILE;

if ( -e MYSQL_PROFILE_FILE ) {
my $original_profile = File::Slurper::read_text(MYSQL_PROFILE_FILE) // 'localhost';
INFO(qq{Reactivating "$original_profile" MySQL profile});
my $original_profile = File::Slurper::read_text(MYSQL_PROFILE_FILE) // 'localhost';
INFO(qq{Reactivating "$original_profile" MySQL profile});

my $output = $self->ssystem_capture_output( '/usr/local/cpanel/scripts/manage_mysql_profiles', '--activate', "$original_profile" );
my $stdout = join qq{\n}, @{ $output->{'stdout'} };

unless ( $stdout =~ m{MySQL profile activation done} ) {
die <<~"EOS";
Unable to reactivate the original remote MySQL profile "$original_profile":
my $output = $self->ssystem_capture_output( '/usr/local/cpanel/scripts/manage_mysql_profiles', '--activate', "$original_profile" );
my $stdout = join qq{\n}, @{ $output->{'stdout'} };

$stdout
unless ( $stdout =~ m{MySQL profile activation done} ) {
die <<~"EOS";
Unable to reactivate the original remote MySQL profile "$original_profile":
Please resolve the reported problems then run this script again with:
$stdout
/scripts/elevate-cpanel --continue
Please resolve the reported problems then run this script again with:
EOS
}
/scripts/elevate-cpanel --continue
unlink MYSQL_PROFILE_FILE;
EOS
}

unlink MYSQL_PROFILE_FILE or WARN( "Could not delete " . MYSQL_PROFILE_FILE . ": $!" );

return;
}

sub _ensure_localhost_mysql_profile_is_active ( $self, $should_create_localhost_profile ) {

if ( Cpanel::MysqlUtils::MyCnf::Basic::is_local_mysql() ) {
return;
}
return if Cpanel::MysqlUtils::MyCnf::Basic::is_local_mysql();

my $profile_manager = Cpanel::MysqlUtils::RemoteMySQL::ProfileManager->new();

# Immediately record the currently active profile, as othrewise you can
# miss it in the try/catch below. Default to localhost, because if there's
# no answer, something's probably wrong in a way we don't *want* to touch.
my $profile = $profile_manager->get_active_profile('dont_die') || 'localhost';
if($should_create_localhost_profile) {
INFO( "Saving the currently active MySQL Profile ($profile) to " . MYSQL_PROFILE_FILE );
File::Slurper::write_text( MYSQL_PROFILE_FILE, $profile );
}

# Validate that the current “localhost” profile exists, and contains valid settings.
try {
$profile_manager->validate_profile('localhost');
Expand All @@ -90,24 +95,17 @@ sub _ensure_localhost_mysql_profile_is_active ( $self, $should_create_localhost_

# Otherwise attempt to recreate it, overwriting the existing profile.
catch {
if ($should_create_localhost_profile) {
INFO("Attempting to create new localhost MySQL profile...");
$self->_create_new_localhost_profile($profile_manager);
$self->_ensure_localhost_mysql_profile_is_active(0);
}
else {
die "Unable to generate/enable localhost MySQL profile: $_\n";
}
die "Unable to generate/enable localhost MySQL profile: $_\n" unless $should_create_localhost_profile;
INFO("Attempting to create new localhost MySQL profile...");
$self->_create_new_localhost_profile($profile_manager);
$self->_ensure_localhost_mysql_profile_is_active(0);
};

return;
}

sub _create_new_localhost_profile ( $self, $profile_manager ) {

my $active_profile = $profile_manager->get_active_profile('dont_die');
File::Slurper::write_text( MYSQL_PROFILE_FILE, $active_profile );

my $password = Cpanel::PasswdStrength::Generate::generate_password( 16, no_othersymbols => 1 );

try {
Expand Down

0 comments on commit 0353a8c

Please sign in to comment.