2010-10-04 19:18:30 +00:00
#!/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.
2012-05-30 15:29:18 +00:00
You can set the XCATPGPW environment variable to a password for the database
to avoid prompting otherwise it will prompt for the
2010-10-04 19:18:30 +00:00
password to assign to the xcatadm userid for the Unix id and PgSQL database
id.
2012-05-30 15:29:18 +00:00
2010-10-04 19:18:30 +00:00
Setups up AIX 6.1+ and Linux, but most work needs to be done on AIX.
2012-05-30 15:29:18 +00:00
See man pgsqlsetup for more information and the documentation.
2012-07-09 18:24:31 +00:00
If the -P flag is supplied, we are supporting setup under PCM.
Additional setup for PCM is required.
2010-10-04 19:18:30 +00:00
=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.
2012-08-08 17:45:31 +00:00
if ($^O =~ /^aix/i) {
unshift(@INC, qw(/usr/opt/perl5/lib/5.8.2/aix-thread-multi /usr/opt/perl5/lib/5.8.2 /usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.8.2));
2010-10-04 19:18:30 +00:00
}
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;
2012-05-30 15:29:18 +00:00
# Default install location for all but version 9.0
2010-10-04 19:18:30 +00:00
$::installdir = "/var/lib/pgsql"; # install location
$::installdatadir = "/var/lib/pgsql/data"; # install location
2012-05-31 13:51:55 +00:00
$::pgcmddir = "/usr/bin"; # pg cmds location
2013-04-03 16:30:24 +00:00
$::debiancfgdir = "/etc/postgresql/9.1/main";
2012-07-09 18:24:31 +00:00
my $INIT;
my $SETUPODBC;
my $NOSTART;
my $SETUPPCM;
my $HELP;
my $VERSION;
2013-04-03 16:30:24 +00:00
my $debianflag = 0;
2010-10-04 19:18:30 +00:00
# parse the options
if (
!GetOptions(
2012-07-09 18:24:31 +00:00
'i|init' => \$INIT,
'o|odbc' => \$SETUPODBC,
'N|nostart' => \$NOSTART,
'P|PCM' => \$SETUPPCM,
'h|help' => \$HELP,
'v|version' => \$VERSION,
2010-10-04 19:18:30 +00:00
'V|verbose' => \$::VERBOSE,
)
)
{
&usage;
exit(1);
}
# display the usage if -h or --help is specified
2012-07-09 18:24:31 +00:00
if ($HELP)
2010-10-04 19:18:30 +00:00
{
&usage;
exit(0);
}
# display the version statement if -v or --version is specified
2012-07-09 18:24:31 +00:00
if ($VERSION)
2010-10-04 19:18:30 +00:00
{
my $version = xCAT::Utils->Version();
xCAT::MsgUtils->message("I", $version);
exit 0;
}
2012-07-09 18:24:31 +00:00
# Database name xcatdb
$::dbname="xcatdb";
if ((!($INIT)) && (!($SETUPODBC)))
2010-10-04 19:18:30 +00:00
{
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;
2012-07-09 18:24:31 +00:00
if ((!($INIT)) && ($SETUPODBC))
2010-10-04 19:18:30 +00:00
{
$odbconly = 1;
}
#
# Get OS
#
if (xCAT::Utils->isAIX())
{
$::osname = 'AIX';
}
else
{
$::osname = 'Linux';
2012-05-30 15:29:18 +00:00
# determine whether redhat or sles
$::linuxos = xCAT::Utils->osver();
2013-04-03 16:30:24 +00:00
if ( -e "/etc/debian_version" ){
$debianflag = 1;
}
2010-10-04 19:18:30 +00:00
}
2012-08-29 13:50:56 +00:00
#
# check to see if perl-DBD-Pg is installed
#
my $cmd = "rpm -qa | grep perl-DBD-Pg";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$cmd = "dpkg -l | grep libdbd-pg-perl";
}
2012-08-29 13:50:56 +00:00
my @output=xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
my $message =
"\nperl-DBD-Pg 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.";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$message = "\nlibdb-pg-perl is not installed. Use apt-get to install it.";
}
2012-08-29 13:50:56 +00:00
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
2010-10-04 19:18:30 +00:00
#
# check to see if postgresql is installed
#
my $cmd = "rpm -qa | grep postgresql";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$cmd = "dpkg -l | grep postgresql | awk '{print \$2}'";
}
2012-05-30 15:29:18 +00:00
my @output=xCAT::Utils->runcmd($cmd, 0);
2010-10-04 19:18:30 +00:00
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.";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$message = "\npostgresql is not installed. Use apt-get to install it.";
}
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", " $cmd failed. $message");
exit(1);
}
2012-10-18 16:21:37 +00:00
# check if 9.X release not built by us is installed, setup different
2012-05-31 17:06:57 +00:00
if (grep(/postgresql9/, @output)) { # postgresql 9.x
# figure out which 9.x release and build path
my @parseout= split(/\-/, $output[0]);
my @ptflevel= split ("postgresql9",$parseout[0]);
$::postgres9=@ptflevel[1]; # set it to the PTF level
$::installdir = "/var/lib/pgsql/9.$::postgres9"; # override default
$::installdatadir = "/var/lib/pgsql/9.$::postgres9/data"; # override default
$::pgcmddir = "/usr/pgsql-9.$::postgres9/bin"; # pg cmds location
2012-05-30 15:29:18 +00:00
}
2010-10-04 19:18:30 +00:00
2013-04-03 16:30:24 +00:00
if ($debianflag){
undef $::postgres9;
$::installdatadir = "/var/lib/postgresql/9.1/main";
$::pgcmddir = "/usr/lib/postgresql/9.1/bin";
for my $line (@output){
if ($line =~ /postgresql\-9/){
my $pgsqlver = substr $line, 11;
$::pgcmddir = "/usr/lib/postgresql/${pgsqlver}/bin";
$::installdatadir = "/var/lib/postgresql/${pgsqlver}/main";
$::debiancfgdir = "/etc/postgresql/${pgsqlver}/main";
last;
}
}
}
2010-10-04 19:18:30 +00:00
# check to see if PostgreSQL is running
2012-05-31 15:10:32 +00:00
my $pgsqlrunning = 0;
my $xcatrunningpgsql = 0;
2010-10-04 19:18:30 +00:00
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))
{
2012-05-31 15:10:32 +00:00
$pgsqlrunning = 1;
2010-10-04 19:18:30 +00:00
}
}
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)
{
2012-07-09 18:24:31 +00:00
if ($INIT)
2010-10-04 19:18:30 +00:00
{
my $message =
"The /etc/xcat/cfgloc file is already configured for PostgreSQL. xcat database initialization will not take place.";
xCAT::MsgUtils->message("I", "$message");
}
2012-05-31 15:10:32 +00:00
$xcatrunningpgsql = 1;
2010-10-04 19:18:30 +00:00
}
}
# if not just odbc update
2012-05-30 15:29:18 +00:00
# Get admin password or use the env variable
2010-10-04 19:18:30 +00:00
#
2012-05-31 15:10:32 +00:00
if (($odbconly == 0) && ($xcatrunningpgsql == 0))
2010-10-04 19:18:30 +00:00
{ # not just updating the odbc and xcat not already running on pg
2012-05-30 15:29:18 +00:00
if ($ENV{'XCATPGPW'}) # passwd supplied
{
2012-08-30 16:05:33 +00:00
my $pw=$ENV{'XCATPGPW'};
if ($pw =~ m/[^a-zA-Z0-9]/){ # if not alpha-numerid
my $warning =
"The password in the env variable XCATPGPW is not alpha-numeric.";
xCAT::MsgUtils->message("E", $warning);
exit 1;
}
2012-05-30 15:29:18 +00:00
$::adminpassword = $ENV{'XCATPGPW'};
} else {
2012-08-30 16:05:33 +00:00
my $msg = "Input the password for xcatadm id: ";
2012-05-30 15:29:18 +00:00
xCAT::MsgUtils->message('I', "$msg");
`stty -echo`;
chop($::adminpassword = <STDIN>);
`stty echo`;
2012-08-30 16:05:33 +00:00
if ($::adminpassword =~ m/[^a-zA-Z0-9]/){ # if not alpha-numerid
my $warning =
"The input password is not alpha-numeric. Rerun the command an input an alpha-numeric password.";
xCAT::MsgUtils->message("E", $warning);
exit 1;
}
2012-05-30 15:29:18 +00:00
}
2010-10-04 19:18:30 +00:00
}
# initial setup request and not already running pgsql
2012-07-09 18:24:31 +00:00
if (($INIT) && ($xcatrunningpgsql == 0))
2010-10-04 19:18:30 +00:00
{
2012-07-09 18:24:31 +00:00
if (!($NOSTART)) { # if not just setting up the database
2010-10-04 19:18:30 +00:00
#
# Backup current database
#
my $homedir = xCAT::Utils->getHomeDir();
$::backupdir = $homedir;
if ($::osname eq 'AIX')
{
$::backupdir .= "xcat-dbback";
}
else
{
$::backupdir .= "/xcat-dbback";
}
2012-05-30 18:25:48 +00:00
&backupxcatdb;
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
# shutdown the xcatd daemon
2010-10-04 19:18:30 +00:00
&shutdownxcatd;
2012-07-09 18:24:31 +00:00
}
my $hname;
$hname = `hostname`;
chomp $hname;
if (!($NOSTART)) { # if not just setting up the database
2010-10-04 19:18:30 +00:00
#
# 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);
2012-07-09 18:24:31 +00:00
if ($::RUNCMD_RC == 0) # entry in site table
2010-10-04 19:18:30 +00:00
{
(my $attr, my $master) = split(",", $output[0]);
(my $q, $hname) = split("\"", $master);
chomp $hname;
}
2012-07-09 18:24:31 +00:00
}
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
my $ipaddr = xCAT::NetworkUtils->getipaddr($hname);
if ($ipaddr)
{
2010-10-04 19:18:30 +00:00
$::MN = $ipaddr;
2012-07-09 18:24:31 +00:00
}
else
{
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", "Hostname resolution for $hname failed.");
exit(1);
2012-07-09 18:24:31 +00:00
}
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
# 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')
{
2012-05-31 15:10:32 +00:00
&mkpgusers;
2012-07-09 18:24:31 +00:00
}
#
# Init Pg database and setup pg_hba.conf and postgresql.conf
#
&initpgdb;
#
# Start Postgresql server
#
if ($pgsqlrunning == 0) # if not already running
{
2012-05-31 15:10:32 +00:00
&pgstart;
2012-07-09 18:24:31 +00:00
}
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
#
# Setup Postgresql to restart on reboot
#
&pgreboot;
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
# if xcat not already configured to run Postgresql add xCAT database
if ($xcatrunningpgsql == 0)
{
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
#
# Create xcatd database
# Create xcatadm in the database
#
&setupxcatdb;
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
#
# create cfgloc file
#
&createcfgloc;
2010-10-04 19:18:30 +00:00
2012-07-09 18:24:31 +00:00
if (!($NOSTART)) { # if not just setting up the database
2010-12-30 12:41:39 +00:00
#
# Restore backed up database into PostgreSQL
#
&restorexcatdb;
2012-07-09 18:24:31 +00:00
}
}
if (!($NOSTART)) { # if not just setting up the database
xCAT::MsgUtils->message("I",
2010-12-30 12:41:39 +00:00
"xCAT is running on the PostgreSQL database.\n");
2012-07-09 18:24:31 +00:00
} else {
xCAT::MsgUtils->message("I",
"xCAT PostgreSQL database is setup.\n");
}
2010-10-04 19:18:30 +00:00
} # end initialization
2012-07-09 18:24:31 +00:00
if ($SETUPODBC)
2010-10-04 19:18:30 +00:00
{
#
# set up the ODBC on the Management Node
#
2012-05-23 13:59:42 +00:00
&setupODBC;
2010-10-04 19:18:30 +00:00
}
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 =
2012-07-09 18:24:31 +00:00
"pgsqlsetup <-h|--help>\n <-v|--version>\n <-i|--init> [-N|--nostart] [-P|--PCM] [-o|--odbc] [-V|--verbose]\n <-o|--odbc> [-V|--verbose]";
2010-10-04 19:18:30 +00:00
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
{
2010-12-30 12:41:39 +00:00
xCAT::MsgUtils->message(
2010-10-04 19:18:30 +00:00
"I",
"Backing up xCAT Database to $::backupdir.\nThis could take several minutes."
);
2010-12-30 12:41:39 +00:00
if (!(-e $::backupdir))
{ # does not exist, make it
2010-10-04 19:18:30 +00:00
my $cmd = "mkdir -p $::backupdir";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2010-12-30 12:41:39 +00:00
}
else
{ # remove contents
2010-10-04 19:18:30 +00:00
my $cmd = "rm -f $::backupdir/*";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2010-12-30 12:41:39 +00:00
}
2010-10-04 19:18:30 +00:00
2010-12-30 12:41:39 +00:00
# back it up
2012-05-30 15:29:18 +00:00
my $cmd = "XCATBYPASS=1 dumpxCATdb -p $::backupdir";
2010-12-30 12:41:39 +00:00
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2010-10-04 19:18:30 +00:00
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
2012-05-30 15:29:18 +00:00
Note this does not support postgresql 9.0 on AIX
2010-10-04 19:18:30 +00:00
=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~;
2012-04-24 02:03:10 +00:00
# secure passwd in verbose mode
my $tmpv = $::VERBOSE;
$::VERBOSE = 0;
2010-10-04 19:18:30 +00:00
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0)
{
2012-04-24 02:03:10 +00:00
xCAT::MsgUtils->message("E", " command failed.");
2010-10-04 19:18:30 +00:00
exit(1);
}
2012-04-24 02:03:10 +00:00
$::VERBOSE = $tmpv;
2010-10-04 19:18:30 +00:00
}
#
# 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");
}
2012-10-25 13:57:24 +00:00
#-----------------------------------------------------------------------------
=head3 runpgcmd_chkoutput
Run a commmand as the postgres id
Input: command
Output: return code
Note: this version checks for specific error conditions
=cut
#-----------------------------------------------------------------------------
sub runpgcmd_chkoutput
{
my $orgcmd = shift;
my $rc =0;
my $cmd = "\'";
$cmd .= $orgcmd;
$cmd .= ' 2>&1';
$cmd .= "\'";
my $retries=0;
2012-10-25 14:09:17 +00:00
my @output;
2012-10-25 13:57:24 +00:00
while ($retries < 6) {
$retries++;
2012-10-25 14:09:17 +00:00
@output =`su - postgres -c $cmd`;
2012-10-25 13:57:24 +00:00
if (grep(/could not connect to database postgres/, @output)) {
sleep 5;
$rc=1
} else {
$rc=0;
foreach my $line (@output) {
print "$line \n";
}
last;
}
}
2012-10-25 14:09:17 +00:00
if ($rc == 1) { # print out the postgresql connect failure
foreach my $line (@output) {
print "$line \n";
}
}
2012-10-25 13:57:24 +00:00
return $rc;
}
2010-10-04 19:18:30 +00:00
#-----------------------------------------------------------------------------
=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')
{
2012-05-30 15:29:18 +00:00
$cmd = "$::installdir/bin/initdb -D $::installdatadir ";
2010-10-04 19:18:30 +00:00
} else {
2012-05-31 13:51:55 +00:00
$cmd = "$::pgcmddir/initdb -D $::installdatadir ";
2010-10-04 19:18:30 +00:00
}
&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";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$hbafile = $::debiancfgdir . "/pg_hba.conf";
}
2010-10-04 19:18:30 +00:00
$cmd = "fgrep $::MN $hbafile";
xCAT::Utils->runcmd($cmd, -1);
2012-05-30 15:29:18 +00:00
if ($::RUNCMD_RC != 0) # not already there
2010-10-04 19:18:30 +00:00
{
2012-05-30 15:29:18 +00:00
$cmd = "cp $hbafile $hbafile.org"; # backup the original
xCAT::Utils->runcmd($cmd, -1);
2010-10-04 19:18:30 +00:00
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";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$pgconf = $::debiancfgdir . "/postgresql.conf";
}
2010-10-04 19:18:30 +00:00
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
2012-05-30 15:29:18 +00:00
if ($::installdatadir) { # for protection
$cmd = "chown postgres $::installdatadir/*";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", " $cmd failed.");
2012-05-30 15:29:18 +00:00
}
$cmd = "chgrp postgres $::installdatadir/* ";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", " $cmd failed.");
2012-05-30 15:29:18 +00:00
}
2010-10-04 19:18:30 +00:00
2012-05-30 15:29:18 +00:00
}
2010-10-04 19:18:30 +00:00
}
#-----------------------------------------------------------------------------
=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
{
2012-05-31 17:06:57 +00:00
if (defined($::postgres9)) { # set to the PTF level of postgresql 9.X
$cmd = "service postgresql-9.$::postgres9 start";
2012-05-30 15:29:18 +00:00
} else {
$cmd = "service postgresql start";
}
2012-11-08 14:47:15 +00:00
system($cmd);
if ($? > 0) {
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", " $cmd failed.");
exit(1);
}
2012-10-18 16:21:37 +00:00
# check to see if running before continuing
my $retries =0;
my $pgstarted =0;
2012-10-24 17:51:40 +00:00
if (defined($::postgres9)) { # set to the PTF level of postgresql 9.X
2012-10-18 16:21:37 +00:00
$cmd = "service postgresql-9.$::postgres9 status";
2012-10-24 17:51:40 +00:00
} else {
2012-10-18 16:21:37 +00:00
$cmd = "service postgresql status";
2012-10-24 17:51:40 +00:00
}
while ($retries < 30) {
$retries++;
2012-10-18 16:21:37 +00:00
my @status=xCAT::Utils->runcmd($cmd, -1);
2013-04-15 07:13:00 +00:00
if (grep(/[r|R]unning/, @status)) {
2012-10-18 16:21:37 +00:00
$pgstarted=1;
last;
}
2012-10-18 16:23:04 +00:00
sleep 1;
2012-10-18 16:21:37 +00:00
}
if ($pgstarted ==0) { # failed to started
xCAT::MsgUtils->message("E", " service postgresql start failed.");
exit(1);
}
} # end linux
2010-10-04 19:18:30 +00:00
}
#-----------------------------------------------------------------------------
=head3 pgreboot
2012-05-30 15:29:18 +00:00
Setup for postgresql to start on reboot, only Linux
2010-10-04 19:18:30 +00:00
=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
{
2012-05-31 17:06:57 +00:00
if (defined($::postgres9)) { # set to the postgresql ptf level
2013-04-03 16:30:24 +00:00
$cmd = "chkconfig postgresql-9.$::postgres9 on";
2012-05-30 15:29:18 +00:00
} else {
2013-04-03 16:30:24 +00:00
$cmd = "chkconfig postgresql on";
}
if ($debianflag){
$cmd = "update-rc.d postgresql defaults";
2012-05-30 15:29:18 +00:00
}
2010-10-04 19:18:30 +00:00
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
2012-05-30 15:29:18 +00:00
Add xcatadm to the database and make owner of the database
2010-10-04 19:18:30 +00:00
=cut
#-----------------------------------------------------------------------------
sub setupxcatdb
{
my $cmd;
2012-05-30 18:04:28 +00:00
# create the database
2010-10-04 19:18:30 +00:00
if ($::osname eq 'AIX')
{
2012-05-30 18:04:28 +00:00
$cmd = "$::installdir/bin/createdb $::dbname ";
2012-05-31 13:51:55 +00:00
} else { #Linux
$cmd = "$::pgcmddir/createdb $::dbname ";
2010-10-04 19:18:30 +00:00
}
2012-10-25 13:57:24 +00:00
# We need to check the output, because sometimes
# postmaster daemon is not ready at this point to create the database
my $rc = &runpgcmd_chkoutput($cmd);
if ($rc > 0) {
2012-05-30 18:04:28 +00:00
xCAT::MsgUtils->message("E",
"Failed creating the $::dbname database");
2012-10-25 13:57:24 +00:00
exit(1);
2012-05-30 18:04:28 +00:00
}
2010-10-04 19:18:30 +00:00
if ($::osname eq 'AIX')
{
2012-05-30 18:04:28 +00:00
$cmd = "/var/lib/pgsql/bin/psql -d $::dbname -U postgres";
} else { # Linux
2012-05-31 13:51:55 +00:00
$cmd = "$::pgcmddir/psql -d $::dbname -U postgres";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$cmd = "su - postgres -c '$::pgcmddir/psql -d $::dbname -U postgres'";
}
2010-10-04 19:18:30 +00:00
}
2012-05-30 18:04:28 +00:00
my $pgsql;
my $timeout = 10; # sets Expect default timeout, 0 accepts immediately
my $pwd_sent = 0;
my $pg_prompt = "$::dbname=# ";
my $expect_log = undef;
my $debug = 0;
$pgsql = new Expect;
#
# -re $pg_prompt
# For example xcatdb=#
#
#
# disable command echoing
#$pgsql->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$pgsql->exp_internal(1);
$pgsql->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$pgsql->log_stdout(1);
$pgsql->log_stdout($debug);
my $createuser="CREATE USER xcatadm WITH PASSWORD \'$::adminpassword\';\r";
my $spawncmd;
$spawncmd = $cmd;
unless ($pgsql->spawn($spawncmd))
{
xCAT::MsgUtils->message("E",
"Unable to run $spawncmd to add xcat admin user.");
return;
}
my @result = $pgsql->expect(
$timeout,
[
$pg_prompt,
sub {
$pgsql->send("$createuser");
$pgsql->clear_accum();
$pgsql->exp_continue();
$pgsql->send("\q;\r");
}
]
);
######################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $errmsg = $result[1];
$pgsql->soft_close();
xCAT::MsgUtils->message("E",
2012-10-24 17:51:40 +00:00
"Failed creating xcatadm id.");
2012-05-30 18:04:28 +00:00
exit(1);
}
$pgsql->soft_close();
2012-06-01 12:48:04 +00:00
# setup root user
$pgsql = new Expect;
#
# -re $pg_prompt
# For example xcatdb=#
#
#
# disable command echoing
#$pgsql->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$pgsql->exp_internal(1);
$pgsql->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$pgsql->log_stdout(1);
$pgsql->log_stdout($debug);
$createuser="CREATE USER root WITH PASSWORD \'$::adminpassword\';\r";
$spawncmd;
$spawncmd = $cmd;
unless ($pgsql->spawn($spawncmd))
{
xCAT::MsgUtils->message("E",
"Unable to run $spawncmd to add xcat root user.");
return;
}
my @result = $pgsql->expect(
$timeout,
[
$pg_prompt,
sub {
$pgsql->send("$createuser");
$pgsql->clear_accum();
$pgsql->exp_continue();
$pgsql->send("\q;\r");
}
]
);
######################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $errmsg = $result[1];
$pgsql->soft_close();
xCAT::MsgUtils->message("E",
"Failed creating root id. $errmsg");
exit(1);
}
$pgsql->soft_close();
2010-10-04 19:18:30 +00:00
}
#-----------------------------------------------------------------------------
=head3 setupODBC
2012-05-23 13:59:42 +00:00
Will setup the ODBC interface to the ODBC. Only supported on Linux
2010-10-04 19:18:30 +00:00
=cut
#-----------------------------------------------------------------------------
sub setupODBC
{
2012-05-23 13:59:42 +00:00
if ($::osname eq 'AIX') {
my $message =
"Setup of the ODBC is only supported on Linux.";
2012-05-23 17:52:21 +00:00
xCAT::MsgUtils->message("E", "$message");
2012-05-23 13:59:42 +00:00
exit(1);
}
2010-10-04 19:18:30 +00:00
#
# check to see if correct rpms are installed
#
# for all OS need unixODBC rpm
2012-05-23 13:59:42 +00:00
my $message;
2010-10-04 19:18:30 +00:00
my $cmd = "rpm -qa | grep unixODBC";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$cmd = "dpkg -l | grep unixodbc";
}
2012-05-23 17:52:21 +00:00
xCAT::Utils->runcmd($cmd, -1);
2010-10-04 19:18:30 +00:00
if ($::RUNCMD_RC != 0)
{
2013-04-03 16:30:24 +00:00
$message =
"unixODBC rpm is not installed. Install from the OS CDs.";
if ($debianflag){
$message = "unixodbc is not installed. Install it first.";
}
2012-05-23 17:52:21 +00:00
xCAT::MsgUtils->message("E", "$message");
2012-05-23 13:59:42 +00:00
exit(1);
}
$cmd = "rpm -qa | grep postgresql-odbc";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$cmd = "dpkg -l | grep odbc-postgresql";
}
2012-05-23 17:52:21 +00:00
xCAT::Utils->runcmd($cmd, -1);
2012-05-23 13:59:42 +00:00
if ($::RUNCMD_RC != 0)
{
2013-04-03 16:30:24 +00:00
$message =
2012-05-23 17:52:21 +00:00
"postgresql-odbc rpm is not installed. Install from the OS CDs.";
2013-04-03 16:30:24 +00:00
if ($debianflag){
$message = "odbc-postgresql is not installed. Install it first.";
}
2012-05-23 17:52:21 +00:00
xCAT::MsgUtils->message("E", " $message");
2010-10-04 19:18:30 +00:00
exit(1);
}
2012-05-23 13:59:42 +00:00
my $xcatconfig = "/etc/xcat/cfgloc";
if (!(-e $xcatconfig))
{
$message =
"The $xcatconfig file is missing. You need to configure xCAT for PostgreSQL before setting up the ODBC.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
$cmd = "fgrep -i host $xcatconfig";
my @output;
@output = xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # cannot get the necessary hostname or ip
{
$message =
"Cannot find host info in the /etc/xcat/cfgloc file. Configuration of ODBC cannot continue.";
2012-05-23 17:52:21 +00:00
xCAT::MsgUtils->message("E", "$message");
2012-05-23 13:59:42 +00:00
exit(1);
}
2012-07-11 12:06:17 +00:00
# get host and password database from cfgloc
my( $connstring, $adminid, $passwd) = split(/\|/, $output[0]);
my ($hdr, $id, $server) = split(/=/, $connstring);
my ($database,$footer) = split(/;/, $id);
2012-05-23 13:59:42 +00:00
# the odbcinst.ini file should have been created during install of the
# unixODBC and postgresql-ODBC rpms
my $odbcfile = "/etc/odbc.ini";
my $odbcinstfile = "/etc/odbcinst.ini";
if (!( -e $odbcinstfile)) {
$message =
"Cannot find $odbcinstfile. Configuration of ODBC cannot continue.";
2012-05-23 17:52:21 +00:00
xCAT::MsgUtils->message("E", "$message");
2012-05-23 13:59:42 +00:00
exit(1);
}
# setup the DSN odbc.ini file
2012-07-11 12:25:00 +00:00
$cmd = "fgrep -i xcatdb $odbcfile";
2012-05-23 13:59:42 +00:00
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then xcat entry not there
{
my $entry =
2012-05-30 15:29:18 +00:00
"[xCATDB]\nDriver = PostgreSQL\nSERVER = $server\nPORT = 3306\nDATABASE = $database";
2012-05-23 13:59:42 +00:00
$cmd = "echo \"$entry\" >> $odbcfile";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
$message = "$cmd failed. Could not setup ODBC DNS file.";
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
}
else
{ # entry already there
$message = "$odbcfile already configured, will not change.";
xCAT::MsgUtils->message("I", "$message");
}
# setup $roothome/.odbc.ini so root will not have to specify password
# when accessing through ODBC
my $homedir = xCAT::Utils->getHomeDir();
my $rootodbcfile = $homedir;
$rootodbcfile .= "/.odbc.ini";
# setup the DSN odbc.ini file
$cmd = "fgrep -i XCATDB $rootodbcfile";
xCAT::Utils->runcmd($cmd, -1);
if ($::RUNCMD_RC != 0) # then xcat entry not there
{
my $entry =
2012-05-30 15:29:18 +00:00
"[xCATDB]\nSERVER =$server\nDATABASE = $database\nUSER = $adminid\nPASSWORD = $passwd";
2012-05-23 13:59:42 +00:00
$cmd = "echo \"$entry\" >> $rootodbcfile";
# secure passwd in verbose mode
my $tmpv = $::VERBOSE;
$::VERBOSE = 0;
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2012-05-23 17:52:21 +00:00
$message = "Could not setup root ODBC file $rootodbcfile.";
2012-05-23 13:59:42 +00:00
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
$::VERBOSE = $tmpv;
}
else
{ # entry already there
2012-06-01 12:48:04 +00:00
$message = "$rootodbcfile already configured, will not change. Make sure the userid and password are correct for PostgreSQL";
2012-05-23 13:59:42 +00:00
xCAT::MsgUtils->message("I", "$message");
}
# allow readonly by root
chmod 0600, $rootodbcfile;
2010-10-04 19:18:30 +00:00
}
#-----------------------------------------------------------------------------
=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.");
}
}
2012-05-30 15:29:18 +00:00
my $pgentry = "Pg:dbname=$::dbname;host=$::MN|xcatadm|$::adminpassword";
2011-02-09 17:35:16 +00:00
$cmd = "echo \"$pgentry\" > $cfgloc";
2012-04-24 02:03:10 +00:00
# secure passwd in verbose mode
my $tmpv = $::VERBOSE;
$::VERBOSE = 0;
2010-10-04 19:18:30 +00:00
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2012-04-24 02:03:10 +00:00
$message = "command failed. Could not setup cfgloc";
2010-10-04 19:18:30 +00:00
xCAT::MsgUtils->message("E", "$message");
exit(1);
}
2012-04-24 02:03:10 +00:00
$::VERBOSE = $tmpv;
2010-10-04 19:18:30 +00:00
# allow readonly by root
chmod 0600, $cfgloc;
}
#-----------------------------------------------------------------------------
=head3 restorexcatdb
Restores the database from ~/xcat-dbback and restarts the xcatd using
2012-05-23 13:59:42 +00:00
PostgreSQL
2010-10-04 19:18:30 +00:00
=cut
#-----------------------------------------------------------------------------
sub restorexcatdb
{
2011-02-06 12:35:49 +00:00
# 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);
2010-10-04 19:18:30 +00:00
# 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);
}
2012-05-30 18:04:28 +00:00
my $tmpv = $::VERBOSE;
$::VERBOSE = 0;
2010-10-04 19:18:30 +00:00
# restore it
2011-02-06 12:35:49 +00:00
my $cmd = "XCATBYPASS=1 XCATCFG=\"$xcatcfg\" restorexCATdb -p $::backupdir";
2013-01-15 06:32:16 +00:00
# secure passwords in verbose mode
my $tmpv = $::VERBOSE;
$::VERBOSE = 0;
2010-10-04 19:18:30 +00:00
xCAT::Utils->runcmd($cmd, 0);
2013-01-15 06:32:16 +00:00
$::VERBOSE = $tmpv;
2010-10-04 19:18:30 +00:00
if ($::RUNCMD_RC != 0)
{
2012-05-30 18:04:28 +00:00
xCAT::MsgUtils->message("E", " restorexCATdb -p $::backupdir failed.");
2010-10-04 19:18:30 +00:00
exit(1);
}
2012-05-30 18:04:28 +00:00
$::VERBOSE = $tmpv;
2010-10-04 19:18:30 +00:00
#
# start the daemon
#
my $xcmd;
if ($::osname eq 'AIX')
{
$xcmd = "startsrc -s xcatd";
}
else
{
$xcmd = "service xcatd start";
}
system($xcmd);
}