Update of /cvsroot/sitesampler/sitesampler/support
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13573/support
Added Files:
SiteSampler_InstConfig.pm run.pl SiteSampler_Installation.pm
Log Message:
initial commit of installation code
--- NEW FILE: run.pl ---
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use SiteSampler_Installation;
use SiteSampler_InstConfig;
my $ss_inst = SiteSampler_Installation->new;
$ss_inst->run;
print Dumper($ss_inst);
--- NEW FILE: SiteSampler_Installation.pm ---
package SiteSampler_Installation;
use SiteSampler_InstConfig;
use strict;
sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}
sub with_cgi_script{
my $self = shift;
my $key = $self->with_cgi_script_key;
if (@_ == 1){
$self->{$key} = (shift);
}
return $self->{$key};
}
sub with_cgi_script_key{ 'with_cgi_script_key' }
sub started_key{ 'Started_Install' }
sub stopped_key{ 'Stopped Install' }
sub config_key{ 'Configuration_Object' }
sub start{
my $self = shift;
$self->get_set($self->started_key, 1);
print "Welcome to SiteSampler click-stream analyzer configuration and installation.\n\n";
$self->checkRoot;
my $config = SiteSampler_InstConfig->new;
$self->get_set($self->config_key, $config);
}
sub stop{
my $self = shift;
$self->get_set($self->stopped_key, 1);
$self->clean_up;
}
sub finish{
my $self = shift;
exit(0);
}
sub get_set{
my $self = shift;
my($member_key) = shift;
if(@_ == 1 ){
$self->{$member_key} = shift;
}
return($self->{$member_key});
}
sub run{
my $self = shift;
$self->start;
$self->fill_config;
$self->test_config;
$self->commit_install;
$self->audit_install;
$self->stop;
#$self->finish;
}
sub test_config{
my $self = shift;
}
sub fill_config {
my $self = shift;
$self->init_base;
$self->init_db;
$self->init_install_paths;
#cgi script installation comes with three stages: creation(modification),
#copying(actual installation), and testing; these are happening in these:
#fill_config commit_install and audit_install;
$self->isWithScript ? $self->init_cgi_script : 0;
}
sub init_cgi_script{
my $self = shift;
$self->init_cgi_dir;
$self->get_script_name;
}
sub init_cgi_dir{
my $self = shift;
print qq|What is the path to your executable bin (cgi-bin or mod_perl enabled directory)? [/usr/local/apache2/cgi-bin]\n|;
$self->config->set_cgiscript_path( $self->get_stdin_str );
}
sub get_script_name{
my $self = shift;
print qq|What would you like to name the web accessible script? <sitesampler>\n|;
$self->config->set_cgiscript_name( $self->get_stdin_str );
}
sub isWithScript{
my $self = shift;
print "You will need to provide path to Apache1/2 cgi-bin or mod_perl enabled directory to install sitesampler.cgi script. (Yes)if you know that path and would like to install cgi script ; (No) If you would like to copy script manually later. \n";
if ($self->get_stdin_bool){
$self->with_cgi_script(1);
}
else{
$self->with_cgi_script;
}
}
sub checkRoot{
my $self = shift;
print qq|This installation needs to be run as a privileged user to modify file and directory permissions. If you logged in as a user that does not have sufficiennt privileges, this installation will fail. Continue?(y/n)\n|;
if ($self->get_stdin_bool){
print qq|assuming that user has sufficient permissions; based on user input ..\n|;
}
else{
print qq|Cancelled by user\n|;
$self->finish;
}
}
sub commit_install{
my $self = shift;
$self->create_install_dirs;
$self->create_codebase;
$self->createDB;
$self->with_cgi_script ? $self->create_cgi_script : 0;
}
sub create_install_dirs{
my $self = shift;
print qq|Creating installation paths\n|;
$self->config->make_install_paths;
}
sub create_codebase{
my $self = shift;
print qq|Copying perl modules.. \n|;
$self->config->copy_codebase;
print qq|Altering DataSettings.pm to add database connection information\n|;
$self->config->writeDataSettings;
}
sub createDB{
my $self = shift;
print qq|Creating database, inserting default values.. \n|;
$self->config->load_sql;
$self->config->writeCore;
}
sub create_cgi_script{
my $self = shift;
print qq|Creating and installing cgi script \n|;
$self->config->write_cgi_script;
}
sub audit_install{
my $self = shift;
}
sub init_base{
my $self = shift;
$self->config->set_base;
}
sub config{
my $self = shift;
return $self->{$self->config_key} ;
}
sub init_db{
my $self = shift;
$self->setDBuser; #database user
$self->setDBname; #database name
$self->setDBpass; #confusingly and surprizingly, this is database password
$self->test_DBconnection;
}
sub test_DBconnection{
my $self = shift;
$self->config->test_dbconn;
}
sub setDBuser{
my $self = shift;
print qq|Please, enter database user name<postgres>\n|;
$self->config->set_dbuser( $self->get_stdin_str );
}
sub setDBname{
my $self = shift;
print qq|Please, enter database name<sitesampler>\n|;
$self->config->set_dbname( $self->get_stdin_str );
}
sub setDBpass{
my $self = shift;
print qq|Please, enter database password\n|;
$self->config->set_dbpass( $self->get_stdin_str );
}
sub init_install_paths{
my $self = shift;
$self->init_targetdir;
$self->init_perlmods_path;
$self->init_tags_path;
$self->init_cache_path;
$self->init_logs_path;
}
sub init_targetdir{
my $self = shift;
print qq|Please, enter directory path, where SiteSampler will be installed.[/usr/local/sitesampler]\n|;
$self->config->set_install_path( $self->get_stdin_str );
}
sub init_perlmods_path{
my $self = shift;
print qq|Please, enter directory path, where SiteSampler perl-modules will exist.[/usr/local/sitesampler/perlmods]\n|;
$self->config->set_perlmods_path( $self->get_stdin_str );
}
sub init_tags_path{
my $self = shift;
print qq|Please, enter directory path, where SiteSampler site project tags will exist.[/usr/local/sitesampler/site_data]\n|;
$self->config->set_tag_path( $self->get_stdin_str );
}
sub init_cache_path{
my $self= shift;
print qq|Please, enter directory path, where SiteSampler cache will be installed.[/usr/local/sitesampler/cache]\n|;
$self->config->set_cache_path( $self->get_stdin_str );
}
sub init_logs_path{
my $self = shift;
print qq|Please, enter directory path, where SiteSampler logs will live.[/usr/local/sitesampler/logs]\n|;
$self->config->set_log_path( $self->get_stdin_str );
}
sub get_stdin_bool{
my $self = shift;
my $a = <STDIN>; chomp($a);
my $retval = 0;
if ($a =~ /y/i){
$retval = 1;
}
return $retval;
}
sub get_stdin_str{
my $self = shift;
my $a = <STDIN>; chomp ($a);
my $retval = undef;
if ($a){
$retval = $a;
}
return $retval;
}
sub clean_up{
my $self = shift;
}
1;
=head1 NAME
SiteSampler_Installation
=head1 SYNOPSIS
use SiteSampler_Installation;
my $obj = SiteSampler_Installation->new();
=head1 DESCRIPTION
SiteSampler_Installation
=head1 AUTHOR
Farid Aliev, E<lt>fa...@gm...<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Farid Aliev
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.7 or,
at your option, any later version of Perl 5 you may have available.
=cut
--- NEW FILE: SiteSampler_InstConfig.pm ---
package SiteSampler_InstConfig;
use strict;
use File::Copy;
use File::Path;
use File::Spec;
use DirHandle;
use FileHandle;
use DBI;
sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}
sub make_install_paths{
my $self = shift;
$self->create_install_path;
$self->create_perlmods_path ; # PerlMods
$self->create_cache_path ; # SitesamplerCache
$self->create_log_path; # SiteSamplerLogs
$self->create_tag_path; #TagDirectory
}
sub create_install_path{
my $self = shift;
my $path = $self->{$self->install_path_key};
$self->create_dir($path, $self->creation_mode);
}
sub create_perlmods_path{
my $self = shift;
my $path = $self->{$self->perlmods_path_key};
$self->create_dir($path, $self->creation_mode);
}
sub create_cache_path{
my $self = shift;
my $path = $self->{$self->cache_path_key};
$self->create_dir($path, $self->creation_mode);
}
sub create_log_path{
my $self = shift;
my $path = $self->{$self->log_path_key};
$self->create_dir($path, $self->creation_mode);
}
sub create_tag_path{
my $self = shift;
my $path = $self->{$self->tag_path_key};
$self->create_dir($path, $self->creation_mode);
}
sub copy_codebase{
my $self = shift;
my $install_dir = File::Spec->canonpath( $self->{$self->install_path_key} );
my $perlmods_dir = File::Spec->canonpath( $self->{$self->perlmods_path_key} );
my $base_dir = File::Spec->canonpath( $self->{$self->base_path_key} );
my $dir_handle = DirHandle->new($base_dir)
or (die qq|Unknown error: could not open directory $base_dir : $!|);
foreach my $node( File::Spec->no_upwards($dir_handle->read) ){
if ($node =~ /CVS/ ){ next }
$node = File::Spec->catfile($base_dir, $node);
my $source = $node;
my $dest = $perlmods_dir;
if ($node =~ /support$/) { $dest = $install_dir }
my $retval = system("cp -rf $source $dest");
if ($retval){
die qq|Unknown error: cp command returned this: $retval \n|;
}
}
}
sub writeDataSettings{
my $self = shift;
my $filename = File::Spec->catfile( ($self->{$self->perlmods_path_key}),'SiteSampler', 'Data', 'DataSettings.pm');
if(! -e $filename){
die qq| Cannot find the Data Settings file, DataSettings.pm.\n\n|;
}
my $dbname = $self->{$self->dbname_key};
my $dbuser = $self->{$self->dbuser_key};
my $dbpass = $self->{$self->dbpass_key};
my $dataFile = FileHandle->new;
$dataFile->open($filename,q|<|) or die(qq|$filename, $!|);
my @code = <$dataFile>;
for my $line(@code){
if ($line =~ /return\(\'PostgreSQL\'/){
$line = qq| return('PostgreSQL', "dbi:Pg:dbname=$dbname", "$dbuser", "$dbpass", {AutoCommit => 1});\n|;
}
}
$dataFile->open($filename,q|>|) or die(qq|$filename, $!|);#for writing
$dataFile->print(@code);
$dataFile->close;
return;
}
sub write_cgi_script{
my $self = shift;
my $base_script = File::Spec->catfile( ($self->{$self->support_path_key}),
($self->default_cgiscript_name) );
#print qq|base script is : |,$base_script;
my $target_script = File::Spec->catfile( ($self->{$self->cgiscript_path_key}), ($self->{$self->cgiscript_name_key}) ) ;
#print qq|target script is : |,$target_script;
File::Copy::copy($base_script , $target_script)
or die(qq|Could not copy sitesampler base cgi script into executable directory: $! \n|);
chmod(0755, $target_script) or die qq|Unknown error while changing permissions on cgi script: chmod failed with: $!\n|;
$self->edit_script_uselib;
return(1);
}
sub edit_script_uselib{
my $self = shift;
my $filename = File::Spec->catfile( ($self->{$self->cgiscript_path_key}) , ($self->{$self->cgiscript_name_key}) );
if(! -e $filename){
die qq| Cannot find created cgi script, $filename. \n\n|;
}
my $lib = File::Spec->catdir( $self->{$self->perlmods_path_key});
my $dataFile = FileHandle->new;
$dataFile->open($filename,'<') or die(qq|$filename, $!|);
my @code = <$dataFile>;
for my $line(@code){
if ($line =~ /use lib/){
$line = qq|use lib('$lib'); #inserted by install\n|;
}
}
$dataFile->open($filename,'>') or die(qq|$filename, $!|);#for writing
$dataFile->print(@code);
$dataFile->close;
return;
}
#object hash keys
sub base_path_key{ 'BaseDir' } # this is where this code has been checked-out to.
sub support_path_key{ 'SupportDir' } # this is where this code is running from.
sub dbname_key{ 'DBname' }
sub dbuser_key{ 'DBuser' }
sub dbpass_key{ 'DBpass' }
sub install_path_key{ 'InstallDir' }
sub perlmods_path_key { 'PerlMods' }
sub cache_path_key{ 'SitesamplerCache' }
sub log_path_key{ 'SiteSamplerLogs' }
sub tag_path_key{ 'TagDirectory' }
sub latest_logs_path_key{ 'LatestLogsDir' }
sub temp_logs_path_key{ 'TempLogsDir' }
sub arch_logs_path_key{ 'ArchivedLogsDir' }
sub locked_logs_path_key{ 'LockedLogsDir' }
sub error_logs_path_key{ 'ErroredLogsDir' }
sub template_path_key{ 'TemplateDir' }
sub image_path_key{ 'WebUI_ImageDir' }
sub cgiscript_path_key{ 'cgiscript_path' }
sub cgiscript_name_key{ 'cgiscript_name' }
#DEFAULTS..
#db defaults
sub default_dbname{'sitesampler'}
sub default_dbuser{'postgres'}
sub default_dbpass{''}
#installation defaults
sub default_install_path{'/usr/local/sitesampler'}
sub default_perlmods_path{'/usr/local/sitesampler/perl_mods'}
sub default_cache_path{'/usr/local/sitesampler/cache'}
sub default_log_path{'/usr/local/sitesampler/logs'}
sub default_tag_path{'/usr/local/sitesampler/site_data'}
#logs paths defaults
sub default_log_errors_path{'/usr/local/sitesampler/logs/errors'}
sub default_log_latest_path{'/usr/local/sitesampler/logs/latest'}
sub default_log_temp_path{'/usr/local/sitesampler/logs/temp'}
sub default_log_arch_path{'/usr/local/sitesampler/logs/archived'}
sub default_log_locked_path{'/usr/local/sitesampler/logs/locked'}
sub default_template_path{''}
sub default_cgiscript_path{'/usr/local/apache2/cgi-bin'}
sub default_cgiscript_name{'sitesampler'}
sub set_base{
my $self = shift;
my $support = File::Spec->rel2abs( File::Spec->curdir() );
my $base = File::Spec->rel2abs( File::Spec->updir( $support ) );
$self->get_set( $self->base_path_key, $base );
$self->get_set($self->support_path_key, $support);
}
sub test_dbconn{
my $self = shift;
my $dbh = $self->dbh;
$dbh->disconnect;
$self->unset_dbh;
return 1;
}
sub set_dbname{
my $self = shift;
my $dbname = (defined ($_ = shift)) ? $_ : $self->default_dbname;
$self->get_set($self->dbname_key, $dbname);
}
sub set_dbuser{
my $self = shift;
my $dbuser = (defined ($_ = shift)) ? $_ : $self->default_dbuser;
$self->get_set($self->dbuser_key, $dbuser);
}
sub set_dbpass{
my $self = shift;
my $dbpass = (defined ($_ = shift)) ? $_ : $self->default_dbpass;
$self->get_set($self->dbpass_key, $dbpass);
}
sub set_install_path{
my $self = shift;
my $install_path = (defined ($_ = shift)) ? $_ : $self->default_install_path;
$self->get_set($self->install_path_key, $install_path);
}
sub set_perlmods_path{
my $self = shift;
my $perlmods_path = (defined ($_ = shift)) ? $_ : $self->default_perlmods_path;
$self->get_set($self->perlmods_path_key, $perlmods_path);
}
sub set_cache_path{
my $self = shift;
my $cache_path = (defined ($_ = shift)) ? $_ : $self->default_cache_path;
$self->get_set($self->cache_path_key, $cache_path);
}
sub set_tag_path{
my $self = shift;
my $tag_path = (defined ($_ = shift)) ? $_ : $self->default_tag_path;
$self->get_set($self->tag_path_key, $tag_path);
}
sub set_log_path{
my $self = shift;
my $log_path = (defined ($_ = shift)) ? $_ : $self->default_log_path;
$self->get_set($self->log_path_key, $log_path);
}
sub set_cgiscript_path{
my $self = shift;
my $cgiscript_path = (defined ($_ = shift)) ? $_ : $self->default_cgiscript_path;
$self->get_set($self->cgiscript_path_key, $cgiscript_path);
}
sub set_cgiscript_name{
my $self = shift;
my $cgiscript_name = (defined ($_ = shift)) ? $_ : $self->default_cgiscript_name;
$self->get_set($self->cgiscript_name_key, $cgiscript_name);
}
sub get_set{
my $self = shift;
my($member_key) = shift;
if(@_ == 1 ){
$self->{$member_key} = shift;
}
return($self->{$member_key});
}
sub db_err_check{
my $self = shift;
if( $DBI::errstr){
print (qq|There is DBI ERROR, installation is terminated: |, $DBI::errstr, $!);
}
}
sub create_dir{
my $self = shift;
my $dir = shift;
my $creation_mode = shift;
eval( mkpath($dir, 1, $creation_mode));
if ($@){
print qq|Error: couldn't create directory $dir: $@\n|;
$self->finish;
}
else{
return 1;
}
}
sub creation_mode{
my $self = shift;
return ($self->default_creation_mode);
}
sub default_creation_mode{'0755'}
sub dbh{
my $self = shift;
my $key = $self->dbh_key;
unless (exists $self->{$key}){
my $dbname = $self->get_set($self->dbname_key);
my $dbuser = $self->get_set($self->dbuser_key);
my $dbpass = $self->get_set($self->dbpass_key);
$self->{$key} = DBI->connect("dbi:Pg:dbname=$dbname",
"$dbuser",
"$dbpass",
{ AutoCommit => 1, RaiseError => 1 } );
$self->db_err_check;
}
return ( $self->{$key} );
}
sub unset_dbh{
my $self = shift;
delete $self->{$self->dbh_key};
}
sub dbh_key{ 'db_connection handle' }
sub load_sql{
my $self = shift;
my $dbh = $self->dbh;
my $sqlFile = FileHandle->new
or die(qq|Could not create file handle for the SQL file sitesampler.sql|);
my $schema_file = File::Spec->catfile($self->{$self->support_path_key}, );
$sqlFile->open($schema_file,'<')
or die qq|Could not open the file $schema_file: $!|;
my @sql = <$sqlFile>;
$sqlFile->close;
my $query = join ' ', @sql;
my $dbname = $self->get_set($self->dbname_key);
$query =~ s/postgres/$dbname/;
my $res = $dbh->do("$query;") if $query;
$self->db_err_check;
$dbh->disconnect;
$self->unset_dbh;
return 1;
}
sub writeCore{
my $self = shift;
}
1;
=head1 NAME
SiteSampler_InstConfig
=head1 SYNOPSIS
use SiteSampler_InstConfig;
my $obj = SiteSampler_InstConfig->new();
=head1 DESCRIPTION
SiteSampler_InstConfig
=head1 AUTHOR
Farid Aliev, E<lt>fa...@gm...<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Farid Aliev
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut
|