diff --git a/xCAT-client/bin/db2sqlsetup b/xCAT-client/bin/db2sqlsetup new file mode 100755 index 000000000..927afb712 --- /dev/null +++ b/xCAT-client/bin/db2sqlsetup @@ -0,0 +1,1421 @@ +#!/usr/bin/env perl +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +# + +#----------------------------------------------------------------------------- + +=head1 db2setup + + + + This script is called after the installation of DB2 + on xCAT. It will automate the setup of the DB2 to run + xCAT on the IBM DB2 database on AIX and Linux. + On AIX ( AIX61J - AIX61 TL5(AIX 6.1.5.0)) or later and Linux. + As an option it will also setup the ODBC for C/C++ applications + to be able to use the database. + It will setup an xcatdb + database ,a db2instance id, name xcatdb ( same as database), + and instance password to be used in the /etc/xcat/cfgloc file + to access the database. + It will interact for the password to assign to the xcatdb + for the Unix id and DB2 instance id, + unless the XCATDB2PW env var is set to the password for the instance id. + For Client setup, it will interact for the hostname or ip address + of the DB2 Server as known by the Service Node, unless the + XCATDB2SERVER environement variable is set. + See man db2setup for more information and the xCAT2SetupDB2.pdf + documentation. + The script will assume the default install paths of /opt/IBM/db2 for AIX and + /opt/ibm/db2 for Linux, unless overridden with the XCATDB2INSPATH + environment variable. +=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. +# TODO: Remove during FVT should not need because DBD is compiled with +# 5.8.8 and we require 61J or later which runs with 5.8.8 Perl. +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 = "db2sqlsetup"; +my $args = join ' ', @ARGV; +$::command = "$0 $args"; +Getopt::Long::Configure("bundling"); +$Getopt::Long::ignorecase = 0; +$::databaseloc = "/var/lib/db2"; # database loccation + +# parse the options +if ( + !GetOptions( + 'i|init' => \$::INIT, + 'S|Server' => \$::SERVER, + 'C|Client' => \$::CLIENT, + '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); +} +if ((!($::SERVER)) && (!($::CLIENT))) +{ + xCAT::MsgUtils->message("I", "Either -S or -C flag must be chosen"); + &usage; + exit(1); +} +if (($::SERVER) && ($::CLIENT)) +{ + xCAT::MsgUtils->message("I", "Either -S or -C flag must be chosen"); + &usage; + exit(1); +} +if ((!($::SERVER)) && (!($::CLIENT)) && ($::SETUPODBC)) +{ + xCAT::MsgUtils->message("I", + "Either -S or -C flag must be chosen with the -o flag"); + &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(); +} + +# overide the default install path with environment variable +if ($ENV{'XCATDB2INSPATH'}) # input where DB2 is installed +{ + $::installdb2dir = $ENV{'XCATDB2INSPATH'}; +} +else +{ + if ($::osname eq 'AIX') + { + $::installdb2dir = "/opt/IBM/db2/V9.7"; # default + } + else + { # linux + $::installdb2dir = "/opt/ibm/db2/V9.7"; # default + } +} + +# +# check to see if DB2 is installed, in given location +# +if (!(-e ($::installdb2dir))) +{ + my $message = + "\nDB2 is not installed. It should be first obtained and installed into $::installdb2dir."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); +} + +# If setting up the Server, check to see if DB2 is running +$::db2running = 0; +$::xcatrunningdb2 = 0; +if ($::SERVER) +{ + my $cmd = "ps -ef | grep db2"; + my @output = xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC == 0) # already running + { + my $db2check = "db2agent"; # see if really running + if (grep(/$db2check/, @output)) + { + + if ($::INIT) + { + my $message = + "DB2 is running. Database initialization will not take place."; + xCAT::MsgUtils->message("I", "$message"); + } + $::db2running = 1; + } + } + if (-e ("/etc/xcat/cfgloc")) # check to see if xcat is using DB2 already + { # cfgloc exists + $cmd = "fgrep DB2 /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 DB2. xCAT appears to be already running on DB2. xcat database configuration will not take place."; + xCAT::MsgUtils->message("I", "$message"); + } + $::xcatrunningdb2 = 1; + } + } +} + +# if not just odbc update and xcat MN is not already running on DB2 +# or if we are setting up the Client and not just the ODBC +# then Get instance password +# +if ((($odbconly == 0) && ($::xcatrunningdb2 == 0)) || (($::CLIENT) && ($odbconly == 0))) +{ + if ($ENV{'XCATDB2PW'}) # passwd supplied + { + $::adminpassword = $ENV{'XCATDB2PW'}; + } + else + { #prompt for password + + my $msg = "Input the password for xcatdb instance id: "; + xCAT::MsgUtils->message('I', "$msg"); + `stty -echo`; + chop($::adminpassword = ); + `stty echo`; + } +} + +# initial setup request and not already running db2 +if (($::INIT) && ($::xcatrunningdb2 == 0)) +{ + if ($::SERVER) + { # setting up server + + # + # 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); + } + } # end setting up SERVER + + # DB2 not running, or setting up Client, continue setting up DB2 + if (($::db2running == 0) || ($::CLIENT)) + { + + # Add DB2 user and group and xcatdb instance for AIX/Linux + &mkdb2user; + + # Setup /etc/services + &setupservices; + + # Setup the xcatdb instance + &setupinstance; + + # + # Setup DB2 env variables + # + &setupdb2env; + + # + # Start DB2 server + # + if ($::SERVER) + { + &db2start; + + # + # Setup DB2 to restart on reboot + # + &db2reboot; + } + + } + + # On server, xcat not already configured to run DB2 add xCAT database + if (($::SERVER) && ($::xcatrunningdb2 == 0)) + { + # + # update DBM config + # + &updateDBM; + + # + # Create the xcatdb database + # + &createxcatdb; + + # + # create cfgloc file + # + &createcfgloc; + } + + # if setting up Client catalog the MN (db2 server address) + # and create /etc/xcat/cfgloc + if ($::CLIENT) + { + &catalogServer; + &createcfgloc; + } + + # + # Restore backed up database into DB2 + # + if ($::SERVER) + { + &restorexcatdb; + + xCAT::MsgUtils->message("I", + "xCAT is now running on the DB2 database.\n"); + + } + else # client + { + # + # start the daemon + # + my $xcmd; + if ($::osname eq 'AIX') + { + $xcmd = "startsrc -s xcatd"; + + } + else + { + $xcmd = "service xcatd start"; + } + system($xcmd); + xCAT::MsgUtils->message("I", "xCAT DB2 Client setup is complete.\n"); + } +} # end initialization + +if ($::SETUPODBC) +{ + + # + # set up the ODBC on the Management Node + # + + &setupODBC; + +} + +exit; + +##################################### +# subroutines +##################################### + +#----------------------------------------------------------------------------- + +=head3 usage + + Displays message for -h option + +=cut + +#----------------------------------------------------------------------------- + +sub usage +{ + xCAT::MsgUtils->message( + 'I', + "Usage:\ndb2sqlsetup - Performs the setup of IBM DB2 for xCAT to use as its database. See man db2sqlsetup for more information." + ); + my $msg = + "db2sqlsetup <-h|--help>\n <-v|--version>\n <-i|--init> <-S|-C> [-o|--setupODBC] [-V|--verbose]\n <-o|--setupODBC> <-S|-C> [-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 + +{ + + # If there is no backup or the /etc/xcat/cfgloc file does not point to + # pgsql, then we backup the database + my $sitefile = "$::backupdir/site.csv"; + + if ((!(-e $sitefile)) || ($::xcatrunningdb2 == 0)) + { + 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 mkdb2user + + adds db2instance user and group and xcatdb instance id +=cut + +#----------------------------------------------------------------------------- + +sub mkdb2user +{ + + # + # mk db2 instance group and user + # + + my $cmd; + if ($::osname eq 'AIX') + { + $cmd = "lsgroup xcatdb"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + #group does not exist, need to make it + $cmd = "mkgroup xcatdb"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + $cmd = "lsuser xcatdb"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + #user does not exist, need to make it + $cmd = "mkuser pgrp=xcatdb home=/var/lib/db2 shell=/bin/ksh xcatdb"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + # touch .profile to makesure it stays owned by xcatd + $cmd = "touch /var/lib/db2/.profile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "chmod 777 /var/lib/db2/.profile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + } + + # set xcatdb id password + $cmd = qq~echo "xcatdb:$::adminpassword" | /bin/chpasswd -c~; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + else + { # Linux + + $cmd = "egrep -i \"^xcatdb:\" /etc/group"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + #group does not exist, need to make it + $cmd = "groupadd xcatdb"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + $cmd = "egrep -i \"^xcatdb:\" /etc/passwd"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + #user does not exist, need to make it + $cmd = "useradd -d /var/lib/db2 -g xcatdb -m -s /bin/bash xcatdb"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + } + + # set xcatdb id password + $cmd = qq~echo $::adminpassword | passwd xcatdb --stdin~; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } +} + +#----------------------------------------------------------------------------- + +=head3 setupservices + + + add db2 xcatdb instance entries into /etc/services on Server or Client + + +=cut + +#----------------------------------------------------------------------------- +sub setupservices +{ + my $cmd; + $cmd = "egrep -i \"^db2c_xcatdb\" /etc/services"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { # if not already there + open(FILE, ">>/etc/services") + or die "cannot open file /etc/services\n"; + if ($::SERVER) + { # on the server + print FILE "# xcatdb db2 entries +DB2_xcatdb 60000/tcp +DB2_xcatdb_1 60001/tcp +DB2_xcatdb_2 60002/tcp +DB2_xcatdb_END 60003/tcp +db2c_xcatdb 50001/tcp # Port for server connection"; + } + else + { # on the client + print FILE "# xcatdb db2 entries +db2c_xcatdb 50001/tcp # Port for server connection"; + } + close FILE; + } + +} + +#----------------------------------------------------------------------------- + +=head3 setupinstance + + + Create the db2 server or client instance + For server + run /instance/db2icrt -a server -p db2c_xcatdb -s ese -u xcatdb xcatdb + For Client + run /instance/db2icrt -s client xcatdb + + +=cut + +#----------------------------------------------------------------------------- + +sub setupinstance +{ + my $rc = 0; + + #check if instance already exists + my $cmd = $::installdb2dir; + $cmd .= "\/instance\/db2ilist "; + my @output = xCAT::Utils->runcmd($cmd, 0); + my $check = "xcatdb"; # see if instance exists + if (!(grep(/$check/, @output))) + { + + # if xcatdb instance does not exist, setup + $cmd = $::installdb2dir; # set db2 path to the create instance cmd + $cmd .= "\/instance\/db2icrt "; + if ($::SERVER) + { + + $cmd .= "-a server -p db2c_xcatdb -s ese -u xcatdb xcatdb "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { # if error exit + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + # setup instance + $cmd = $::installdb2dir; # + $cmd .= "\/instance\/db2iset "; + $cmd .= "-g DB2_PARALLEL_IO=* "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + } + $cmd = $::installdb2dir; + $cmd .= "\/instance\/db2iset "; + $cmd .= "-g DB2AUTOSTART=yes "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + } + $cmd = $::installdb2dir; + $cmd .= "\/instance\/db2iset "; + $cmd .= "-g DB2_STRIPED_CONTAINERS=ON "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + } + + $cmd = "echo \"EXTSHM=ON\" >> /var/lib/db2/sqllib/db2profile "; + xCAT::Utils->runcmd($cmd,0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + $cmd = "echo \"export EXTSHM\" >>/var/lib/db2/sqllib/db2profile "; + xCAT::Utils->runcmd($cmd,0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + + } + else + { # client setup + $cmd .= "-s client xcatdb "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { # if error exit + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "echo \"EXTSHM=ON\" >> /var/lib/db2/sqllib/db2profile "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + $cmd = "echo \"export EXTSHM\" >>/var/lib/db2/sqllib/db2profile "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + + } + } + # for either client or server update + # su to xcatdb and run more setup + $cmd = "export EXTSHM=ON "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + $cmd = "db2set DB2ENVLIST=EXTSHM "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + + +} + +#----------------------------------------------------------------------------- + +=head3 rundb2cmd + + + Run a commmand as the xcatdb instance id + Input: command + +=cut + +#----------------------------------------------------------------------------- +sub rundb2cmd +{ + my $orgcmd = shift; + my $rc = 0; + my $cmd = "\'"; + $cmd .= $orgcmd; + $cmd .= ' 2>&1'; + $cmd .= "\'"; + system("su - xcatdb -c $cmd"); + if ($? > 0) # error + { + $rc = $? >> 8; + } + return ($rc); +} + +#----------------------------------------------------------------------------- + +=head3 setupdb2env + + + Setup the db2 environment variables and libpaths + +=cut + +#----------------------------------------------------------------------------- +sub setupdb2env + +{ + if ($::osname eq 'AIX') + { + my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { # if not already there + open(FILE, ">>/etc/profile") + or die "cannot open file /etc/profile\n"; + print FILE "export DB2INSTANCE=xcatdb \n"; + print FILE "export EXTSHM=ON \n"; + close FILE; + } + + } + else + { # LInux + my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile.d/xcat.sh"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { # if not already there + open(FILE, ">>/etc/profile.d/xcat.sh") + or die "cannot open file /etc/profile.d/xcat.sh\n"; + print FILE "export DB2INSTANCE=xcatdb \n"; + print FILE "export EXTSHM=ON \n"; + close FILE; + } + my $cmd = "egrep -i \"DB2INSTANCE\" /etc/profile.d/xcat.csh"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { # if not already there + open(FILE, ">>/etc/profile.d/xcat.csh") + or die "cannot open file /etc/profile.d/xcat.csh\n"; + print FILE "setenv DB2INSTANCE \"xcatdb\" \n"; + print FILE "setenv EXTSHM \"ON\" \n"; + close FILE; + + } + my $path = $::installdb2dir; + $path .="\/lib64"; + # add path of db2 libraries to ld.so.conf + my $cmd = "egrep -i \"V9.7\" /etc/ld.so.conf"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { # if not already there + open(FILE, ">>/etc/ld.so.conf") + or die "cannot open file /etc/ld.so.conf\n"; + print FILE "$path\n"; + close FILE; + $cmd = "ldconfig"; + xCAT::Utils->runcmd($cmd); + + } + + } +} + +#----------------------------------------------------------------------------- + +=head3 db2start + + + Start the db2 server + +=cut + +#----------------------------------------------------------------------------- + +sub db2start +{ + xCAT::MsgUtils->message("I", "Starting the DB2 Server"); + my $cmd; + + # su to xcatdb to start the server + $cmd = "db2start "; + my $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("W", " $cmd failed."); + } + +} + +#----------------------------------------------------------------------------- + +=head3 catalogServer + + + Catalog the location of the db2 server on the Service Node + +=cut + +#----------------------------------------------------------------------------- + +sub catalogServer +{ + xCAT::MsgUtils->message("I", "Cataloging the DB2 Server"); + my $MN; + if ($ENV{'XCATDB2SERVER'}) + { # hostname or ip address supplied + $MN = $ENV{'XCATDB2SERVER'}; + } + else + { + my $msg = + "Input the hostname or ip address of the Management Node as known by this Service node. Should be the same as the site.master attribute: "; + + xCAT::MsgUtils->message('I', "$msg"); + chop($MN = ); + } + my $cmd; + my $rc = 0; + + # su to xcatdb and catalog the database server and database + $cmd = "db2 catalog tcpip node mn remote $MN server db2c_xcatdb "; + $rc = &rundb2cmd($cmd); + $cmd = "db2 terminate"; # refresh the cache + $rc = &rundb2cmd($cmd); + $cmd = "db2 catalog db xcatdb as xcatdb at node mn "; + $rc = &rundb2cmd($cmd); + $cmd = "db2 terminate"; # refresh the cache + $rc = &rundb2cmd($cmd); + +} +#----------------------------------------------------------------------------- + +=head3 updateDBM + + + Setup configuration requirements for the DBM + +=cut + +#----------------------------------------------------------------------------- + +sub updateDBM +{ + xCAT::MsgUtils->message("I", + "Updating the DBM."); + my $cmd; + my $rc = 0; + + # su to xcatdb to update the DBM + $cmd = "db2 -tvf /opt/xcat/share/xcat/tools/updateDBM.sql "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + + +} + +#----------------------------------------------------------------------------- + +=head3 createxcatdb + + + Create the xcat database + +=cut + +#----------------------------------------------------------------------------- + +sub createxcatdb +{ + xCAT::MsgUtils->message("I", + "Creating the xCAT database. This can take several minutes."); + my $cmd; + my $rc = 0; + + # su to xcatdb to create the database + $cmd = "db2 -tvf /opt/xcat/share/xcat/tools/createdb.sql "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + + # restart the instance to apply the change + xCAT::MsgUtils->message("I", + "Database created. Restarting the instance to apply the change. "); + $cmd = "db2 connect reset "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + $cmd = "db2 force applications all; db2 terminate; "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("I", " $cmd warning."); + } + $cmd = "db2stop "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd warning."); + exit(1); + } + $cmd = "db2start "; + $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd warning."); + exit(1); + } + +} + +#----------------------------------------------------------------------------- + +=head3 db2reboot + + + Setup for db2 to start on reboot + +=cut + +#----------------------------------------------------------------------------- + +sub db2reboot +{ + xCAT::MsgUtils->message("I", "Setting up DB2 to start on reboot"); + my $cmd; + + # su to xcatdb + $cmd = "db2iauto -on xcatdb "; + my $rc = &rundb2cmd($cmd); + if ($rc != 0) + { + xCAT::MsgUtils->message("W", " $cmd failed."); + + } +} + +#----------------------------------------------------------------------------- + +=head3 setupODBC + + Will setup the ODBC. C++ applications are running + that need access to the DB2 database for example LoadLeveler. + +=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); + } + + my @rpmoutput; + my $odbcinstfile; + my $odbcfile; + my $message; + + my $homedir = xCAT::Utils->getHomeDir(); + my $xcatconfig = "/etc/xcat/cfgloc"; + my $xcatconfigbackup = "/etc/xcat/cfgloc.db2"; + if (!(-e ($xcatconfig)) && (!(-e ($xcatconfigbackup)))) + { + $message = + "The $xcatconfig and $xcatconfigbackup files are missing. You need to configure xCAT for DB2 before setting up the ODBC."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + $cmd = "fgrep -i DB2 $xcatconfig"; + my @output; + @output = xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then try backup + { + $cmd = "fgrep -i DB2 $xcatconfigbackup"; + @output = xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then try backup + { + $message = + "Cannot find DB2 info in the cfgloc or cfgloc.db2 file. Configuration of ODBC cannot continue."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + } + + # instance, database and password from cfgloc + (my $databasehdr, my $instance, my $passwd) = split(/\|/, $output[0]); + (my $header, my $database) = split(/:/, $databasehdr); + + # The unixODBC Driver Manager loads the DB2 Driver dynamically + # so the shared object must be extracted from the driver + #if it does not already exist + if (!(-e "/var/lib/db2/sqllib/lib/libdb2.so")) + { + if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX')) + { + $cmd = "ar -x /var/lib/db2/sqllib/lib/libdb2.a"; + @rpmoutput = xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = "Cannot configure the ODBC."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + $cmd = "mv shr.o /var/lib/db2/sqllib/lib/libdb2.so"; + @rpmoutput = xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = "Cannot configure the ODBC."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + } + else + { #sles TBD + xCAT::MsgUtils->message("E", + " SLES ODBC support not implemented yet"); + exit(1); + } + } + + if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX')) + { + $odbcinstfile = "/etc/odbcinst.ini"; + $odbcfile = "/etc/odbc.ini"; + } + else + { #sles + $odbcinstfile = "/etc/unixODBC/odbcinst.ini"; + $odbcfile = "/etc/unixODBC/odbc.ini"; + xCAT::MsgUtils->message("E", " SLES ODBC support not implemented yet"); + exit(1); + } + + # setup the odbcinst.ini file + my $sharedlib = "/var/lib/db2/sqllib/lib/libdb2.so"; + $cmd = "fgrep -i driver $odbcinstfile "; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then driver entry not there + { + my $entry = + "[DB2]\nDescription = ODBC for DB2\nDriver = $sharedlib"; + $cmd = "echo \"$entry\" >> $odbcinstfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup ODBC."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + } + $entry = "FileUsage = 1"; + $cmd = "echo \"$entry\" >> $odbcinstfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup ODBC."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + } + $entry = "DontDLClose = 1"; + $cmd = "echo \"$entry\" >> $odbcinstfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup $odbcinstfile."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + } + } + else + { # entry already there + $message = "$odbcinstfile already configured, will not change."; + xCAT::MsgUtils->message("I", "$message"); + } + + # setup the Database and instance Name in the odbc.ini file + $cmd = "fgrep -i XCATDB $odbcfile"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then xcat entry not there + { + my $entry = "[$instance]\nDriver = DB2\nDATABASE = xcatdb"; + $cmd = "echo \"$entry\" >> $odbcfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup $odbcfile."; + 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 $rootodbcfile = $homedir; + if ($::osname eq "AIX") + { + $rootodbcfile .= ".odbc.ini"; + } + else + { + $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 = + "[$instance]\nDATABASE = $database\nUSER = $instance\nPASSWORD = $passwd"; + $cmd = "echo \"$entry\" >> $rootodbcfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup $rootodbcfile."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + } + else + { # entry already there + $message = "$rootodbcfile already configured, will not change."; + xCAT::MsgUtils->message("I", "$message"); + } + + # allow readonly by root + chmod 0600, $rootodbcfile; + + # if on the client, must setup /var/lib/db2/sqllib/cfg/db2cli.ini + # this is a db2 support bug workaround + + if ($::CLIENT) + { + my $db2clifile = "/var/lib/db2/sqllib/cfg/db2cli.ini"; + my $db2clifilebackup = "/var/lib/db2/sqllib/cfg/db2cli.ini.org"; + if (-e ( $db2clifile)) { + $cmd = "fgrep -i XCATDB $db2clifile"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then xcat entry not there + { + $cmd = "cp $db2clifile $db2clifilebackup "; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup ODBC."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + my $entry = "\n[$instance]\nuid=$instance\npasswd=$passwd"; + $cmd = "echo \"$entry\" >> $db2clifile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup $db2clifile."; + xCAT::MsgUtils->message("E", "$message"); + + } + } + else + { # entry already there + $message = "$db2clifile already configured, will not change."; + xCAT::MsgUtils->message("I", "$message"); + } + } + } +} + +#----------------------------------------------------------------------------- + +=head3 createcfgloc + + Creates the cfgloc file + to run xCAT on DB2 + +=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 $db2entry = "DB2:xcatdb|xcatdb|$::adminpassword"; + $cmd = "echo \"$db2entry\" >> $cfgloc"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup cfgloc"; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + + # allow readonly by root + chmod 0600, $cfgloc; + +} + +#----------------------------------------------------------------------------- + +=head3 restorexcatdb + + Restores the database from ~/xcat-dbback and restarts the xcatd using + DB2 + +=cut + +#----------------------------------------------------------------------------- + +sub restorexcatdb +{ + + # restore the database + xCAT::MsgUtils->message( + "I", + "Restoring the xCat Database with $::backupdir to DB2 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 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); +} + diff --git a/xCAT-client/bin/mysqlsetup b/xCAT-client/bin/mysqlsetup new file mode 100755 index 000000000..99327abba --- /dev/null +++ b/xCAT-client/bin/mysqlsetup @@ -0,0 +1,1545 @@ +#!/usr/bin/env perl +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +# + +#----------------------------------------------------------------------------- + +=head1 mysqlsetup + + + + This is script is called after the installation or upgrade of MySQL + on xCAT. It will automate the setup of the MySQL and xCAT to run + xCAT on the MySQL DB. + Note: if will setup an xcatdb,a xcatadmin, and a MySQL root password. + It will interact for the + password to assign, unless the XCATMYSQLADMIN_PW and the XCATMYSQLROOT_PW + env variables are set to the admin and mysql root password, resp. + Setups up AIX and Linux, but most work needs to be done on AIX. + See man mysqlsetup for more information and the xCAT2.SetupMySQL.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 = "mysqlsetup"; +my $args = join ' ', @ARGV; +$::command = "$0 $args"; +Getopt::Long::Configure("bundling"); +$Getopt::Long::ignorecase = 0; +$::installdir = "/usr/local/mysql"; # current release of xcat-mysql + +#$::installdir="/opt/xcat/mysql"; # next release of xcat-mysql + +# parse the options +if ( + !GetOptions( + 'i|init' => \$::INIT, + 'u|update' => \$::UPDATE, + 'f|hostfile=s' => \$::HOSTFILE, + '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)) && (!($::UPDATE)) && (!($::SETUPODBC))) +{ + xCAT::MsgUtils->message("I", "Either -i or -u or -o flag must be chosen"); + &usage; + exit(1); +} + +# check to see if only odbc update, no passwords needed +my $odbconly = 0; +if ((!($::INIT)) && (!($::UPDATE)) && ($::SETUPODBC)) +{ + $odbconly = 1; + +} +if ((!($::HOSTFILE)) && ($::UPDATE) && ($::SETUPODBC)) +{ + $odbconly = 1; + +} +if (($::INIT) && ($::UPDATE)) +{ + my $warning = + " The -i and -u flags may not be input to the command. Use one or the other. \n "; + xCAT::MsgUtils->message("E", $warning); + exit 1; +} +if (($::UPDATE) && ((!($::HOSTFILE)) && (!($::SETUPODBC)))) +{ + my $warning = + " The -u flag requires the -o flag or the -f flag pointing to a file that contains the list of hosts that you would like to add to database access."; + xCAT::MsgUtils->message("E", $warning); + exit 1; +} +if (($::HOSTFILE) && (!(-e ($::HOSTFILE)))) +{ + my $warning = " The -f flag is pointing to a non-existing file."; + xCAT::MsgUtils->message("E", $warning); + exit 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 mysql is installed +# +my $cmd = "rpm -qa | grep mysql"; +xCAT::Utils->runcmd($cmd, 0); +if ($::RUNCMD_RC != 0) +{ + my $message = + "\nMySQL 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 MySQL is running +$::mysqlrunning = 0; +$::xcatrunningmysql = 0; +my $cmd = "ps -ef | grep mysqld"; +my @output = xCAT::Utils->runcmd($cmd, 0); +my $mysqlcheck = "mysql.sock"; # see if really running +if (grep(/$mysqlcheck/, @output)) +{ + + if ($::INIT) + { + my $message = + "MySQL is running. Database initialization will not take place."; + xCAT::MsgUtils->message("I", "$message"); + } + $::mysqlrunning = 1; +} +if (-e ("/etc/xcat/cfgloc")) # check to see if xcat is using mysql +{ # cfgloc exists + $cmd = "fgrep mysql /etc/xcat/cfgloc"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC == 0) + { + if ($::INIT) + { + my $message = + "The /opt/xcat/cfgloc file is already configured for MySQL. xcat database initialization will not take place."; + xCAT::MsgUtils->message("I", "$message"); + } + $::xcatrunningmysql = 1; + } +} + +# +# if AIX, Set memory unlimited. Linux defaults unlimited +# +if ($::osname eq 'AIX') +{ + &setulimits; +} + +# if not just odbc update +# Get root and admin passwords +# +if ($odbconly == 0) +{ # not just updating the odbc + if ($ENV{'XCATMYSQLADMIN_PW'}) # input env sets the password + { + + $::adminpassword = $ENV{'XCATMYSQLADMIN_PW'}; + + } + else # prompt for password + { + my $msg = "Input the password for xcatadmin in the MySQL database: "; + xCAT::MsgUtils->message('I', "$msg"); + `stty -echo`; + chop($::adminpassword = ); + `stty echo`; + } + if ($ENV{'XCATMYSQLROOT_PW'}) # input env sets the password + { + + $::rootpassword = $ENV{'XCATMYSQLROOT_PW'}; + + } + else # prompt for password + { + + my $msg = "Input the password for root in the MySQL database: "; + xCAT::MsgUtils->message('I', "$msg"); + `stty -echo`; + chop($::rootpassword = ); + `stty echo`; + + } +} + +# initial setup request +if ($::INIT) +{ + + # + # Backup current database + # + my $homedir = xCAT::Utils->getHomeDir(); + $::backupdir = $homedir; + if ($::osname eq 'AIX') + { + $::backupdir .= "xcat-dbback"; + } + else + { + $::backupdir .= "/xcat-dbback"; + } + + &backupxcatdb; + + # + # 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 ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($hname); + my $ipaddr = xCAT::NetworkUtils->getipaddr($hname); + if ($ipaddr) + { + $::MN = $ipaddr; + } + else + { + xCAT::MsgUtils->message("E", "Hostname resolution for $hname failed."); + exit(1); + } + + # MySQL not running, then initialize the database + if ($::mysqlrunning == 0) + { + + # Add mysql user and group for AIX + # Correct directory permissions + # + &fixinstalldir; + + # + # Init mysql db and setup my.cnf + # + &initmysqldb; + + # + # Start MySQL server + # + &mysqlstart; + + # + # Setup MySQL to restart on reboot + # + &mysqlreboot; + + # + # set mysql root password in database + # + # + &setupmysqlroot; + + } + + # if xcat not already configured to run mysql, then add xcat info to the DB + if ($::xcatrunningmysql == 0) + { + + # + # Create xcatd database + # Create xcatadmin in the database + # Add Management Node to database access + # + &setupxcatdb; + + # + # create cfgloc file + # + &createcfgloc; + } + + # + # Restore backed up database into MySQL + # + &restorexcatdb; + + if ($::osname eq 'AIX') + { + xCAT::MsgUtils->message("I", "xCAT is now running on the MySQL database.\nYou should log out and back in, so that the new ulimit settings will take affect."); + } else { + xCAT::MsgUtils->message("I", "xCAT is now running on the MySQL database."); + } + +} # end initialization + +if ($::SETUPODBC) +{ + + # + # set up the ODBC on the Management Node + # + + &setupODBC; + +} + +# if input a list of hosts to add to the database, to give access to MySQL +if ($::HOSTFILE) +{ + &addhosts; + +} +exit; + +##################################### +# subroutines +##################################### + +#----------------------------------------------------------------------------- + +=head3 usage + + Displays message for -h option + +=cut + +#----------------------------------------------------------------------------- + +sub usage +{ + xCAT::MsgUtils->message( + 'I', + "Usage:\nmysqlsetup - Performs the setup of MySQL for xCAT to use as its database. See man mysqlsetup for more information." + ); + my $msg = + "mysqlsetup <-h|--help>\n <-v|--version>\n <-i|--init> [-f|hostfile] [-o|--setupODBC] [-V|--verbose]\n <-u|--update> <-f|hostfile> [-o|--setupODBC] [-V|--verbose]\n <-o|--setupODBC> [-V|--verbose]"; + + xCAT::MsgUtils->message('I', "$msg"); +} + +#----------------------------------------------------------------------------- + +=head3 setulimits + + sets ulimits unlimited, needed to run MySQL + update /etc/security/limits with the info + +=cut + +#----------------------------------------------------------------------------- + +sub setulimits +{ + my $limitsfile = "/etc/security/limits"; + my $limitstmpfile = "/etc/security/limits.tmp"; + my $limitsbackup = "/etc/security/limits.backup"; + + # backup ulimits if not already backed up + if (!(-e ("$limitsbackup"))) + { + $cmd = "cp $limitsfile $limitsbackup"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + + # add ulimits for root to /etc/security/limits + unless (open(LIMITS, "<$limitsfile")) + { + xCAT::MsgUtils->message('E', "Error opening $limitsfile."); + exit(1); + } + unless (open(LIMITSTMP, ">$limitstmpfile")) + { + xCAT::MsgUtils->message('E', "Error opening $limitstmpfile."); + exit(1); + } + my $rootstanza = 0; + foreach my $line () + { + if ($rootstanza == 1) + { # dealing with root stanza, skip all entries + if (!($line =~ /:/)) + { # still in root stanza + next; # skip root stanza info + } + else + { # continue through the file + $rootstanza = 0; + } + } + print LIMITSTMP $line; + if ($line =~ /root:/) + { # at root stanza, add unlimits + print LIMITSTMP " fsize = -1\n"; + print LIMITSTMP " core= -1\n"; + print LIMITSTMP " cpu= -1\n"; + print LIMITSTMP " data= -1\n"; + print LIMITSTMP " rss= -1\n"; + print LIMITSTMP " stack= -1\n"; + print LIMITSTMP " nofiles= -1\n"; + print LIMITSTMP "\n"; + $rootstanza = 1; + } + } + + close(LIMITS); + close(LIMITSTMP); + + # copy new limits to old + $cmd = "cp $limitstmpfile $limitsfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + +} + +#----------------------------------------------------------------------------- + +=head3 backupxcatdb + + Backup xCATdb + +=cut + +#----------------------------------------------------------------------------- + +sub backupxcatdb + +{ + + # If there is no backup or the /etc/xcat/cfgloc file does not point to + # mysql, then we backup the database + my $sitefile = "$::backupdir/site.csv"; + + if ((!(-e $sitefile)) || ($::xcatrunningmysql == 0)) + { + 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 fixinstall + + If AIX, Fixes ownership and permssion on install + adds mysql user and group + +=cut + +#----------------------------------------------------------------------------- + +sub fixinstalldir +{ + + if ($::osname eq 'AIX') + { + + # + # mk mysql group and user + # + my $cmd = "lsgroup mysql"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + # mysql group does not exist, need to make it + $cmd = "mkgroup mysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + $cmd = "lsuser mysql"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + # mysql user does not exist, need to make it + $cmd = "mkuser pgrp=mysql mysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + + # + # correct installed directory permissions + # + xCAT::MsgUtils->message( + 'I', + "Fixing install directory permissions.\nThis may take a few minutes." + ); + my $mysqldir = $::installdir; + $mysqldir .= "\/*"; + $cmd = "chown -R mysql $mysqldir"; + xCAT::Utils->runcmd($cmd, 0); + + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "chgrp -R mysql $mysqldir"; + xCAT::Utils->runcmd($cmd, 0); + + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + } +} + +#----------------------------------------------------------------------------- + +=head3 initmysqldb + + + Create the MySQL data directory and initialize the grant tables + Setup my.cnf + +=cut + +#----------------------------------------------------------------------------- +sub initmysqldb +{ + my $cmd; + + if (($::osname eq 'AIX') && (!(-e "/etc/my.cnf"))) + { + $cmd = "cp $::installdir/support-files/my-large.cnf /etc/my.cnf"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + } + + + # for AIX, insert datadir=/var/lib/mysql in the [mysqld] stanza + # of the /etc/my.cnf file,if it is not already there + if ($::osname eq 'AIX') + { + $cmd = "fgrep datadir /etc/my.cnf"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + $cmd = + "awk '{gsub(\"\\\\[mysqld]\",\"\\[mysqld]\\ndatadir=/var/lib/mysql \"); print}' /etc/my.cnf > /etc/my.cnf.xcat"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "cp -p /etc/my.cnf.xcat /etc/my.cnf"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + + # + # make data dir owned by mysql and root everything else + # + my $mysqldir = $::installdir; + $mysqldir .= "\/*"; + $cmd = "chown -R root $mysqldir"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + my $mysqldatadir = "$::installdir/data"; + $mysqldatadir .= "\/*"; + $cmd = "chown -R mysql $mysqldatadir"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "chgrp -R mysql $mysqldatadir"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + # make the database directory if it does not exist and + # make mysql the owner + if (!(-e ("/var/lib/mysql"))) + { + $cmd = "mkdir -p /var/lib/mysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "chown -R mysql /var/lib/mysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + $cmd = "chgrp -R mysql /var/lib/mysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + } + } # end AIX only + + # Create the MySQL data directory and initialize the grant tables + # if not already setup + my $cmd2 = + "ulimit -n unlimited; ulimit -m unlimited; ulimit -d unlimited;ulimit -f unlimited; ulimit -s unlimited;"; + if ($::osname eq 'AIX') + { + $cmd = $cmd2; + $cmd .= + "$::installdir/scripts/mysql_install_db --user=mysql --basedir=$::installdir"; + } + else + { + $cmd = "/usr/bin/mysql_install_db --user=mysql"; + } + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + + exit(1); + } +} + +#----------------------------------------------------------------------------- + +=head3 mysqlstart + + + Start the mysql server + +=cut + +#----------------------------------------------------------------------------- + +sub mysqlstart +{ + my $cmd; + if ($::osname eq 'AIX') + { + my $hostname = `hostname`; + chomp $hostname; + + #$cmd = "$::installdir/bin/mysqld_safe --user=mysql &"; #doesnot return + my $cmd2 = + "ulimit -n unlimited; ulimit -m unlimited; ulimit -d unlimited;ulimit -f unlimited; ulimit -s unlimited;"; + $cmd = $cmd2; + $cmd .= + "$::installdir/bin/mysqld --user=mysql --basedir=$::installdir --datadir=/var/lib/mysql --user=mysql --log-error=/var/lib/mysql/$hostname.err --pid-file=/var/lib/mysql/$hostname.pid --socket=/tmp/mysql.sock --port=3306 &"; + } + else + { + if ($::linuxos =~ /rh.*/) + { + $cmd = "service mysqld start"; + } + else + { # sles + $cmd = "service mysql start"; + } + } + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + # make sure running + $cmd = "ps -ef | grep mysqld"; + my $i; + for ($i = 0 ; $i < 12 ; $i++) + { + my @output = xCAT::Utils->runcmd($cmd, 0); + my $mysqlcheck = "--datadir"; # see if really running + if (grep(/$mysqlcheck/, @output)) + { + sleep 10; # give a few extra seconds to be sure + return; + } + else + { + sleep 10; # wait for daemon + } + } + xCAT::MsgUtils->message("E", + " Could not start the mysql daemon, in time allocated ( 2minutes)"); + exit(1); + +} + +#----------------------------------------------------------------------------- + +=head3 mysqlreboot + + + Setup for MySQL to start on reboot + +=cut + +#----------------------------------------------------------------------------- + +sub mysqlreboot +{ + my $cmd; + if ($::osname eq 'AIX') + { + $cmd = "fgrep mysql /etc/inittab"; + xCAT::Utils->runcmd($cmd, -1); + + # if not already in inittab + if ($::RUNCMD_RC != 0) + { + + # backup inittab + if (!(-e ("/etc/inittab.org"))) + { + $cmd = "cp -p /etc/inittab /etc/inittab.org"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", + " $cmd failed. Could not backup inittab."); + exit(1); + } + } + + $cmd = + "awk '{gsub(\"xcatd:2:once:/opt/xcat/sbin/restartxcatd > /dev/console 2>&1\",\"mysql:2:once:/usr/local/mysql/bin/mysqld_safe --user=mysql \\& \\nxcatd:2:once:/opt/xcat/sbin/restartxcatd > /dev/console 2>\\&1\"); print}' /etc/inittab > /etc/inittab.xcat"; + + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + # put the new mysql entry in /etc/inittab + $cmd = "cp -p /etc/inittab.xcat /etc/inittab"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", + " $cmd failed. MySQL will not restart on reboot."); + } + } + } + else # linux + { + if ($::linuxos =~ /rh.*/) + { + $cmd = "chkconfig mysqld on"; + } + else + { # sles + $cmd = "chkconfig mysql on"; + } + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", + " $cmd failed. MySQL will not restart on reboot."); + } + } + +} + +#----------------------------------------------------------------------------- + +=head3 setupmysqlroot + + + Set mysql root password in the database + +=cut + +#----------------------------------------------------------------------------- + +sub setupmysqlroot + +{ + my $cmd; + + # set root password in database + if ($::osname eq 'AIX') + { + my $cmd2 = + "ulimit -n unlimited; ulimit -m unlimited; ulimit -d unlimited;ulimit -f unlimited; ulimit -s unlimited;"; + $cmd = $cmd2; + $cmd .= "$::installdir/bin/mysqladmin -u root password $::rootpassword"; + } + else + { + $cmd = "/usr/bin/mysqladmin -u root password $::rootpassword"; + } + xCAT::Utils->runcmd($cmd, 0); + + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message( + "I", + "Warning, mysqladmin -u root password command failed, trying to set root password in MySQL. If root id has been defined in MySQL, and has a password then this message can be ignored." + ); + + } + +} + +#----------------------------------------------------------------------------- + +=head3 setupxcatdb + + Creates the xcatdb in MySQL + Add xcatadmin to the database + Give Management Node database access + +=cut + +#----------------------------------------------------------------------------- + +sub setupxcatdb + +{ + my $mysql; + my $timeout = 10; # sets Expect default timeout, 0 accepts immediately + my $pwd_sent = 0; + my $pwd_prompt = 'Enter password: '; + my $mysql_prompt = 'mysql> '; + my $expect_log = undef; + my $debug = 0; + if ($::VERBOSE) + { + $debug = 1; + } + $mysql = new Expect; + my $createuser = + "CREATE USER xcatadmin IDENTIFIED BY \'$::adminpassword\';\r"; + my $grantall = ""; + $grantall = "GRANT ALL on xcatdb.* TO xcatadmin@"; + $grantall .= "\'"; + $grantall .= "$::MN"; + $grantall .= "\'"; + $grantall .= " IDENTIFIED BY \'$::adminpassword\';\r"; + + # + # -re $pwd_prompt + # Enter the password for root + # + # -re $mysql_prompt + # mysql> Enter the Create Database SQL command and exit + # + # + + # disable command echoing + #$mysql->slave->stty(qw(sane -echo)); + + # + # exp_internal(1) sets exp_internal debugging + # to STDERR. + # + #$mysql->exp_internal(1); + $mysql->exp_internal($debug); + + # + # log_stdout(0) prevent the program's output from being shown. + # + #$mysql->log_stdout(1); + $mysql->log_stdout($debug); + + my $spawncmd; + if ($::osname eq 'AIX') + { + $spawncmd = "$::installdir/bin/mysql -u root -p"; + } + else + { + $spawncmd = "/usr/bin/mysql -u root -p"; + } + unless ($mysql->spawn($spawncmd)) + { + xCAT::MsgUtils->message("E", + "Unable to run $spawncmd to create database and add MN."); + return; + + } + + # + # setup SQL commands + # + + my @result = $mysql->expect( + $timeout, + [ + $pwd_prompt, + sub { + $mysql->send("$::rootpassword\r"); + $mysql->clear_accum(); + $mysql->exp_continue(); + } + ], + [ + $mysql_prompt, + sub { + + $mysql->send("CREATE DATABASE xcatdb;ALTER DATABASE xcatdb DEFAULT CHARACTER SET latin1;\r"); + $mysql->clear_accum(); + $mysql->send("$createuser"); + $mysql->clear_accum(); + $mysql->send("$grantall"); + $mysql->clear_accum(); + $mysql->send("exit;\r"); + + } + ] + ); + ########################################## + # Expect error - report and quit + ########################################## + if (defined($result[1])) + { + my $errmsg = $result[1]; + $mysql->soft_close(); + xCAT::MsgUtils->message("E", + "Perl Expect failed creating database. $errmsg"); + exit(1); + + } + $mysql->soft_close(); + +} + +#----------------------------------------------------------------------------- + +=head3 addhosts + + Will add all host ids that need access to the MySQL database. + User will input names, ipaddress or wild cards like 9.112.%.% + or %.ibm.com in a file after the -f flag. + +=cut + +#----------------------------------------------------------------------------- + +sub addhosts + +{ + my @hosts; + my $debug = 0; + if ($::VERBOSE) + { + $debug = 1; + } + + open(HOSTFILE, "<$::HOSTFILE") + or + xCAT::MsgUtils->message('S', "Cannot open $::HOSTFILE for node list. \n"); + foreach my $line () + { + chop $line; + push @hosts, $line; # add hosts + } + close HOSTFILE; + my $mysql; + my $timeout = 10; # sets Expect default timeout, 0 accepts immediately + my $pwd_sent = 0; + my $pwd_prompt = 'Enter password: '; + my $mysql_prompt = 'mysql> '; + my $expect_log = undef; + + foreach my $host (@hosts) + { + my $grantall = ""; + $grantall = "GRANT ALL on xcatdb.* TO xcatadmin@"; + $grantall .= "\'"; + $grantall .= "$host"; + $grantall .= "\'"; + $grantall .= " IDENTIFIED BY \'$::adminpassword\';\r"; + $mysql = new Expect; + + # + # -re $pwd_prompt + # Enter the password for root + # + # -re $mysql_prompt + # mysql> Enter the GRANT ALL SQL command for each host and exit + # + # + + # disable command echoing + #$mysql->slave->stty(qw(sane -echo)); + + # + # exp_internal(1) sets exp_internal debugging + # to STDERR. + # + #$mysql->exp_internal(1); + $mysql->exp_internal($debug); + + # + # log_stdout(0) prevent the program's output from being shown. + # + #$mysql->log_stdout(1); + $mysql->log_stdout($debug); + my $spawncmd; + if ($::osname eq 'AIX') + { + $spawncmd = "$::installdir/bin/mysql -u root -p"; + } + else + { + $spawncmd = "/usr/bin/mysql -u root -p"; + } + unless ($mysql->spawn($spawncmd)) + { + xCAT::MsgUtils->message( + "E", + "Unable to run $spawncmd to grant host access to the database." + ); + return; + + } + + # + # setup SQL commands + # + + my @result = $mysql->expect( + $timeout, + [ + $pwd_prompt, + sub { + $mysql->send("$::rootpassword\r"); + $mysql->clear_accum(); + $mysql->exp_continue(); + } + ], + [ + $mysql_prompt, + sub { + + $mysql->send("$grantall"); + $mysql->clear_accum(); + $mysql->send("exit;\r"); + + } + ] + ); + ########################################## + # Expect error - report and quit + ########################################## + if (defined($result[1])) + { + my $errmsg = $result[1]; + $mysql->soft_close(); + xCAT::MsgUtils->message("E", + "Perl Expect failed adding hosts. $errmsg"); + exit(1); + + } + $mysql->soft_close(); + } +} + +#----------------------------------------------------------------------------- + +=head3 setupODBC + + Will setup the ODBC. Only needed if C, C++ applications are running + that need access to the MySQL database for example LoadLeveler. + +=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); + } + + # for aix and redhat + if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX')) + { + $cmd = "rpm -qa | grep mysql-connector-odbc"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = + "\nxcat-connector-odbc is not installed. It should be first obtained from the xcat dependency tarballs and installed before running this command. If on Linux, install from the OS CDs."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + } + else # sles + { + $cmd = "rpm -qa | grep mysql-client"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = + "\nmysql-client is not installed. It should be first installed from the SLES CDs."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + $cmd = "rpm -qa | grep libmysqlclient"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = + "\nlibmysqlclient is not installed. It should be first installed from the SLES CDs."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + $cmd = "rpm -qa | grep MyODBC-unixODBC"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + my $message = + "\n$cmd failed. MyODBC-unixODBC is not installed. It should be first installed from the SLES CDs."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + } + } # end sles + my @rpmoutput; + my $odbcinstfile; + my $odbcfile; + my $message; + + # configure the ODBC, again SLES different than the rest + my $xcatconfig = "/etc/xcat/cfgloc"; + my $xcatconfigbackup = "/etc/xcat/cfgloc.mysql"; + if (!(-e ($xcatconfig)) && (!(-e ($xcatconfigbackup)))) + { + $message = + "The $xcatconfig and $xcatconfigbackup files are missing. You need to configure xCAT for MySQL 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) # then try backup + { + $cmd = "fgrep -i host $xcatconfigbackup"; + @output = xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then try backup + { + $message = + "Cannot find host info in the cfgloc or cfgloc.mysql file. Configuration of ODBC cannot continue."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + } + + # get host and password from cfgloc + (my $connstring, my $adminid, my $passwd) = split(/\|/, $output[0]); + (my $database, my $id, my $server) = split(/=/, $connstring); + + if (($::linuxos =~ /rh.*/) || ($::osname eq 'AIX')) + { + $odbcinstfile = "/etc/odbcinst.ini"; + $odbcfile = "/etc/odbc.ini"; + $cmd = "rpm -ql mysql-connector-odbc | grep libmyodbc..so"; + @rpmoutput = xCAT::Utils->runcmd($cmd, 0); + } + else + { #sles + $odbcinstfile = "/etc/unixODBC/odbcinst.ini "; + $odbcfile = "/etc/unixODBC/odbc.ini "; + $cmd = "rpm -ql rpm -ql MyODBC-unixODBC | grep libmyodbc..so"; + @rpmoutput = xCAT::Utils->runcmd($cmd, 0); + } + if ($::RUNCMD_RC != 0) + { + my $message = "Cannot configure the ODBC."; + xCAT::MsgUtils->message("E", " $cmd failed. $message"); + exit(1); + } + + # setup the odbcinst.ini file + my $sharedlib = $rpmoutput[0]; + $cmd = "fgrep -i driver $odbcinstfile "; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then driver entry not there + { + my $entry = + "[MySQL]\nDescription = ODBC for MySQL\nDriver = $sharedlib"; + $cmd = "echo \"$entry\" >> $odbcinstfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup ODBC."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + } + } + else + { # entry already there + $message = "$odbcinstfile already configured, will not change."; + xCAT::MsgUtils->message("I", "$message"); + } + + # setup the DSN odbc.ini file + $cmd = "fgrep -i XCATDB $odbcfile"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) # then xcat entry not there + { + my $entry = + "[xCATDB]\nDriver = MySQL\nSERVER = $server\nPORT = 3306\nDATABASE = xcatdb"; + $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; + if ($::osname eq "AIX") + { + $rootodbcfile .= ".odbc.ini"; + } + else + { + $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 = + "[xCATDB]\nSERVER =$server\nDATABASE = xcatdb\nUSER = xcatadmin\nPASSWORD = $passwd"; + $cmd = "echo \"$entry\" >> $rootodbcfile"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup root ODBC file."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + } + else + { # entry already there + $message = "$rootodbcfile already configured, will not change."; + xCAT::MsgUtils->message("I", "$message"); + } + + # allow readonly by root + chmod 0600, $rootodbcfile; + +} + +#----------------------------------------------------------------------------- + +=head3 createcfgloc + + Creates the cfgloc.mysql file which will be copied to cfgloc + to run xCAT on MySQL + +=cut + +#----------------------------------------------------------------------------- + +sub createcfgloc + +{ + my $cfglocmysql = "/etc/xcat/cfgloc.mysql"; + my $cfglocmysqlbackup = "/etc/xcat/cfgloc.mysql.backup"; + my $cmd; + my $message; + if (-e ($cfglocmysql)) + { + $cmd = "mv $cfglocmysql $cfglocmysqlbackup"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed."; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + } + my $mysqlentry = + "mysql:dbname=xcatdb;host=$::MN|xcatadmin|$::adminpassword"; + $cmd = "echo \"$mysqlentry\" >> $cfglocmysql"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup cfgloc.mysql"; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + + # allow readonly by root + chmod 0600, $cfglocmysql; + +} + +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- + +=head3 restorexcatdb + + Restores the database from ~/xcat-dbback and restarts the xcatd using + MySQL + +=cut + +#----------------------------------------------------------------------------- + +sub restorexcatdb +{ + + # copy the mysql cfgloc file + my $cmd; + if ($::xcatrunningmysql == 0) + { # xcat cfgloc not configured + + # if they had an old cfgloc on another database, save it + if ((-e ("/etc/xcat/cfgloc")) && (!(-e ("/etc/xcat/cfgloc.olddb")))) + { + $cmd = "cp /etc/xcat/cfgloc /etc/xcat/cfgloc.olddb"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + } + } + + # put in place cfgloc for mysql + $cmd = "cp /etc/xcat/cfgloc.mysql /etc/xcat/cfgloc"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + + # allow readonly by root + chmod 0600, "/etc/xcat/cfgloc"; + + # restore the database + xCAT::MsgUtils->message( + "I", + "Restoring the xCat Database with $::backupdir to MySQL 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 restorexCATdb -p $::backupdir"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + + # + # restart the daemon + # + my $xcmd; + if ($::osname eq 'AIX') + { + $xcmd = "$::XCATROOT/sbin/restartxcatd"; + + } + else + { + $xcmd = "service xcatd restart"; + } + system($xcmd); +} + diff --git a/xCAT-client/bin/pgsqlsetup b/xCAT-client/bin/pgsqlsetup new file mode 100755 index 000000000..68014e2f5 --- /dev/null +++ b/xCAT-client/bin/pgsqlsetup @@ -0,0 +1,914 @@ +#!/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 = ); + `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 now 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 + +{ + + # If there is no backup or the /etc/xcat/cfgloc file does not point to + # pgsql, then we backup the database + my $sitefile = "$::backupdir/site.csv"; + + if ((!(-e $sitefile)) || ($::xcatrunningpgsql == 0)) + { + 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~; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) + { + + xCAT::MsgUtils->message("E", " $cmd failed."); + exit(1); + } + } + + # + # 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"; + xCAT::Utils->runcmd($cmd, 0); + if ($::RUNCMD_RC != 0) + { + $message = "$cmd failed. Could not setup cfgloc"; + xCAT::MsgUtils->message("E", "$message"); + exit(1); + + } + + # allow readonly by root + chmod 0600, $cfgloc; + +} + +#----------------------------------------------------------------------------- + +=head3 restorexcatdb + + Restores the database from ~/xcat-dbback and restarts the xcatd using + MySQL + +=cut + +#----------------------------------------------------------------------------- + +sub restorexcatdb +{ + + # 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 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); +} +