Show
Ignore:
Timestamp:
05/01/08 20:53:01 (8 months ago)
Author:
kake
Message:

First bash at new install process - do not expect this commit to be bug-free.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/new-install-process/Build.PL

    r1156 r1158  
     1use File::Copy; 
     2use File::Spec; 
     3use Module::Build; 
    14use strict; 
    2 use lib "lib"; 
    3 use Data::Dumper; 
    4 use Getopt::Long; 
    55 
    6 eval { 
    7     require Config::Tiny; 
    8     # OpenGuides::Build and OpenGuides::Config both use Config::Tiny. 
    9     require OpenGuides::Build; 
    10     require OpenGuides::Config; 
    11 }; 
     6# This magically subclasses Module::Build. We override the install action 
     7# to install the template files for us. 
     8my $class = Module::Build->subclass( 
     9  code => q{ 
     10    sub ACTION_install { 
     11      my $self = shift; 
     12      my $ret = $self->SUPER::ACTION_install(@_); 
    1213 
    13 die "Problem loading OpenGuides module or a missing module\n\n$@.\n" if $@; 
     14      # Slightly hacky. We require one of the modules that we just installed. 
     15      # then, we pull its path out of %INC, strip off the filename, and 
     16      # install the templates there. Well, they have to go _somewhere_, and 
     17      # perl doesn't have a good place for this stuff otherwise. 
    1418 
    15 my $force; 
     19      require OpenGuides or die; 
     20      my (undef, $path, undef) = File::Spec->splitpath($INC{'OpenGuides.pm'}); 
    1621 
    17 GetOptions('force' => \$force); 
     22      mkdir( File::Spec->catfile( $path, "OpenGuides" ) ); 
     23      die "Can't make template install folder: $!\n" unless (-d File::Spec->catfile( $path, "OpenGuides" ) ); 
    1824 
    19 unless ($force) { 
    20         print <<EOF; 
     25      mkdir( File::Spec->catfile( $path, "OpenGuides", "templates" ) ); 
     26      die "Can't make template install folder: $!\n" unless (-d File::Spec->catfile( $path, "OpenGuides", "templates" ) ); 
    2127 
    22 Beginning install process... if you already have an OpenGuides 
    23 configuration file and you don't want to have to type in all your config 
    24 parameters over again, abort this process now, copy that file to this 
    25 directory, and start again. 
     28      print "Installing templates to $path/OpenGuides/templates\n"; 
     29      opendir TEMPLATES, "templates" or die "Can't open template source folder: $!\n"; 
     30      for (grep { /\.(tt)$/ } readdir(TEMPLATES)) { 
     31        print "  installing template $_\n"; 
     32        File::Copy::copy( 
     33          File::Spec->catfile('templates', $_), 
     34          File::Spec->catfile( $path, "OpenGuides", "templates", $_ ) 
     35        ) or die "Error copying $_: $!\n";; 
     36      } 
     37      closedir(TEMPLATES); 
    2638 
    27 EOF 
    28  
    29 my $continue = Module::Build->y_n("Continue with install?", "y"); 
    30 exit 0 unless $continue; 
    31 } 
    32  
    33 my $existing_config_file = 'wiki.conf'; 
    34 my $existing_config; 
    35  
    36 if (-f $existing_config_file) { 
    37     $existing_config = OpenGuides::Config->new(file => $existing_config_file); 
    38 } else { 
    39     print <<EOF; 
    40 No existing configuration file found; assuming this is a new install. 
    41 See the message above if this isn't correct. 
    42  
    43 EOF 
    44     $existing_config = OpenGuides::Config->new(); 
    45 } 
    46  
    47 my %yn_vars = map { $_ => 1 } 
    48    qw(use_plucene enable_page_deletion navbar_on_home_page backlinks_in_title 
    49       moderation_requires_password enable_node_image enable_common_categories 
    50       enable_common_locales recent_changes_on_home_page 
    51       random_page_omits_locales random_page_omits_categories 
    52       content_above_navbar_in_html show_gmap_in_node_display force_wgs84 
    53       send_moderation_notifications); 
    54  
    55 my $skip_config = $force ? 'y' : Module::Build->y_n("Skip OpenGuides configuration?", "n"); 
    56 if ( $skip_config ) { 
    57     print <<EOF; 
    58 =========================================================================== 
    59 Skipping OpenGuides configuration - any configuration options previously 
    60 saved will be used instead.  You may tweak your configuration now by 
    61 editing the 'wiki.conf' file produced by this script. 
    62 =========================================================================== 
    63 EOF 
    64 } 
    65  
    66 my @answers; 
    67  
    68 # It is an ancient Configurer, and he chooseth one of three.  
    69 my $dbtype; 
    70 my $dbtype_qu = $existing_config->dbtype__qu; 
    71 if ( $skip_config ) { 
    72     $dbtype = $existing_config->dbtype; 
    73 } else { 
    74     until ( $dbtype ) { 
    75         my $def = $existing_config->dbtype; 
    76         $dbtype = Module::Build->prompt("\n$dbtype_qu", $def); 
    77         $dbtype = lc($dbtype); 
    78         $dbtype =~ s/^\s*//; 
    79         $dbtype =~ s/\s*$//; 
    80         unless ( $dbtype eq "postgres" or $dbtype eq "mysql" 
    81                  or $dbtype eq "sqlite" ) { 
    82             undef $dbtype; 
    83         } 
     39      return $ret; 
    8440    } 
    85 } 
    86  
    87 # Check they have the relevant DBD driver installed. 
    88 my %drivers = ( postgres => "DBD::Pg", 
    89                 mysql    => "DBD::mysql", 
    90                 sqlite   => "DBD::SQLite", 
    91               ); 
    92 eval "require $drivers{$dbtype}"; 
    93 warn "$drivers{$dbtype} is needed to run a $dbtype database" if $@; 
    94  
    95 push @answers, { question => $dbtype_qu, 
    96                  variable => "dbtype", 
    97                  value    => $dbtype }; 
    98  
    99 my $install_directory; # used to suggest template paths 
    100 my $use_plucene = 1; # keep track of this so we know what to put in prereqs 
    101 my $centre_lat = ''; # contains centre lat derived from Google Maps URL 
    102 foreach my $var ( qw( 
    103    dbname dbuser dbpass dbhost dbport script_name 
    104    install_directory template_path custom_template_path script_url 
    105    custom_lib_path use_plucene indexing_directory enable_page_deletion 
    106    admin_pass stylesheet_url site_name navbar_on_home_page 
    107    recent_changes_on_home_page random_page_omits_locales 
    108    random_page_omits_categories content_above_navbar_in_html home_name 
    109    site_desc default_city default_country contact_email default_language 
    110    formatting_rules_node backlinks_in_title gmaps_api_key centre_long 
    111    centre_lat show_gmap_in_node_display default_gmaps_zoom 
    112    default_gmaps_search_zoom force_wgs84 google_analytics_key 
    113    licence_name licence_url licence_info_url moderation_requires_password 
    114    enable_node_image enable_common_categories enable_common_locales 
    115    spam_detector_module host_checker_module static_path static_url 
    116    send_moderation_notifications 
    117   ) ) { 
    118     my $q_method = $var . "__qu"; 
    119     my $qu  = $existing_config->$q_method; 
    120     my $type = $yn_vars{$var} ? "y_n" : ""; 
    121     my $def = $existing_config->$var; 
    122     my $val = $def; 
    123  
    124     # Override dbname question for SQLite only. 
    125     if ( $dbtype eq "sqlite" and $var eq "dbname" ) { 
    126         $qu = "what's the full filename of the SQLite database this site runs on?"; 
    127     } 
    128  
    129     if ( $dbtype eq "sqlite" and 
    130          ( $var eq "dbuser" or $var eq "dbpass" or $var eq "dbhost" or 
    131            $var eq "dbport") 
    132        ) { 
    133         print "$var not relevant for SQLite... skipping...\n" 
    134             unless $skip_config; 
    135         push @answers, { question => $qu, 
    136                             variable => $var, 
    137                          value    => "not-used" }; 
    138         next; 
    139     } 
    140  
    141     # We don't ask this for new installs as Search::InvertedIndex is 
    142     # deprecated 
    143     if ( $var eq "use_plucene" and $existing_config->$var == 1) { 
    144         print "Skipping question about plucene\n" 
    145             unless $skip_config; 
    146         push @answers, { question => $qu, 
    147                          variable => $var, 
    148                          value => 1 }; 
    149         next; 
    150     } 
    151  
    152     # Make sensible suggestions for template paths if we don't already 
    153     # have them stored.  Not really a default, but a useful hint/shortcut. 
    154     if ( $var eq "template_path" && !defined $existing_config->$var ) { 
    155         $def = $install_directory; 
    156         $def .= "/" unless $def =~ m|/$|; 
    157         $def .= "templates"; 
    158     } 
    159     if ( $var eq "custom_template_path" && !defined $existing_config->$var ) { 
    160         $def = $install_directory; 
    161         $def .= "/" unless $def =~ m|/$|; 
    162         $def .= "custom-templates"; 
    163     } 
    164  
    165     # If a Google Maps URL was provided last time we know the centre_lat 
    166     if ( $var eq 'centre_lat' && $centre_lat ) { 
    167         $val = $centre_lat; 
    168         next; 
    169     }     
    170  
    171     # Here is where we actually ask the questions. 
    172     unless ( $skip_config ) { 
    173         if ( $type eq "y_n" ) { 
    174             # may be stored as true/false integer value 
    175             if ( $def =~ /^\d+$/ ) { 
    176                 $def = $def ? "y" : "n"; 
    177             } 
    178             $val = Module::Build->y_n("\n$qu ", $def); 
    179         } else { 
    180             $val = Module::Build->prompt("\n$qu ", $def); 
    181         } 
    182     } 
    183  
    184     # Allow user to use a Google Maps URL rather than enter lat/long by hand. 
    185     # We assume centre_long is being asked for first; ensure so in big list above. 
    186     if ( $var eq 'centre_long' ) { 
    187         if ( $val =~ /ll=([-\d.]+),([-\d.]+)/ ) { 
    188             print "Got a Google Maps URL with centre long,lat: [$1, $2]\n"; 
    189             $val = $1; 
    190             $centre_lat = $2; 
    191         } 
    192     } 
    193  
    194     # Store install_directory so we can use it to suggest template paths. 
    195     $install_directory = $val if $var eq "install_directory"; 
    196  
    197     # Keep track of chosen search method so we know what to put in prereqs. 
    198     # From Module::Build docs: ->y_n returns a Perl boolean true or false. 
    199     $use_plucene = 1 if $var eq "use_plucene" and $val; 
    200  
    201     # Make sure that script_url ends in a / 
    202     if ( $var eq "script_url" and $val !~ /\/$/ ) { 
    203         $val .= "/"; 
    204     } 
    205  
    206     push @answers, { question => $qu, 
    207                      variable => $var, 
    208                      value    => $val }; 
    209 } 
    210  
    211 # Now deal with the geo stuff. 
    212 my $geo_handler; 
    213 my $geo_handler_qu = "Distance calculation methods available are:" 
    214                    . "\n  1) British National Grid" 
    215                    . "\n  2) Irish National Grid" 
    216                    . "\n  3) UTM ellipsoid" 
    217                    . "\nWhich would you like to use?"; 
    218  
    219 if ( $skip_config ) { 
    220     # We default to GB National Grid for historical reasons. 
    221     $geo_handler = $existing_config->geo_handler; 
    222 } else { 
    223     my $choice; 
    224     until ( $choice ) { 
    225         my $def = $existing_config->geo_handler; 
    226         $choice = Module::Build->prompt("\n".$geo_handler_qu, $def); 
    227         $choice =~ s/^\s*//; 
    228         $choice =~ s/\s*$//; 
    229         unless ( $choice eq "1" or $choice eq "2" or $choice eq "3" ) { 
    230             undef $choice; 
    231         } 
    232     } 
    233     $geo_handler = $choice; 
    234 } 
    235  
    236 $geo_handler_qu =~ s/\n//gs; 
    237 push @answers, { 
    238                  question => $geo_handler_qu, 
    239                  variable => "geo_handler", 
    240                  value    => $geo_handler, 
    241                }; 
    242  
    243 if ( $geo_handler eq "3" ) { 
    244     my $qu = $existing_config->ellipsoid__qu; 
    245     my $ellipsoid; 
    246     if ( $skip_config ) { 
    247         $ellipsoid = $existing_config->ellipsoid; 
    248     } else { 
    249         my $def = $existing_config->ellipsoid; 
    250         $ellipsoid = Module::Build->prompt("\n".$qu, $def); 
    251         $ellipsoid =~ s/^\s*//; 
    252         $ellipsoid =~ s/\s*$//; 
    253     } 
    254     push @answers, { 
    255                      question => $qu, 
    256                      variable => "ellipsoid", 
    257                      value    => $ellipsoid, 
    258                    }; 
    259 } 
    260  
    261 # Create a user-friendly config file from answers to prompts. 
    262 open FILE, ">wiki.conf" or die "Can't open wiki.conf for writing: $!"; 
    263 foreach my $ans (@answers) { 
    264     print FILE "# $ans->{question}\n"; 
    265     print FILE "$ans->{variable} = $ans->{value}\n\n"; 
    266 } 
    267 close FILE or die "Can't close wiki.conf: $!"; 
     41  }, 
     42); 
    26843 
    26944##### 
     
    27146##### 
    27247 
    273 # We currently only support Plucene for new installs, but may support 
    274 # others in future 
    275 my $search_module = $use_plucene ? "Plucene" : "Search::InvertedIndex"; 
    276  
    27748# Create the build object. 
    278 my $build = OpenGuides::Build->new( 
     49my $build = $class->new( 
    27950    sign => 1, 
    28051    dist_name => "OpenGuides", 
    28152    module_name => "OpenGuides", 
    282     dist_version_from => "wiki.cgi", 
     53    dist_version_from => "lib/OpenGuides.pm", 
    28354    license => "perl", 
    28455    requires => { 
     
    29869        'Config::Tiny'                        => 0, 
    29970        'Data::Dumper'                        => 0, 
    300         $drivers{$dbtype}                     => 0, 
    30171        'File::Spec::Functions'               => 0, 
    30272        'File::Temp'                          => 0, 
    303                 'Geo::Coordinates::UTM'               => 0, 
     73        'Geo::Coordinates::UTM'               => 0, 
    30474        'Geography::NationalGrid'             => 0, 
    30575        'HTML::Entities'                      => 0, 
     
    30777        'MIME::Lite'                          => 0, 
    30878        'Parse::RecDescent'                   => 0, 
    309         $search_module                        => 0, 
    31079        'POSIX'                               => 0, 
    31180        'Template'                            => '2.15', # for hash.delete and string.remove vmethods 
     
    32392        'Geo::HelmertTransform'  => 0,      # for correct WGS84 lat/long 
    32493                                            # when using grid systems 
     94        'Plucene'             => 0, 
    32595 
    32696    }, 
    327     dynamic_config => 1, 
    328     create_makefile_pl => "passthrough" 
     97    create_makefile_pl => "passthrough", 
     98    script_files => [ 
     99                      "bin/openguides-install", 
     100                      "bin/openguides-newpage-script", 
     101                      "bin/openguides-preferences-script", 
     102                      "bin/openguides-search-script", 
     103                      "bin/openguides-wiki-script", 
     104                    ], 
    329105); 
    330106 
     
    333109$build->add_to_cleanup( "t/templates/tmp/" ); 
    334110 
    335 # Tell OpenGuides::Build which additional scripts and templates to install. 
    336 $build->config_data( __extra_scripts =>  
    337                       [ "wiki.conf", "preferences.cgi", "search.cgi", 
    338                         "newpage.cgi" ] ); 
    339 $build->config_data( __templates     => [ 
    340                       "admin_home.tt", 
    341                       "admin_revert_user.tt", 
    342                       "backlink_results.tt", 
    343                       "banner.tt", 
    344                       "blacklisted_host.tt", 
    345                       "delete_confirm.tt", 
    346                       "delete_done.tt", 
    347                       "delete_password_wrong.tt", 
    348                       "differences.tt", 
    349                       "display_metadata.tt", 
    350                       "edit_form.tt", 
    351                       "edit_form_actions.tt", 
    352                       "error.tt", 
    353                       "footer.tt", 
    354                       "header.tt", 
    355                       "home_node.tt", 
    356                       "map_index.tt", 
    357                       "missing_metadata.tt", 
    358                       "moderate_confirm.tt", 
    359                       "moderate_password_wrong.tt", 
    360                       "navbar.tt", 
    361                       "navbar_categories.tt", 
    362                       "navbar_locales.tt", 
    363                       "navbar_help.tt", 
    364                       "navbar_home_link.tt", 
    365                       "navbar_options.tt", 
    366                       "navbar_revision_info.tt", 
    367                       "navbar_search.tt", 
    368                       "navbar_this_page.tt", 
    369                       "navbar_tools.tt", 
    370                       "needing_moderation.tt", 
    371                       "newpage.tt", 
    372                       "node.tt", 
    373                       "node_history.tt", 
    374                       "node_image_fields.tt", 
    375                       "node_photo_notes.tt", 
    376                       "node_rdf.tt", 
    377                       "openguides_information_boxes.tt", 
    378                       "preferences.tt", 
    379                       "random_page_failure.tt", 
    380                       "rdf_index.tt", 
    381                       "recent_changes.tt", 
    382                       "search_results.tt", 
    383                       "site_index.tt", 
    384                       "search.tt", 
    385                       "spam_detected.tt", 
    386                       "userstats.tt", 
    387                       "wanted_pages.tt" 
    388     ] ); 
    389  
    390 $build->config_data( __static_files => [ 
    391     # XXX to be supplied when we have some 
    392     ] ); 
    393  
    394111# Finally write the build script. 
    395112$build->create_build_script;