← Back to team overview

mvhub-dev team mailing list archive

[Merge] lp:~leegoodrich/mvhub/create_config_in_mv_setup into lp:mvhub

 

Lee Goodrich has proposed merging lp:~leegoodrich/mvhub/create_config_in_mv_setup into lp:mvhub.

Requested reviews:
  mvhub-dev (mvhub-dev)


This request adds automated config file creation and updating to 
nsp.conf and mvh.conf from template.conf. New keys are added with 
default values only if key does not currently exist in the nsp and mvh 
config files currently.
-- 
https://code.launchpad.net/~leegoodrich/mvhub/create_config_in_mv_setup/+merge/21961
Your team mvhub-dev is subscribed to branch lp:mvhub.
=== modified file 'app-mvhub/project-tools/bin/mv_setup'
--- app-mvhub/project-tools/bin/mv_setup	2010-03-10 19:51:30 +0000
+++ app-mvhub/project-tools/bin/mv_setup	2010-03-24 12:53:19 +0000
@@ -7,6 +7,8 @@
 use Carp;
 use DBI;
 use IO::Prompt;
+use Config::Simple;
+use MVHub::Utils::ConfigSimple;
 
 use English '-no_match_vars';
 
@@ -14,15 +16,17 @@
  local $OUTPUT_AUTOFLUSH=1;
 
     my $USERNAME = get_username_or_die();
-
+	my $CFG = MVHub::Utils::ConfigSimple::create_config_from($ENV{MV_CONFIG_FILE});
+	my $base_dir = $CFG->param('BASE.dir');
+	
     # should have nothing left on command line
     # at this point
     die usage() if scalar @ARGV;
 
     system('clear');
-
-    print "(re)making dirs in /var/www/mvhub/$USERNAME...";
-    my @dirs = get_dirs_with( '/var/www/mvhub/', $USERNAME );
+    
+    print "(re)making dirs in $base_dir$USERNAME...";
+    my @dirs = get_dirs_with( "$base_dir", $USERNAME );
     make_dirs_from( $USERNAME, \@dirs );
     print "..done\n";
 
@@ -38,6 +42,7 @@
         print "...done\n" 
         	if make_dbs_for( $DBH, $USERNAME );;
     }
+    create_config_files_for($USERNAME, $CFG);
 }
 
 sub get_username_or_die {
@@ -313,13 +318,17 @@
     my $dbh      = shift or croak 'missing param: $dbh';
     my $username = shift or croak 'missing param: $username';
     my $suffix   = shift or croak 'missing param:   $db_name';
-
-    # KLUDGE PATH
+	
+	my $CFG = 
+	  MVHub::Utils::ConfigSimple::create_config_from($ENV{MV_CONFIG_FILE});
+	my $base_dir = $CFG->param('BASE.dir');
+	my $path_to_project_tools = $CFG->param('RELATIVE_PATH.project_tools_dir');
+	my $db_password = $CFG->param('DATABASE.database_magic_word');
+	
     my $dir =
-      "/var/www/mvhub/$ENV{USER}/link-to-live-code/app-mvhub/project-tools/";
+      "$base_dir/$ENV{USER}/$path_to_project_tools";
 
-    # KLUDGE HARD-CODED password
-    my $cmd = "export PGPASSWORD='test' ";
+    my $cmd = "export PGPASSWORD=$db_password ";
     $cmd .= "&& psql -h localhost -U $username -d  $username.$suffix ";
     $cmd .= "-f ${dir}/test_${suffix}_db.sql >/dev/null 2>/dev/null";
 
@@ -327,3 +336,105 @@
       or warn "failed to load data for $username.$suffix";
 
 }
