← Back to team overview

mvhub-dev team mailing list archive

[Branch ~mvhub-commit/mvhub/trunk] Rev 388: remerged refactor_mv_setup

 

Merge authors:
  Lee Goodrich (leegoodrich)
------------------------------------------------------------
revno: 388 [merge]
committer: Lee Goodrich <lgoodrich@xxxxxxxxxx>
branch nick: trunk
timestamp: Fri 2010-05-21 14:57:37 -0400
message:
  remerged refactor_mv_setup
modified:
  lib-mvhub/lib/MVHub/Utils/Setup.pm


--
lp:mvhub
https://code.launchpad.net/~mvhub-commit/mvhub/trunk

Your team MVHub Developers is subscribed to branch lp:mvhub.
To unsubscribe from this branch go to https://code.launchpad.net/~mvhub-commit/mvhub/trunk/+edit-subscription
=== modified file 'lib-mvhub/lib/MVHub/Utils/Setup.pm'
--- lib-mvhub/lib/MVHub/Utils/Setup.pm	2010-05-21 18:52:33 +0000
+++ lib-mvhub/lib/MVHub/Utils/Setup.pm	2010-05-21 18:57:10 +0000
@@ -10,25 +10,24 @@
 use IO::Prompt;
 use File::Basename;
 use File::Copy;
-use File::Remove;
 
 use MVHub::Utils::DB;
 
 our @EXPORT_OK = qw(
-    @SITE_CODES
-    add_apache_config
-    add_dns
-    get_production_data
-    get_testing_data
-    make_config_files_for
-    make_database_user
-    make_databases_for
-    make_dirs_from
-    load_production_data
-    load_test_data_for
-    remove_cron_jobs
-    restart_services
-    update_etc_files
+  @SITE_CODES
+  add_apache_config
+  add_dns
+  get_production_data
+  get_testing_data
+  make_config_files_for
+  make_database_user
+  make_databases_for
+  make_dirs_from
+  load_production_data
+  load_test_data_for
+  remove_cron_jobs
+  restart_services
+  update_etc_files
 );
 
 our @SITE_CODES = qw/ mvh nsp /;
@@ -38,20 +37,20 @@
     my $tmp_file = "$file.tmp";
 
     open( IN, $file )
-        or die "bad open: $file  error: $! \n";
+      or die "bad open: $file  error: $! \n";
     my @lines = <IN>;
     close(IN) or die "bad close $file error: $!\n";
 
     push @lines, $append_lines;
 
     open( OUT, ">", "$tmp_file" )
-        or die "couldn't open $tmp_file.tmp error: $! \n";
+      or die "couldn't open $tmp_file.tmp error: $! \n";
     ( print OUT @lines )
-        or die "couldn't write to $tmp_file $!\n";
+      or die "couldn't write to $tmp_file $!\n";
 
     close(OUT) or die "couldn't close $tmp_file $!\n";
     File::Copy::move( $tmp_file, $file )
-        or die "couldn't rename $tmp_file to $file\n";
+      or die "couldn't rename $tmp_file to $file\n";
 }
 
 sub _add_calculable_values {
@@ -59,11 +58,10 @@
     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( '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" );
     $template_cfg->param( 'SITE.website_code', "$site_code" );
@@ -127,8 +125,8 @@
      usename = ?
 SQL
 
-    my $matching_db_users_aref
-        = $dbh->selectall_arrayref( $sql, {}, ($username) );
+    my $matching_db_users_aref =
+      $dbh->selectall_arrayref( $sql, {}, ($username) );
     return scalar @$matching_db_users_aref;
 }
 
@@ -145,10 +143,10 @@
      rolname = ?
 SQL
 
-    my $role_rows_aref
-        = $dbh->selectall_arrayref( $sql, { Slice => {} }, ($username) );
+    my $role_rows_aref =
+      $dbh->selectall_arrayref( $sql, { Slice => {} }, ($username) );
     my $result = $$role_rows_aref[0]{rolcreaterole}
-        && $$role_rows_aref[0]{rolcreatedb};
+      && $$role_rows_aref[0]{rolcreatedb};
 
     return $result ? 1 : 0;
 }
@@ -166,8 +164,8 @@
      rolname = ?
 SQL
 
-    my $role_rows_aref
-        = $dbh->selectall_arrayref( $sql, { Slice => {} }, ($username) );
+    my $role_rows_aref =
+      $dbh->selectall_arrayref( $sql, { Slice => {} }, ($username) );
     return $$role_rows_aref[0]{rolsuper} ? 1 : 0;
 }
 
@@ -188,7 +186,7 @@
     $cmd =~ s/^\s+//;
     my @cmd = split /\s+/, $cmd;
     ( system(@cmd) == 0 )
-        or die "$cmd failed ABORTING\n";
+      or die "$cmd failed ABORTING\n";
 }
 
 sub _get_dbh_for_template1_db {
@@ -209,7 +207,7 @@
     );
 
     my $dbh = DBI->connect( $data_source, $database_username, $auth, \%attr )
-        or die "couldn't connect to database";
+      or die "couldn't connect to database";
     return $dbh;
 
 }
