\!/ KyuuKazami \!/

Path : /scripts/
Upload :
Current File : //scripts/rpmup2

#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - scripts/rpmup                             Copyright 2015 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
#
# This script effectively does a yum -y update
# Only update methods for RHEL 4-6 are supported

package scripts::rpmup;

use strict;
use warnings;

use Cpanel::Imports;
use Getopt::Param              ();
use Cpanel::Update::Config     ();
use Cpanel::SysPkgs            ();
use Cpanel::PackMan            ();
use Cpanel::ProgLang           ();
use Cpanel::Debug              ();
use Cpanel::SafeRun::Errors    ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::Cron::Utils        ();
use Cpanel::ServerTasks        ();
use Cpanel::GenSysInfo         ();    # Already included in Cpanel::SysPkgs
use Try::Tiny;

my @EA4_REQUIRED_RPMS = ( 'ea-apache24', 'ea-apache24-config' );

my $REBOOT_NEEDED_FOR_KERNEL_UPDATE_FLAG_FILE = '/var/cpanel/reboot_required_for_kernel_update';

our $DISABLE_INSTALLED_PHPDSO_CACHE = 0;

my @Pre = (
    sub {
        my $pkm = Cpanel::PackMan->instance;
        eval { $pkm->pkg_hr("ea-apache24") };
        if ($@) {
            logger->info("Resolving potential yum cache issue …\n");
            $pkm->sys->_call_yum( sub { print $_[0] }, "makecache" );
        }
    },
    sub { return cleanup_php_threaded_mpm(@_) },
    sub { return cleanup_php_dso_conflict(@_) },
);

# Determines the threading status of the Apache MPM.  It does not execute
# the Apache binary because EA4's configuration loads MPMs dynamically.  This
# means a lot of work would need to be put forth to generate a temporary
# configuration and/or execute the current system one (which could be broken).
#
# NOTE:
#   Until https://jira.cpanel.net/browse/HB-690 can update
#   Cpanel::PackMan with the 'provides' information, this code manually
#   queries the Apache MPM package to determine the threading status.
#
# USAGE:
#   If it's unable to determine the threading status, then it returns an empty string
#   Otherwise, the typical values are 'threaded' and 'forked'
sub get_apache_thread_status {
    my $status = '';

    my $mpm_package = Cpanel::SafeRun::Errors::saferunnoerror( q{/bin/rpm}, q{-q}, q{--nodigest}, q{--nosignature}, q{--whatprovides}, q{ea-apache24-mpm}, q{--queryformat}, q/%{name}/ );

    if ( $mpm_package && $mpm_package =~ /\Aea\-apache/ ) {
        my $provides = Cpanel::SafeRun::Errors::saferunnoerror( q{/bin/rpm}, q{-q}, q{--provides}, $mpm_package );
        $status = $1 if $provides =~ /ea\-apache24\-mpm\s*=\s*(\w+)/;    # e.g. ea-apache24-mpm = threaded
    }

    return lc $status;
}

my $installed_packages_cache;

# Retrieve a list of PHP packages that contain the mod_php (libphp5) Apache module
sub get_installed_phpdso_packages {
    my ( $self, $php, $pkm ) = @_;

    return @$installed_packages_cache if $installed_packages_cache && !$DISABLE_INSTALLED_PHPDSO_CACHE;

    my @want_packages = map { "$_-php" } sort @{ $php->get_installed_packages() };    # oldest to newest for logic below
    my $results = $pkm->multi_pkg_info( 'disable-excludes' => 1, 'packages' => \@want_packages );

    my %installed_packages = map  { ( $_->{state} eq 'installed' || $_->{state} eq 'updatable' ) ? ( $_->{package} => 1 ) : () } @{$results};
    my @installed          = grep { $installed_packages{$_} } @want_packages;

    $installed_packages_cache = \@installed;

    return @installed;
}

