Skip to content

Commit

Permalink
Merge pull request #367 from cPholloway/RE-38
Browse files Browse the repository at this point in the history
Restore config files for packages provided by the EA4 repo
  • Loading branch information
toddr authored Feb 2, 2024
2 parents 3669239 + 42ac85d commit b0774e8
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 12 deletions.
134 changes: 128 additions & 6 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ BEGIN { # Suppress load of all of these at earliest point.
$INC{'Elevate/Motd.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Notify.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Roles/Run.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/RPM.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Script.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Service.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/SystemctlService.pm'} = 'script/elevate-cpanel.PL.static';
Expand Down Expand Up @@ -2118,6 +2119,7 @@ EOS
use Carp ();
use Simple::Accessor qw(
cpev
rpm
);

# use Log::Log4perl qw(:easy);
Expand Down Expand Up @@ -2147,6 +2149,10 @@ EOS
}
}

sub _build_rpm ($self) {
return Elevate::RPM->new( cpev => $self );
}

sub run_once ( $self, $subname ) {

my $cpev = $self->cpev;
Expand Down Expand Up @@ -2284,6 +2290,7 @@ EOS
use cPstrict;

use Elevate::Constants ();
use Elevate::RPM ();

use Cwd ();

Expand Down Expand Up @@ -2312,6 +2319,7 @@ EOS

$self->run_once('_backup_ea4_profile');
$self->run_once('_backup_ea_addons');
$self->run_once('_backup_config_files');
$self->run_once('_cleanup_rpm_db');

return;
Expand All @@ -2322,6 +2330,8 @@ EOS
$self->run_once('_restore_ea4_profile');
$self->run_once('_restore_ea_addons');

$self->run_once('_restore_config_files');

return;
}

Expand Down Expand Up @@ -2462,6 +2472,46 @@ EOS
return 1;
}

sub _backup_config_files ($self) {

cpev::remove_from_stage_file('ea4_config_files');

my $ea4_regex = qr/^EA4(:?-c7)?/a;
my $ea4_config_files = $self->rpm->get_config_files_for_repo($ea4_regex);

cpev::update_stage_file( { ea4_config_files => $ea4_config_files } );

return;
}

our %config_files_to_ignore = (
'ea-nginx' => {
'/etc/nginx/conf.d/ea-nginx.conf' => 1,
'/etc/nginx/ea-nginx/settings.json' => 1,
},
'ea-apache24' => {
'/etc/apache2/conf/httpd.conf' => 1,
},
);

sub _restore_config_files ($self) {

my $config_files = cpev::read_stage_file('ea4_config_files');

foreach my $key ( sort keys %$config_files ) {
INFO("Restoring config files for package: '$key'");

my @config_files_to_restore = @{ $config_files->{$key} };
if ( exists $config_files_to_ignore{$key} ) {
@config_files_to_restore = grep { !$config_files_to_ignore{$key}{$_} } @config_files_to_restore;
}

$self->rpm->restore_config_files(@config_files_to_restore);
}

return;
}

1;

} # --- END lib/Elevate/Components/EA4.pm
Expand Down Expand Up @@ -4409,6 +4459,79 @@ EOS

} # --- END lib/Elevate/Roles/Run.pm

{ # --- BEGIN lib/Elevate/RPM.pm

package Elevate::RPM;

use cPstrict;

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

use Simple::Accessor qw{
cpev
};

sub _build_cpev {
die q[Missing cpev];
}

sub get_config_files_for_repo ( $self, $repo ) {

my @installed = cpev::get_installed_rpms_in_repo($repo);
my $config_files = $self->_get_config_files( \@installed );

return $config_files;
}

sub _get_config_files ( $self, $pkgs ) {

my %config_files;
foreach my $pkg (@$pkgs) {

my $out = $self->cpev->ssystem_capture_output( '/usr/bin/rpm', '-qc', $pkg ) || {};

if ( $out->{status} != 0 ) {

WARN( <<~"EOS");
Failed to retrieve config files for $pkg. If you have custom config files for this pkg,
then you will need to manually evaluate the configs and potentially move the rpmsave file back
into place.
EOS

}
else {

my @pkg_config_files = grep { $_ =~ m{^/} } @{ $out->{stdout} };

$config_files{$pkg} = \@pkg_config_files;
}
}

return \%config_files;
}

sub restore_config_files ( $self, @files ) {

my $suffix = '.rpmsave';

foreach my $file (@files) {
next unless length $file;

my $backup_file = $file . $suffix;

next unless -e $backup_file;

File::Copy::move( $backup_file, $file ) or WARN("Unable to restore config file $backup_file: $!");
}

return;
}

1;

} # --- END lib/Elevate/RPM.pm

{ # --- BEGIN lib/Elevate/Script.pm

package Elevate::Script;
Expand Down Expand Up @@ -4491,13 +4614,12 @@ EOS

use Elevate::Roles::Run (); # for fatpck

use # hide
Simple::Accessor qw{
use Simple::Accessor qw{
name
file
short_name
cpev
};
};

# use Elevate::Roles::Run();
our @ISA;
Expand Down Expand Up @@ -4601,10 +4723,9 @@ EOS

use Elevate::Roles::Run (); # for fatpck

use # hide
Simple::Accessor qw{
use Simple::Accessor qw{
name
};
};

# use Elevate::Roles::Run();
our @ISA;
Expand Down Expand Up @@ -5108,6 +5229,7 @@ use Elevate::Logger ();
use Elevate::Motd ();
use Elevate::Notify ();
use Elevate::Roles::Run (); # used as parent, but ensure fatpack
use Elevate::RPM ();
use Elevate::Script ();
use Elevate::Service ();
use Elevate::SystemctlService ();
Expand Down
5 changes: 5 additions & 0 deletions lib/Elevate/Components/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cPstrict;
use Carp ();
use Simple::Accessor qw(
cpev
rpm
);

use Log::Log4perl qw(:easy);
Expand Down Expand Up @@ -47,6 +48,10 @@ BEGIN {
}
}

