File:  [LON-CAPA] / loncom / metadata_database / configure_mysql_db.pl
Revision 1.2: download - view: text, annotated - select for diffs
Tue Jul 22 19:24:35 2003 UTC (20 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Pretty major rewrite:
    Now attempt to detect absence of DBI and MySQL DBD component and, if
possible, fix it automatically.
    Added creation of dynamicmetadata table in database.

This script is *almost* ready to be run during "make install".

    1: #!/usr/bin/perl -w
    2: # The LearningOnline Network 
    3: # Red Hat 7.3 installation script
    4: #
    5: # $Id: configure_mysql_db.pl,v 1.2 2003/07/22 19:24:35 matthew Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: use strict;
   28: use lib '/home/httpd/lib/perl/';
   29: use LONCAPA::Configuration;
   30: 
   31: my $runstatus = shift();
   32: if (! defined($runstatus)) {
   33:     $runstatus = 'firstrun';
   34: }
   35: 
   36: my $rpmname = "perl-DBD-MySQL-1.2216-4.i386.rpm";
   37: 
   38: my $perlvar = &LONCAPA::Configuration::read_conf('loncapa.conf');
   39: delete($perlvar->{'lonReceipt'}); # remove since sensitive and not needed
   40: 
   41: ##
   42: ## Set up mysql to run on boot
   43: ##
   44: my $add_result = system("/sbin/chkconfig --add mysqld 2>/dev/null");
   45: my $on_result = system("/sbin/chkconfig mysqld on 2>/dev/null");
   46: if ($add_result != 0 || $on_result != 0) {
   47:     print "Your system is misconfigured.  ".
   48:         "You do not have MySQL installed.\n";
   49:     exit 1;
   50: }
   51: 
   52: ##
   53: ## Make sure it is running now
   54: ##
   55: my $status = system("/etc/rc.d/init.d/mysqld status 1>/dev/null");
   56: if ($status != 0) {
   57:     print(`/etc/rc.d/init.d/mysqld restart`);
   58:     print("Waiting 5 seconds for mysql daemon to restart.\n");
   59:     sleep 5;
   60:     my $status = system("/etc/rc.d/init.d/mysqld status");
   61:     if ($status != 0) {
   62:         die "Unable to start mysql daemon\nHalting\n";
   63:     }
   64: }
   65: 
   66: ##
   67: ## Make sure the mysql is configured
   68: ##
   69: if ($runstatus eq 'firstrun' && (! exists($perlvar->{'lonSqlAccess'}) || 
   70:                                  ! defined($perlvar->{'lonSqlAccess'}))) {
   71:     ##
   72:     ## Get root password for mysql client
   73:     print <<END;
   74: Please enter a root password for the MySQL database.
   75: It does not have to match your root account password, but you will need
   76: to remember it.
   77: END
   78:     my $rootpass = <>;
   79:     chomp $rootpass;
   80:     print("\n");
   81: 
   82:     print("Starting mysql client and setting up permissions\n");
   83:     open MYSQL, "|mysql -u root mysql" || die "Unable to start mysql\n";
   84:     print MYSQL <<"ENDMYSQLPERMISSIONINIT";
   85: CREATE DATABASE IF NOT EXISTS loncapa;
   86: USE mysql;
   87: INSERT IGNORE INTO user (Host, User, Password)
   88:     VALUES ('localhost','www',password('localhostkey'));
   89: INSERT IGNORE INTO db VALUES ('localhost','loncapa','www',
   90:     'Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
   91: SET PASSWORD FOR root\@localhost=PASSWORD('$rootpass');
   92: DELETE FROM user WHERE host<>'localhost';
   93: FLUSH PRIVILEGES;
   94: EXIT
   95: ENDMYSQLPERMISSIONINIT
   96:     $perlvar->{'lonSqlAccess'} = 'localhostkey';
   97: }
   98: 
   99: my $dbi_is_present = eval "use DBI;"; # will be defined on error
  100: if (defined($dbi_is_present)) {
  101:     print "Your system is misconfigured.  ".
  102:         "You are missing the perl DBI package\n";
  103:     exit 1;
  104: }
  105: 
  106: my $dbh;
  107: my $MySQL_Check = eval '$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar->{\'lonSqlAccess\'});';
  108: if (! defined($MySQL_Check)) {
  109:     if ($runstatus ne 'firstrun') {
  110:         print "Your system is misconfigured.\n";
  111:         exit 1;
  112:     }
  113:     #
  114:     # Try to install from the current directory.  
  115:     # This is potentially dangerous
  116:     my $install_result = system("rpm -Uvh $rpmname 2>/dev/null 1>/dev/null");
  117:     if (defined($install_result) && $install_result ne '0') {
  118:         print "Your system is misconfigured.  ".
  119:             "You are missing the perl DBD MySQL package\n";
  120:         exit 1;
  121:     }
  122:     exec "./".$0." secondrun";
  123: }
  124: 
  125: use DBI;
  126: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar->{'lonSqlAccess'});
  127: if (! defined($dbh)) {
  128:     die "Unable to connect to mysql database.\n";
  129: }
  130: $dbh->do(<<ENDDBINIT);
  131: CREATE TABLE IF NOT EXISTS metadata (title TEXT, 
  132:                                      author TEXT, 
  133:                                      subject TEXT, 
  134:                                      url TEXT, 
  135:                                      keywords TEXT, 
  136:                                      version TEXT, 
  137:                                      notes TEXT, 
  138:                                      abstract TEXT, 
  139:                                      mime TEXT, 
  140:                                      language TEXT, 
  141:                                      creationdate DATETIME, 
  142:                                      lastrevisiondate DATETIME, 
  143:                                      owner TEXT, 
  144:                                      copyright TEXT, 
  145:                                      FULLTEXT idx_title (title), 
  146:                                      FULLTEXT idx_author (author), 
  147:                                      FULLTEXT idx_subject (subject), 
  148:                                      FULLTEXT idx_url (url), 
  149:                                      FULLTEXT idx_keywords (keywords), 
  150:                                      FULLTEXT idx_version (version), 
  151:                                      FULLTEXT idx_notes (notes), 
  152:                                      FULLTEXT idx_abstract (abstract), 
  153:                                      FULLTEXT idx_mime (mime), 
  154:                                      FULLTEXT idx_language (language), 
  155:                                      FULLTEXT idx_owner (owner), 
  156:                                      FULLTEXT idx_copyright (copyright)
  157:                                      ) TYPE=MYISAM
  158: ENDDBINIT
  159: 
  160: $dbh->do(<<ENDDBINIT);
  161: CREATE TABLE IF NOT EXISTS dynamicmetadata (url TEXT NOT NULL, 
  162:                                             count INT UNSIGNED,
  163:                                             num_uses INT UNSIGNED,
  164:                                             clear REAL UNSIGNED,
  165:                                             depth REAL UNSIGNED,
  166:                                             helpful REAL UNSIGNED,
  167:                                             correct REAL UNSIGNED,
  168:                                             technical REAL UNSIGNED,
  169:                                             avetries REAL UNSIGNED,
  170:                                             stdno INT UNSIGNED)
  171: 
  172: ENDDBINIT
  173: 
  174: if ($dbh->err) {
  175:     print "Error creating tables: ".$dbh->errstr()."\n";
  176: }
  177: $dbh->disconnect();
  178: 
  179: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>