source: main/trunk/greenstone2/bin/script/activate.pl@ 25892

Last change on this file since 25892 was 25892, checked in by ak19, 12 years ago

First commit for allowing a SOLR collection to be activated without having to run ant restart to get Greenstone to allow searching the updated solr index.

  • Property svn:executable set to *
File size: 28.1 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# activate.pl -- to be called after building a collection to activate it.
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 2009 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29
30# This program is designed to support the building process of Greenstone.
31# It deactivates the collection just built, if the web server is running
32# and is a persistent web server (or if the library_URL provided as
33# parameter to this script is of a currently running web server). It then
34# moves building to index, before activating the collection on the GS2 or
35# GS3 web server again if necessary.
36
37
38BEGIN {
39 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
40 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
41 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
42}
43
44
45use strict;
46no strict 'refs'; # allow filehandles to be variables and vice versa
47no strict 'subs'; # allow barewords (eg STDERR) as function arguments
48
49use File::Basename;
50use File::Find;
51
52use HTTP::Response;
53use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
54 # don't import LWP::Simple's head function by name since it can conflict with CGI:head())
55#use CGI qw(:standard); # then only CGI.pm defines a head()
56use Net::Ping;
57use URI;
58
59use colcfg;
60use scriptutil;
61use util;
62#use enum;
63
64# enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
65# Unfortunately, not part of perl's core
66#use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
67
68# global variables
69#my $default_verbosity = LEVEL_ERROR; # by default we display basic error messages
70
71my $default_verbosity = 2; # by default we display basic error and info messages
72
73sub print_task_msg {
74 my ($task_msg, $verbosity_setting) = @_;
75
76 $verbosity_setting = $default_verbosity unless $verbosity_setting;
77 #$verbosity_setting = 1 unless defined $verbosity;
78 if($verbosity_setting >= 1) {
79 print STDERR "\n";
80 print STDERR "************************\n";
81 print STDERR "* $task_msg\n";
82 print STDERR "************************\n";
83 }
84}
85
86# Prints messages if the verbosity is right. Does not add new lines.
87sub print_msg {
88 my ($msg, $min_verbosity, $verbosity_setting) = @_;
89
90 # only display error messages if the current
91 # verbosity setting >= the minimum verbosity level
92 # needed for that message to be displayed.
93
94 $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
95 $min_verbosity = 1 unless defined $min_verbosity;
96 if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
97 print STDERR "$msg";
98 }
99}
100
101# Method to send a command to a GS2 or GS3 library_URL
102# the commands used in this script can be activate, deactivate, ping,
103# and is-persistent (is-persistent only implemented for GS2).
104sub config {
105 my ($library_url, $command, $check_message_against_regex, $site, $expected_error_code) = @_;
106 # Gatherer.java's configGS3Server doesn't use the site variable
107 # so we don't have to either
108
109 # for GS2, getting the HTTP status isn't enough, we need to read the output
110 # since this is what CollectionManager.config() stipulates.
111 # Using LWP::UserAgent::get($url) for this
112
113 if(!defined $library_url) {
114 return 0;
115 }
116 else {
117 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
118 # Need to set this, else it takes I don't know how long to timeout
119 # http://www.perlmonks.org/?node_id=618534
120
121 # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
122 # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
123
124 my $response_obj = $ua->get( $library_url.$command);
125
126 # $response_obj->content stores the content and $response_obj->code the HTTP response code
127 my $response_code = $response_obj->code();
128
129 if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
130 &print_msg("*** Command $library_url$command\n", 3);
131 &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
132
133 # check the page content is as expected
134 my $response_content = $response_obj->content;
135 if($response_content =~ m/$check_message_against_regex/) {
136 &print_msg(" Response as expected.\n", 3);
137 return 1;
138 } else {
139 &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
140 &print_msg("*** Got message:\n$response_content.\n", 4);
141 return 0; # ping on a collection may "not succeed."
142 }
143 }
144 elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
145 # check the page content is as expected
146 if(defined $expected_error_code && $response_code == $expected_error_code) {
147 &print_msg(" Response status $response_code as expected.\n", 3);
148 } else {
149 &print_msg("*** Command $library_url$command\n");
150 &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
151 }
152 return 0; # return false, since the response_code was an error, expected or not
153 }
154 }
155}
156
157sub deactivate_collection {
158 my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
159
160 if($gs_mode eq "gs2") {
161 my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
162 my $check_message_against_regex = q/configured release-collection/;
163 config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
164 }
165 elsif ($gs_mode eq "gs3") {
166 my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
167 my $check_message_against_regex = "collection: $qualified_collection deactivated";
168 config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
169 }
170}
171
172sub activate_collection {
173 my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
174
175 if($gs_mode eq "gs2") {
176 my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
177 my $check_message_against_regex = q/configured add-collection/;
178 config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
179 }
180 elsif ($gs_mode eq "gs3") {
181 my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
182 my $check_message_against_regex = "collection: $qualified_collection activated";
183 config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
184 }
185}
186
187sub ping {
188 my ($library_url, $command, $gs_mode, $site) = @_;
189
190 # If the GS server is not running, we *expect* to see a "500" status code.
191 # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
192 # When pinging an inactive collection, it will say it did "not succeed". This is
193 # a message of interest to return.
194 my $check_responsemsg_against_regex = q/(succeeded)/;
195 my $expected_error_code = 500;
196 return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code);
197}
198
199# send a pingaction to the GS library. General server-level ping.
200sub ping_library {
201 my ($library_url, $gs_mode, $site) = @_;
202
203 my $command = "";
204 if($gs_mode eq "gs2") {
205 $command = "?a=ping";
206 }
207 elsif ($gs_mode eq "gs3") {
208 $command = "?a=s&sa=ping";
209 }
210 return &ping($library_url, $command, $gs_mode, $site);
211}
212
213
214# send a pingaction to a collection in GS library to check if it's active
215sub ping_library_collection {
216 my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
217
218 my $command = "";
219 if($gs_mode eq "gs2") {
220 $command = "?a=ping&c=$qualified_collection";
221 }
222 elsif ($gs_mode eq "gs3") {
223 $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";
224 }
225 return &ping($library_url, $command, $gs_mode, $site);
226}
227
228# return true if server is persistent, by calling is-persistent on library_url
229# this is only for GS2, since the GS3 server is always persistent
230sub is_persistent {
231 my ($library_url, $gs_mode) = @_;
232
233 if($gs_mode eq "gs3") { # GS3 server is always persistent
234 return 1;
235 }
236
237 my $command = "?a=is-persistent";
238 my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
239 return config($library_url, $command, $check_responsemsg_against_regex);
240}
241
242sub get_library_URL {
243 my $gs_mode = shift(@_); # gs3 or gs2
244
245 # If we get here, we are dealing with a server included with GS.
246 # For GS3, we ask ant for the library URL.
247 # For GS2, we derive the URL from the llssite.cfg file.
248
249 my $url = undef;
250
251 if($gs_mode eq "gs2") {
252 my $llssite_cfg = &util::filename_cat($ENV{'GSDLHOME'}, "llssite.cfg");
253
254 if(-f $llssite_cfg) {
255 # check llssite.cfg for line with url property
256 # for server.exe also need to use portnumber and enterlib properties
257
258 # Read in the entire contents of the file in one hit
259 if (!open (FIN, $llssite_cfg)) {
260 &print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
261 return undef;
262 }
263
264 my $contents;
265 sysread(FIN, $contents, -s FIN);
266 close(FIN);
267
268 my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
269 my $enterlib = "";
270 my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
271
272 foreach my $line (@lines) {
273 if($line =~ m/^url=(.*)$/) {
274 $url = $1;
275 } elsif($line =~ m/^enterlib=(.*)$/) {
276 $enterlib = $1;
277 } elsif($line =~ m/^portnumber=(.*)$/) {
278 $portnumber = $1;
279 }
280 }
281
282 if(!$url) {
283 return undef;
284 }
285 elsif($url eq "URL_pending") { # library is not running
286 # do not process url=URL_pending in the file, since for server.exe
287 # this just means the Enter Library button hasn't been pressed yet
288 $url = undef;
289 }
290 else {
291 # In the case of server.exe, need to do extra work to get the proper URL
292 # But first, need to know whether we're indeed dealing with server.exe:
293
294 # compare the URL's domain to the full URL
295 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
296 my $uri = URI->new( $url );
297 my $host = $uri->host;
298 #print STDERR "@@@@@ host: $host\n";
299 if($url =~ m/http:\/\/$host(\/)?$/) {
300 #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
301 # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
302 # If the domain with http:// prefix is completely the same as the URL, assume server.exe
303 # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
304 $url = $url.":".$portnumber.$enterlib;
305 } # else, apache web server
306
307 }
308 }
309 } elsif($gs_mode eq "gs3") {
310 # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
311 # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
312
313 # Or, run the new target get-default-servlet-url
314 # the output can look like:
315 #
316 # Buildfile: build.xml
317 # [echo] os.name: Windows Vista
318 #
319 # get-default-servlet-url:
320 # [echo] http://localhost:8383/greenstone3/library
321 # BUILD SUCCESSFUL
322 # Total time: 0 seconds
323
324 #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
325 # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
326
327 # The get-default-servlet-url ant target can be run from anywhere by specifying the
328 # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
329 # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
330 # to run the activate.pl script in the first place
331 my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
332
333 if (open(PIN, "$perl_command |")) {
334 while (defined (my $perl_output_line = <PIN>)) {
335 if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
336 $url="http://".$1;
337 }
338 }
339 close(PIN);
340 } else {
341 &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
342 }
343 }
344
345 # either the url is still undef or it is now set
346 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
347 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
348 return $url;
349}
350
351### UNUSED METHODS TO MOVE TO util.pm?
352
353# This method is now unused. Using ping_library instead to send the ping action to a
354# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
355# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
356# and http://www.perlmonks.org/?node_id=618534
357sub is_URL_active {
358 my $url = shift(@_); # gs3 or gs2 URL
359
360 my $status = 0;
361 if(defined $url) {
362 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
363 # Need to set this, else it takes I don't know how long to timeout
364 # http://www.perlmonks.org/?node_id=618534
365
366 $status = LWP::Simple::head($url); # returns empty list of headers if it fails
367 # LWP::Simple::get($url) is more intensive, so don't need to do that
368 #print STDERR "**** $url is alive.\n" if $status;
369 }
370 return $status;
371}
372
373# Pinging seems to always return true, so this method doesn't work
374sub pingHost {
375 my $url = shift(@_); # gs3 or gs2 URL
376
377 my $status = 0;
378 if(defined $url) {
379 # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
380 # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
381
382 #my $host = $url;
383 #$host =~ s@^http:\/\/(www.)?@@;
384 #$host =~ s@\/.*@@;
385 #print STDERR "**** HOST: $host\n";
386
387 # More robust way
388 # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
389 my $uri = URI->new( $url );
390 my $host = $uri->host;
391
392 # Ping the host. http://perldoc.perl.org/Net/Ping.html
393 my $p = Net::Ping->new();
394 $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
395 print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
396 $p->close();
397 }
398 # return whether pinging was a success or failure
399 return $status;
400}
401
402
403# Most of the arguments are familiar from the building scripts like buildcol.pl
404# The special optional argument -library_url is for when we're dealing with a web
405# library server such as an apache that's separate from any included with GS2.
406# In such a case, this script's caller should pass in -library_url <URL>.
407#
408# $site argument must be specified in the cmdline for collectionConfig.xml to get
409# generated which makes $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
410sub main
411{
412 my ($argc,@argv) = @_;
413
414 if (($argc==0) || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
415 my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
416
417
418 print STDERR "\n";
419 print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -removeold -keepold -verbosity v\n";
420 print STDERR "\t-library_url URL] <[colgroup/]collection>\n";
421 print STDERR "\n";
422
423 exit(-1);
424 }
425
426 # get the collection details
427 my $qualified_collection = pop @argv; # qualified collection
428
429 my $collect_dir = undef; #"collect"; # can't be "collect" when only -site is provided for GS3
430 my $build_dir = undef;
431 my $index_dir = undef;
432 my $site = undef;
433
434 my $removeold = 0;
435 my $keepold = 0;
436 my $incremental = 0; # used by solr
437
438 my $library_url = undef; # to be specified on the cmdline if not using a GS-included web server
439
440 while (my $arg = shift @argv) {
441 if ($arg eq "-collectdir") {
442 $collect_dir = shift @argv;
443 }
444 elsif ($arg eq "-builddir") {
445 $build_dir = shift @argv;
446 }
447 elsif ($arg eq "-indexdir") {
448 $index_dir = shift @argv;
449 }
450 elsif ($arg eq "-site") {
451 $site = shift @argv;
452 }
453 elsif ($arg eq "-removeold") {
454 $removeold = 1;
455 }
456 elsif ($arg eq "-keepold") {
457 $keepold = 1;
458 }
459 elsif ($arg eq "-incremental") {
460 $incremental = 1;
461 }
462 elsif ($arg eq "-library_url") {
463 $library_url = shift @argv;
464 }
465 elsif ($arg eq "-verbosity") {
466 $default_verbosity = shift @argv; # global variable
467
468 # ensure we're working with ints not strings (int context not str context), in case verbosity=0
469 # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
470 $default_verbosity = int($default_verbosity || 0); ### is this the best way?
471 }
472 }
473
474 # work out the building and index dirs
475 my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
476 $build_dir = &util::filename_cat($collection_dir, "building") unless (defined $build_dir);
477 $index_dir = &util::filename_cat($collection_dir, "index") unless (defined $index_dir);
478
479 &print_task_msg("Running Collection Activation Stage");
480
481 # can't do anything without a build directory with something in it to move into index
482 if(!&util::dir_exists($build_dir)) {
483 &print_msg("No building folder at $build_dir to move to index.\n");
484 exit -1;
485 } elsif (&util::is_dir_empty($build_dir)) {
486 &print_msg("Nothing in building folder $build_dir to move into index folder.\n");
487 exit -1;
488 }
489
490 # get and check the collection name
491 if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
492 &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
493 exit -1;
494 }
495
496 # Read in the collection configuration file.
497 # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get
498 # generated and does $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
499 my ($collect_cfg_filename, $gs_mode) = &colcfg::get_collect_cfg_name(STDERR);
500 my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
501
502 # look for build.cfg/buildConfig.xml
503 my $build_cfg_filename ="";
504
505 if ($gs_mode eq "gs2") {
506 $build_cfg_filename = &util::filename_cat($build_dir,"build.cfg");
507 } else {
508 $build_cfg_filename = &util::filename_cat($build_dir, "buildConfig.xml");
509 # gs_mode is GS3. Set the site now if this was not specified as cmdline argument
510 #$site = "localsite" unless defined $site;
511 }
512
513 # We need to know the buildtype for Solr.
514 # Any change of indexers is already detected and handled by the calling code (buildcol or
515 # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
516
517 # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
518 my $buildtype;
519 if (defined $collectcfg->{'buildtype'}) {
520 $buildtype = $collectcfg->{'buildtype'};
521 } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
522 $buildtype = "mgpp";
523 } else {
524 $buildtype = "mg"; #mg is the default
525 }
526
527 my $solr_server;
528 my @corenames = ();
529 if($buildtype eq "solr") { # start up the jetty server
530 my $solr_ext = $ENV{'GEXT_SOLR'}; # from solr_passes.pl
531 unshift (@INC, "$solr_ext/perllib");
532 require solrserver;
533
534 # Solr cores are named without taking the collection-group name into account, since solr
535 # is used for GS3 and GS3 doesn't use collection groups but has the site concept instead
536 my ($colname, $colgroup) = &util::get_collection_parts($qualified_collection);
537
538 # See solrbuilder.pm to get the indexing levels (document, section) from the collectcfg file
539 # Used to generate core names from them and remove cores by name
540 foreach my $level ( @{$collectcfg->{'levels'}} ){
541 my ($pindex) = $level =~ /^(.)/;
542 my $indexname = $pindex."idx";
543 push(@corenames, "$site-$colname-$indexname"); #"$site-$colname-didx", "$site-$colname-sidx"
544 }
545
546 # If the Solr/Jetty server is not already running, the following starts
547 # it up, and only returns when the server is "reading and listening"
548 $solr_server = new solrserver($build_dir);
549 $solr_server->start();
550
551 # We'll be moving building to index. For solr collection, there's further
552 # special processing to make a corresponding change to the solr.xml
553 # by removing the temporary building cores and (re)creating the index cores
554 }
555
556 # Now the logic in GLI's CollectionManager.java (processComplete()
557 # and installCollection()) and Gatherer.configGS3Server().
558
559 # 1. Get library URL
560
561 # For web servers that are external to a Greenstone installation,
562 # the user can pass in their web server's library URL.
563 # For web servers included with GS (like tomcat for GS3 and server.exe
564 # and apache for GS2), we work out the library URL:
565 if(!$library_url) {
566 $library_url = &get_library_URL($gs_mode); # returns undef if no server is running
567 }
568
569 # CollectionManager's installCollection phase in GLI
570 # 2. Ping the library URL, and if it's a persistent server and running, release the collection
571
572 my $is_persistent_server = undef;
573 if($library_url) { # undef if no valid server URL
574
575 &print_msg("Pinging $library_url\n");
576 if(&ping_library($library_url, $gs_mode, $site)) { # server running
577
578 # server is running, so release the collection if
579 # the server is persistent and the collection is active
580 &print_msg("Checking if Greenstone server is persistent\n");
581 $is_persistent_server = &is_persistent($library_url, $gs_mode);
582
583 if($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
584
585 &print_msg("Checking if the collection $qualified_collection is already active\n");
586 my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
587
588 if($collection_active) {
589 &print_msg("De-activating collection $qualified_collection\n");
590 &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
591 }
592 }
593 }
594 }
595
596 # 3. Do all the moving building to index stuff now
597
598 # If removeold: replace index dir with building dir.
599 # If keepold: move building's contents into index, where only duplicates will get deleted.
600 # removeold and keepold can't both be on at the same time
601 # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
602 my $incremental_mode;
603 ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
604 $incremental,
605 $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building
606 $collectcfg);
607
608 if($removeold) {
609
610 if(&util::dir_exists($index_dir)) {
611 &print_task_msg("Removing \"index\"");
612
613 if ($buildtype eq "solr") {
614 # if solr, remove any cores that are using the index_dir before deleting this dir
615 foreach my $corename (@corenames) {
616 $solr_server->admin_unload_core($corename);
617 }
618 }
619
620 &util::rm_r($index_dir);
621
622 # Wait for a couple of seconds, just for luck
623 sleep 2;
624
625 if (&util::dir_exists($index_dir)) {
626 &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
627 }
628 }
629
630 # if remote GS server: gliserver.pl would call activate.pl to activate
631 # the collection at this point since activate.pl lives on the server side
632
633 if ($buildtype eq "solr") {
634 # if solr, remove any cores that are using the building_dir before moving this dir onto index
635 foreach my $corename (@corenames) {
636 $solr_server->admin_unload_core("building-$corename");
637 }
638 }
639
640 # Move the building directory to become the new index directory
641 &print_task_msg("Moving \"building\" -> \"index\"");
642 &util::mv($build_dir, $index_dir);
643 if(&util::dir_exists($build_dir) || !&util::dir_exists($index_dir)) {
644 &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
645 }
646 }
647 elsif ($keepold || $incremental) {
648 if ($buildtype eq "solr") {
649 # if solr, remove any cores that may be using the building_dir before moving this dir onto index
650 foreach my $corename (@corenames) {
651 $solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
652 }
653 }
654
655 # Copy just the contents of building dir into the index dir, overwriting
656 # existing files, but don't replace index with building.
657 &print_task_msg("Moving \"building\" -> \"index\"");
658 &util::mv_dir_contents($build_dir, $index_dir);
659 }
660
661 if ($buildtype eq "solr") {
662 # Call CREATE action to get the old cores pointing to the index folder
663 foreach my $corename (@corenames) {
664 if($removeold) {
665 # Call CREATE action to get all cores pointing to the index folder, since building is now index
666 $solr_server->admin_create_core($corename, $index_dir);
667
668 } elsif ($keepold || $incremental) {
669 # Call RELOAD core. Should already be using the index_dir directory for $keepold and $incremental case
670
671 # Ping to see if corename exists, if it does, reload, else create
672 if ($solr_server->admin_ping_core($corename)) {
673 $solr_server->admin_reload_core($corename);
674 } else {
675 $solr_server->admin_create_core($corename, $index_dir);
676 }
677 }
678 }
679
680 # copy the just updated ext/solr/solr.xml to web/ext/solr/solr.xml
681 $solr_server->copy_solrxml_to_web();
682 }
683
684 # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again
685
686 # Check for success: if building does not exist OR is empty
687 if(!&util::dir_exists($build_dir) || &util::is_dir_empty($build_dir)) {
688
689 if($library_url) { # undef if no valid server URL
690
691 &print_msg("Pinging $library_url\n");
692 if(&ping_library($library_url, $gs_mode, $site)) { # server running
693
694 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
695 if(!defined $is_persistent_server) {
696 &print_msg("Checking if Greenstone server is persistent\n");
697 $is_persistent_server = &is_persistent($library_url, $gs_mode);
698 }
699
700
701 if($is_persistent_server) { # persistent server, so can try activating collection
702
703 &print_msg("Checking if the collection $qualified_collection is not already active\n");
704 my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
705
706 if(!$collection_active) {
707 &print_msg("Activating collection $qualified_collection\n");
708 &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
709
710 # unless an error occurred, the collection should now be active:
711 $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
712 if(!$collection_active) {
713 &print_msg("ERROR: collection $qualified_collection did not get activated\n");
714 }
715 }
716 }
717 }
718 }
719 } else { # installcollection failed
720 #CollectionManager.Preview_Ready_Failed
721 &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
722 }
723
724 &print_msg("\n");
725
726 if($buildtype eq "solr") {
727 if ($solr_server->explicitly_started()) {
728 $solr_server->stop();
729 }
730 }
731}
732
733&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.