#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#

#-----------------------------------------------------------------------------

=head1   pgsqlsetup 



 This is script is called after the installation or upgrade of PostgreSQL 
   on xCAT.  It will automate the setup of the PostgreSQL and xCAT to run
   xCAT on the PostgreSQL DB on AIX and Linux.
   On AIX 6.1 or later , It will setup an xcatdb database ,a postgres id, 
   a xcatadm id and password to be used in the /etc/xcat/cfgloc file
   to access the database.
   On Linux , It will setup an xcatdb database , and get an xcatadm password
   to be used in the /etc/xcat/cfgloc file to access the database.
   On Linux,  the xcatadm does not need to be created and postgres is already
   created by the install.
   It will interact for the
   password to assign to the xcatadm userid for the Unix id and PgSQL database 
   id.   
   Setups up AIX 6.1+ and Linux,  but most work needs to be done on AIX. 
   See man pgsqlsetup for more information and the xCAT2SetupPostgresql.pdf 
   documentation. 

=cut

BEGIN
{
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
    $::XCATDIR  = $ENV{'XCATDIR'}  ? $ENV{'XCATDIR'}  : '/etc/xcat';
}

# if AIX - make sure we include perl 5.8.2 in INC path.
#       Needed to find perl dependencies shipped in deps tarball.
if ($^O =~ /^aix/i)
{
    use lib "/usr/opt/perl5/lib/5.8.2/aix-thread-multi";
    use lib "/usr/opt/perl5/lib/5.8.2";
    use lib "/usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-multi";
    use lib "/usr/opt/perl5/lib/site_perl/5.8.2";
}

use lib "$::XCATROOT/lib/perl";
use DBI;
use xCAT::Utils;
use xCAT::NetworkUtils;
use Getopt::Long;
use xCAT::MsgUtils;
use xCAT::Table;
use Expect;
use Socket;

use strict;

#-----------------------------------------------------------------------------
# Main

$::progname = "pgsqlsetup";
my $args = join ' ', @ARGV;
$::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
$::installdir             = "/var/lib/pgsql";         # install location
$::installdatadir         = "/var/lib/pgsql/data";    # install location

# parse the options
if (
    !GetOptions(
                'i|init'    => \$::INIT,
                'o|odbc'    => \$::SETUPODBC,
                'h|help'    => \$::HELP,
                'v|version' => \$::VERSION,
                'V|verbose' => \$::VERBOSE,
    )
  )
{
    &usage;
    exit(1);
}

# display the usage if -h or --help is specified
if ($::HELP)
{
    &usage;
    exit(0);
}

# display the version statement if -v or --version is specified
if ($::VERSION)
{
    my $version = xCAT::Utils->Version();
    xCAT::MsgUtils->message("I", $version);
    exit 0;
}
if ((!($::INIT)) && (!($::SETUPODBC)))
{
    xCAT::MsgUtils->message("I", "Either -i  or -o flag must be chosen");
    &usage;
    exit(1);
}

# check to see if only odbc update,  no passwords needed
my $odbconly = 0;
if ((!($::INIT)) && ($::SETUPODBC))
{
    $odbconly = 1;

}

#
# Get OS
#
if (xCAT::Utils->isAIX())
{
    $::osname = 'AIX';
}
else
{
    $::osname = 'Linux';
}

# determine whether redhat or sles
$::linuxos = xCAT::Utils->osver();

#
# check to see if postgresql is installed
#
my $cmd = "rpm -qa | grep postgresql";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
    my $message =
      "\nPostgreSQL is not installed.  If on AIX, it should be first obtained from the xcat dependency tarballs and installed before running this command.\n If on Linux, install from the OS CDs.";
    xCAT::MsgUtils->message("E", " $cmd failed. $message");
    exit(1);
}

# check to see if  PostgreSQL  is running
$::pgsqlrunning     = 0;
$::xcatrunningpgsql = 0;
my $cmd = "ps -ef | grep postgres";
my @output = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC == 0)    # already running
{
    my $pgsqlcheck = "postgres:";    # see if really running
    if (grep(/$pgsqlcheck/, @output))
    {

        if ($::INIT)
        {
            my $message =
              "PostgreSQL is running.  Database initialization will not take place.";
            xCAT::MsgUtils->message("I", "$message");
        }
        $::pgsqlrunning = 1;
    }
}
if (-e ("/etc/xcat/cfgloc"))    # check to see if xcat is using pgsql
{                               # cfgloc exists
    $cmd = "fgrep Pg /etc/xcat/cfgloc";
    xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC == 0)
    {
        if ($::INIT)
        {
            my $message =
              "The /etc/xcat/cfgloc file is already configured for PostgreSQL. xcat database initialization will not take place.";
            xCAT::MsgUtils->message("I", "$message");
        }
        $::xcatrunningpgsql = 1;
    }
}

