[Bio] / FigCommon / envsub.postgres.pl Repository:
ViewVC logotype

View of /FigCommon/envsub.postgres.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (download) (as text) (annotate)
Mon Dec 5 19:10:06 2005 UTC (13 years, 11 months ago) by olson
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, rast_rel_2008_06_18, rast_rel_2008_06_16, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2008_07_21, rast_rel_2010_0928, rast_2008_0924, mgrast_version_3_2, mgrast_dev_12152011, rast_rel_2008_04_23, help, mgrast_dev_06072011, rast_rel_2008_09_30, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, caBIG-05Apr06-00, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, rast_rel_2011_0119, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, rast_rel_2008_10_09, mgrast_dev_04012011, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, rast_rel_2010_0827, mgrast_rel_2008_1110, myrast_33, rast_rel_2011_0928, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, mgrast_dev_04052011, mgrast_dev_02222011, caBIG-13Feb06-00, rast_rel_2009_03_26, mgrast_dev_10262011, rast_rel_2008_11_24, rast_rel_2008_08_07, HEAD
Changes since 1.9: +17 -0 lines
Added license words.

#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
# 
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License. 
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

#
# Postgres configuration.
#
# Determine the identifier for this seed install.
# For unix-like systems, it's the inode of the directory holding
# the FIGdisk.
#

print "In envsub postgres\n";

#
# Determine figdisk identifier.
#

my $fig_ident;

my @s = stat($fig_disk);
$fig_ident = $s[1];
if ($fig_ident == 0)
{
    #
    # Stat didn't work. Identify as the pathname.
    #

    $fig_ident = $fig_disk;
    $fig_ident =~ s,\W,,g;
}
print "Have fig_ident=$fig_ident\n";

#
# Create the db directory if necessary
#

my $dbdir = "$ENV{HOME}/FIGdb";
if (! -d $dbdir)
{
    mkdir($dbdir) or die "Could not create database directory $dbdir";
}

#
# Look for initdb in the environment dir, or in the path  if it's not there.
#

my $initdb = "$env_bin/initdb";
if (! -x $initdb)
{
    $initdb = find_in_path("initdb");
}

if (! -x $initdb)
{
    die "Could not find Postgres initialization program 'initdb'.";
}

my $postmaster = "$env_bin/postmaster";
if (! -x $postmaster)
{
    $postmaster = find_in_path("postmaster");
}

if (! -x $postmaster)
{
    die "Could not find Postgres postmaster.";
}

my $pg_ctl = "$env_bin/pg_ctl";
if (! -x $pg_ctl)
{
    $pg_ctl = find_in_path("pg_ctl");
}

if (! -x $pg_ctl)
{
    die "Could not find Postgres pg_ctl.";
}

#
# Configure pgdata directory
#

my $pgdata = "$dbdir/$fig_ident";
    
if (-d $pgdata)
{
    #
    # pg database directory already there. Look to see if it appears
    # to be the right one for this version of postgres.
    #

    my $version;
    
    open(FH, "$initdb --version|") or die "Cannot run $initdb: $!\n";
    $_ = <FH>;
    close(FH);
    chop;
    if (/initdb.* (\d\.\d)/)
    {
	$version = $1;
    }
    else
    {
	warn "Cannot determine postgres version from output: $_\n";
    }
	
    open(FH, "<$pgdata/PG_VERSION");
    my $dbver = <FH>;
    chop($dbver);
    close(FH);

    if ($dbver != $version)
    {
	warn "Existing pgdata directory $pgdata is for the wrong version; moving out of the way.";

	rename($pgdata, "$pgdata.bak.$$");
    }
}

if (! -d $pgdata)
{
    mkdir($pgdata) or die "Could not create database directory $pgdata";

    #
    # Initialize the db directory.
    #
    
    #
    # Find the share directory where postgres.bki lives.
    #

    my @possible_dirs = ("$env_dir/share/postgresql", "$env_dir/share");

    my $share;
    for my $d (@possible_dirs)
    {
	if (-f "$d/postgres.bki")
	{
	    $share = $d;
	    last;
	}
    }

    if (!$share)
    {
	die "Couldn't find postgres shared data directory\n";
    }

    if (!open(IN, "$initdb --locale=C -D $pgdata -L $share |"))
    {
	die "Execution of $initdb failed: $!\n";
    }

    while (<IN>)
    {
	chomp;
	print "Got $_\n";
    }

    if (!close(IN))
    {
	if ($! == 0)
	{
	    #
	    # Nonzero exit code
	    #
	    
	    my $rc = $? >> 8;
	    die "$initdb exited with return code $rc";
	}
	else
	{
	    die "$initdb failed: $!\n";
	}
    }
}