@@ -236,10 +234,10 @@
     my $filename = shift or croak 'missing Param $filename';
 
     open my $out_fh, '>', $filename
-        or die "failed to open $filename for writing error:$!\n";
+      or die "failed to open $filename for writing error:$!\n";
     print $out_fh "#Config file\n [ignore]\n";
     close $out_fh
-        or die "failed to close $filename error:$!\n";
+      or die "failed to close $filename error:$!\n";
     return 1;
 }
 
@@ -270,7 +268,7 @@
     my $filename = shift or croak 'missing parameter: $filename';
 
     open my $in_fh, $filename
-        or die "failed to open $filename error:$!";
+      or die "failed to open $filename error:$!";
 
     my @contents = <$in_fh>;
     my $regex    = qr/$host/;
@@ -289,8 +287,7 @@
         }
         elsif ( $output =~ /ROLLBACK/ ) {
             print "FAIL\n";
-            print
-                "The following OUTPUT occured while processing $filename:\n";
+            print "The following OUTPUT occured while processing $filename:\n";
             die "$output\n\n****Processing $filename FAILED****\n";
         }
     }
@@ -348,9 +345,8 @@
     my @website_codes = @_;
 
     foreach my $prefix (@website_codes) {
-        my $update_user_cfg_filename
-            = _determine_user_config_filename_from( $username, $prefix,
-            $cfg );
+        my $update_user_cfg_filename =
+          _determine_user_config_filename_from( $username, $prefix, $cfg );
         my $update_user_cfg = MVHub::Utils::ConfigSimple::create_config_from(
             $update_user_cfg_filename);
         my $dbh = MVHub::Utils::DB::get_dbh($update_user_cfg_filename);
@@ -377,8 +373,8 @@
     my $dbh = shift;
     my $version;
 
-    my $exist_sql
-        = "SELECT 1 FROM information_schema.tables WHERE table_name='version_log' LIMIT 1";
+    my $exist_sql =
+"SELECT 1 FROM information_schema.tables WHERE table_name='version_log' LIMIT 1";
     my $table_exists = ( $dbh->selectrow_array($exist_sql) )[0];
 
     if ($table_exists) {
@@ -409,7 +405,7 @@
         }
         else {
             die
-                "ERROR: '$script' does not conform to naming standard: nnn_word_description.sql\n";
+"ERROR: '$script' does not conform to naming standard: nnn_word_description.sql\n";
         }
     }
     return sort @scripts;
@@ -419,42 +415,50 @@
     my $base_dir = shift or croak 'missing param: $base_dir';
     my $username = shift or croak 'missing param: $username';
     my @dirs     = (
-        {   dir         => 'conf',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'cover_db',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'reports',
+        {
+            dir         => 'conf',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'cover_db',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'reports',
             owner       => '',
             group       => '',
             permissions => 'u=rwx,g=rwsx,o=r-x',
         },
-        {   dir         => 'reports',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'source-code',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'log/mvh',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'log/nsp',
-            owner       => '',
-            group       => '',
-            permissions => 'u=rwx,g=rwsx,o=rx',
-        },
-        {   dir         => 'tmp',
+        {
+            dir         => 'reports',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'source-code',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'log/mvh',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'log/nsp',
+            owner       => '',
+            group       => '',
+            permissions => 'u=rwx,g=rwsx,o=rx',
+        },
+        {
+            dir         => 'tmp',
             owner       => '',
             group       => '',
             permissions => 'u=rwx,g=rwsx,o=rx',
@@ -462,9 +466,9 @@
     );
     foreach my $dir_href (@dirs) {
         $dir_href->{owner} = $username
-            if not $dir_href->{owner};
+          if not $dir_href->{owner};
         $dir_href->{group} = $username
-            if not $dir_href->{group};
+          if not $dir_href->{group};
 
         $dir_href->{dir} = "$base_dir/$username/" . $dir_href->{dir};
     }
@@ -487,8 +491,8 @@
         $cmd .= "/tmp/${site_code}_production_data.dump";
 
         ( system($cmd) == 0 )
-            or die
-            "Failed to rsync ${timestamp}_${site_code}_production_db.sql\n";
+          or die
+          "Failed to rsync ${timestamp}_${site_code}_production_db.sql\n";
     }
     print "done\n";
 }
@@ -502,19 +506,18 @@
 
     print "Loading production data...";
     foreach my $suffix (@website_codes) {
-        my $cfg_filename
-            = _determine_user_config_filename_from( $username, $suffix,
-            $cfg );
-        my $user_cfg
-            = MVHub::Utils::ConfigSimple::create_config_from($cfg_filename);
+        my $cfg_filename =
+          _determine_user_config_filename_from( $username, $suffix, $cfg );
+        my $user_cfg =
+          MVHub::Utils::ConfigSimple::create_config_from($cfg_filename);
 
         _set_db_ENV_vars($user_cfg);
 
-        $cmd
-            = "pg_restore -O -x -d $username.$suffix /tmp/${suffix}_production_data.dump >/dev/null";
+        $cmd =
+"pg_restore -O -x -d $username.$suffix /tmp/${suffix}_production_data.dump >/dev/null";
 
         ( system($cmd) == 0 )
-            or warn "failed to load ${suffix}_production_data.dump";
+          or warn "failed to load ${suffix}_production_data.dump";
     }
     print "done\n";
 }