#  if not just odbc update
#  Get  admin password
#
if (($odbconly == 0)  && ($::xcatrunningpgsql == 0))
{    # not just updating the odbc  and xcat not already running on pg
    my $msg = "Input the password for xcatadm userid: ";
    xCAT::MsgUtils->message('I', "$msg");
    `stty -echo`;
    chop($::adminpassword = <STDIN>);
    `stty echo`;
}

# initial setup request and not already running pgsql
if (($::INIT) && ($::xcatrunningpgsql == 0))
{


    #
    # Backup current database
    #
    my $homedir = xCAT::Utils->getHomeDir();
    $::backupdir = $homedir;
    if ($::osname eq 'AIX')
    {
        $::backupdir .= "xcat-dbback";
    }
    else
    {
        $::backupdir .= "/xcat-dbback";
    }

    &backupxcatdb;

    # shutdown the xcatd daemon while migrating
    &shutdownxcatd;

    #
    #  Get MN name from site.master in backed up database
    # if that does not exist use resolved hostname
    # double check site.master for resolution
    my $sitefile = "$::backupdir/site.csv";
    my $cmd      = "grep master $sitefile";
    my @output   = xCAT::Utils->runcmd($cmd, -1);
    my $hname;
    if ($::RUNCMD_RC != 0)    # no entry in site table
    {
        $hname = `hostname`;
        chomp $hname;
    }
    else                      # from site.master
    {
        (my $attr, my $master) = split(",", $output[0]);
        (my $q, $hname) = split("\"", $master);
        chomp $hname;

    }

    my $ipaddr = xCAT::NetworkUtils->getipaddr($hname);
    if ($ipaddr)
    {
        $::MN = $ipaddr;
    }
    else
    {
        xCAT::MsgUtils->message("E", "Hostname resolution for $hname failed.");
        exit(1);
    }

    # PostgreSQL not running, then initialize the database
    if ($::pgsqlrunning == 0)
    {

        # Add postgres user and group and xcatadm for AIX
        # create the /var/lib/pgsql data directory and set permissions.
        # All this is done on Linux install automatically:
        #  postgres is created on Linux and xcatadm does not need to be a
        #  real userid on Linux. Also directory permissions are correct on Linux
        #
        if ($::osname eq 'AIX')
        {
            &mkpgusers;
        }

        #
        # Init Pg database and setup pg_hba.conf and postgresql.conf
        #
        &initpgdb;

        #
        # Start Postgresql server
        #
        &pgstart;

        #
        # Setup Postgresql to restart on reboot
        #
        #&pgreboot;

    }

    # if xcat not already configured to run Postgresql add xCAT database
    if ($::xcatrunningpgsql == 0)
    {

        #
        # Create xcatd  database
        # Create xcatadm in the database
        #
        &setupxcatdb;

        #
        # create cfgloc file
        #
        &createcfgloc;

        #
        # Restore backed up database into PostgreSQL
        #
        &restorexcatdb;
    }

    xCAT::MsgUtils->message("I",
                           "xCAT is running on the PostgreSQL database.\n");

}    # end initialization

if ($::SETUPODBC)
{

    #
    #  set up the ODBC on the Management Node
    #

    xCAT::MsgUtils->message("I", "setup ODBC is not supported yet.");

    #&setupODBC;

}

exit;

#####################################
#  subroutines
#####################################

#-----------------------------------------------------------------------------

=head3    usage 
	
	Displays message for -h option

=cut

#-----------------------------------------------------------------------------

sub usage
{
    xCAT::MsgUtils->message(
        'I',
        "Usage:\npgsqlsetup - Performs the setup of PostgreSQL for xCAT to use as its database. See man pgsqlsetup for more information."
        );
    my $msg =
      "pgsqlsetup <-h|--help>\n           <-v|--version>\n           <-i|--init> [-o|--setupODBC] [-V|--verbose]\n           <-o|--setupODBC> [-V|--verbose]";

    xCAT::MsgUtils->message('I', "$msg");
}