+
+sub create_config_files_for{
+	my $username = shift;
+	
+	my $CFG = MVHub::Utils::ConfigSimple::create_config_from($ENV{MV_CONFIG_FILE});
+	my $base_dir = $CFG->param('BASE.dir');
+	
+	my @WEBSITE_CODES = qw/ mvh nsp /;
+	my @KEYS_FOR_USER_GENERATED_VALUES = qw/ DATABASE.database_magic_word /;
+	my $template_cfg = new 
+Config::Simple($CFG->param('ABSOLUTE_PATH.template_conf_dir') . "/template.conf");
+	
+	
+	foreach my $site_code (@WEBSITE_CODES){
+		my $user_cfg = new Config::Simple(syntax=>'ini');
+		if (-e "$base_dir/$username/conf/$site_code.conf") {
+			$user_cfg-> read("$base_dir/$username/conf/$site_code.conf");	
+		}
+		
+		print "Editing Config: $base_dir/$username/conf/$site_code.conf\n";
+		
+		my %site_specific_values = get_site_specific_values($site_code);
+		
+		$template_cfg = add_site_specific_values($template_cfg, $username, %site_specific_values);
+		$template_cfg = add_calculable_values($template_cfg, $username, $site_code);
+		$template_cfg = add_user_generated_values($template_cfg, $user_cfg,@KEYS_FOR_USER_GENERATED_VALUES);
+		
+		merge_conf_objects($template_cfg, $user_cfg);
+		
+		$user_cfg->write("$base_dir/$username/conf/$site_code.conf");
+	}
+}
+
+sub merge_conf_objects{
+	my $template_cfg = shift or croak 'missing param: $template_cfg';
+	my $user_cfg = shift or croak 'missing param: $user_cfg';
+	
+	foreach my $key ($template_cfg->param()){
+
+		if (! ($user_cfg->param($key))){
+			$user_cfg->param($key, $template_cfg->param($key));
+		}
+	}	
+	return $user_cfg;
+}
+
+sub get_site_specific_values{
+	my $site_code = shift or croak 'missing param: $site_code';
+
+	my %hash_of_site_specific_values = (
+	'mvh' => { 
+		'SITE.location' => '"Massachusetts Merrimack Valley"', 
+		'SITE.website_description' => 'Merrimack Valley Hub',
+		'NOTIFICATION.team_name' => 'Merrimack Valley Hub Team'},
+	'nsp' => {
+		'SITE.location' => '"Massachusetts North Shore"', 
+		'SITE.website_description' => 'North Shore Portal',
+		'NOTIFICATION.team_name' => 'North Shore Portal Team'});
+	return %{$hash_of_site_specific_values{$site_code}};
+}
+
+sub add_site_specific_values{
+	my $template_cfg = shift or croak 'missing param: $template_cfg';
+	my $username = shift or croak 'missing param: $username';
+	my %site_specific_values = %_;
+	
+	foreach my $key (keys %site_specific_values){
+		$template_cfg->param($key, $site_specific_values{$key});
+	}
+	return $template_cfg;
+}
+ 
+sub add_calculable_values{
+	my $template_cfg = shift or croak 'missing param: $template_cfg';
+	my $username = shift or croak 'missing param: $username';
+	my $site_code = shift or croak 'missing param: $site_code';
+	
+	$template_cfg->param('DATABASE.database_name',"$username.$site_code");
+	$template_cfg->param('DATABASE.database_user',"$username");
+	$template_cfg->param('NOTIFICATION.admin_email', "$username\@thecsl.org");
+	$template_cfg->param('NOTIFICATION.dev_email', "$username\@thecsl.org");
+	$template_cfg->param('SITE.website_name',"$site_code.$username.testing123.net");
+	
+	return $template_cfg;
+}
+
+
+sub add_user_generated_values{
+	my $template_cfg = shift or croak 'missing param: $template_cfg';
+	my $user_cfg = shift or croak 'missing param: $user_cfg';
+	my @keys = @_;
+
+	foreach my $key (@keys){
+		if (!($user_cfg->param($key))){
+			prompt("Please enter a value for $key: ");
+			$template_cfg->param($key,$_);
+		}
+	}
+	return $template_cfg;
+}
+
+

=== modified file 'app-mvhub/project-tools/templates/template.conf'
--- app-mvhub/project-tools/templates/template.conf	2010-03-08 08:04:23 +0000
+++ app-mvhub/project-tools/templates/template.conf	2010-03-24 12:53:19 +0000
@@ -99,9 +99,11 @@
 
 [RELATIVE_PATH]
 conf_dir=link-to-live-code/app-mvhub/conf/
+template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
 template_html_dir=link-to-live-code/app-mvhub/conf/templates/html/
 template_text_dir=link-to-live-code/app-mvhub/conf/templates/text/
 reports_dir=reports/
+project_tools_dir=link-to-live-code/app-mvhub/project-tools/
 tmp_dir=tmp/
 
 [COOKIES]

=== modified file 'lib-mvhub/t/conf/all.conf'
--- lib-mvhub/t/conf/all.conf	2010-02-01 01:50:40 +0000
+++ lib-mvhub/t/conf/all.conf	2010-03-24 12:53:19 +0000
@@ -86,9 +86,11 @@
 
 [RELATIVE_PATH]
 conf_dir=link-to-live-code/app-mvhub/conf/
+template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
 template_html_dir=link-to-live-code/app-mvhub/conf/templates/html/
 template_text_dir=link-to-live-code/app-mvhub/conf/templates/text/
 reports_dir=reports/
+project_tools_dir=link-to-live-code/app-mvhub/project-tools/
 tmp_dir=tmp/
 
 [COOKIES]


References