@@ -524,25 +527,23 @@
     my $cfg      = shift or croak 'missing param: $cfg';
     my @website_codes = @_;
 
-    my $path_to_project_tools
-        = $cfg->param('ABSOLUTE_PATH.project_tools_dir');
+    my $path_to_project_tools = $cfg->param('ABSOLUTE_PATH.project_tools_dir');
     my $cmd;
 
     print "Loading test data...";
     foreach my $suffix (@website_codes) {
-        my $cfg_filename
-            = _determine_user_config_filename_from( $username, $suffix,
-            $cfg );
-        my $user_cfg
-            = MVHub::Utils::ConfigSimple::create_config_from($cfg_filename);
+        my $cfg_filename =
+          _determine_user_config_filename_from( $username, $suffix, $cfg );
+        my $user_cfg =
+          MVHub::Utils::ConfigSimple::create_config_from($cfg_filename);
 
         _set_db_ENV_vars($user_cfg);
 
-        $cmd
-            = "psql -f ${path_to_project_tools}/test_${suffix}_db.sql >/dev/null";
+        $cmd =
+          "psql -f ${path_to_project_tools}/test_${suffix}_db.sql >/dev/null";
 
         ( system($cmd) == 0 )
-            or warn "failed to load data for $username.$suffix";
+          or warn "failed to load data for $username.$suffix";
     }
     print "done\n";
 }
@@ -553,16 +554,15 @@
     my @website_codes                  = @_;
     my @keys_for_user_generated_values = qw/ DATABASE.database_magic_word /;
 
-    my $template_config_file
-        = $cfg->param('ABSOLUTE_PATH.template_conf_dir') . "/template.conf";
+    my $template_config_file =
+      $cfg->param('ABSOLUTE_PATH.template_conf_dir') . "/template.conf";
     my $template_cfg = new Config::Simple($template_config_file)
-        or die "failed to create config object from $template_config_file";
+      or die "failed to create config object from $template_config_file";
 
     foreach my $site_code (@website_codes) {
 
-        my $cfg_filename
-            = _determine_user_config_filename_from( $username, $site_code,
-            $cfg );
+        my $cfg_filename =
+          _determine_user_config_filename_from( $username, $site_code, $cfg );
         my $user_cfg = new Config::Simple( syntax => 'ini' );
         if ( !-e $cfg_filename ) {
             _make_empty_config_file_named($cfg_filename);
@@ -574,8 +574,8 @@
 
         $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_calculable_values( $template_cfg, $username, $site_code );
         $template_cfg = _add_user_generated_values( $template_cfg, $user_cfg,
             @keys_for_user_generated_values );
 
@@ -592,8 +592,8 @@
     my $dbh = _get_dbh_for_template1_db($cfg);
 
     if ( !_db_user_found( $dbh, $username ) ) {
-        my $sql
-            = "CREATE USER $username WITH PASSWORD 'test' CREATEDB CREATEROLE";
+        my $sql =
+          "CREATE USER $username WITH PASSWORD 'test' CREATEDB CREATEROLE";
         $dbh->do($sql);
     }
 }
@@ -611,7 +611,7 @@
 
     if ( !_db_user_has_needed_roles( $dbh, $user_running_cmd ) ) {
         warn
-            "\nSkipping db stuff $user_running_cmd lacks CREATEDB or CREATEROLE\n";
+"\nSkipping db stuff $user_running_cmd lacks CREATEDB or CREATEROLE\n";
         return;
     }
 
@@ -635,8 +635,8 @@
         # kludge to avoid 'db being currently being accessed' errs
         sleep 1;
 
-        $sql
-            = qq{CREATE DATABASE "$username.${suffix}" OWNER "$username" ENCODING='SQL_ASCII' };
+        $sql =
+qq{CREATE DATABASE "$username.${suffix}" OWNER "$username" ENCODING='SQL_ASCII' };
         $dbh->do($sql);
 
     }
@@ -651,17 +651,17 @@
     print "Updating $username directory structure...";
     foreach my $dir_href (@dir_hrefs) {
         my $mkdir_cmd = "mkdir -p $dir_href->{dir}";
-        my $chown_cmd
-            = "chown -R $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
+        my $chown_cmd =
+          "chown -R $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
         if ( $dir_href->{dir} =~ /log/ ) {
 
-        # root owns log files it creates don't try to change ownership on them
-            $chown_cmd
-                = "chown $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
+          # root owns log files it creates don't try to change ownership on them
+            $chown_cmd =
+              "chown $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
         }
         else {
-            $chown_cmd
-                = "chown -R $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
+            $chown_cmd =
+              "chown -R $dir_href->{owner}:$dir_href->{group} $dir_href->{dir}";
         }
         my $chmod_cmd = "chmod $dir_href->{permissions} $dir_href->{dir}";
         _do_or_die($mkdir_cmd);