#-----------------------------------------------------------------------------

=head3    shutdownxcatd 
	
  shutdown the daemon 

=cut

#-----------------------------------------------------------------------------

sub shutdownxcatd

{
    my $msg = "Shutting down the xcatd daemon during database migration.";
    xCAT::MsgUtils->message('I', "$msg");
    my $xcmd;
    if ($::osname eq 'AIX')
    {
        $xcmd = "stopsrc -s xcatd";

    }
    else
    {
        $xcmd = "service xcatd stop";
    }
    system($xcmd);
}

#-----------------------------------------------------------------------------

=head3    backupxcatdb 
	
   Backup xCATdb 

=cut

#-----------------------------------------------------------------------------

sub backupxcatdb

{


    xCAT::MsgUtils->message(
            "I",
            "Backing up xCAT Database to $::backupdir.\nThis could take several minutes."
            );
    if (!(-e $::backupdir))
    {    # does not exist, make it
            my $cmd = "mkdir -p $::backupdir";
            xCAT::Utils->runcmd($cmd, 0);
            if ($::RUNCMD_RC != 0)
            {
                xCAT::MsgUtils->message("E", " $cmd failed.");
                exit(1);
            }
    }
    else
    {    # remove contents

            my $cmd = "rm -f $::backupdir/*";
            xCAT::Utils->runcmd($cmd, 0);
            if ($::RUNCMD_RC != 0)
            {
                xCAT::MsgUtils->message("E", " $cmd failed.");
                exit(1);
            }
    }

    # back it up
    my $cmd = " dumpxCATdb -p $::backupdir";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
    }

}

#-----------------------------------------------------------------------------

=head3  mkpgusers 
	
         adds postgres user and group and xcatadm id 
         Only needed on AIX,  Linux install does this	
=cut

#-----------------------------------------------------------------------------

sub mkpgusers
{

    #
    # mk postgres group and user
    #
    my $cmd = "lsgroup postgres";
    xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC != 0)
    {

        # postgres group does not exist, need to make it
        $cmd = "mkgroup postgres";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
    }
    $cmd = "lsuser postgres";
    xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC != 0)
    {

        #postgres user does not exist, need to make it
        $cmd = "mkuser pgrp=postgres home=/var/lib/pgsql postgres";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }

    }

    #
    # mk xcatadm user
    #
    $cmd = "lsuser xcatadm";
    xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC != 0)
    {

        #xcatadm user does not exist, need to make it
        $cmd = "mkuser xcatadm";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }

        # set xcatadm id password
        $cmd = qq~echo "xcatadm:$::adminpassword" | /bin/chpasswd -c~;

        # secure passwd in verbose mode
        my $tmpv = $::VERBOSE;
        $::VERBOSE = 0;        
        xCAT::Utils->runcmd($cmd, -1);
        if ($::RUNCMD_RC != 0)
        {

            xCAT::MsgUtils->message("E", " command failed.");
            exit(1);
        }
        $::VERBOSE = $tmpv;
    }

    #
    # Make the postgresql database directory and make postgres owner
    #
    if (!(-e $::installdatadir))
    {    # if it does not already exist
        $cmd = "mkdir $::installdatadir";
        xCAT::Utils->runcmd($cmd, 0);

        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
    }
    $cmd = "chown  postgres $::installdatadir";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {
        xCAT::MsgUtils->message("E", " $cmd failed.");
        exit(1);
    }

    $cmd = "chgrp postgres $::installdatadir";
    xCAT::Utils->runcmd($cmd, 0);

    if ($::RUNCMD_RC != 0)
    {
        xCAT::MsgUtils->message("E", " $cmd failed.");
        exit(1);
    }

}

#-----------------------------------------------------------------------------

=head3   runpostgrescmd  

	
  Run a commmand as the postgres id 
  Input: command 

=cut

#-----------------------------------------------------------------------------
sub runpostgrescmd
{
    my $orgcmd = shift;
    my $cmd =  "\'";
    $cmd .= $orgcmd;
    $cmd .= ' 2>&1';
    $cmd .=  "\'";
    system("su - postgres -c $cmd");


}

#-----------------------------------------------------------------------------

