Skip to content

Commit

Permalink
Merge pull request #450 from cPholloway/RE-337
Browse files Browse the repository at this point in the history
Convert multiple NICs blocker to a component
  • Loading branch information
toddr authored Jun 25, 2024
2 parents a2d14ec + 74075c9 commit 98f79e2
Show file tree
Hide file tree
Showing 10 changed files with 698 additions and 52 deletions.
231 changes: 210 additions & 21 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ BEGIN { # Suppress load of all of these at earliest point.
$INC{'Elevate/Components/KernelCare.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Kernel.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/MySQL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/NICs.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/NixStats.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PECL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PerlXS.pm'} = 'script/elevate-cpanel.PL.static';
Expand All @@ -72,6 +73,7 @@ BEGIN { # Suppress load of all of these at earliest point.
$INC{'Elevate/Logger.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Marker.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Motd.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/NICs.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';
Expand Down Expand Up @@ -112,6 +114,10 @@ BEGIN { # Suppress load of all of these at earliest point.

use constant IGNORE_OUTDATED_SERVICES_FILE => q[/etc/cpanel/local/ignore_outdated_services];

use constant SBIN_IP => q[/sbin/ip];

use constant ETH_FILE_PREFIX => q[/etc/sysconfig/network-scripts/ifcfg-];

1;

} # --- END lib/Elevate/Constants.pm
Expand Down Expand Up @@ -1625,17 +1631,17 @@ EOS
use cPstrict;

use Elevate::Constants ();
use Elevate::NICs ();

# use Elevate::Blockers::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Blockers::Base); }

use Cwd ();

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

use constant SBIN_IP => q[/sbin/ip];
use constant ETH_FILE_PREFIX => Elevate::Constants::ETH_FILE_PREFIX;
use constant SBIN_IP => Elevate::Constants::SBIN_IP;

sub check ($self) {
return 1 unless $self->should_run_leapp; # skip when --no-leapp is provided
Expand All @@ -1644,38 +1650,90 @@ EOS

sub _blocker_bad_nics_naming ($self) {
return $self->has_blocker( q[Missing ] . SBIN_IP . ' binary' ) unless -x SBIN_IP;
my @eths = _get_nics();
my @eths = Elevate::NICs::get_nics();
if ( @eths >= 2 ) {
return $self->has_blocker( <<~'EOS');
Your machine has multiple network interface cards (NICs) using kernel-names (ethX).
Since the upgrade process cannot guarantee their stability after upgrade, you cannot upgrade.
WARN( <<~'EOS' );
Your machine has multiple network interface cards (NICs) using
kernel-names (ethX).
EOS

if ( $self->is_check_mode() ) {
INFO( <<~'EOS' );
Since the upgrade process cannot guarantee their stability after
upgrade, we will need to rename these interfaces before upgrading.
EOS
return 0;
}

return if $self->_nics_have_missing_ifcfg_files(@eths);

my $pretty_distro_name = $self->upgrade_to_pretty_name();
WARN( <<~"EOS" );
Prior to elevating this system to $pretty_distro_name, we will
automatically rename these interfaces.
Please provide those interfaces new names before continuing the update.
EOS

if ( !$self->getopt('non-interactive') ) {
if (
!IO::Prompt::prompt(
'-one_char',
'-yes_no',
'-tty',
-default => 'y',
'Do you consent to renaming your NICs to use non kernel-names [Y/n]: ',
)
) {
return $self->has_blocker( <<~"EOS" );
The system cannot be elevated to $pretty_distro_name until the
NICs using kernel-names (ethX) have been updated with new names.
Please provide those interfaces new names before continuing the
update.
To have this script perform the upgrade, run this script again
and consent to allow it to rename the NICs.
EOS
}
}
}

return 0;
}

sub _get_nics {
my $ip_info = Cpanel::SafeRun::Errors::saferunnoerror( SBIN_IP, 'addr' ) // '';
sub _nics_have_missing_ifcfg_files ( $self, @nics ) {

my @eths;
foreach my $line ( split /\n/xms, $ip_info ) {
$line =~ /^[0-9]+: \s (eth[0-9]):/xms
or next;
my @nics_missing_nic_path;
foreach my $nic (@nics) {
my $nic_path = ETH_FILE_PREFIX . $nic;

my $eth = $1;
my $value = readlink "/sys/class/net/$eth"
or next;
my $err_msg = <<~"EOS";
The file for the network interface card (NIC) using kernel-name ($nic) does
not exist at the expected location ($nic_path). We are unable to
change the name of this NIC due to this. You will need to resolve this
issue manually before elevate can continue.
$value =~ m{/virtual/}xms
and next;
EOS

push @eths, $eth;
unless ( -s $nic_path ) {
ERROR($err_msg);
push @nics_missing_nic_path, $nic;
}
}

return @eths;
if (@nics_missing_nic_path) {
my $missing_nics = join "\n", @nics_missing_nic_path;
return $self->has_blocker( <<~"EOS" );
This script is unable to rename the following network interface cards
due to a missing ifcfg file:
$missing_nics
Please provide these interfaces new names before continuing the update.
EOS
}

return;
}

1;
Expand Down Expand Up @@ -2469,6 +2527,7 @@ EOS
qw(
check_installed_devel_kernels
cl_mysql_repository_setup
persistentnetnamesdisable
verify_check_results
)
);
Expand Down Expand Up @@ -4382,6 +4441,97 @@ EOS

} # --- END lib/Elevate/Components/MySQL.pm

{ # --- BEGIN lib/Elevate/Components/NICs.pm

package Elevate::Components::NICs;

use cPstrict;

use File::Slurper ();

use Elevate::Constants ();
use Elevate::NICs ();

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

# use Elevate::Components::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Components::Base); }

use constant ETH_FILE_PREFIX => Elevate::Constants::ETH_FILE_PREFIX;
use constant NIC_PREFIX => q[cp];
use constant PERSISTENT_NET_RULES_PATH => q[/etc/udev/rules.d/70-persistent-net.rules];

sub pre_leapp ($self) {

$self->_rename_nics();

return;
}

sub _rename_nics ($self) {

my @nics = Elevate::NICs::get_nics();
return unless scalar @nics > 1;

foreach my $nic (@nics) {
my $nic_path = ETH_FILE_PREFIX . $nic;

my $die_msg = <<~"EOS";
The file for the network interface card (NIC) using kernel-name ($nic) does
not exist at the expected location ($nic_path). We are unable to
change the name of this NIC due to this. You will need to resolve this
issue manually before elevate can continue. Once the issue has been
resolved, you can continue this script by executing:
/scripts/elevate-cpanel --continue
EOS
die "$die_msg\n" unless -s $nic_path;

my $new_nic = NIC_PREFIX . $nic;
INFO("Renaming $nic to $new_nic");

my $device_line_found = 0;
my $txt = File::Slurper::read_binary($nic_path);
my @nic_lines = split( "\n", $txt );
foreach my $line (@nic_lines) {
if ( $line =~ m{^\s*DEVICE\s*=\s*\Q$nic\E\s*$} ) {
$device_line_found = 1;
$line = "DEVICE=$new_nic";
last;
}
}
die qq[Unable to rename $nic to $new_nic. The line beginning with 'DEVICE' in $nic_path was not found.\n] unless $device_line_found;

my $new_nic_path = ETH_FILE_PREFIX . $new_nic;
File::Slurper::write_binary( $new_nic_path, join( "\n", @nic_lines ) );

unlink $nic_path;

next unless -s PERSISTENT_NET_RULES_PATH;
my $rules_txt = File::Slurper::read_binary( PERSISTENT_NET_RULES_PATH() );
my @rules_lines = split( "\n", $rules_txt );
foreach my $line (@rules_lines) {
$line =~ s/NAME="\Q$nic\E"/NAME="$new_nic"/;
}

my $new_rules_txt = join( "\n", @rules_lines );
$new_rules_txt .= "\n";
File::Slurper::write_binary( PERSISTENT_NET_RULES_PATH(), $new_rules_txt );
}

return;
}

sub post_leapp ($self) {
return;
}

1;

} # --- END lib/Elevate/Components/NICs.pm

{ # --- BEGIN lib/Elevate/Components/NixStats.pm

package Elevate::Components::NixStats;
Expand Down Expand Up @@ -6462,6 +6612,42 @@ EOS

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

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

package Elevate::NICs;

use cPstrict;

use Elevate::Constants ();

use Cpanel::SafeRun::Errors ();

sub get_nics () {
my $sbin_ip = Elevate::Constants::SBIN_IP();
my $ip_info = Cpanel::SafeRun::Errors::saferunnoerror( $sbin_ip, 'addr' ) // '';

my @eths;
foreach my $line ( split /\n/xms, $ip_info ) {
$line =~ /^[0-9]+: \s (eth[0-9]):/xms
or next;

my $eth = $1;
my $value = readlink "/sys/class/net/$eth"
or next;

$value =~ m{/virtual/}xms
and next;

push @eths, $eth;
}

return @eths;
}

1;

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

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

package Elevate::Notify;
Expand Down Expand Up @@ -7642,6 +7828,7 @@ use Elevate::Components::LiteSpeed ();
use Elevate::Components::KernelCare ();
use Elevate::Components::Kernel ();
use Elevate::Components::MySQL ();
use Elevate::Components::NICs ();
use Elevate::Components::NixStats ();
use Elevate::Components::PECL ();
use Elevate::Components::PerlXS ();
Expand All @@ -7667,6 +7854,7 @@ use Elevate::Leapp ();
use Elevate::Logger ();
use Elevate::Marker ();
use Elevate::Motd ();
use Elevate::NICs ();
use Elevate::Notify ();
use Elevate::Roles::Run (); # used as parent, but ensure fatpack
use Elevate::RPM ();
Expand Down Expand Up @@ -8336,6 +8524,7 @@ sub run_stage_2 ($self) {
$self->run_component_once( 'AutoSSL' => 'pre_leapp' );
$self->run_component_once( 'KernelCare' => 'pre_leapp' );
$self->run_component_once( 'Grub2' => 'pre_leapp' );
$self->run_component_once( 'NICs' => 'pre_leapp' );

return ACTION_REBOOT_NEEDED;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Elevate/Blockers/Leapp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sub _check_for_inhibitors ($self) {
qw(
check_installed_devel_kernels
cl_mysql_repository_setup
persistentnetnamesdisable
verify_check_results
)
);
Expand Down
Loading

0 comments on commit 98f79e2

Please sign in to comment.