JFIF$        dd7 

Viewing File: /usr/local/bin/ea_install_profile

#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - ea_install_profile                     Copyright(c) 2016 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package ea_install_profile;

use strict;

use Cpanel::JSON;
use Cpanel::PackMan;
use Cpanel::Config::Httpd;
use Data::Dumper;
use Path::Tiny 'path';

sub usage {
    my ($msg) = @_;

    print "Error: $msg\n" if defined $msg;
    print "usage: ea_install_profile [--firstinstall|--install] profile_file\n";
    print "\n";
    print "   With no flags, $0 will only do conflict resolution on the profile.\n";
    print "\n";
    print "   --install: Resolve conflicts and install the profile.\n";
    print "   --firstinstall: Attempt to do the install without conflict resolution and fallback\n";
    print "                   to doing --install if it fails.  This is only intended to be used\n";
    print "                   on a fresh cPanel install.\n";
    print "\n";
    exit( $msg ? 1 : 0 );
}

sub script {
    my (@args) = @_;

    die "May only be run if you are using EasyApache 4" if ( !Cpanel::Config::Httpd::is_ea4() );

    my $profile;
    my $install      = 0;
    my $firstinstall = 0;

    my $idx = 0;
    if ( $args[$idx] eq "--firstinstall" ) {
        $idx++;
        $firstinstall = 1;
    }
    if ( $args[$idx] eq "--install" ) {
        $idx++;
        $install = 1;
    }

    usage() if ( !defined $args[$idx] );

    $profile = $args[$idx];

    if ( !-f $profile ) {
        $profile = "/etc/cpanel/ea4/profiles/custom/" . $profile;
        usage("Cannot find profile") if !-f $profile;
    }

    my @addl_prefixes = eval {
        map { $_->basename } path("/etc/cpanel/ea4/additional-pkg-prefixes/")->children;
    };

    print "Loading profile …\n";
    my %server_pkgs;
    @server_pkgs{ Cpanel::PackMan->instance->list( 'prefix' => 'ea-' ) } = ();
    for my $prefix (@addl_prefixes) {
        @server_pkgs{ Cpanel::PackMan->instance->list( 'prefix' => "$prefix-" ) } = ();
    }

    my $data = Cpanel::JSON::LoadFile($profile);

    # 1. Fix ea-apachae24 package names with underscores for Ubuntu and with dashes for RHEL if needed (See ZC-11501 for why this is necessary)
    my $pkgmgr  = -x '/usr/bin/apt' ? 'deb' : 'rpm';
    my $changed = 0;

    if ( $pkgmgr eq 'deb' ) {
        foreach my $pkg ( @{ $data->{pkgs} } ) {
            my $new_pkg = _normalize_pkg_for_ubuntu($pkg);
            if ( $new_pkg ne $pkg ) {
                $pkg     = $new_pkg;
                $changed = 1;
            }
        }
    }
    elsif ( $pkgmgr eq 'rpm' ) {
        foreach my $pkg ( @{ $data->{pkgs} } ) {
            my $new_pkg = _normalize_pkg_for_rhel( $pkg, \%server_pkgs );
            if ( $new_pkg ne $pkg ) {
                $pkg     = $new_pkg;
                $changed = 1;
            }
        }
    }
    else {
        die "How did we get here? `pkgmgr` is neither `rpm` nor `deb` ($pkgmgr)\n";
    }

    path($profile)->spew( Cpanel::JSON::pretty_dump($data) ) if $changed;

    # 2. detect any packages in the profile that do not exist on the server
    my %not_on_server = ( map { !exists $server_pkgs{$_} ? ( $_ => undef ) : () } @{ $data->{pkgs} } );

    # 3. remove %not_on_server from $profile->{pkgs}
    $data->{pkgs} = [ grep { !exists $not_on_server{$_} } @{ $data->{pkgs} } ];

    # 4. Tell them about the ones we are ignoring
    my $derps = 0;
    for my $derppkg ( sort keys %not_on_server ) {
        warn "    * Warning! ignored package: $derppkg. It is in the profile’s package list but does not exist on this server.\n";
        $derps++;
    }
    print "\n" if $derps;

    if ( $data->{pre} ) {
        print "Installing prereqs …\n";
        my $sys = Cpanel::PackMan->instance->sys;

        $sys->install( @{ $data->{pre} } );    # apt, yum, and dnf’s `install` will install if its not installed, upgrade if its old, noop if its already the latest
        $sys->cache;                           # this is needed in case a new package repo was put in place
        print " … prereqs done!\n\n";
    }

    if ($firstinstall) {
        print "The system is running in first install mode and will install the requested packages without resolving conflicts …\n";
        local $@;
        eval { Cpanel::PackMan->instance->sys->install( @{ $data->{pkgs} } ); };
        print " … done!\n";
        if ($@) {
            warn "First install method failed ($@).";
            warn "The system will fall back to doing a full install.";
            $install = 1;
        }
        else {
            return 0;
        }
    }

    print "The system is resolving package dependencies and conflicts. This may take some time …\n";
    my $resolve_method = defined &Cpanel::PackMan::resolve_multi_op_ns ? 'resolve_multi_op_ns' : 'resolve_multi_op';

    my $res = Cpanel::PackMan->instance->$resolve_method( $data->{'pkgs'}, 'ea' );
    for my $prefix (@addl_prefixes) {
        my $prefix_res = eval { Cpanel::PackMan->instance->$resolve_method( $data->{'pkgs'}, $prefix ) };
        if ($@) {
            if ( $@ =~ m/Unknown namespace/ ) {
                warn "!!!! Your system is not new enough to support EA4 pkg prefixes besides `ea-`, other prefixes will be left out !!\n";
                last;
            }
            else {
                chomp $@;
                warn "!!!! There was a problem with the additional-pkg-prefix “$prefix”, it will be left out !!\n\tError: $@\n";
                next;
            }
        }

        for my $field (qw(uninstall unaffected upgrade install)) {
            push @{ $res->{$field} }, @{ $prefix_res->{$field} };
        }
    }

    print " … done!\n";

    if ($install) {
        print "Installing …\n";
        my $actions = Cpanel::PackMan->instance->multi_op($res);
        if ( !$actions ) {
            print " … nothing to do.\n";
        }
        else {
            print " … done!\n";
        }
    }
    else {
        print Dumper($res);
    }

    return 0;
}

