Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 181 lines (147 sloc) 5.187 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#!/usr/bin/perl -w

=head1 Name

dbprobe_pg.pl - installation script to probe PostgreSQL configuration

=head1 Description

This script is called during "make" to probe the PostgreSQL
configuration. It accomplishes this by parsing the output from
pg_config and asking the user questions. Output collected in
"database.db".

=head1 Author

Sam Tregar <stregar@about-inc.com>

=head1 See Also

L<Bric::Admin>

=cut

use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
use Bric::Inst qw(:all);
use File::Spec::Functions;
use Data::Dumper;

# check whether questions should be asked
my $QUIET = ($ARGV[0] and $ARGV[0] eq 'QUIET') || $ENV{DEVELOPER};

print "\n\n==> Probing PostgreSQL Configuration <==\n\n";

our %DB;

my $passwordsize = 10;
my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
my $randpassword = join '', map $alphanumeric[rand @alphanumeric], 0..$passwordsize;

our $REQ;
do "./required.db" or die "Failed to read required.db : $!";

# setup some defaults
$DB{db_type} = $REQ->{DB_TYPE};
$DB{root_user} = get_default('PG_SUPERUSER') || 'postgres';
$DB{root_pass} = $ENV{PG_SUPERPASS} || get_default( 'PG_SUPERPASS') || '';
$DB{sys_user} = get_default('PG_BRICUSER') || 'bric';
$DB{sys_pass} = $QUIET ? $randpassword : 'NONE';
$DB{db_name} = get_default('PG_DB') || 'bric';
$DB{host_name} = $ENV{PG_HOSTNAME} || get_default('PG_HOSTNAME') || '';
$DB{host_port} = $ENV{PG_HOSTPASS} || get_default('PG_HOSTPASS') || '';
$DB{version} = '';

get_include_dir();
get_lib_dir();
get_bin_dir();
get_psql();
get_version();
get_host();
get_users();

# all done, dump out postgresql database, announce success and exit
open(OUT, ">database.db") or die "Unable to open database.db : $!";
print OUT Data::Dumper->Dump([\%DB],['DB']);
close OUT;

print "\n\n==> Finished Probing PostgreSQL Configuration <==\n\n";
exit 0;


sub get_include_dir {
    print "Extracting postgres include dir from $REQ->{PG_CONFIG}.\n";

    my $data = `$REQ->{PG_CONFIG} --includedir`;
    hard_fail("Unable to extract needed data from $REQ->{PG_CONFIG}.")
    unless $data;
    chomp($data);
    $DB{include_dir} = $data;
}

sub get_lib_dir {
    print "Extracting postgres lib dir from $REQ->{PG_CONFIG}.\n";

    my $data = `$REQ->{PG_CONFIG} --libdir`;
    hard_fail("Unable to extract needed data from $REQ->{PG_CONFIG}.")
    unless $data;
    chomp($data);
    $DB{lib_dir} = $data;
}

sub get_bin_dir {
    print "Extracting postgres bin dir from $REQ->{PG_CONFIG}.\n";

    my $data = `$REQ->{PG_CONFIG} --bindir`;
    hard_fail("Unable to extract needed data from $REQ->{PG_CONFIG}.")
    unless $data;
    chomp($data);
    $DB{bin_dir} = $data;
}

sub get_psql {
    print "Finding psql.\n";
    my $psql = catfile($DB{bin_dir}, 'psql');
    hard_fail("Unable to locate psql executable.")
    unless -e $psql and -x $psql;
    $DB{exec} = $psql;
}

sub get_version {
    print "Finding PostgreSQL version.\n";
    my $data = `$REQ->{PG_CONFIG} --version`;
    hard_fail("Unable to extract needed data from $REQ->{PG_CONFIG}.")
      unless $data;
    chomp $data;
    $data =~ s/\s*PostgreSQL\s+(\d\.\d(\.\d)?).*/$1/;
    $DB{version} = $data;
}

# ask the user for user settings
sub get_users {
    print "\n";
    $DB{create_db} = ask_yesno(
        'Should the installer connect to the database as a super user?',
        get_default('CREATE_DB') || 1,
        $QUIET
    );
    if ($DB{create_db}) {
        print "\n";
        ask_confirm('Postgres Super Username', \$DB{root_user}, $QUIET);
        ask_password(qq{Password for super user "$DB{root_user}"}, \$DB{root_pass}, $QUIET);

        unless ($DB{host_name}) {
            print "\n";
            print "Should the installer become the Postgres system user?\n";
            if ( ask_yesno(
                'This requires that the installer be run as root.',
                get_default('PG_BECOME_USER') || 0,
                $QUIET,
            )) {
                $DB{system_user} = $DB{root_user};
                while(1) {
                    ask_confirm('Postgres System Username', \$DB{system_user}, $QUIET);
                    $DB{system_user_uid} = (getpwnam($DB{system_user}))[2];
                    last if defined $DB{system_user_uid};
                    print "User \"$DB{system_user}\" not found! This user must exist ".
                        "on your system.\n";
                }
            }
        }
    }

    while(1) {
        ask_confirm("Bricolage Postgres Username", \$DB{sys_user}, $QUIET);
        if ($DB{sys_user} eq $DB{root_user}) {
            print "Bricolage Postgres User cannot be the same as the Postgres Root User.\n";
        } else {
            last;
        }
    }

    ask_password(qq{Password for Postgres user "$DB{sys_user}"}, \$DB{sys_pass}, $QUIET);
    ask_confirm("Bricolage Database Name", \$DB{db_name}, $QUIET);
}

# ask for host specifics
sub get_host {
    print "\n";
    ask_confirm(
        "Postgres Database Server Hostname (default is unset, i.e., localhost)",
        \$DB{host_name},
        $QUIET,
    );
    ask_confirm(
        "Postgres Database Server Port Number (default is unset, i.e., 5432)",
        \$DB{host_port},
        $QUIET,
    );
}
Something went wrong with that request. Please try again.