[Bio] / Sprout / NMPDRSetup.pl Repository:
ViewVC logotype

Annotation of /Sprout/NMPDRSetup.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     =head1 NMPDR Setup Script
4 :    
5 :     This script sets up a new version of the NMPDR web complex. It takes a single
6 :     positional parameter-- the numeric ID of the new version. So, for example
7 :    
8 :     NMPDRSetup 07
9 :    
10 :     would set up version 7.
11 :    
12 :     The script should run as part of the previous version. It will set up a new
13 :     version on the same machine. At some later point we may want to make it more
14 :     flexible.
15 :    
16 :     To run this script in the background, use something like
17 :    
18 :     nohup NMPDRSetup -background -user=You 07 >null &
19 :    
20 :     This will cause the output, error, and trace files to be produced in the
21 :     FIG temporary directory. The output file will be C<outYou.log>, the
22 :     error file will be C<errYou.log>, and the trace file C<traceYou.log>.
23 :    
24 : parrello 1.13 Currently, the development NMPDR runs on the %DEVSERVER% server, while
25 :     the production NMPDR runs on the %PRODSERVER% server. When copying files
26 : parrello 1.1 between servers for purposes of synchronization, it is important that
27 :     the rsync originate from the destination server so that the ownership is
28 :     not all messed up.
29 :    
30 :     =over 4
31 :    
32 :     =item trace
33 :    
34 :     Numeric trace level. A higher trace level causes more messages to appear. The
35 : parrello 1.13 default trace level is 3.
36 : parrello 1.1
37 :     =item user
38 :    
39 :     User name to append to trace and output file names. If none is specified, the
40 :     PID is used.
41 :    
42 :     =item envDir
43 :    
44 :     Fully-qualified name of the environment directory. The default is
45 :     C<$FIG_Config::nmpdr_base/env>.
46 :    
47 :     =item relName
48 :    
49 :     Name to use for the release created. The default is C<nmpdr_XX>, where XX is the vesion
50 :     number. In general, any use of C<XX> in the release name will be replaced by the two-
51 :     digit version number in the positional parameters.
52 :    
53 :     =item blastMAT
54 :    
55 :     Location of the BLAST tar file. The default is C<$FIG_Config::nmpdr_base/BLASTMAT.tgz>. If
56 :     C<copy> is specified, the BLAST directory is copied from this version to the new version.
57 :    
58 :     =item h
59 :    
60 :     Display this command's parameters and options.
61 :    
62 :     =item dataDir
63 :    
64 :     Path for the SEED data directory. The default is C<0>, which indicates the most
65 :     recently-modified directory will be used.
66 :    
67 :     =item force
68 :    
69 :     Delete any existing SEED. If this option is not specified, the program will fail
70 :     if it finds a SEED already at the location of the new NMPDR.
71 :    
72 :     =item test
73 :    
74 :     If specified, information is displayed but nothing is changed.
75 :    
76 :     =item stage
77 :    
78 :     Specify the stage at which to start. The current stages are as follows
79 :    
80 : parrello 1.8 =over 8
81 : parrello 1.1
82 : parrello 1.8 =item 1
83 : parrello 1.1
84 : parrello 1.8 Set up directories and check out the source.
85 : parrello 1.1
86 : parrello 1.8 =item 2
87 : parrello 1.1
88 : parrello 1.8 Bootstrap and build the new FIG.
89 : parrello 1.1
90 : parrello 1.8 =item 3
91 : parrello 1.2
92 : parrello 1.8 Perform configuration fixes.
93 :    
94 :     =item 4
95 :    
96 :     Fix file permissions.
97 :    
98 :     =item 5
99 :    
100 : parrello 1.13 Copy the wiki pages.
101 : parrello 1.8
102 :     =item 6
103 :    
104 :     Copy the drug target files.
105 : parrello 1.1
106 :     =back
107 :    
108 :     =item group
109 :    
110 :     Name of the group to be given write permission for all the files. The default
111 : parrello 1.13 is C<fig>.
112 : parrello 1.1
113 :     =item server
114 :    
115 : parrello 1.13 Symbolic name of the new version. This indicates the type of NMPDR being created,
116 : parrello 1.4 and is usually the name of a symlink known to the Apache configuration. The
117 :     default is C<next>, which indicates the development version. For the experimental
118 :     version, it should be C<dev>.
119 : parrello 1.1
120 :     =back
121 :    
122 :     =head2 Maintenance Notes
123 :    
124 :     This script will need to be updated if the default directories change. In addition,
125 :     the CVS information is hard-coded.
126 :    
127 :     =cut
128 :    
129 :     use strict;
130 :     use Tracer;
131 :     use DocUtils;
132 :     use Cwd;
133 :     use File::Copy;
134 :     use File::Path;
135 :     use File::stat;
136 :     use IO::Handle;
137 :    
138 :     # Get the command-line options.
139 :     my ($options, @parameters) = StandardSetup(['Sprout', 'DocUtils'],
140 :     { envDir => ["$FIG_Config::nmpdr_base/FIGdisk/env",
141 :     'environment directory containing pointers to applications and development tools'],
142 : parrello 1.13 blastMAT => ["copy", 'BLASTMAT TAR file or "copy" to copy the source BLASTMAT directory'],
143 :     dataDir => ["0", 'fully-qualified name of the SEED data directory to use; default is most recent'],
144 :     relName => ["nmpdr_XX", 'name to use for the release created (default "nmpdr_XX")'],
145 :     test => [0, 'print some data but do not run'],
146 :     stage => [0, 'stage at which to start'],
147 :     force => [0, 'force an update over a possible existing SEED'],
148 :     group => ['fig', 'group to be given write permission for the new files'],
149 :     server => ['next', 'NMPDR version type'],
150 :     trace => [3, 'tracing level'],
151 : parrello 1.1 },
152 : parrello 1.3 '<versionNumber>',
153 : parrello 1.1 @ARGV);
154 :    
155 :     # Get the main directory names.
156 :     my $arch = $FIG_Config::arch;
157 :     my $env_dir = $options->{envDir};
158 :     my $blastmat_tarfile = $options->{blastMAT};
159 :     my $base_dir = $FIG_Config::nmpdr_base;
160 :    
161 : parrello 1.2 # Get the new version number.
162 : parrello 1.1 my $version = $parameters[0];
163 : parrello 1.2 # Compute the current version number.
164 :     $FIG_Config::nmpdr_version =~ /(\d+)/;
165 :     my $old_version = $1;
166 : parrello 1.1
167 :     # Set up the CVS data. Note that we are using an anonymous login. You can
168 :     # update from the NMPDR server but you can't commit!
169 :     my $cvsroot = ":pserver:anonymous\@biocvs.mcs.anl.gov:/disks/cvs/bio";
170 : parrello 1.3 my $cvs_packages = "nmpdr_base";
171 : parrello 1.1
172 : parrello 1.3 # Check the parameters.
173 :     if (! $FIG_Config::nmpdr_base) {
174 : parrello 1.13 Confess("Please specify a directory value for FIG_Config::nmpdr_base.");
175 : parrello 1.3 } elsif (! -d $FIG_Config::nmpdr_base) {
176 :     Confess("NMPDR base directory \"$FIG_Config::nmpdr_base\" not found.");
177 :     } elsif ($blastmat_tarfile ne 'copy' && ! -e $blastmat_tarfile) {
178 :     Confess("BlastMAT TAR file not found at $blastmat_tarfile.");
179 :     } elsif (! -d $env_dir) {
180 :     Confess("Environment directory $env_dir not found.");
181 :     } elsif (! $version) {
182 :     Confess("No version number specified.");
183 :     } elsif ($version !~ /^\d+$/) {
184 :     Confess("Invalid (non-numeric) version number $version.");
185 : parrello 1.1 }
186 : parrello 1.13 # Make the version number pretty. This means stripping off the "v" (if any)
187 :     # and converting to two digits.
188 :     $version =~ s/^v//;
189 : parrello 1.1 $version = sprintf("%02d", $version);
190 :     # Add the version number to the release name.
191 :     my $version_rel = $options->{relName};
192 :     $version_rel =~ s/XX/$version/e;
193 :     # Now we can start the setup.
194 :     Trace("Creating NMPDR version $version with name $version_rel.");
195 :     # Get the directory for the version-specific stuff.
196 :     my $version_dir = "$base_dir/v$version";
197 :     Insure($version_dir);
198 :     # This will be the directory of FIG stuff.
199 :     my $target_dir = "$version_dir/FIGdisk";
200 :     # Compute the Sprout database name.
201 :     my $sprout_db_name = "nmpdr_v${version}_sprout";
202 :     Trace("Starting at stage $options->{stage}.") if T(2);
203 :     #
204 :     # Stage 1: SEED directories and CVS source.
205 :     #
206 :     if ($options->{stage} <= 1) {
207 :     # Insure we're not wiping out an old SEED.
208 :     if (-e $target_dir) {
209 :     if ($options->{force}) {
210 :     # Here we have an old seed but we want to build over the
211 :     # top of it.
212 :     Trace("Deleting SEED already in place.") if T(2);
213 :     if ($options->{test}) {
214 :     Trace("Tree $target_dir would be deleted.") if T(2);
215 :     } else {
216 :     rmtree($target_dir);
217 :     }
218 :     } else {
219 :     Confess("A SEED was already found at $target_dir. Use \"-force\" to force an update anyway.");
220 :     }
221 :     }
222 :     # At this point, if we're still alive, there's no target directory, so
223 :     # we want to create it.
224 :     Insure($target_dir);
225 :     # Build the main FIG directory.
226 :     Insure("$target_dir/FIG");
227 :     # Build the temporary directory.
228 :     Insure("$target_dir/FIG/Tmp");
229 :     # Build the sprout data directory.
230 :     Insure("$target_dir/FIG/SproutData");
231 :     # Now we create the code directories.
232 :     Insure("$target_dir/dist");
233 :     Insure("$target_dir/dist/releases");
234 : parrello 1.2 # Finally, we create the directory that holds the cover pages.
235 : parrello 1.1 Insure("$version_dir/html");
236 :     # Change to the target directory. This means we're inside FIGdisk and above
237 :     # FIG.
238 :     ChDir($target_dir);
239 :     # Symbolically link to the environment directory.
240 :     if ($options->{test}) {
241 :     Trace("Symlink from env to $env_dir would have been created.") if T(2);
242 :     } else {
243 :     do_symlink($env_dir, "env");
244 :     }
245 :     # Create the BLASTMat directory.
246 :     if ($blastmat_tarfile eq 'copy') {
247 :     # Here we are copying the BLAST data from the previous version.
248 :     my $new_blastmat = "$target_dir/BLASTMAT";
249 :     Insure($new_blastmat);
250 :     if ($options->{test}) {
251 :     Trace("BLASTMAT data will be copied from $FIG_Config::blastmat to $target_dir/BLASTMAT.") if T(2);
252 :     } else {
253 :     Trace("Copying BLASTMAT directory from $FIG_Config::blastmat to $new_blastmat.") if T(2);
254 :     for my $fileName (OpenDir($FIG_Config::blastmat)) {
255 :     copy "$FIG_Config::blastmat/$fileName", "$new_blastmat/$fileName";
256 :     }
257 :     }
258 :     } else {
259 :     # Here we are extracting the BLAST data from a TAR file.
260 :     if ($options->{test}) {
261 :     Trace("BLASTMAT would have been pulled from $blastmat_tarfile.") if T(2);
262 :     } else {
263 :     do_untar($blastmat_tarfile);
264 :     }
265 :     }
266 :     # Next we copy the Sprout group file.
267 :     my $newSproutDataDir = "$target_dir/FIG/SproutData";
268 :     if ($options->{test}) {
269 :     Trace("Sprout groups table would be copied from $FIG_Config::sproutData to $newSproutDataDir.") if T(2);
270 :     } else {
271 :     Trace("Copying Sprout groups table from $FIG_Config::sproutData to $newSproutDataDir.") if T(2);
272 :     copy "$FIG_Config::sproutData/groups.tbl", "$newSproutDataDir/groups.tbl";
273 :     }
274 :     # Now we need to symbolically link the data directory to the NMPDR master SEED.
275 :     my $best_data = "";
276 :     if ($options->{dataDir} ne "0") {
277 :     # Here the user specified a particular directory.
278 :     $best_data = $options->{dataDir};
279 :     } else {
280 :     # Here we want the most recently-modified data directory under
281 :     # $base_dir/FIGdisk/FIG.
282 :     my $data_base = "$base_dir/FIGdisk/FIG";
283 :     my @possibleDatas = grep { $_ =~ /^Data/ } OpenDir("$data_base");
284 :     my $best_time = 0;
285 :     Trace(scalar(@possibleDatas) . " subdirectories found under $data_base.") if T(2);
286 :     for my $possibleData (@possibleDatas) {
287 :     # Note we're using the File::stat thing here, which returns a hash of the
288 :     # file info, instead of the built-in function.
289 :     my $this_data = "$data_base/$possibleData";
290 :     my $fileInfo = stat $this_data;
291 :     Trace("Directory $this_data has modify time " . $fileInfo->mtime . ".") if T(3);
292 :     if ($fileInfo->mtime >= $best_time) {
293 :     Trace("Directory $this_data selected.") if T(3);
294 :     $best_data = $this_data;
295 :     $best_time = $fileInfo->mtime;
296 :     }
297 :     }
298 :     # If we didn't find any data, it's an error.
299 :     if (! $best_data) {
300 :     Confess("Could not find a data directory under $data_base.");
301 :     }
302 :     }
303 :     # Verify that we have a directory.
304 :     if (! -d $best_data) {
305 :     Confess("Cannot use $best_data because it is not a directory.");
306 :     }
307 :     # Symlink to the data directory we found. Now it will appear to be a part
308 :     # of the normal FIG data structure tree.
309 :     if ($options->{test}) {
310 :     Trace("Directory $best_data will be used for FIG/Data.") if T(2);
311 :     } else {
312 :     do_symlink($best_data, "FIG/Data");
313 :     }
314 :     # Change to the code directory.
315 :     ChDir("dist/releases");
316 :     # Create a directory to hold the release.
317 :     my $rel = $version_rel;
318 :     Insure($rel);
319 :     ChDir($rel);
320 :     # Set up the CVS log file. Note it will be in the same directory as
321 :     # our other log files with the same suffix.
322 :     my $lf = "$FIG_Config::temp/CVS$options->{user}.log";
323 :     if ($options->{test}) {
324 :     Trace("CVS checkout would be to " . getcwd() . ".") if T(2);
325 :     } else {
326 :     Trace("Checking out from CVS.") if T(2);
327 : parrello 1.4 my $rc = system("cvs -d $cvsroot co -P $cvs_packages >$lf 2>&1 ");
328 : parrello 1.1 if ($? != 0) {
329 :     # Here the checkout failed.
330 :     Confess("Error during CVS checkout for $cvsroot.");
331 :     } else {
332 :     Trace("CVS checkout log is in $lf.") if T(2);
333 :     }
334 :     # Now copy the configuration script and make it executable.
335 :     copy("FigCommon/configure", "$target_dir/configure") ||
336 :     Confess("Could not copy configuration script.");
337 :     chmod 0775, "$target_dir/configure";
338 :     # Copy the make file.
339 :     copy("ReleaseTools/Makefile.top", "Makefile") ||
340 :     Confess("Could not copy master make file.");
341 :     # Write out the release name.
342 :     Open(\*RELFILE, ">$target_dir/DEFAULT_RELEASE");
343 :     print RELFILE "$rel\n";
344 :     close(RELFILE);
345 :     # Hack out the PostGres initialization. NMPDR is MySQL-only.
346 :     unlink("FigCommon/env.$arch.pl");
347 :     }
348 :     Trace("Stage 1 complete.") if T(2);
349 :     }
350 :     #
351 :     # STAGE 2: Bootstrap and build the new FIG.
352 :     #
353 :     if ($options->{stage} <= 2) {
354 :     # Change to the target FIG directory. We'll run the configuration
355 :     # script from here, and it requires another log file.
356 :     ChDir($target_dir);
357 :     if ($options->{test}) {
358 :     Trace("Configuration script would be executed from $target_dir.") if T(2);
359 :     } else {
360 :     delete $ENV{PERL5LIB};
361 :     my $lf = "$FIG_Config::temp/Config$options->{user}.log";
362 :     Trace("Executing configuration script from $target_dir.") if T(2);
363 :     system("./configure $arch >$lf 2>&1");
364 :     if ($? != 0) {
365 :     Trace("Error during configuration script.") if T(0);
366 :     }
367 :     }
368 :     Trace("Stage 2 complete.") if T(2);
369 :     }
370 :     #
371 : parrello 1.2 # STAGE 3: Configure FIG_Config and the config script
372 :     # and protect from updates.
373 : parrello 1.1 #
374 :     if ($options->{stage} <= 3) {
375 : parrello 1.13 # Compute the configuration directory locations.
376 :     my $old_config_dir = "$FIG_Config::fig_disk/config";
377 :     my $new_config_dir = "$target_dir/config";
378 :     Insure($new_config_dir);
379 :     # Copy the old configuration. This destroys the config file created by the
380 :     # bootstrap.
381 :     my $old_config = "$old_config_dir/FIG_Config.pm";
382 :     my $new_config = "$new_config_dir/FIG_Config.pm";
383 :     if ($options->{test}) {
384 :     Trace("$old_config would be copied to $new_config.") if T(2);
385 :     } else {
386 :     Trace("Copying $old_config to $new_config") if T(2);
387 :     copy $old_config, $new_config;
388 :     }
389 :     # Next, we update the few lines at the beginning that have all the magic in them.
390 :     # Every other config value is derived from these three.
391 : parrello 1.1 my %changes = (
392 : parrello 1.13 version => $version,
393 :     old_version => $FIG_Config::version,
394 :     nmpdr_site_type => "/$options->{server}",
395 :     );
396 : parrello 1.1 if ($options->{test}) {
397 : parrello 1.13 Trace("Configuration file update would run from $new_config_dir.") if T(2);
398 : parrello 1.1 } else {
399 : parrello 1.13 Trace("Updating configuration file $new_config.") if T(2);
400 :     DocUtils::ModifyConfigFile($new_config, \%changes, []);
401 : parrello 1.1 }
402 :     # The next step is to update the shell script. We need to add CVS information and
403 :     # the temp directory.
404 :     my %elims = (CVSROOT => $cvsroot,
405 :     CVS_RSH => 'ssh',
406 :     TMPDIR => "$target_dir/FIG/Tmp",
407 :     );
408 : parrello 1.13 # Switch to the new configuration directory.
409 :     ChDir($new_config_dir);
410 : parrello 1.1 # Check to see if we're in test mode.
411 :     if ($options->{test}) {
412 : parrello 1.13 Trace("File fig-user-env.sh would be updated in " . getcwd() . ".") if T(2);
413 : parrello 1.1 for my $elim (keys %elims) {
414 :     Trace("export $elim = \"$elims{$elim}\"") if T(2);
415 :     }
416 :     } else {
417 :     # Slurp in the entire file.
418 :     my @lines = Tracer::GetFile("fig-user-env.sh");
419 :     # Loop through the variables.
420 :     for my $elim (keys %elims) {
421 :     # Remove any existing lines for the variable.
422 :     @lines = grep { $_ !~ /(export $elim)|($elim=)/ } @lines;
423 :     # Add the new lines.
424 :     Trace("Updating $elim in fig-user-env.sh.");
425 :     push @lines, "$elim=\"$elims{$elim}\"";
426 :     push @lines, "export $elim";
427 :     }
428 :     # Write the file back out.
429 :     Open(\*FIGOUT, ">fig-user-env.sh~");
430 :     for my $line (@lines) {
431 :     print FIGOUT "$line\n";
432 :     }
433 :     close FIGOUT;
434 :     rename "fig-user-env.sh~", "fig-user-env.sh";
435 :     }
436 : parrello 1.2 # Copy the WebApplication configuration subdirectory from the
437 : parrello 1.13 # current version to the next version.
438 :     Insure("$new_config_dir/WebApplication");
439 :     if ($options->{test}) {
440 :     Trace("WebApplication configuration files would be copied to $new_config_dir/WebApplication.") if T(2);
441 :     } else {
442 :     my @lines = `cp -r $old_config_dir/WebApplication/* $new_config_dir/WebApplication`;
443 :     Trace("WebApplication configuration files copied:\n" . join("\n", @lines)) if T(2);
444 :     }
445 : parrello 1.1 # Finally, create a marker file to protect this installation from
446 :     # automatic updates.
447 :     my $marker_file = "$target_dir/config/DISABLE_P2P";
448 :     if ($options->{test}) {
449 :     Trace("Marker file $marker_file would be created.") if T(2);
450 :     } else {
451 :     Trace("Creating marker file $marker_file.") if T(2);
452 :     Open(\*FIGOUT, ">$marker_file");
453 :     print FIGOUT "\n";
454 :     close FIGOUT;
455 :     }
456 :     # We're done.
457 :     Trace("Stage 3 complete.") if T(2);
458 :     }
459 :     #
460 :     # STAGE 4: fix the file permissions.
461 :     #
462 :     if ($options->{stage} <= 4) {
463 :     # Get the group name and ID.
464 :     my $gname = $options->{group};
465 :     my $gid = getgrnam($gname);
466 :     if ($options->{test}) {
467 :     Trace("Permission fix would occur in $version_dir. Group $gname has ID $gid.") if T(2);
468 :     } else {
469 :     # Set the permissions.
470 :     Tracer::SetPermissions($version_dir, $options->{group}, 02664, tmp => 02777);
471 : parrello 1.13 Trace("Permissions fixed in $version_dir.") if T(2);
472 : parrello 1.1 }
473 : parrello 1.2 Trace("Stage 4 complete.") if T(2);
474 :     }
475 :     #
476 : parrello 1.13 # STAGE 5: Copy the wiki pages.
477 : parrello 1.2 #
478 :     if ($options->{stage} <= 5) {
479 : parrello 1.13 # Make sure we have a place to put them.
480 :     Insure("$target_dir/FIG/WikiData");
481 :     # Compute the sync command.
482 :     my $command = "rsync --archive --recursive --perms --group --exclude=Trash/ $FIG_Config::wiki_data/ $target_dir/FIG/WikiData/";
483 :     # Execute it.
484 :     if ($options->{test}) {
485 :     Trace("Command to execute:\n$command") if T(2);
486 :     } else {
487 :     Trace("Copying wiki pages.") if T(2);
488 :     my @syncData = `$command`;
489 :     Trace("Copy results\n" . join("\n", @syncData)) if T(3);
490 :     }
491 : parrello 1.1 Trace("Stage 5 complete.") if T(2);
492 :     }
493 :     #
494 : parrello 1.2 # STAGE 6: Copy the drug target files.
495 : parrello 1.1 #
496 : parrello 1.2 if ($options->{stage} <= 6) {
497 : parrello 1.13 # Compute the sync command.
498 :     my $command = "rsync --archive --recursive $FIG_Config::var/DrugTargets $target_dir/FIG/var/";
499 :     if ($options->{test}) {
500 :     Trace("Command to execute:\n$command") if T(2);
501 :     } else {
502 :     Trace("Copying drug target files.") if T(2);
503 :     my @syncData = `$command`;
504 :     Trace("Copy results\n" . join("\n", @syncData)) if T(3);
505 :     }
506 : parrello 1.2 Trace("Stage 6 complete.") if T(2);
507 : parrello 1.1 }
508 : parrello 1.2
509 : parrello 1.1 Trace("Processing complete.") if T(0);
510 :    
511 :     # Forge a symbolic Link.
512 :     sub do_symlink {
513 :     my($from, $to) = @_;
514 :     # Erase the old link if necessary.
515 :     if (-e $to) {
516 :     Trace("Erasing old link $to.") if T(3);
517 :     unlink $to;
518 :     }
519 :     symlink($from, $to) ||
520 :     Confess("Symlink $from $to failed: $!");
521 :     }
522 :    
523 :     # Untar a bunch of files.
524 :     sub do_untar {
525 :     my($file) = @_;
526 :     my $args = "-x -f $file";
527 :     if ($file =~ /gz$/) {
528 :     $args .= " -z";
529 :     } elsif ($file =~ /bz2/) {
530 :     $args .= " -j";
531 :     }
532 :     my $cmd = "tar $args";
533 :     my $rc = system($cmd);
534 :     if ($rc != 0) {
535 :     Confess("tar failed with rc=$rc: $cmd");
536 :     }
537 :     }
538 :    
539 : parrello 1.9 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3