[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Update to tools/dbsetup.pl



Minor fix, changed postgress to postgres.

New diff

Chris Bennett

--- /home/chris101/LSMB/ledgersmb/tools/dbsetup.pl	Tue Mar 27 20:34:52 2012
+++ tools/dbsetup.pl	Wed Oct 17 15:04:54 2012
@@ -1,5 +1,6 @@
 #!/usr/bin/perl
-package LedgerSMB::Scripts::dirt;
+package LedgerSMB::Scripts::dbsetup;
+our $VERSION = 0.3;
 use warnings;
 use strict;
 
@@ -19,6 +20,9 @@
 chomp $srcdir;
 my $dstdir = `pwd`;
 chomp $dstdir;
+my $ledhttpconfdir = "/var/www/conf/modules";
+my $web_files_owner = "www";
+my $web_files_group = "www";
 my $coa = "$srcdir/sql/coa/us/chart/General.sql";
 my $chart_name;
 my $gifi;
@@ -34,10 +38,11 @@
 my $admin_username = 'admin';
 my $admin_password = 'admin';
 my $interactive = 0;
-my $no_postgress_pass = 0;
-my $progress = '';
+my $no_postgres_pass = 0;
+my $progress = undef;
 my $help;
 my $dbh;
+my $logger;
 
 # Usage explanation section
 
@@ -46,7 +51,7 @@
 
 This script wants to be run as the root user. You will need to have
 permission to write the new ledgersmb-httpd.conf.
-Unless you select --no_postgress_pass, you will be asked to enter the
+Unless you select --no_postgres_pass, you will be asked to enter the
 password of the '$postgres_username' user.
 
 This script creates a '$owner' superuser on the specified PostgreSQL host,
@@ -69,26 +74,33 @@
  --srcdir		The path where the sources for LedgerSMB are located
 			[$srcdir]
  --dstdir		The path where the sources will be located when invoked
-			from the webserver [$dstdir]
+			from the webserver
+			[$dstdir]
+ --ledhttpconfdir	The directory that ledgersmb-httpd.conf will be installed
+			[$ledhttpconfdir]
+ --web_files_owner	The correct owner of the web site files
+			[$web_files_owner]
+ --web_files_group	The correct group of the web site files
+			[$web_files_group]
  --host			The PostgreSQL host to connect to (see 'man psql') [$host]
  --port			The PostgreSQL port to connect to (see 'man psql') [$port]
  --pgsql_contrib	The directory where the pg_trgm.sql and tablefunc.sql
 			are located [required for PostgreSQL version < 9.1.x]
  --company		The name of the database to be created for the company [*]
- --countrycode		The name of the 2 character country code. Defaults to 'US'
+ --countrycode		The name of the 2 character country code [$countrycode]
  --owner		The name of the superuser which is to become owner of the
 			company's database [$owner]
  --password		The password to be used to create the 'ledgersmb' user
 			on the specified PostgreSQL server [$pass]
  --coa			The path locating the file to be used to load the
 			Chart of Accounts data
-			Defaults to '$srcdir/sql/coa/us/chart/General.sql'
+			[$srcdir/sql/coa/us/chart/General.sql]
  --chart		Chart used
  --gifi			The path locating the file to be
 			used to load the GIFI data with the chart of accounts
  --postgres_username	postgres username, only use if not 'postgres'
  --postgres_password	postgres user password. Defaults to none
- --no_postgress_pass	Means no postgres password is needed
+ --no_postgres_pass	Means no postgres password is needed
  --postgres_db		postgres database name. Defaults to 'postgres'
  --admin_firstname	Admin firstname
  --admin_middlename	Admin middlename
@@ -105,6 +117,9 @@
 GetOptions (
 	'srcdir:s' => \$srcdir,
 	'dstdir:s' => \$dstdir,
+	'ledhttpconfdir:s' => \$ledhttpconfdir,
+	'web_files_owner:s' => \$web_files_owner,
+	'web_files_group:s' => \$web_files_group,
 	'host:s' => \$host,
 	'port:i' => \$port,
 	'pgsql_contrib=s' => \$pgsql_contrib_dir,
@@ -117,7 +132,7 @@
 	'countrycode:s' => \$countrycode,
 	'postgres_username:s' => \$postgres_username,
 	'postgres_password=s' => \$postgres_password,
-	'no_postgress_pass:s' => \$no_postgress_pass,
+	'no_postgres_pass:s' => \$no_postgres_pass,
 	'postgres_db:s' => \$postgres_db,
 	'admin_firstname:s' => \$admin_firstname,
 	'admin_middlename:s' => \$admin_middlename,
@@ -140,7 +155,7 @@
 	exit;
 }
 
-unless (($postgres_password) || ($no_postgress_pass)) {
+unless (($postgres_password) || ($no_postgres_pass)) {
 	print $usage;
 	print "\nmissing or empty --postgres_password option\n";
 	exit;
@@ -148,6 +163,10 @@
 unless (stat "/tmp/ledgersmb") {
 	mkdir "/tmp/ledgersmb";
 }
+unless (stat "$ledhttpconfdir") {
+	warn ("Cannot find directory $ledhttpconfdir for ledgersmb-httpd.conf Will install in $dstdir");
+	$ledhttpconfdir = $dstdir;
+}
 if (stat "$srcdir/ledgersmb.conf") {
 	require LedgerSMB;
 	require LedgerSMB::Database;
@@ -159,19 +178,22 @@
 	require LedgerSMB::Sysconfig;
 };
 
-my $logger = Log::Log4perl->get_logger('LedgerSMB::Scripts::dirt');
-my $creds = dirt_get_credentials();
+if ($progress) {
+	$logger = Log::Log4perl->get_logger('LedgerSMB::Scripts::dbsetup');
+}
+my $creds = dbsetup_get_credentials();
 my $request = {};
 $request->{database} = $company_name;
 
-$ENV{SCRIPT_NAME} = "dirt.pl";
+$ENV{SCRIPT_NAME} = "dbsetup.pl";
 # ENVIRONMENT NECESSARY
 $ENV{PGUSER} = $creds->{login};
 $ENV{PGPASSWORD} = $creds->{password};
 $ENV{PGDATABASE} = $request->{database};
 
+chown_web_files($web_files_owner, $web_files_group, $dstdir);
 create_ledgersmb_httpd_conf();
-unless ($no_postgress_pass) {
+unless ($no_postgres_pass) {
 	$dbh = DBI->connect("DBI:Pg:dbname=$postgres_db;host=$host;port=5432", $postgres_username, $postgres_password, {'RaiseError' => 0, pg_enable_utf8 => 1 });
 } else {
 	$dbh = DBI->connect("DBI:Pg:dbname=$postgres_db;host=$host;port=5432", $postgres_username, {'RaiseError' => 0, pg_enable_utf8 => 1 });
@@ -204,12 +226,14 @@
 }
 
 my $rc = $database->create();#TODO what if createdb fails?
-$logger->info("create rc=$rc");
+if ($progress) {
+	$logger->info("create rc=$rc");
+}
 my $mc = $database->load_modules('LOADORDER');
 if ($progress) {
 	print "\$mc = $mc\n";
+	$logger->info("load modules mc=$mc");
 }
-$logger->info("load modules mc=$mc");
 $database->process_roles('Roles.sql');
 
   # Load a chart of accounts
@@ -301,7 +325,7 @@
 
 
 
-sub dirt_get_credentials {
+sub dbsetup_get_credentials {
 my $return_value = {};
 #$logger->debug("\$auth=$auth");#be aware of passwords in log!
 ($return_value->{login}, $return_value->{password}) = ($owner, $pass);
@@ -319,17 +343,17 @@
 
 sub create_ledgersmb_httpd_conf {
 open (my $tmpl_fh, "<", "$srcdir/ledgersmb-httpd.conf.template");
-open (my $lhttpconf_fh, ">", "$dstdir/ledgersmb-httpd.conf");
+open (my $lhttpconf_fh, ">", "$ledhttpconfdir/ledgersmb-httpd.conf");
 
 my @tmpl = <$tmpl_fh>;
-$logger->info("Creating $dstdir/ledgersmb-httpd.conf\n");
 if ($progress) {
-	print "Creating $dstdir/ledgersmb-httpd.conf\n";
+	$logger->info("Creating $ledhttpconfdir/ledgersmb-httpd.conf\n");
+	print "Creating $ledhttpconfdir/ledgersmb-httpd.conf\n";
 }
 for my $line (@tmpl) {
 	$line =~ s/WORKING_DIR/$dstdir/g;
-	$logger->info("$line");
 	if ($progress) {
+		$logger->info("$line");
 		print "$line";
 	}
 	print $lhttpconf_fh $line;
@@ -347,4 +371,10 @@
 	system "cp $srcdir/ledgersmb.conf.default $srcdir/ledgersmb.conf;$editor $srcdir/ledgersmb.conf";
 }
 
+sub chown_web_files {
+	my $web_files_owner = shift;
+	my $web_files_group = shift;
+	my $dstdir = shift;
+	system "chown -R $web_files_owner:$web_files_group $dstdir";
+}
 1;