Procházet zdrojové kódy

Update alternatives

Andy Baugh před 5 roky
rodič
revize
ad841aa8ce
1 změnil soubory, kde provedl 49 přidání a 68 odebrání
  1. 49 68
      lib/Troglodyne/API/Postgres.pm

+ 49 - 68
lib/Troglodyne/API/Postgres.pm

@@ -95,23 +95,11 @@ sub start_postgres_install {
         }
 
         require Cpanel::SafeRun::Object;
-        my $run_result = Cpanel::SafeRun::Object->new(
-            'program' => 'yum',
-            'args'    => [ qw{install -y}, @RPMS ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        my $exit = $run_result->error_code() || 0;
+        my $exit = _saferun( $lh, 'yum', qw{install -y}, @RPMS );
         return _cleanup("$exit") if $exit;
 
         # Init the DB
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "/usr/pgsql-$ver2install/bin/initdb",
-            'args'    => [ '-D', "/var/lib/pgsql/$ver2install/data/" ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        $exit = $run_result->error_code() || 0;
+        $exit = _saferun( $lh, "/usr/pgsql-$ver2install/bin/initdb", '-D', "/var/lib/pgsql/$ver2install/data/" );
         return _cleanup("$exit") if $exit;
 
         require File::Slurper;
@@ -136,28 +124,16 @@ sub start_postgres_install {
         # Upgrade the cluster
         # /usr/pgsql-9.6/bin/pg_upgrade --old-datadir /var/lib/pgsql/data/ --new-datadir /var/lib/pgsql/9.6/data/ --old-bindir /usr/bin/ --new-bindir /usr/pgsql-9.6/bin/
         my ( $old_datadir, $old_bindir ) = ( $str_ver + 0 < 9.5 ) ? ( '/var/lib/pgsql/data', '/usr/bin' ) : ( "/var/lib/pgsql/$str_ver/data/", "/usr/pgsql-$str_ver/bin/" );
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "/usr/pgsql-$ver2install/bin/pg_upgrade",
-            'args'    => [
+        $exit = _saferun( $lh, "/usr/pgsql-$ver2install/bin/pg_upgrade",
                 '--old-datadir', $old_datadir,
                 '--new-datadir', "/var/lib/pgsql/$ver2install/data/",
                 '--old-bindir', $old_bindir,
                 '--new_bindir', "/usr/pgsql-$ver2install/bin/",
-            ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
         );
-        $exit = $run_result->error_code() || 0;
         return _cleanup("$exit") if $exit;
 
         # Start the server.
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "systemctl",
-            'args'    => [ 'start', "postgresql-$ver2install" ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        $exit = $run_result->error_code() || 0;
+        $exit = _saferun( $lh, qw{systemctl start} "postgresql-$ver2install" );
         return _cleanup("$exit") if $exit;
 
         if( $ccs_installed ) {
@@ -168,27 +144,20 @@ sub start_postgres_install {
             mkdir($ccs_pg_datadir);
 
             # Init the DB
-            $run_result = Cpanel::SafeRun::Object->new(
-                'program' => "/usr/pgsql-$ver2install/bin/initdb",
-                'args'    => [ '-D', $ccs_pg_datadir ],
-                'stdout'  => $lh,
-                'stderr'  => $lh,
-            );
-            $exit = $run_result->error_code() || 0;
-            return _cleanup("$exit") if $exit;
+            {
+                local $ENV{'PGSETUP_INITDB_OPTIONS'} = "-U caldav --locale=C -E=UTF8";
+                $exit = _saferun( $lh, "/usr/pgsql-$ver2install/bin/initdb", '-D', $ccs_pg_datadir );
+                return _cleanup("$exit") if $exit;
+            }
 
-            $run_result = Cpanel::SafeRun::Object->new(
-                'program' => "/usr/pgsql-$ver2install/bin/pg_upgrade",
-                'args'    => [
-                    '--old-datadir', "$ccs_pg_datadir.old",
-                    '--new-datadir', $ccs_pg_datadir,
-                    '--old-bindir', $old_bindir,
-                    '--new_bindir', "/usr/pgsql-$ver2install/bin/",
-                ],
-                'stdout'  => $lh,
-                'stderr'  => $lh,
+            # Upgrade the DB
+            $exit = _saferun( $lh, "/usr/pgsql-$ver2install/bin/pg_upgrade",
+                '--old-datadir', "$ccs_pg_datadir.old",
+                '--new-datadir', $ccs_pg_datadir,
+                '--old-bindir', $old_bindir,
+                '--new_bindir', "/usr/pgsql-$ver2install/bin/",
+                qw{-c -U caldav},
             );
-            $exit = $run_result->error_code() || 0;
             return _cleanup("$exit") if $exit;
         }
 
@@ -202,33 +171,33 @@ sub start_postgres_install {
 
         print $lh "\n\nNow cleaning up old postgresql version...\n";
         my $svc2remove = ( $str_ver + 0 < 9.5 ) ? 'postgresql' : "postgresql-$str_ver";
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "systemctl",
-            'args'    => [ 'disable', $svc2remove ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        $exit = $run_result->error_code() || 0;
+        $exit = _saferun( $lh, qw{systemctl disable}, $svc2remove );
         return _cleanup("$exit") if $exit;
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "yum",
-            'args'    => [ '-y', 'remove', $svc2remove ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        $exit = $run_result->error_code() || 0;
+        $exit = _saferun( $lh, qw{yum -y remove}, $svc2remove );
         return _cleanup("$exit") if $exit;
 
         print $lh "\n\nNow enabling postgresql-$ver2install on startup...\n";
-        $run_result = Cpanel::SafeRun::Object->new(
-            'program' => "systemctl",
-            'args'    => [ 'enable', "postgresql-$ver2install" ],
-            'stdout'  => $lh,
-            'stderr'  => $lh,
-        );
-        $exit = $run_result->error_code() || 0;
+        $exit = _saferun( $lh, qw{systemctl enable}, "postgresql-$ver2install" );
         return _cleanup("$exit") if $exit;
 
+        # Update alternatives. Should be fine to use --auto, as no other alternatives will exist for the installed version.
+        # Create alternatives for pg_ctl, etc. as those don't get made by the RPM.
+        print $lh "\n\nUpdating alternatives to ensure the newly installed version is considered canonical...\n";
+        my @normie_alts = qw{pg_ctl initdb pg_config pg_upgrade};
+        my @manual_alts = qw{clusterdb createdb createuser dropdb droplang dropuser pg_basebackup pg_dump pg_dumpall pg_restore psql psql-reindexdb vaccumdb};
+        foreach my $alt ( @normie_alts ) {
+            $exit = _saferun( $lh, qw{update-alternatives --install}, "/usr/bin/$alt", "pgsql-$alt", "/usr/pgsql-$ver2install/bin/$alt", "50" );
+            return _cleanup("$exit") if $exit;
+            $exit = _saferun( $lh, qw{update-alternatives --auto}, "pgsql-$alt" );
+            return _cleanup("$exit") if $exit;
+        }
+        foreach my $alt ( @manual_alts ) {
+            $exit = _saferun( $lh, qw{update-alternatives --auto}, "pgsql-$alt" );
+            return _cleanup("$exit") if $exit;
+            $exit = _saferun( $lh, qw{update-alternatives --auto}, "pgsql-${alt}man" );
+            return _cleanup("$exit") if $exit;
+        }
+
         print $lh "\n\nWriting new .bash_profile for the 'postgres' user...\n";
         my $bash_profile = "[ -f /etc/profile ] && source /etc/profile
 PGDATA=/var/lib/pgsql/$ver2install/data
@@ -254,6 +223,18 @@ export PATH=\$PATH:/usr/pgsql-$ver2install/bin\n";
     };
 }
 
+sub _saferun {
+    my ( $lh, $prog, @args ) = @_;
+    my $run_result = Cpanel::SafeRun::Object->new(
+        'program' => $prog,
+        'args'    => [ @args ],
+        'stdout'  => $lh,
+        'stderr'  => $lh,
+    );
+    my $exit = $run_result->error_code() || 0;
+    return $exit;
+}
+
 sub _cleanup {
     eval { symlink( $_[0], "$dir/INSTALL_EXIT_CODE" ); };
     unlink("$dir/INSTALL_IN_PROGRESS");