Project: Samantha


Back To DocsBack To Index3/31/00
makedb.pl - William Fong (wfong@users.sourceforge.net)

The makedb.pl script creates the necessary MySQL databases and sets up the permissions for each table.  It also writes a configuration file in /etc under the file name projsam.conf.  A detailed description of what this script does is as follows:

BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }
This stops warning messages when $DOWARN was set to 0.  This was needed on the root test.  Wanted the script to look pretty, and seeing those error messages wasn't.
use DBI;
Use MySQL database features.
print "\n\n\n-----------------------------";
print "\nWelcome to Project: Samantha.\n";
print "\nPlease visit http://proj-samantha.sourceforge.net for the latest information.\n";
print "\nThis program will create your \"Samantha\" Database for MySQL. All you need to do is provide us with some information.\n";
Here, we gave some information to the user running the script.  We didn't try to make this look pretty with automatic word wrapping because I didn't want to make the user install another Perl module that was only used for cosmetics.  This was meant to run on a server and probably never seen again.
$DOWARN = 0;
$d = int(rand 100);
open(MYRANDFILE, ">/etc/projsam.$d");
print MYRANDFILE "$d";
eval( "close(MYRANDFILE)");
This section is our root test.  We set $DOWARN to zero that way this section won't report and warnings.  We make a random number and create the file /etc/projsam.### and try to write to it.  Basically, if we can't write the file, we aren't root.  Used random numbers to make SURE there wasn't that file to begin with.
if (-e "/etc/projsam.$d") {
# Good, we are root, so we dont need to do anything but delete our crap
system("rm -f /etc/projsam.$d");
}
else {
# Ok, looks like we don't have root, time to bring them the bad news...
print "Looks like you aren't root. Please change to root and come back.\n\n";
exit 0;
}
$DOWARN = 1;
Here, if the file exists, we just delete it.  If the file didn't exist, we tell the user to change to root and come back in and drop the user out of the script.  Then we set $DOWARN back to 1 so we can start receiving errors.
print "\n\nWhat is the root password for MySQL? Not your UNIX root user password. > ";
$rootpasswd = <STDIN>;
chomp $rootpasswd;
print "\nWhat password would you like to use for the regular user Samantha uses? > ";
$userpasswd = <STDIN>;
chomp $userpasswd;
Here we ask the user for the MySQL password and the password for the user tables.  We didn't want to specify in the scripts the password because then everyone would be downloading the same passwords and it seems like a security risk.
$dbhandle = DBI->connect("dbi:mysql:mysql", "root", "$rootpasswd") || &error("Could not connect to the database.".$?);
$sql = "CREATE DATABASE samanthadb";
#This was the original line...
#$dbhandle->do($sql) || &error($dbhandle->errstr);
$dbhandle->do($sql) || warn 'Database already created... but that\'s ok!';
We open the database as root and connect to the mysql database.  We create the database, but if it errors out, we just get a nice warning and continue.
$sql = "USE samanthadb";
$dbhandle->do($sql) || &error($dbhandle->errstr);
$sql = "CREATE TABLE mp3list_tbl(";
$sql .= "mp3id int(7) NOT NULL PRIMARY KEY AUTO_INCREMENT";
$sql .= ", filename varchar(250)";
$sql .= ", title varchar(75)";
$sql .= ", artist varchar(75)";
$sql .= ", album varchar(30)";
$sql .= ", year int(4)";
$sql .= ", genre varchar(15)";
$sql .= ", bitrate int(3))";
$dbhandle->do($sql) || warn 'Table already created... but that\'s ok!';
We create the mp3list database here.  Self explanatory.
$sql = "CREATE TABLE votestat_tbl(";
$sql .= "mp3id int(7) NOT NULL PRIMARY KEY";
$sql .= ", votes int(3)";
$sql .= ", ipaddr text)";
$dbhandle->do($sql) || warn 'Table already created... but that\'s ok!';
Again, pretty self explanatory.
$sql = "GRANT SELECT,INSERT,UPDATE,CREATE ON samanthadb.votestat_tbl TO samanthad1\@localhost IDENTIFIED BY '$userpasswd'";
$dbhandle->do($sql) || &error($dbhandle->errstr);
$sql = "GRANT SELECT ON samanthadb.mp3list_tbl TO samanthad1\@localhost IDENTIFIED BY '$userpasswd'";
$dbhandle->do($sql) || &error($dbhandle->errstr);
$sql = "GRANT SELECT,INSERT,UPDATE,CREATE ON samanthadb.mp3list_tbl TO samanthad2\@localhost IDENTIFIED BY '$userpasswd'";
$dbhandle->do($sql) || &error($dbhandle->errstr);
$sql = "GRANT SELECT ON samanthadb.votestat_tbl TO samanthad2\@localhost IDENTIFIED BY '$userpasswd'";
$dbhandle->do($sql) || &error($dbhandle->errstr);
$dbhandle->disconnect();
Just created the two users.  First user, samanthad1 is going to be our Web user.  She'll be in charge of making sure she can write only to the votestat_tbl table and have only read access to mp3list.  samanthad2 is our internal/backend user who'll be writing the new files to the database as they are being imported, and reading from votestat_tbl to find out what needs to be played.
print "\n\n\n\n----------------------------------------------\n";
print "Database has been created.\n";
print "Database Name: samanthadb\n";
print "Database Users:\n";
print " For Web Access - samanthad1\n";
print " For Internal Scripts - samanthad2\n";
print " Passwords For Both Users - $userpasswd\n";
Here we are telling the user what we did.  We created the database, and added the two users, and tell them what password they chose.
$exist="0";
if (-e "/etc/projsam.conf" ) {
print "/etc/projsam.conf already exists! Dumping contents to /etc/projsam1.conf";
# Find out if projsam1.conf already exists.
if (-e "/etc/projsam1.conf" ){
open(OLDCONFIG, ">>/etc/projsam1.conf");
print OLDCONFIG "\n ------- Adding Old projsam.conf file below ------- \n";
}
else {
open(OLDCONFIG, ">/etc/projsam1.conf");
print OLDCONFIG "\n ------- Starting New projsam1.conf ------- \n";
}
open(NEWCONFIG, "/etc/projsam.conf");
print OLDCONFIG <NEWCONFIG>;
close(NEWCONFIG);
close(OLDCONFIG);
$exist="1";
}
Here we check to see if there was a projsam.conf file.  Not sure why there would be one, but, we need to be sure!  We not only check to see if the original/main file projsam.conf exists, but if the backup, projsam1.conf, exists.  If the backup exists, we add the old configuration file to the end.  If not, we just made a new file.  We also set a variable, $exists, that we'll use at the end to tell the user that we created this file.
open(NEWCONFIG, ">/etc/projsam.conf");

# List of things to write to config

# Print $userpasswd variable
print NEWCONFIG "userpasswd = $userpasswd\n";
# End of list to write to config

close(NEWCONFIG);
Here is where we write the configuration files.  Only one configuration to write so far, which is the table password.  We can add other configuration parameters later.
print "\nFinished writing variables to configuration file located in /etc/projsam.conf\n";

# If the config file already existed, let's tell the user here.
if ( $exist == "1" ) {
print "There was an existing configuration, so we copied all the information to /etc/projsam1.conf\n";
}

print "Everything is done!\n";
All this is just more text and cleanup.  Nothing exciting.

That's the code!  If you have any questions, please contact William Fong (wfong@users.sourceforge.net).

 

SourceForge Logo