#
# Okay, database initialized.
#
# Update the index file in the figdb dir so people know
# what's going on.
#

if (! -f "$dbdir/README")
{
    open(F, ">$dbdir/README");
    print F <<END;
This is the FIG Postgres data directory. Subdirectories contain
Postgres database clusters for SEED installations.

Cluster directory                SEED directory
END
}
else
{
    open(F, ">>$dbdir/README");
}

my $mywd = `/bin/pwd`;
chop($mywd);
printf F "%-32s %s %s\n", $pgdata, $ENV{FIG_HOME}, $mywd;

close(F);

#
# We should be ready to go. Put the config file directives in.
#

#
# We use the current account as the db user.
#

my $dbuser = (getpwuid($>))[0];
my $dbport = 10000;
my $logfile = "$dbdir/postgres.log";
my $dbname = "fig";
my $sproutDbname = "Sprout";
my $dbbufs = 100;

#
# Compute postgres shared buffer sizes. 
#
# We'd like to have at least 4700 buffers (that seems enough
# with current (jan 2004) data sizes to make the postgres
# backend run in RAM and not page.
#

#
# These routines come from formulas published in the Postgres
# documentation, section 16.5.1:
#
#	http://www.postgresql.org/docs/current/static/kernel-resources.html
# 

sub req_shm
{
    my($bufs, $max_conn) = @_;

    my $shm = 250 *1024 + 8.2 * 1024 * $bufs + 14.2 *1024 * $max_conn;
    return int($shm);
}

sub bufs_from_shm
{
    my($shm, $max_conn) = @_;

    my $bufs = ($shm - 250 * 1024 - 14.2 * 1024 * $max_conn) / (8.2 * 1024);

    return int($bufs);
}

my $physmem = $OtherConfig->{memory};
my $shmmax = $OtherConfig->{shmmax};

if ($physmem != -1 && $shmmax != -1)
{
    my $max_conn = 100; 	# Postgres default.
    my $mem_wanted;

    #
    # Cap shared memory at 25% of physical memory.
    #
    if ($shmmax > (0.25 * $physmem))
    {
	$mem_wanted = int(0.25 * $physmem);
    }
    else
    {
	$mem_wanted = $shmmax;
    }
    my $max_bufs = &bufs_from_shm($mem_wanted, $max_conn);

    #
    # Back it off a bit to make sure it'll start.
    #
    $max_bufs = int(0.95 * $max_bufs);

    print "Computed max_bufs=$max_bufs from physmem=$physmem and shmmax=$shmmax\n";
    $dbbufs = $max_bufs if $max_bufs > 0;
}

#
# This is pretty ugly, but necessary sometimes.
#
# Override defaults with FIGCONFIG_XX
#

if ($ENV{FIGCONFIG_DBNAME} ne "")
{
    $dbname = $ENV{FIGCONFIG_DBNAME};
}

if ($ENV{FIGCONFIG_SPROUT_DBNAME} ne "")
{
    $sproutDbname = $ENV{FIGCONFIG_SPROUT_DBNAME};
}

if ($ENV{FIGCONFIG_DBUSER} ne "")
{
    $dbuser = $ENV{FIGCONFIG_DBUSER};
}

if ($ENV{FIGCONFIG_DBPORT} ne "")
{
    $dbport = $ENV{FIGCONFIG_DBPORT};
}

if ($ENV{FIGCONFIG_DBBUFS} ne "")
{
    $dbbufs = $ENV{FIGCONFIG_DBBUFS};
}

push(@$Config, ["dbms", "Pg", 1]);
push(@$Config, ["db", $dbname, 1]);
push(@$Config, ["dbuser", $dbuser, 1]);
push(@$Config, ["dbpass", "", 1]);
push(@$Config, ["dbport", $dbport, 0]);
push(@$Config, ["sproutDB", $sproutDbname, 1]);

push(@$Env, ["PGDATA", $pgdata]);
push(@$Env, ["PGPORT", $dbport]);

#
# Create the start/stop servers script
#

open(FH, ">>$start_servers");
print FH "$postmaster -B $dbbufs -p $dbport -D $pgdata >$logfile 2>&1 </dev/null &\n";
close(FH);

open(FH, ">>$stop_servers");
print FH "$pg_ctl -D $pgdata -w stop\n";
close(FH);


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3