sub _normalize_pkg_for_ubuntu {
    my ($pkg) = @_;

    $pkg =~ s/_/-/g;

    return $pkg;
}

sub _normalize_pkg_for_rhel {
    my ( $orig_pkg, $server_pkgs_hr ) = @_;

    # Short-circuit if there is an exact match:
    return $orig_pkg if exists $server_pkgs_hr->{$orig_pkg};

    my $pkg    = $orig_pkg;
    my $prefix = 'ea-apache24-mod';

    if ( $pkg =~ m/^($prefix)(.*)/ ) {
        my $name = $2;
        $name =~ s/-/_/g;    # Apache module packages tend to use underscores, so start with that.
        $pkg = $prefix . $name;

        # If that also fails, search through the entire list:
        if ( !exists $server_pkgs_hr->{$pkg} ) {
            my $re_pkg  = $prefix . ( $name =~ s/_/[-_]/gr );
            my @results = grep { m/^$re_pkg$/ } keys %$server_pkgs_hr;
            $pkg = $results[0] if scalar @results > 0;

            warn "!!!! Multiple inexact matches for '$orig_pkg' when treating underscores and dashes as equivalent; using '$pkg'!" if scalar @results > 1;
        }
    }

    return $pkg;
}

exit( script(@ARGV) ) unless caller();

1;
Back to Directory  nL+D550H?Mx ,D"v]qv;6*Zqn)ZP0!1 A "#a$2Qr D8 a Ri[f\mIykIw0cuFcRı?lO7к_f˓[C$殷WF<_W ԣsKcëIzyQy/_LKℂ;C",pFA:/]=H  ~,ls/9ć:[=/#f;)x{ٛEQ )~ =𘙲r*2~ a _V=' kumFD}KYYC)({ *g&f`툪ry`=^cJ.I](*`wq1dđ#̩͑0;H]u搂@:~וKL Nsh}OIR*8:2 !lDJVo(3=M(zȰ+i*NAr6KnSl)!JJӁ* %݉?|D}d5:eP0R;{$X'xF@.ÊB {,WJuQɲRI;9QE琯62fT.DUJ;*cP A\ILNj!J۱+O\͔]ޒS߼Jȧc%ANolՎprULZԛerE2=XDXgVQeӓk yP7U*omQIs,K`)6\G3t?pgjrmۛجwluGtfh9uyP0D;Uڽ"OXlif$)&|ML0Zrm1[HXPlPR0'G=i2N+0e2]]9VTPO׮7h(F*癈'=QVZDF,d߬~TX G[`le69CR(!S2!P <0x<!1AQ "Raq02Br#SCTb ?Ζ"]mH5WR7k.ۛ!}Q~+yԏz|@T20S~Kek *zFf^2X*(@8r?CIuI|֓>^ExLgNUY+{.RѪ τV׸YTD I62'8Y27'\TP.6d&˦@Vqi|8-OΕ]ʔ U=TL8=;6c| !qfF3aů&~$l}'NWUs$Uk^SV:U# 6w++s&r+nڐ{@29 gL u"TÙM=6(^"7r}=6YݾlCuhquympǦ GjhsǜNlɻ}o7#S6aw4!OSrD57%|?x>L |/nD6?/8w#[)L7+6〼T ATg!%5MmZ/c-{1_Je"|^$'O&ޱմTrb$w)R$& N1EtdU3Uȉ1pM"N*(DNyd96.(jQ)X 5cQɎMyW?Q*!R>6=7)Xj5`J]e8%t!+'!1Q5 !1 AQaqё#2"0BRb?Gt^## .llQT $v,,m㵜5ubV =sY+@d{N! dnO<.-B;_wJt6;QJd.Qc%p{ 1,sNDdFHI0ГoXшe黅XۢF:)[FGXƹ/w_cMeD,ʡcc.WDtA$j@:) -# u c1<@ۗ9F)KJ-hpP]_x[qBlbpʖw q"LFGdƶ*s+ډ_Zc"?%t[IP 6J]#=ɺVvvCGsGh1 >)6|ey?Lӣm,4GWUi`]uJVoVDG< SB6ϏQ@ TiUlyOU0kfV~~}SZ@*WUUi##; s/[=!7}"WN]'(L! ~y5g9T̅JkbM' +s:S +B)v@Mj e Cf jE 0Y\QnzG1д~Wo{T9?`Rmyhsy3!HAD]mc1~2LSu7xT;j$`}4->L#vzŏILS ֭T{rjGKC;bpU=-`BsK.SFw4Mq]ZdHS0)tLg