# EA-3753: This removes duplicate PHP DSO packages because the Apache
# webserver only works correctly if 1 is installed.  Older EA4 RPMs
# did not enforce this correctly.
sub cleanup_php_dso_conflict {
    my $self = shift;
    my $pkm  = Cpanel::PackMan->instance();

    my $php = eval { Cpanel::ProgLang->new( type => 'php' ) };
    return 1 unless $php;

    my @packages = sort $self->get_installed_phpdso_packages( $php, $pkm );    # oldest to newest
    return 1 unless @packages;

    # Prefer the default PHP.  If that's not set for some reason, then choose the newest.
    my $default = eval { $php->get_system_default_package() } || $packages[-1];                   # could be undef due to bad cfg
    my $keep    = ( grep { $_ eq "$default-php" } @packages ) ? "$default-php" : $packages[-1];
    my @erase   = grep { $_ ne $keep } @packages;

    return 1 unless @erase;

    print locale->maketext("You can only install a single [asis,PHP] [output,acronym,DSO,Dynamically Shared Object] package on the [asis,Apache] webserver."), "\n";
    print locale->maketext( "The system will remove the [list_and_quoted,_1] [asis,PHP] [numerate,_2,package,packages].", \@erase, $#erase + 1 ), "\n";

    return ( eval { $pkm->sys->uninstall(@erase) } ? 1 : 0 );
}

# EA-3954: Need to check if the system has a PHP DSO package installed
# while a threaded Apache MPM is in-use.  Older EA4 rpms did not
# properly conflict.  This removes the packages from the system so that
# the subsequent yum update will work when the conflict is added after
# the update.
sub cleanup_php_threaded_mpm {
    my $self = shift;

    my $pkm = Cpanel::PackMan->instance();
    my $php = eval { Cpanel::ProgLang->new( type => 'php' ) };
    return 1 unless $php;

    my @packages = $self->get_installed_phpdso_packages( $php, $pkm );
    return 1 unless @packages;

    my $thread_status = $self->get_apache_thread_status();
    return 1 unless $thread_status eq 'threaded';

    print locale->maketext("The [asis,Apache] webserver’s configuration contains a threaded [output,acronym,MPM,Multi-Processing Module]."), "\n";
    print locale->maketext( "The system will remove the [list_and_quoted,_1] [asis,PHP] [numerate,_2,package,packages].", \@packages, $#packages + 1 ), "\n";

    return ( eval { $pkm->sys->uninstall(@packages) } ? 1 : 0 );
}

sub script {
    my ($class) = @_;

    my $self  = bless( {}, $class );
    my $param = Getopt::Param->new;

    $_->($self) for @Pre;

    my $syspkgs         = Cpanel::SysPkgs->new();
    my $update_conf_ref = Cpanel::Update::Config::load();
    if ( $update_conf_ref->{'RPMUP'} eq 'never' ) {
        my $updates_setting = $update_conf_ref->{'UPDATES'} || '';
        my $is_manual = $ENV{'CPANEL_IS_CRON'} ? 0 : 1;    # see upcp for how this is set
        if ( $updates_setting eq 'never' ) {
            require Cpanel::Config::Httpd::EA4;
            if ( Cpanel::Config::Httpd::EA4::is_ea4() && $param->get_param('verbose') ) {
                print locale->maketext(
                    'Because both the “[_1]” and “[_2]” options are set to “[_3]”, the system will not perform any [asis,RPM] updates.',
                    'RPMUP',
                    'UPDATES',
                    'never'
                ) . "\n";
                print locale->maketext(
                    'Change “[_1]” to a different value to enable all [asis,RPM] updates, or change “[_2]” to a different value to enable updates to just [asis,EasyApache 4].',
                    'RPMUP',
                    'UPDATES'
                ) . "\n";
            }

        }
        elsif ( $updates_setting eq 'manual' && !$is_manual ) {

            # Although cPanel updates are enabled, one is not happening
            # at this time because the ENV variable CPANEL_IS_CRON is set
            # to 1 which indicates upcp was not called manually so only
            # maintenance is being run and there is no concern about
            # EA4 not being in sync with cPanel.  When we are doing
            # a manually update we will fall into the block below and
            # and update EA4 if its installed.
            if ( $param->get_param('verbose') ) {
                print locale->maketext(
                    "Because the “[_1]” option is set to “[_2]”, the system will not update any [asis,RPMs].",
                    'RPMUP',
                    'never'
                ) . "\n";
            }
            require Cpanel::Config::Httpd::EA4;
            if ( Cpanel::Config::Httpd::EA4::is_ea4() && $param->get_param('verbose') ) {
                print locale->maketext(
                    "Because the “[_1]” option is set to “[_2]” and this is an automatic update, the system will not update EasyApache 4.",
                    'UPDATES',
                    'manual'
                ) . "\n";
            }
        }
        else {
            # In the event OS updates are disabled, we need to manually
            # request an EA4 update to prevent the system from breaking
            # because EA4 is out of date and cPanel has a newer
            # configuration.
            require Cpanel::Config::Httpd::EA4;
            if ( Cpanel::Config::Httpd::EA4::is_ea4() ) {
                if ( $param->get_param('verbose') ) {
                    print locale->maketext(
                        "Because [asis,cPanel] automatic updates are enabled and the “[_1]” option is set to “[_2]”, the [asis,RPM] update will only apply to EasyApache 4. This ensures that [asis,cPanel] and EasyApache 4 remain compatible.",
                        'RPMUP',
                        'never'
                    ) . "\n";
                }

                # return 1 will exit 1 if ensure_ea4_is_updated fails
                return 1 if !ensure_ea4_is_updated($syspkgs);
            }
            elsif ( $param->get_param('verbose') ) {
                print locale->maketext(
                    "Because the “[_1]” option is set to “[_2]”, the system will not update any [asis,RPMs].",
                    'RPMUP',
                    'never'
                ) . "\n";
            }
        }
        return 0;
    }

    # find_and_fix_rpm_issues is called from scripts/maintenance now
    my $cpconf_ref         = Cpanel::Config::LoadCpConf::loadcpconf_not_copy();
    my $rpmup_allow_kernel = $cpconf_ref->{'rpmup_allow_kernel'};
    $rpmup_allow_kernel ||= 0;

    _run_checkyum( $syspkgs, !$rpmup_allow_kernel );

    if ( $ENV{'FORCEDCPUPDATE'} ) {

        # Can’t logger->info() because scripts/maintenance executes this in a way that supresses it
        print "Running `yum clean all` before update due to force option …\n";
        eval {
            Cpanel::PackMan->instance->sys->_call_yum( sub { print $_[0] }, "clean", "all" );
        };
        if ($@) {

            # Can’t warn() or logger->warn() because scripts/maintenance executes this in a way that supresses them
            print "WARNING: `yum clean all` exited uncleanly: $@";
        }
    }

    # The $syspkgs object will have already notified about the error.
    # We do not die on failure in case _run_checkyum needs to
    # change the kernel exclude back below
    my $exit_code = $syspkgs->update() ? 0 : 1;

    # Remove the cache for GenSysInfo and rebuild it (via rename in place), as the update may have been to a new minor version (ex. CentOS 7.3 -> 7.4)
    {
        local $Cpanel::GenSysInfo::sys_info_file = $Cpanel::GenSysInfo::sys_info_file . ".new";
        Cpanel::GenSysInfo::run();
    }
    rename $Cpanel::GenSysInfo::sys_info_file . ".new", $Cpanel::GenSysInfo::sys_info_file;

    Cpanel::ServerTasks::schedule_task( ['SystemTasks'], 5, "recache_system_reboot_data" );

    # Ensure that the handler for code on reboot is enabled
    my $root_crontab = Cpanel::Cron::Utils::fetch_user_crontab('root');
    if ( $root_crontab !~ /\@reboot\s+\/usr\/local\/cpanel\/bin\/onboot_handler/ ) {
        $root_crontab .= "\@reboot /usr/local/cpanel/bin/onboot_handler\n";
        Cpanel::Cron::Utils::save_root_crontab($root_crontab);
    }

    # TODO: Remove this in cPanel & WHM v68
    if ( -e $REBOOT_NEEDED_FOR_KERNEL_UPDATE_FLAG_FILE ) {
        if ( !unlink($REBOOT_NEEDED_FOR_KERNEL_UPDATE_FLAG_FILE) ) {
            Cpanel::Debug::log_warn("Failed to unlink($REBOOT_NEEDED_FOR_KERNEL_UPDATE_FLAG_FILE): $!");
        }
    }

    if ( !$rpmup_allow_kernel ) {

        # Only call checkyum to put back the kernel exclude
        # if we took it out above.
        _run_checkyum( $syspkgs, $rpmup_allow_kernel );
    }
    return $exit_code;

}

sub ensure_ea4_is_updated {
    my ($syspkgs) = @_;
    return $syspkgs->update( 'pkglist' => \@EA4_REQUIRED_RPMS );
}

sub _run_checkyum {
    my ( $syspkgs, $kernel ) = @_;

    my %exclude_options = %Cpanel::SysPkgs::DEFAULT_EXCLUDE_OPTIONS;
    $exclude_options{'kernel'} = $kernel ? 1 : 0;

    $syspkgs->reinit( \%exclude_options );
    if ( !$syspkgs->check() ) {
        die "The system failed to parse the yum.conf file.";
    }

    return 1;
}

unless ( caller() ) {
    exit( __PACKAGE__->script(@ARGV) );
}


@KyuuKazami