sub _build_rpm ($self) {
return Elevate::RPM->new( cpev => $self );
}

sub run_once ( $self, $subname ) {

my $cpev = $self->cpev;
Expand Down
55 changes: 55 additions & 0 deletions lib/Elevate/Components/EA4.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Perform am EA4 backup pre-elevate then restore it after the elevation process.
use cPstrict;

use Elevate::Constants ();
use Elevate::RPM ();

use Cwd ();
use Log::Log4perl qw(:easy);
Expand Down Expand Up @@ -42,6 +43,7 @@ sub pre_leapp ($self) { # run to perform the backup

$self->run_once('_backup_ea4_profile');
$self->run_once('_backup_ea_addons');
$self->run_once('_backup_config_files');
$self->run_once('_cleanup_rpm_db');

return;
Expand All @@ -52,6 +54,19 @@ sub post_leapp ($self) {
$self->run_once('_restore_ea4_profile');
$self->run_once('_restore_ea_addons');

# This needs to happen last (after EA4 has been reinstalled)
#
# On a new install, the RPM behavior for %config is to move the preexisting config file
# to '.rpmorig' and replace the config file with config file provided by the RPM
#
# On a new install, the RPM behavior for %config(noreplace) is to remove the preexisting
# config file and place the config file provided by the RPM at '.rpmnew'
#
# There should not be any need to restart services to pick up the new config files since the last
# step of stage 5 is to reboot the server so the services will be restarted and pick up the configs
# after this anyway
$self->run_once('_restore_config_files');

return;
}

Expand Down Expand Up @@ -199,4 +214,44 @@ sub _restore_ea4_profile ($self) {
return 1;
}

sub _backup_config_files ($self) {

cpev::remove_from_stage_file('ea4_config_files');

my $ea4_regex = qr/^EA4(:?-c7)?/a;
my $ea4_config_files = $self->rpm->get_config_files_for_repo($ea4_regex);

cpev::update_stage_file( { ea4_config_files => $ea4_config_files } );

return;
}

our %config_files_to_ignore = (
'ea-nginx' => {
'/etc/nginx/conf.d/ea-nginx.conf' => 1,
'/etc/nginx/ea-nginx/settings.json' => 1,
},
'ea-apache24' => {
'/etc/apache2/conf/httpd.conf' => 1,
},
);

sub _restore_config_files ($self) {

my $config_files = cpev::read_stage_file('ea4_config_files');

foreach my $key ( sort keys %$config_files ) {
INFO("Restoring config files for package: '$key'");

my @config_files_to_restore = @{ $config_files->{$key} };
if ( exists $config_files_to_ignore{$key} ) {
@config_files_to_restore = grep { !$config_files_to_ignore{$key}{$_} } @config_files_to_restore;
}

$self->rpm->restore_config_files(@config_files_to_restore);
}

return;
}

1;
83 changes: 83 additions & 0 deletions lib/Elevate/RPM.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package Elevate::RPM;

=encoding utf-8
=head1 NAME
Elevate::RPM
Logic wrapping the 'rpm' system binary
=cut

use cPstrict;

use Log::Log4perl qw(:easy);

use Simple::Accessor qw{
cpev
};

sub _build_cpev {
die q[Missing cpev];
}

sub get_config_files_for_repo ( $self, $repo ) {

my @installed = cpev::get_installed_rpms_in_repo($repo);
my $config_files = $self->_get_config_files( \@installed );

return $config_files;
}

sub _get_config_files ( $self, $pkgs ) {

my %config_files;
foreach my $pkg (@$pkgs) {

my $out = $self->cpev->ssystem_capture_output( '/usr/bin/rpm', '-qc', $pkg ) || {};

if ( $out->{status} != 0 ) {

# warn and move on if rpm -qc fails
WARN( <<~"EOS");
Failed to retrieve config files for $pkg. If you have custom config files for this pkg,
then you will need to manually evaluate the configs and potentially move the rpmsave file back
into place.
EOS

}
else {

# rpm -qc will return absolute paths if the package has config files
# In the event that package does not contain any files, it will return
# "(contains no files)"
# We need to filter anything that is not an absolute path out
my @pkg_config_files = grep { $_ =~ m{^/} } @{ $out->{stdout} };

$config_files{$pkg} = \@pkg_config_files;
}
}

return \%config_files;
}

sub restore_config_files ( $self, @files ) {

# %config and %config(noreplace) both get moved to '.rpmsave' when removing an RPM
my $suffix = '.rpmsave';

foreach my $file (@files) {
next unless length $file;

my $backup_file = $file . $suffix;

next unless -e $backup_file;

File::Copy::move( $backup_file, $file ) or WARN("Unable to restore config file $backup_file: $!");
}

return;
}

1;
Loading

0 comments on commit b0774e8

Please sign in to comment.