=head3   initpgdb 

	
    Create the PostgreSQL database and setup the config files

=cut

#-----------------------------------------------------------------------------
sub initpgdb
{
    my $cmd;

    # init the database,  must su to postgres
    
    if ($::osname eq 'AIX')
    {
      $cmd = "/var/lib/pgsql/bin/initdb -D /var/lib/pgsql/data ";
    } else {
      $cmd = "initdb -D /var/lib/pgsql/data ";
    }
    &runpostgrescmd($cmd);

    # insert  MN ip address in the # IPv4 local connections: stanza of
    # the /var/lib/pgsql/data/pg_hba.conf file
    # if it is not already there

    my $hbafile = $::installdatadir;
    $hbafile .= "\/pg_hba.conf";
    $cmd = "fgrep  $::MN $hbafile";
    xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC != 0)
    {
        my $insertstr = "host    all          all        ";
        $insertstr .= $::MN;
        $insertstr .= "\/32      md5 ";
        $cmd =
          "awk '{gsub(\"\IPv4 local connections:\",\"\IPv4 local connections:\\n$insertstr \"); print}'   $hbafile > $hbafile.xcat";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {

            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
        $cmd = "cp -p  $hbafile.xcat $hbafile  ";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {

            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
        $cmd = "rm  $hbafile.xcat ";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {

            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
    }

    # setup the postgresql.conf file
    my $pgconf = $::installdatadir;
    $pgconf .= "\/postgresql.conf";
    my $str = "\"setup by xCAT\"";
    $cmd = "fgrep $str $pgconf";
    my @output = xCAT::Utils->runcmd($cmd, -1);
    if ($::RUNCMD_RC != 0)    #  not setup
    {
        $cmd = "echo \"#setup by xCAT\" >> $pgconf";
        `$cmd`;
        if ($::osname eq 'Linux')
        {
            $cmd = qq~ echo listen_addresses = \\'*\\' >> $pgconf~;
            `$cmd`;
        }

        # only for AIX, already setup for Linux
        if ($::osname eq 'AIX')
        {
            $cmd = qq~ echo listen_addresses = \\'$::MN\\' >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo logging_collector = on >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_directory = \\'pg_log\\' >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_filename = \\'postgresql-%a.log\\' >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_truncate_on_rotation = on >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_rotation_age = 1d >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_rotation_size = 0 >> $pgconf~;
            `$cmd`;
            $cmd = qq~echo log_min_messages = notice >> $pgconf~;
            `$cmd`;
        }
    }

    # make sure everything in /var/lib/pgsql/data is owned by postgres
    $cmd = "chown postgres /var/lib/pgsql/data/*";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {

        xCAT::MsgUtils->message("E", " $cmd failed.");
    }
    $cmd = "chgrp postgres /var/lib/pgsql/data/* ";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {

        xCAT::MsgUtils->message("E", " $cmd failed.");
    }

}

#-----------------------------------------------------------------------------

=head3   pgstart 

	
    Start the Postgresql server 

=cut

#-----------------------------------------------------------------------------

sub pgstart
{
    my $cmd;
    xCAT::MsgUtils->message("I", "Starting the PosgreSQL Server");
    if ($::osname eq 'AIX')
    {

        # must su to postgres id,  root cannot start postgesql
        $cmd = "/var/lib/pgsql/bin/pg_ctl -D /var/lib/pgsql/data  start";
        &runpostgrescmd($cmd);
    }
    else    # linux
    {
        $cmd = "service postgresql start";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
            exit(1);
        }
    }

}

#-----------------------------------------------------------------------------

=head3   pgreboot 

	
    Setup for postgresql to start on reboot 

=cut

#-----------------------------------------------------------------------------

sub pgreboot
{
    my $cmd;
    if ($::osname eq 'AIX')
    {
        xCAT::MsgUtils->message(
            "I",
            " Autosetup on AIX to start Postgresql on reboot not supported yet."
            );

    }
    else    # linux
    {
        $cmd = "chkconfig postgresql on";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E",
                        " $cmd failed. PostgreSQL will not restart on reboot.");
        }
    }

}

#-----------------------------------------------------------------------------

=head3    setupxcatdb 
	
      Creates the xcatdb in Postgresql 
      Add xcatadm to the database and make owner of xcatdb

=cut

#-----------------------------------------------------------------------------

sub setupxcatdb

