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

Last change on this file since 26567 was 26567, checked in by ak19, 11 years ago

When a GS2 collection contains both collect.cfg and collectionConfig.xml (as advanced beatles does) the old code used to end up reading in the GS3 collectionConfig.xml instead of the GS2 collect.cfg and set the GS_mode to GS3. Now colcfg::get_collect_cfg_name takes the gs_mode (instead of determining this and returning it) and works out the collectcfg file name for the gs_mode. That means that the calling functions now need to work out the gs_mode. They do so by setting the gs_mode to gs3 if the site flag is present in the commandline, if not then it defaults to gs2. So from now on, the site flag must be specified for GS3 collections.

  • Property svn:executable set to *
File size: 28.5 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 # get and check the collection name
482 if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
483 &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
484 exit -1;
485 }
486
487 # Read in the collection configuration file.
488 # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get
489 # generated and does $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
490 my $gs_mode = "gs2";
491 if ((defined $site) && ($site ne "")) { # GS3
492 $gs_mode = "gs3";
493 }
494 my $collect_cfg_filename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
495 my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
496
497 # look for build.cfg/buildConfig.xml
498 my $build_cfg_filename ="";
499
500 if ($gs_mode eq "gs2") {
501 $build_cfg_filename = &util::filename_cat($build_dir,"build.cfg");
502 } else {
503 $build_cfg_filename = &util::filename_cat($build_dir, "buildConfig.xml");
504 # gs_mode is GS3. Set the site now if this was not specified as cmdline argument
505 #$site = "localsite" unless defined $site;
506 }
507
508 # We need to know the buildtype for Solr.
509 # Any change of indexers is already detected and handled by the calling code (buildcol or
510 # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
511
512 # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
513 my $buildtype;
514 if (defined $collectcfg->{'buildtype'}) {
515 $buildtype = $collectcfg->{'buildtype'};
516 } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
517 $buildtype = "mgpp";
518 } else {
519 $buildtype = "mg"; #mg is the default
520 }
521
522 # can't do anything without a build directory with something in it to move into index
523 # Except if we're (doing incremental) building for solr, where we want to still
524 # activate and deactivate collections including for the incremental case
525 if(!&util::dir_exists($build_dir)) {
526 &print_msg("No building folder at $build_dir to move to index.\n");
527 exit -1 unless ($buildtype eq "solr"); #&& $incremental);
528 } elsif (&util::is_dir_empty($build_dir)) {
529 &print_msg("Nothing in building folder $build_dir to move into index folder.\n");
530 exit -1 unless ($buildtype eq "solr"); #&& $incremental);
531 }
532
533
534 my $solr_server;
535 my @corenames = ();
536 if($buildtype eq "solr") { # start up the jetty server
537 my $solr_ext = $ENV{'GEXT_SOLR'}; # from solr_passes.pl
538 unshift (@INC, "$solr_ext/perllib");
539 require solrserver;
540
541 # Solr cores are named without taking the collection-group name into account, since solr
542 # is used for GS3 and GS3 doesn't use collection groups but has the site concept instead
543 my ($colname, $colgroup) = &util::get_collection_parts($qualified_collection);
544
545 # See solrbuilder.pm to get the indexing levels (document, section) from the collectcfg file
546 # Used to generate core names from them and remove cores by name
547 foreach my $level ( @{$collectcfg->{'levels'}} ){
548 my ($pindex) = $level =~ /^(.)/;
549 my $indexname = $pindex."idx";
550 push(@corenames, "$site-$colname-$indexname"); #"$site-$colname-didx", "$site-$colname-sidx"
551 }
552
553 # If the Solr/Jetty server is not already running, the following starts
554 # it up, and only returns when the server is "reading and listening"
555 $solr_server = new solrserver($build_dir);
556 $solr_server->start();
557
558 # We'll be moving building to index. For solr collection, there's further
559 # special processing to make a corresponding change to the solr.xml
560 # by removing the temporary building cores and (re)creating the index cores
561 }
562
563 # Now the logic in GLI's CollectionManager.java (processComplete()
564 # and installCollection()) and Gatherer.configGS3Server().
565
566 # 1. Get library URL
567
568 # For web servers that are external to a Greenstone installation,
569 # the user can pass in their web server's library URL.
570 # For web servers included with GS (like tomcat for GS3 and server.exe
571 # and apache for GS2), we work out the library URL:
572 if(!$library_url) {
573 $library_url = &get_library_URL($gs_mode); # returns undef if no server is running
574 }
575
576 # CollectionManager's installCollection phase in GLI
577 # 2. Ping the library URL, and if it's a persistent server and running, release the collection
578
579 my $is_persistent_server = undef;
580 if($library_url) { # undef if no valid server URL
581
582 &print_msg("Pinging $library_url\n");
583 if(&ping_library($library_url, $gs_mode, $site)) { # server running
584
585 # server is running, so release the collection if
586 # the server is persistent and the collection is active
587 &print_msg("Checking if Greenstone server is persistent\n");
588 $is_persistent_server = &is_persistent($library_url, $gs_mode);
589
590 if($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
591
592 &print_msg("Checking if the collection $qualified_collection is already active\n");
593 my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
594
595 if($collection_active) {
596 &print_msg("De-activating collection $qualified_collection\n");
597 &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
598 }
599 }
600 }
601 }
602
603 # 3. Do all the moving building to index stuff now
604
605 # If removeold: replace index dir with building dir.
606 # If keepold: move building's contents into index, where only duplicates will get deleted.
607 # removeold and keepold can't both be on at the same time
608 # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
609 my $incremental_mode;
610 ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
611 $incremental,
612 $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building
613 $collectcfg);
614
615 if($removeold) {
616
617 if(&util::dir_exists($index_dir)) {
618 &print_task_msg("Removing \"index\"");
619
620 if ($buildtype eq "solr") {
621 # if solr, remove any cores that are using the index_dir before deleting this dir
622 foreach my $corename (@corenames) {
623 $solr_server->admin_unload_core($corename);
624 }
625 }
626
627 &util::rm_r($index_dir);
628
629 # Wait for a couple of seconds, just for luck
630 sleep 2;
631
632 if (&util::dir_exists($index_dir)) {
633 &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
634 }
635 }
636
637 # if remote GS server: gliserver.pl would call activate.pl to activate
638 # the collection at this point since activate.pl lives on the server side
639
640 if ($buildtype eq "solr") {
641 # if solr, remove any cores that are using the building_dir before moving this dir onto index
642 foreach my $corename (@corenames) {
643 $solr_server->admin_unload_core("building-$corename");
644 }
645 }
646
647 # Move the building directory to become the new index directory
648 &print_task_msg("Moving \"building\" -> \"index\"");
649 &util::mv($build_dir, $index_dir);
650 if(&util::dir_exists($build_dir) || !&util::dir_exists($index_dir)) {
651 &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
652 }
653 }
654 elsif ($keepold || $incremental) {
655 if ($buildtype eq "solr") {
656 # if solr, remove any cores that may be using the building_dir before moving this dir onto index
657 foreach my $corename (@corenames) {
658 $solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
659 }
660 }
661
662 # Copy just the contents of building dir into the index dir, overwriting
663 # existing files, but don't replace index with building.
664 &print_task_msg("Moving \"building\" -> \"index\"");
665 &util::mv_dir_contents($build_dir, $index_dir);
666 }
667
668 if ($buildtype eq "solr") {
669 # Call CREATE action to get the old cores pointing to the index folder
670 foreach my $corename (@corenames) {
671 if($removeold) {
672 # Call CREATE action to get all cores pointing to the index folder, since building is now index
673 $solr_server->admin_create_core($corename, $index_dir);
674
675 } elsif ($keepold || $incremental) {
676 # Call RELOAD core. Should already be using the index_dir directory for $keepold and $incremental case
677
678 # Ping to see if corename exists, if it does, reload, else create
679 if ($solr_server->admin_ping_core($corename)) {
680 $solr_server->admin_reload_core($corename);
681 } else {
682 $solr_server->admin_create_core($corename, $index_dir);
683 }
684 }
685 }
686
687 # copy the just updated ext/solr/solr.xml to web/ext/solr/solr.xml
688 $solr_server->copy_solrxml_to_web();
689 }
690
691 # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again
692
693 # Check for success: if building does not exist OR is empty
694 if(!&util::dir_exists($build_dir) || &util::is_dir_empty($build_dir)) {
695
696 if($library_url) { # undef if no valid server URL
697
698 &print_msg("Pinging $library_url\n");
699 if(&ping_library($library_url, $gs_mode, $site)) { # server running
700
701 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
702 if(!defined $is_persistent_server) {
703 &print_msg("Checking if Greenstone server is persistent\n");
704 $is_persistent_server = &is_persistent($library_url, $gs_mode);
705 }
706
707
708 if($is_persistent_server) { # persistent server, so can try activating collection
709
710 &print_msg("Checking if the collection $qualified_collection is not already active\n");
711 my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
712
713 if(!$collection_active) {
714 &print_msg("Activating collection $qualified_collection\n");
715 &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
716
717 # unless an error occurred, the collection should now be active:
718 $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
719 if(!$collection_active) {
720 &print_msg("ERROR: collection $qualified_collection did not get activated\n");
721 }
722 }
723 }
724 }
725 }
726 } else { # installcollection failed
727 #CollectionManager.Preview_Ready_Failed
728 &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
729 }
730
731 &print_msg("\n");
732
733 if($buildtype eq "solr") {
734 if ($solr_server->explicitly_started()) {
735 $solr_server->stop();
736 }
737 }
738}
739
740&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.