CHips L MINI SHELL

CHips L pro

Current Path : /home/thomas/public_html/Fox-C404/root/scripts/
Upload File :
Current File : /home/thomas/public_html/Fox-C404/root/scripts/installpostgres

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/installpostgres                 Copyright 2019 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::installpostgres;

use strict;
use warnings;

use Cpanel::SafeRun::Simple       ();
use Cpanel::Sys::OS::Check        ();
use Cpanel::SysPkgs               ();
use Cpanel::Chkservd::Manage      ();
use Cpanel::PostgresUtils         ();
use Cpanel::PostgresUtils::PgPass ();
use Whostmgr::Postgres            ();

run(@ARGV) unless caller();

sub prompt {
    my ( $message, @args ) = @_;

    chomp $message;
    print $message;

    if ( !@args || $args[0] !~ /\-\-yes/ ) {
        my $res = <STDIN>;
        if ( $res !~ /^y/i ) {
            print "Exiting...\n";
            exit();
        }
    }

    return;
}

sub run {
    my @args = @_;

    if ( -d "/root/.cpupdates" ) {
        system("rm -f /root/.cpupdates/*");
    }

    my $minversion = Cpanel::Sys::OS::Check::has_systemd() ? q{9.2.x} : q{7.4.x};

    my $restart_instruction;
    if ( Cpanel::Sys::OS::Check::has_systemd() ) {
        $restart_instruction = "\tsystemctl restart postgresql";
    }
    else {
        $restart_instruction = "\t/sbin/service postgresql stop\n" . "\t/sbin/service postgresql start\n";
    }

    if ( my $old_pg_home = Cpanel::PostgresUtils::find_pgsql_home() ) {
        my $notice = <<"EOS";
This script installs PostgreSQL $minversion or later.
If your system runs an older version of PostgreSQL, you must
dump your databases to a file, and then restore them after
installation.
PostgreSQL $minversion is NOT backwards compatible.
If no PostgreSQL databases exist on your system, run the
following command to force the creation of a PostgreSQL $minversion -style
setup:
\tmv $old_pg_home $old_pg_home.old
$restart_instruction
Do not run this command if databases exist that you wish to keep!
Are you certain that you wish to proceed? [y/(n)]:
EOS

        prompt( $notice, @args );
    }
    else {
        my $notice = <<"EOS";
This script installs PostgreSQL $minversion or later.
Are you certain that you wish to proceed? [y/(n)]:
EOS

        prompt( $notice, @args );
    }

    print "Installing Postgres...\n";

    my $syspkgobj = Cpanel::SysPkgs->new();
    if ( !$syspkgobj ) { die print "The system could not create the SysPkgs object.\n"; }

    my @pkgs = ( "rh-postgresql", "rh-postgresql-devel", "rh-postgresql-libs", "rh-postgresql-server", "postgresql", "postgresql-devel", "postgresql-libs", "postgresql-server" );
    $syspkgobj->install( 'pkglist' => \@pkgs );

    my $pg_user = Cpanel::PostgresUtils::PgPass::getpostgresuser();
    my $pg_home = Cpanel::PostgresUtils::find_pgsql_home( 'user' => $pg_user );
    my $pg_data = Cpanel::PostgresUtils::find_pgsql_data( 'home' => $pg_home );

    if ( Cpanel::Sys::OS::Check::has_systemd() ) {
        if ( !-e "$pg_data/global" ) {
            Cpanel::SafeRun::Simple::saferunnoerror( '/usr/bin/postgresql-setup', 'initdb' );
        }
        Cpanel::SafeRun::Simple::saferunnoerror( '/usr/local/cpanel/scripts/setpostgresconfig', '--force' );
    }
    else {
        if ( !-e "/etc/rc.d/init.d/postgresql" && -e "/etc/rc.d/init.d/rhdb" ) {
            symlink( "rhdb", "/etc/rc.d/init.d/postgresql" );
        }

        if ( !-e "$pg_data/global" ) {

            Cpanel::SafeRun::Simple::saferunnoerror(qw{/sbin/service postgresql initdb});
            Cpanel::SafeRun::Simple::saferunnoerror( '/usr/local/cpanel/scripts/setpostgresconfig', '--force' );
        }
    }

    Cpanel::SafeRun::Simple::saferun( '/usr/local/cpanel/scripts/cpservice', 'postgresql', 'enable', '35' );

    my ( $status, $message );
    ( $status, $message ) = Whostmgr::Postgres::update_config();
    print $message. "\n";

    Cpanel::SafeRun::Simple::saferunnoerror('/usr/local/cpanel/scripts/restartsrv_postgres');
    my $install_ok = $? == 0 ? 1 : 0;

    if ($install_ok) {

        ( $status, $message ) = Whostmgr::Postgres::create_dbowners_for_cpusers();
        print $message. "\n";

        $install_ok &&= $status;

        if ($install_ok) {

            print q{

The PostgreSQL packages successfully installed. To configure PostgreSQL,
set your password, and enable PostgreSQL for user accounts, navigate to
WHM's Configure PostgreSQL interface (Home >> SQL Services >> Configure PostgreSQL).

};
            print "Setting a random password for the PostgreSQL database user.\n";

            Cpanel::SafeRun::Simple::saferun( '/usr/local/cpanel/bin/postgrescheck', '--check-auth', '--reset-pass-on-fail' );

            Cpanel::SafeRun::Simple::saferun('/usr/local/cpanel/bin/cpsessetup');

            # If PostgreSQL has never been listed under chkservd, enable monitoring for it.
            # (This might happen if the service was uninstalled manually but reinstalled with this script.)
            # Doing this prevents /scripts/check_unmonitored_enabled_services from complaining to the admin
            # who is installing PostgreSQL for the first time on this system.
            my %MONITORED = Cpanel::Chkservd::Manage::getmonitored();
            Cpanel::Chkservd::Manage::enable('postgresql') unless exists $MONITORED{'postgresql'};
        }
    }

    # install_ok value can change in the previous block
    if ( !$install_ok ) {
        print STDERR qq{The PostgreSQL installation failed.\n};
    }

    system '/usr/local/cpanel/bin/build_global_cache';    # reset haspostgres

    exit( $install_ok ? 0 : 1 );
}

1;

Copyright 2K16 - 2K18 Indonesian Hacker Rulez