{
    my $cmd;

    # must su to postgres id
    xCAT::MsgUtils->message(
        "I",
        "Creating xcatadm in the Pg database, when prompted enter same password as was assigned to the xcatadm user id."
        );
    if ($::osname eq 'AIX')
    {
      $cmd = "/var/lib/pgsql/bin/createuser -SDRP xcatadm ";
    } else {
      $cmd = "createuser -SDRP xcatadm ";
    }
    &runpostgrescmd($cmd);
    if ($::osname eq 'AIX')
    {
      $cmd = "/var/lib/pgsql/bin/createdb -O xcatadm xcatdb ";
    } else {
      $cmd = "createdb -O xcatadm xcatdb ";
    }
    &runpostgrescmd($cmd);

}

#-----------------------------------------------------------------------------

=head3   setupODBC 
	
         Will setup the ODBC. Only needed if C, C++ applications are running
		 that need access to the PG database for example LoadLeveler.
		 This code is not supported yet and must be modified for PostgreSQL

=cut

#-----------------------------------------------------------------------------

sub setupODBC

{

    #
    # check to see if correct rpms are installed
    #
    # for all OS need unixODBC rpm
    my $cmd = "rpm -qa | grep unixODBC";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {
        my $message =
          "\nunixODBC is not installed.  If on AIX, it should be first obtained from the xcat dependency tarballs and installed before we can setup the ODBC. If on Linux, install from the OS CDs.";
        xCAT::MsgUtils->message("E", " $cmd failed. $message");
        exit(1);
    }
}

#-----------------------------------------------------------------------------

=head3   createcfgloc 
	
		 Creates the cfgloc file which will be copied to cfgloc
                 to run xCAT on PostgreSQL 

=cut

#-----------------------------------------------------------------------------

sub createcfgloc

{
    my $cfgloc       = "/etc/xcat/cfgloc";
    my $cfglocbackup = "/etc/xcat/cfgloc.xcat.backup";
    my $cmd;
    my $message;

    # if they had an old cfgloc , save it
    if ((-e ($cfgloc)) && (!(-e ($cfglocbackup))))
    {
        $cmd = "mv $cfgloc $cfglocbackup";
        xCAT::Utils->runcmd($cmd, 0);
        if ($::RUNCMD_RC != 0)
        {
            xCAT::MsgUtils->message("E", " $cmd failed.");
        }
    }
    my $pgentry = "Pg:dbname=xcatdb;host=$::MN|xcatadm|$::adminpassword";
    $cmd = "echo \"$pgentry\" > $cfgloc";

    # secure passwd in verbose mode
    my $tmpv = $::VERBOSE;
    $::VERBOSE = 0;    
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {
        $message = "command failed. Could not setup cfgloc";
        xCAT::MsgUtils->message("E", "$message");
        exit(1);

    }
    $::VERBOSE = $tmpv;

    # allow readonly by root
    chmod 0600, $cfgloc;

}

#-----------------------------------------------------------------------------

=head3   restorexcatdb 
	   
		Restores the database from ~/xcat-dbback and restarts the xcatd using
		MySQL
	
=cut

#-----------------------------------------------------------------------------

sub restorexcatdb
{
    # set the env variable for Table.pm for the new database
    my $xcatcfg;
    my $cfgl;
    open($cfgl,"<","/etc/xcat/cfgloc");
    $xcatcfg = <$cfgl>;
    close($cfgl);
    chomp($xcatcfg);
 
    # restore the database
    xCAT::MsgUtils->message(
        "I",
        "Restoring the xCat Database with $::backupdir to PostgreSQL database.\nThis could take several minutes."
        );
    if (!(-d $::backupdir))
    {    # does not exist, error
        xCAT::MsgUtils->message("E",
                       " $::backupdir is missing. Cannot retore the database.");
        exit(1);
    }

    # restore it
    my $cmd = "XCATBYPASS=1 XCATCFG=\"$xcatcfg\" restorexCATdb -p $::backupdir";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {
        xCAT::MsgUtils->message("E", " $cmd failed.");
        exit(1);
    }

    #
    # start the daemon
    #
    my $xcmd;
    if ($::osname eq 'AIX')
    {
        $xcmd = "startsrc -s xcatd";

    }
    else
    {
        $xcmd = "service xcatd start";
    }
    system($xcmd);
}

