[system] / trunk / webwork / system / system_webwork_setup.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/system_webwork_setup.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (download) (as text) (annotate)
Wed Aug 29 18:24:58 2001 UTC (18 years, 4 months ago) by sh002i
File size: 19871 byte(s)
moved back to system/

    1 #!/usr/local/bin/perl
    2 
    3 ################################################################################
    4 # WeBWorK
    5 #
    6 # Copyright (c) 1995-2001 WeBWorK Team, University of Rochester
    7 # All rights reserved
    8 #
    9 # $Id$
   10 ################################################################################
   11 
   12 #################### initialization
   13 
   14 require 5.000;
   15 use Cwd;
   16 use File::Copy;
   17 use File::Find;
   18 use FindBin;
   19 
   20 # get the base (webwork/) directory
   21 my $BASE_DIR = $FindBin::Bin;
   22 
   23 # define built-in defaults
   24 my $DEFAULT_PERL_PATH = '/usr/bin/perl';
   25 my $DEFAULT_CGI_URL = '/cgi-bin/webwork/system/';
   26 my $DEFAULT_HTML_URL = '/webwork_system_html/';
   27 my $DEFAULT_ADMIN_GROUP = 'wwadmin';
   28 
   29 # define code strings
   30 my $CGI_DEBUG_TAG = 'WeBWorKCGIDebugURL';
   31 my $CGI_NODEBUG_TAG = 'WeBWorKCGINoDebugURL';
   32 my $LIB_INIT_LINE_TAG = 'WeBWorKInitLine';
   33 
   34 # scope and undefine setup variables
   35 $no_prompts              = undef;
   36 $system_setup_mode       = undef;
   37 $mainDir                 = undef;
   38 $perlPath                = undef;
   39 $cgiURL                  = undef;
   40 $htmlURL                 = undef;
   41 $groupName               = undef;
   42 $update_stuff_in_courses = undef;
   43 $chgrp_files_and_dirs    = undef;
   44 $chmod_files_and_dirs    = undef;
   45 $local_preprocessor      = undef;
   46 $local_postprocessor     = undef;
   47 
   48 # read defaults in from defaults file
   49 my $DEFAULTS_FILE;
   50 if($ARGV[0] and $ARGV[0] eq '-d') {
   51   if($ARGV[1]) { $DEFAULTS_FILE = $ARGV[1]; }
   52   else { $DEFAULTS_FILE = ((getpwuid $<)[7]) . '/system_webwork_setup.defaults'; }
   53   if(-e $DEFAULTS_FILE) {
   54     print "Reading defaults file...";
   55     require $DEFAULTS_FILE;
   56     print " done.\n";
   57   }
   58 }
   59 
   60 #################### text strings
   61 
   62 my $INTRO_TEXT = q{
   63 +----------------------+
   64 | System Webwork Setup |
   65 +----------------------+
   66 
   67 This script is used to setup the main WeBWorK system. It will create
   68 initialization files, set groups and permissions for files and directories,
   69 and modify files. A "demo" or "working" version of the system can be setup.
   70 
   71 You will need the following information:
   72 
   73 1) The path to perl on your system. Usually this is something like
   74 /usr/bin/perl or /usr/local/bin/perl
   75 
   76 2) The group (e.g. wwadmin) containing the user names of anyone who has the
   77 authority to modify the webwork system files. Who ever runs this script must
   78 be a member of this group. This should be set up by your system
   79 administrator before running this script. (Note: This is not required for a
   80 demo version.)
   81 
   82 3) The cgi WeBWorK URL.  Usually this is something like
   83 http://webwork.math.rochester.edu/cgi-bin/webwork/system. In the above
   84 cases, you can just enter /cgi-bin/webwork/system which is prefered but
   85 if you do, don't forget the initial / .
   86 
   87 4) The html WeBWorK URL.  Usually this is something like
   88 http://webwork.math.rochester.edu/webwork_system_html. Again in this
   89 case /webwork_system_html is prefered.
   90 
   91 };
   92 
   93 my $MODE_TEXT = q{
   94 
   95 You can set up a "working" or a "demo" WeBWorK system.  A "demo" system
   96 should only be used as a sample system, never for a system that will be used
   97 with actual courses with real students.  The main difference between a
   98 "working" version and a "demo" version is that in a "working" version you
   99 will be promped to enter a group (e.g. wwadmin) where as in a "demo"
  100 version, the group will be set yo your own default group (e.g. fac). Anyone
  101 in the group will have permission to modify all webwork files. You can set
  102 up "working" and "demo" courses under either a "working" or a "demo" system,
  103 but normally you would not set up a "working" course under a "demo" system.
  104 
  105 };
  106 
  107 my $MAIN_DIR_TEXT = q{
  108 
  109 The directory containing the WeBWorK system files (as well as this
  110 script) is known as the system directory. In order to modify system
  111 files, I need to know the location of the system directory for this
  112 installation.
  113 
  114 };
  115 
  116 my $PERL_TEXT = q{
  117 
  118 WeBWorK needs to know the path of your perl binary, so that this
  119 information can be used in the headers of cgi scripts. Please make sure
  120 that the version of perl you specify is 5.004 or later.
  121 
  122 };
  123 
  124 my $CGI_URL_TEXT = q{
  125 
  126 In order for generated HTML to be able to invoke CGI scripts, WeBWorK
  127 needs to know the URL which points to the main WeBWorK system cgi
  128 subdirectory. For example,
  129 http://webwork.math.rochester.edu.edu/cgi-bin/webwork/system might be a
  130 valid CGI URL. If both static HTML and CGIs reside on the same host
  131 (which is true in most cases), you can omit the
  132 http://webwork.math.rochester.edu section of the URL. Make sure to
  133 include a leading slash in this case.
  134 
  135 };
  136 
  137 my $HTML_URL_TEXT = q{
  138 
  139 WeBWorK also needs to know the URL of the main HTML directory, the
  140 system html subdirectory. For example,
  141 http://webwork.math.rochester.edu/webwork_system_html or
  142 /webwork_system_html.
  143 
  144 };
  145 
  146 my $GROUP_TEXT = q{
  147 
  148 WeBWorK needs to know what the admin group is. This group should have been
  149 set up by your system administrator and must contain at least your user ID.
  150 All files and directories created will have this as their group.
  151 
  152 };
  153 
  154 my $COURSE_PERMS_TEXT = q{
  155 
  156 You have the option to set permissions for the courses directory. If this is
  157 an inital setup, you should probably do so. If this is not an initial setup,
  158 the courses directory is already set up, or the courses directory is shared
  159 between WeBWorK installations, your probably shouldn't.
  160 
  161 };
  162 
  163 my $CHGRP_TEXT = q{
  164 
  165 You have to option to set the group for all system files and directories. If
  166 this is an initial setup, you should probably do this. If this is not an
  167 initial setup, or you have customized the way system files should be
  168 grouped, you probably shoudn't.
  169 
  170 };
  171 
  172 my $CHMOD_TEXT = q{
  173 
  174 You have to option to set the permissions for all system files and
  175 directories. If this is an initial setup, you should probably do this. If
  176 this is not an initial setup, or you have customized the permissions for the
  177 system files, you probably shoudn't.
  178 
  179 };
  180 
  181 my $CONFIRM_TEXT = q{
  182 
  183 Now that I have the necessary information, I can begin modifying the WeBWorK
  184 system files.
  185 
  186 };
  187 
  188 my $DONE_TEXT = q{
  189 
  190 The system setup script is done. Please read the above messages
  191 carefully to determine if there have been any errors. If so, correct the
  192 problem (usually a permissions problem) and run this script again.
  193 
  194 };
  195 
  196 
  197 
  198 
  199 
  200 ################################################################################
  201 ########## Ask some questions, perform some logic. #############################
  202 ################################################################################
  203 
  204 
  205 
  206 
  207 
  208 #################### introduction
  209 
  210 my $temp;
  211 
  212 unless($no_prompts) {
  213   page($INTRO_TEXT);
  214   $temp = questionChar("Do you want to continue with setup?", 'y', 'y', 'n');
  215   exit unless $temp eq 'y';
  216 }
  217 print "Okay, here we go...\n";
  218 
  219 #################### working or demo?
  220 
  221 unless(defined $system_setup_mode) {
  222   page($MODE_TEXT);
  223   $temp = questionChar("Shall we set up a working version or a demo version?", 'w', 'w', 'd');
  224   $system_setup_mode = "working" if $temp eq 'w';
  225   $system_setup_mode = "demo" if $temp eq 'd';
  226 }
  227 print "System setup mode is: $system_setup_mode\n";
  228 
  229 #################### main directory
  230 
  231 unless(defined $mainDir) {
  232   page($MAIN_DIR_TEXT);
  233   $mainDir = questionString("Where is the WeBWorK system directory?", "$BASE_DIR/system/");
  234   $mainDir .= '/' unless $mainDir =~ m|/$|;  # ensure trailing slash
  235 }
  236 print "We'll use $mainDir as WeBWorK's system directory.\n";
  237 
  238 #################### perl path
  239 
  240 unless(defined $perlPath) {
  241   page($PERL_TEXT);
  242   $perlPath = questionString("What is the full path to PERL?", $DEFAULT_PERL_PATH);
  243 }
  244 print "Path to PERL binary is: $perlPath\n";
  245 
  246 #################### CGI URL
  247 
  248 unless(defined $cgiURL) {
  249   page($CGI_URL_TEXT);
  250   while (1) {
  251     $cgiURL = questionString("What is the CGI URL?", $DEFAULT_CGI_URL);
  252     if( ($cgiURL =~ m|^/|) or ($cgiURL =~ m|^http://|) ) {
  253       last;
  254     } else {
  255       $temp = questionChar("That doesn't look like a valid URL. Would you like to use it anyway?", 'n', 'y', 'n');
  256       last if $temp eq 'y';
  257     }
  258   }
  259   $cgiURL .= "/" unless $cgiURL =~ m"/$"; # ensure trailing slash
  260 }
  261 print "CGI URL is: $cgiURL\n";
  262 
  263 #################### HTML URL
  264 
  265 unless(defined $htmlURL) {
  266   page($HTML_URL_TEXT);
  267   while (1) {
  268     $htmlURL = questionString("What is the HTML URL?", $DEFAULT_HTML_URL);
  269     if( ($htmlURL =~ m|^/|) or ($htmlURL =~ m|^http://|) ) {
  270       last;
  271     } else {
  272       $temp = questionChar("That doesn't look like a valid URL. Would you like to use it anyway?", 'n', 'y', 'n');
  273       last if $temp eq 'y';
  274     }
  275   }
  276   $htmlURL .= "/" unless $htmlURL =~ m"/$" ;
  277 }
  278 print "HTML URL is: $htmlURL\n";
  279 
  280 #################### admin group
  281 
  282 unless(defined $groupName) {
  283   my ($userName, $userGID) = (getpwuid $<)[0,3];
  284   my $userGroupName = (getgrgid $userGID)[0];
  285 
  286   if ($system_setup_mode eq 'demo')  {
  287     # in demo mode, the group is set to the user's primary group
  288     $groupName = $userGroupName;
  289   } else {
  290     # in working mode, we get to chose
  291     page($GROUP_TEXT);
  292     my $validGroup = 0;
  293     while(1) {
  294       $groupName = questionString("What is the admin group name?", $DEFAULT_ADMIN_GROUP);
  295       my @members = split / /, (getgrnam $groupName)[3];
  296       if($groupName eq $userGroupName) {
  297         print "$groupName is ${userName}'s primary group. Good.\n";
  298       } elsif(grep /$userName/, @members) {
  299         print "$userName is a member of $groupName. Good.\n";
  300         last;
  301       } elsif($< == 0) { # we're root!
  302         print "$userName isn't a member of $groupName, but you're root, so who cares?\n";
  303         last;
  304       } else {
  305         print "That group is not valid. Please make sure the group exists and you are a member.\n";
  306       }
  307     }
  308   }
  309 }
  310 print "Admin group is: $groupName\n";
  311 
  312 #################### chmod courses directory
  313 
  314 unless(defined $update_stuff_in_courses) {
  315   page($COURSE_PERMS_TEXT);
  316   $temp = questionChar("Do you want to set default $system_setup_mode permissions for the courses directory?", 'y', 'y', 'n');
  317   $update_stuff_in_courses = ($temp eq 'y');
  318 }
  319 print "Permissions ", ($update_stuff_in_courses ? "will" : "will not"), " be set for the courses directory.\n";
  320 
  321 
  322 #################### chgrp files/directories
  323 
  324 $system_setup_mode eq "demo" and $chgrp_files_and_dirs = 1;
  325 unless(defined $chgrp_files_and_dirs) {
  326   page($CHGRP_TEXT);
  327   $temp = questionChar("Do you want to set the group for system files and directories?", 'y', 'y', 'n');
  328   $chgrp_files_and_dirs = ($temp eq 'y');
  329 }
  330 print "Group ", ($chgrp_files_and_dirs ? "will" : "will not"), " be set for system files and directories.\n";
  331 
  332 #################### chmod files/directories
  333 
  334 $system_setup_mode eq "demo" and $chmod_files_and_dirs = 1;
  335 unless(defined $chmod_files_and_dirs) {
  336   page($CHMOD_TEXT);
  337   $temp = questionChar("Do you want to set the permissions for system files and directories?", 'y', 'y', 'n');
  338   $chmod_files_and_dirs = ($temp eq 'y');
  339 }
  340 print "Permissions ", ($chmod_files_and_dirs ? "will" : "will not"), " be set for system files and directories.\n";
  341 
  342 #################### make sure we want to actually do this
  343 
  344 unless($no_prompts) {
  345   print $CONFIRM_TEXT;
  346   $temp = questionChar("Do you want to continue with setup?", 'y', 'y', 'n');
  347   exit unless $temp eq 'y';
  348 }
  349 print "\Going to make changes now...\n\n";
  350 
  351 
  352 
  353 
  354 
  355 ################################################################################
  356 ########## Now we start changing things... #####################################
  357 ################################################################################
  358 
  359 
  360 
  361 
  362 
  363 #################### run local preprocessor
  364 
  365 if(defined $local_preprocessor) {
  366   print "Executing local preprocessor...\n";
  367   &$local_preprocessor;
  368   print "Done with local preprocessor.\n";
  369 }
  370 
  371 #################### update #! and use lines
  372 # uses: $mainDir, $perlPath
  373 
  374 print "Fixing #! lines...\n";
  375 
  376 foreach my $dir ("${mainDir}cgi/cgi-scripts", "${mainDir}scripts", "${mainDir}courseScripts") {
  377   foreach my $file (<${dir}/*.pl>) {
  378     fixFile($file);
  379   }
  380 }
  381 
  382 sub fixFile
  383 {
  384   my ($file) = @_;
  385 
  386   # read the file
  387   open FILE, $file || die "Couldn't open $file for reading.";
  388   my @lines = <FILE>;
  389   close FILE || die "Couldn't close $file after reading.";
  390 
  391   # fix perl path
  392   $lines[0] =~ m/^#!(\S*)/;
  393   if($1 ne $perlPath) {
  394     $lines[0] =~ s/^#!\S*/#!$perlPath/;
  395     open FILE, ">$file" || die "Couldn't open $file for writing.";
  396     print FILE @lines;
  397     close FILE || die "Couldn't close $file for writing.";
  398   }
  399 }
  400 
  401 print "done fixing #! and \"use\" lines.\n\n";
  402 
  403 #################### write webworkConfig.pm file
  404 # uses: $mainDir, $cgiURL, $htmlURL
  405 
  406 print "Writing lib/webworkConfig.pm file...\n";
  407 open CONFIG_FILE, ">${mainDir}lib/webworkConfig.pm";
  408 print CONFIG_FILE<<END_OF_CONFIG_FILE;
  409 package Global;
  410 
  411 ################################################################################
  412 # WeBWorK
  413 #
  414 # Copyright (c) 1995-2001 WeBWorK Team, University of Rochester
  415 # All rights reserved
  416 ################################################################################
  417 
  418 \$mainDirectory = "$mainDir";
  419 \$htmlWebworkURL = "$htmlURL";
  420 \$cgiWebworkURL = "${cgiURL}";
  421 \$cgiWebworkURL .= "cgi-scripts/" unless \$cgiDebugMode;
  422 
  423 1;
  424 END_OF_CONFIG_FILE
  425 close CONFIG_FILE;
  426 print "Done writing lib/webworkConfig.pm file.\n\n";
  427 
  428 #################### write webworkInit.pm files
  429 # uses: $mainDir
  430 
  431 print "Writing webworkInit.pm files...\n";
  432 foreach my $dir ('cgi/', 'cgi/cgi-scripts/', 'scripts/', 'courseScripts/') {
  433   open INIT_FILE, ">$mainDir${dir}webworkInit.pm";
  434   print INIT_FILE<<END_OF_INIT_FILE;
  435 ################################################################################
  436 # WeBWorK
  437 #
  438 # Copyright (c) 1995-2001 WeBWorK Team, University of Rochester
  439 # All rights reserved
  440 ################################################################################
  441 
  442 use lib '${mainDir}lib/';
  443 
  444 1;
  445 END_OF_INIT_FILE
  446   close INIT_FILE;
  447 }
  448 print "Done writing webworkInit.pm files.\n\n";
  449 
  450 #################### chgrp system stuff
  451 # uses: $chgrp_files_and_dirs, $groupName
  452 
  453 if($chgrp_files_and_dirs) {
  454   print "Setting group on system files and directories...\n";
  455   system "chgrp -R $groupName $mainDir";
  456   system "chgrp -R $groupName $BASE_DIR/system_html";
  457   print "Done setting group.\n\n";
  458 }
  459 
  460 #################### chmod system stuff
  461 # uses: $chmod_files_and_dirs
  462 
  463 if($chmod_files_and_dirs) {
  464   print "Setting permissions on system files and directories for $system_setup_mode mode...\n";
  465   if ($system_setup_mode eq "demo") {
  466     # get some general permissions for files and directories
  467     system "find $mainDir $BASE_DIR/system_html -type d -print0 | xargs -0 chmod 0755";
  468     system "find $mainDir $BASE_DIR/system_html -type f -print0 | xargs -0 chmod 0644";
  469     # add executable privs to scripts
  470     system "find ${mainDir}cgi ${mainDir}scripts -type f -print0 | xargs -0 chmod 0755";
  471   } else {
  472     # get some general permissions for files and directories
  473     system "find $mainDir $BASE_DIR/system_html -type d -print0 | xargs -0 chmod 0775";
  474     system "find $mainDir $BASE_DIR/system_html -type f -print0 | xargs -0 chmod 0664";
  475     # add executable privs to scripts
  476     system "find ${mainDir}cgi ${mainDir}scripts -type f -print0 | xargs -0 chmod 0775";
  477   }
  478   print "done setting permissions.\n\n";
  479 }
  480 
  481 #################### update couses stuff
  482 # uses: $update_stuff_in_courses
  483 
  484 if($update_stuff_in_courses) {
  485   print "Setting permissions for ../courses directory.\n\n";
  486   chmod(0755, "$BASE_DIR/courses") or warn "Warning: I can't set permissions for ../courses directory. It's possible that the directory doesn't exist or you don't have permission to change it.\n";
  487 }
  488 
  489 #################### run local postprocessor
  490 
  491 if(defined $local_postprocessor) {
  492   print "Executing local postprocessor...\n";
  493   &$local_postprocessor;
  494   print "Done with local postprocessor.\n";
  495 }
  496 
  497 #################### finish up
  498 
  499 page($DONE_TEXT);
  500 
  501 ################################################################################
  502 
  503 sub page
  504 {
  505   my @string_lines = split /^/, shift; #/
  506   # not really optimal, but we're going to assume a constant screen height.
  507   my $SCREEN_HEIGHT = 20;
  508   while(@string_lines) {
  509     print join "", @string_lines[0..($SCREEN_HEIGHT>scalar @string_lines ? (scalar @string_lines)-1 : $SCREEN_HEIGHT-1)];
  510     if(scalar @string_lines >= $SCREEN_HEIGHT) {
  511       print "\n[Press ENTER to continues...]";
  512       <STDIN>;
  513       print "\n";
  514     }
  515       @string_lines = @string_lines[$SCREEN_HEIGHT..$#string_lines];
  516   }
  517 }
  518 
  519 sub questionChar
  520 {
  521   my ($question, $default, @valid) = @_;
  522   my $answer;
  523   do {
  524     print $question, " ";
  525     foreach (@valid) {
  526       $_ eq $default and print "[";
  527       print $_;
  528       $_ eq $default and print "]";
  529     }
  530     print " ";
  531     $answer = <STDIN>;
  532     $answer =~ s/^\s*//;
  533     $answer = substr $answer, 0, 1;
  534     $answer = lc $answer;
  535     $answer or $answer = $default;
  536   } while (not grep(/$answer/, @valid));
  537   return $answer;
  538 }
  539 
  540 sub questionString
  541 {
  542   my ($question, $default, $emptyOK) = @_;
  543   my $answer;
  544   print $question, " [", $default, "] ";
  545   $answer = <STDIN>;
  546   chomp $answer;
  547   $answer =~ s/^\s*//;
  548   $answer or $answer = $default;
  549   return $answer;
  550 }
  551 
  552 __END__
  553 
  554 =head1 NAME
  555 
  556 system_webwork_setup.pl - set up the WeBWorK system
  557 
  558 =head1 SYNOPSIS
  559 
  560 system_webwork_setup.pl [B<-d> [defaults-file]]
  561 
  562 =head1 DESCRIPTION
  563 
  564 B<system_webwork_setup.pl> gathers the information necessary for configuration of the WeBWorK system. It then edits several WeBWorK system files, and creates F<webworkInit.pm> modules and the F<webworkConfig.pm> module based on the information gathered. It can optionally read defaults from a file specified on the command line or from the file F<$HOME/system_webwork_setup.defaults>.
  565 
  566 =head1 OPTIONS
  567 
  568 =over 4
  569 
  570 =item B<-d>
  571 
  572 enables defaults-file processing. If no defaults-file is specified, the file F<$HOME/system_webwork_setup.defaults> will be used.
  573 
  574 =head1 DEFAULTS
  575 
  576 The F<system_webwork_setup.defaults> file is intended to be used in a situation where B<system_webwork_setup.pl> must be executed frequently, such as in the case of use with CVS, in which the it must be executed after a checkout and some updates. The defaults file is a perl script that will be require'd by B<system_webwork_setup.pl>. It can set any of the following variables in the usual way:
  577 
  578 =over 4
  579 
  580 =item $no_prompts
  581 
  582 Refrain from prompting at the beginning of the script and before making changes. Also supresses the introductory text.
  583 
  584 =item $system_setup_mode
  585 
  586 Can be set to "working" or "demo". This affects how system file permissions and group ownership are set. In "working" mode, system files are group owned by a webwork admin group (see I<$groupName>) and group writeable while in "demo" mode files are group owned by the current user's default group and are not group writeable.
  587 
  588 =item $mainDir
  589 
  590 Specify WeBWorK's main directory, which should contain the directories cgi/ scripts/ courseScripts/ and lib/.
  591 
  592 =item $perlPath
  593 
  594 Specify the path to the perl interpreter which should be used. This is used to set the shebang line in cgi-scripts and scripts.
  595 
  596 =item $cgiURL
  597 
  598 Specify the externally visible URL to the CGI directory (typically cgi/cgi-scripts/). This will be used when scripts call other scripts.
  599 
  600 =item $htmlURL
  601 
  602 Specify the externally visible URL to the HTML directory (typically system_html/). This will be used when scripts refer to static graphics and text.
  603 
  604 =item $groupName
  605 
  606 When in "working" mode, I<$groupName> is the name of the WeBWorK admin group, members of which should have write access to the system files. (The web server should not be in this group!)
  607 
  608 =item $update_stuff_in_courses
  609 
  610 If true, B<system_webwork_setup.pl> will set permissions in the courses directory and prepare the B<course_webwork_setup.pl> script for execution.
  611 
  612 =item $chgrp_files_and_dirs, $chmod_files_and_dirs
  613 
  614 If true, the group and/or permissions on system files will be set according to I<$system_setup_mode>.
  615 
  616 =item $local_preprocessor, $local_postprocessor
  617 
  618 If set, these variables will be called as subroutine references. I<$local_preprocessor> is called before any changes take place, while I<$local_postprocessor> is called after all changes have taken place.
  619 
  620 =back
  621 
  622 =head1 FILES
  623 
  624 $HOME/system_webwork_setup.defaults
  625 
  626 =head1 AUTHOR
  627 
  628 Samuel Hathaway <sh002i@math.rochester.edu>

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9