source: main/trunk/greenstone2/perllib/servercontrol.pm@ 31518

Last change on this file since 31518 was 31518, checked in by ak19, 7 years ago

Found servercontrol::get_library_url() largely duplicated in util.pm as get_fullgreenstone_url_prefix(). Merging the two functions. Tested that solr-jdbm-demo collection still rebuilds (with and without tomcat running, and with and without port change between sessions).

File size: 30.2 KB
Line 
1#############################################################################
2#
3# activate.pm -- functions to get the GS library URL, ping the library URL,
4# activate and deactivate a collection.
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###############################################################################
27
28package servercontrol;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and vice versa
33no strict 'subs'; # allow barewords (eg STDERR) as function arguments
34
35# Greenstone includes
36use printusage;
37use parse2;
38
39
40# The perl library imports below are used by deprecated methods config_old(), is_URL_active() and pingHost()
41# If the following library imports are not supported by your perl installation, comment out these
42# imports and move the methods config_old(), is_URL_active() and pingHost() out to a temporary file.
43use HTTP::Response;
44use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
45 # don't import LWP::Simple's head function by name since it can conflict with CGI:head())
46#use CGI qw(:standard); # then only CGI.pm defines a head()
47use Net::Ping;
48use URI;
49
50
51sub new
52{
53 my $class = shift(@_);
54
55 my ($qualified_collection, $site, $verbosity, $build_dir, $index_dir, $collect_dir, $library_url, $library_name) = @_;
56
57 # library_url: to be specified on the cmdline if not using a GS-included web server
58 # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
59
60 my $self = {'build_dir' => $build_dir,
61 'index_dir' => $index_dir,
62 'collect_dir' => $collect_dir,
63 'site' => $site,
64 'qualified_collection' => $qualified_collection,
65 #'is_persistent_server' => undef,
66 'library_url' => $library_url || $ENV{'GSDL_LIBRARY_URL'} || undef, # to be specified on the cmdline if not using a GS-included web server
67 'library_name' => $library_name,
68 #'gs_mode' => "gs2",
69 'verbosity' => $verbosity || 2
70 };
71
72 if ((defined $site) && ($site ne "")) { # GS3
73 $self->{'gs_mode'} = "gs3";
74 } else {
75 $self->{'gs_mode'} = "gs2";
76 }
77
78 return bless($self, $class);
79}
80
81## TODO: gsprintf to $self->{'out'} in these 2 print functions
82## See buildcolutils.pm new() for setting up $out
83
84sub print_task_msg {
85 my $self = shift(@_);
86 my ($task_msg, $verbosity_setting) = @_;
87
88 $verbosity_setting = $self->{'verbosity'} unless $verbosity_setting;
89 #$verbosity_setting = 1 unless defined $verbosity;
90 if($verbosity_setting >= 1) {
91 print STDERR "\n";
92 print STDERR "************************\n";
93 print STDERR "* $task_msg\n";
94 print STDERR "************************\n";
95 }
96}
97
98# Prints messages if the verbosity is right. Does not add new lines.
99sub print_msg {
100 my $self = shift(@_);
101 my ($msg, $min_verbosity, $verbosity_setting) = @_;
102
103 # only display error messages if the current
104 # verbosity setting >= the minimum verbosity level
105 # needed for that message to be displayed.
106
107 $verbosity_setting = $self->{'verbosity'} unless defined $verbosity_setting;
108 $min_verbosity = 1 unless defined $min_verbosity;
109 if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
110 print STDERR "$msg";
111 }
112}
113
114# Method to send a command to a GS2 or GS3 library_URL
115# the commands used in this script can be activate, deactivate, ping,
116# and is-persistent (is-persistent only implemented for GS2).
117sub config {
118 my $self = shift(@_);
119 my ($command, $check_message_against_regex, $expected_error_code, $silent) = @_;
120
121 my $library_url = $self->get_library_URL(); #$self->{'library_url'};
122
123
124 # Gatherer.java's configGS3Server doesn't use the site variable
125 # so we don't have to either
126
127 # for GS2, getting the HTTP status isn't enough, we need to read the output
128 # since this is what CollectionManager.config() stipulates.
129 # Using LWP::UserAgent::get($url) for this
130
131 if(!defined $library_url) {
132 return 0;
133 }
134 else {
135 # ampersands need to be escaped
136 # - with single quotes around it for linux for the cmd to run in bash subshell
137 # - with a ^ before it on windows for the cmd to run in a DOS prompt subshell
138 # - or the entire wget command should be nested in double quotes (single quotes don't work on windows)
139 my $wgetCommand = $command;
140
141 my $wget_file_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
142 my $tmpfilename = &util::get_tmp_filename(".html"); # random file name with html extension in tmp location in which we'll store the HTML page retrieved by wget
143
144 # https://www.gnu.org/software/wget/manual/wget.html
145 # output-document set to - (STDOUT), so page is streamed to STDOUT
146 # timeout: 5 seconds, tries: 1
147 # wget sends status information and response code to STDERR, so redirect it to STDOUT
148 # Searching for "perl backtick operator redirect stderr to stdout":
149 # http://www.perlmonks.org/?node=How%20can%20I%20capture%20STDERR%20from%20an%20external%20command%3F
150 ##$wgetCommand = "\"$wget_file_path\" --spider -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # won't save page
151 #$wgetCommand = "\"$wget_file_path\" --output-document=- -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # THIS CAN MIX UP STDERR WITH STDOUT IN THE VERY LINE WE REGEX TEST AGAINST EXPECTED OUTPUT!!
152 $wgetCommand = "\"$wget_file_path\" --output-document=$tmpfilename -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # keep stderr (response code, response_content) separate from html page content
153
154 ##print STDERR "@@@@ $wgetCommand\n";
155
156 my $response_content;
157 my $response_code = undef;
158 #my $response_content = `$wgetCommand`; # Dr Bainbridge advises against using backticks for running a process. If capturing std output, use open():
159 if (open(PIN, "$wgetCommand |")) {
160 while (defined (my $perl_output_line = <PIN>)) {
161 $response_content = $response_content . $perl_output_line;
162 }
163 close(PIN);
164 } else {
165 print STDERR "servercontrol.pm::config() failed to run $wgetCommand\n";
166 }
167
168
169 my @lines = split( /\n/, $response_content );
170 foreach my $line (@lines) {
171 #print STDERR "@@@@ LINE: $line\n";
172 if($line =~ m@failed: Connection timed out.$@) { # linux
173 $response_code = "failed: Connection timed out.";
174 last; # break keyword in perl = last
175 }
176 elsif($line =~ m@Giving up.$@) { # windows (unless -T 5 -t 1 is not passed in)
177 $response_code = "failed: Giving up.";
178 last; # break keyword in perl = last
179 }
180 elsif($line =~ m@failed: Connection refused.$@) {
181 $response_code = "failed: Connection refused.";
182 last; # break keyword in perl = last
183 }
184 elsif($line =~ m@HTTP request sent, @) {
185 $response_code = $line;
186 $response_code =~ s@[^\d]*(.*)$@$1@;
187 last;
188 }
189 }
190
191 if($command =~ m@ping@ && $response_code =~ m@failed: (Connection refused|Giving up)@) {
192 # server not running
193 $self->print_msg("*** Server not running. $library_url$command\n", 3);
194 &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
195 return 0;
196 }
197 if($response_code && $response_code eq "200 OK") {
198 $self->print_msg("*** Command $library_url$command\n", 3);
199 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
200
201 # check the page content is as expected
202 #my $resultstr = $response_content;
203
204 open(FIN,"<$tmpfilename") or die "servercontrol.pm: Unable to open $tmpfilename to read ping response page...ERROR: $!\n";
205 my $resultstr;
206 # Read in the entire contents of the file in one hit
207 sysread(FIN, $resultstr, -s FIN);
208 close(FIN);
209 &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
210
211
212 #$resultstr =~ s@.*gs_content\"\>@@s; ## only true for default library servlet
213 #$resultstr =~ s@</div>.*@@s;
214 if($resultstr =~ m/$check_message_against_regex/) {
215 $self->print_msg(" Response as expected.\n", 3);
216 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
217 return 1;
218 } else {
219 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
220 # don't print out the "ping did not succeed" response, but print out any other messages
221
222 # So we only suppress the ping col "did not succeed" response if we're in silent mode
223 # But if any message other than ping "did not succeed" is returned, we always print it
224 if($resultstr !~ m/did not succeed/ || !$silent) {
225 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
226 $self->print_msg("*** Got message:\n$response_content.\n", 4);
227 $self->print_msg("*** Got result:\n$resultstr\n", 3);
228 }
229 return 0; # ping on a collection may "not succeed."
230 }
231 }
232 elsif($response_code && $response_code =~ m@^(4|5)\d\d@) { # client side errors start with 4xx, server side with 5xx
233 # check the page content is as expected
234 if(defined $expected_error_code && $response_code =~ m@^$expected_error_code@) {
235 $self->print_msg(" Response status $response_code as expected.\n", 3);
236 } else {
237 $self->print_msg("*** Command $library_url$command\n");
238 $self->print_msg("*** Unexpected error type 1. HTTP Response Status: $response_code - Failed.\n");
239 }
240 return 0; # return false, since the response_code was an error, expected or not
241 }
242 else { # also if response_code is still undefined, as can happen with connection timing out
243 $self->print_msg("*** Command $library_url$command\n");
244 if(defined $response_code) {
245 $self->print_msg("*** Unexpected error type 2. HTTP Response Status: $response_code - Failed.\n");
246 } else {
247 $self->print_msg("*** Unexpected error type 3. Failed:\n\n$response_content\n\n");
248 }
249 return 0;
250 }
251 #print STDERR "********** WgetCommand: $wgetCommand\n\n";
252 #print STDERR "********** Response_content:\n$response_content\n\n";
253 #print STDERR "********** Response_CODE: $response_code\n";
254
255 }
256}
257
258sub deactivate_collection {
259 my $self = shift(@_);
260
261 my $gs_mode = $self->{'gs_mode'};
262 my $qualified_collection = $self->{'qualified_collection'};
263
264 if($gs_mode eq "gs2") {
265 my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
266 my $check_message_against_regex = q/configured release-collection/;
267 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
268 }
269 elsif ($gs_mode eq "gs3") {
270 my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
271 my $check_message_against_regex = "collection: $qualified_collection deactivated";
272 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
273 }
274}
275
276sub activate_collection {
277 my $self = shift(@_);
278
279 my $gs_mode = $self->{'gs_mode'};
280 my $qualified_collection = $self->{'qualified_collection'};
281
282 if($gs_mode eq "gs2") {
283 my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
284 my $check_message_against_regex = q/configured add-collection/;
285 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
286 }
287 elsif ($gs_mode eq "gs3") {
288 my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
289 my $check_message_against_regex = "collection: $qualified_collection activated";
290 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
291 }
292}
293
294sub ping {
295 my $self = shift(@_);
296 my $command = shift(@_);
297 my $silent = shift(@_);
298
299 # If the GS server is not running, we *expect* to see a "500" status code.
300 # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
301 # When pinging an inactive collection, it will say it did "not succeed". This is
302 # a message of interest to return.
303 my $check_responsemsg_against_regex = q/(succeeded)/;
304 my $expected_error_code = 500;
305
306 $self->print_msg("*** COMMAND WAS: |$command| ***\n", 4);
307
308 return $self->config($command, $check_responsemsg_against_regex, $expected_error_code, $silent);
309}
310
311# send a pingaction to the GS library. General server-level ping.
312sub ping_library {
313 my $self = shift(@_);
314
315 my $gs_mode = $self->{'gs_mode'};
316
317 my $command = "";
318 if($gs_mode eq "gs2") {
319 $command = "?a=ping";
320 }
321 elsif ($gs_mode eq "gs3") {
322 $command = "?a=s&sa=ping";
323 }
324 return $self->ping($command);
325}
326
327
328# send a pingaction to a collection in GS library to check if it's active
329sub ping_library_collection {
330 my $self = shift(@_);
331 my $silent = shift(@_);
332
333 my $gs_mode = $self->{'gs_mode'};
334 my $qualified_collection = $self->{'qualified_collection'};
335
336 my $command = "";
337 if($gs_mode eq "gs2") {
338 $command = "?a=ping&c=$qualified_collection";
339 }
340 elsif ($gs_mode eq "gs3") {
341 $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";
342 }
343 return $self->ping($command, $silent);
344}
345
346# return true if server is persistent, by calling is-persistent on library_url
347# this is only for GS2, since the GS3 server is always persistent
348sub is_persistent {
349 my $self = shift(@_);
350
351 if($self->{'gs_mode'} eq "gs3") { # GS3 server is always persistent
352 return 1;
353 }
354
355 my $command = "?a=is-persistent";
356 my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
357 return $self->config($command, $check_responsemsg_against_regex);
358}
359
360sub set_library_URL {
361 my $self = shift(@_);
362 my $library_url = shift(@_);
363 $self->{'library_url'} = $library_url;
364}
365
366sub get_library_URL {
367 my $self = shift(@_);
368
369 # For web servers that are external to a Greenstone installation,
370 # the user can pass in their web server's library URL.
371 if($self->{'library_url'}) {
372 return $self->{'library_url'};
373 }
374
375 # For web servers included with GS (like tomcat for GS3 and server.exe
376 # and apache for GS2), we work out the library URL:
377 my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
378 $gs_mode = $self->{'gs_mode'};
379 $lib_name = $self->{'library_name'};
380
381 # If we get here, we are dealing with a server included with GS.
382 # For GS3, we ask ant for the library URL.
383 # For GS2, we derive the URL from the llssite.cfg file.
384
385 my $url = &util::get_full_greenstone_url_prefix($gs_mode, $lib_name); # found largely identical method copied
386 # into util. Don't want duplicates, so calling that from here.
387
388 # either the url is still undef or it is now set
389 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
390 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
391
392 if (defined $url) {
393 $self->{'library_url'} = $url;
394 }
395
396 return $url;
397}
398
399sub OLD_get_library_URL {
400 my $self = shift(@_);
401
402 # For web servers that are external to a Greenstone installation,
403 # the user can pass in their web server's library URL.
404 if($self->{'library_url'}) {
405 return $self->{'library_url'};
406 }
407
408 # For web servers included with GS (like tomcat for GS3 and server.exe
409 # and apache for GS2), we work out the library URL:
410 my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
411 $gs_mode = $self->{'gs_mode'};
412 $lib_name = $self->{'library_name'};
413
414 # If we get here, we are dealing with a server included with GS.
415 # For GS3, we ask ant for the library URL.
416 # For GS2, we derive the URL from the llssite.cfg file.
417
418 my $url = undef;
419
420 if($gs_mode eq "gs2") {
421 my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
422
423 if(-f $llssite_cfg) {
424 # check llssite.cfg for line with url property
425 # for server.exe also need to use portnumber and enterlib properties
426
427 # Read in the entire contents of the file in one hit
428 if (!open (FIN, $llssite_cfg)) {
429 $self->print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
430 return undef;
431 }
432
433 my $contents;
434 sysread(FIN, $contents, -s FIN);
435 close(FIN);
436
437 my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
438 my $enterlib = "";
439 my $portnumber = "8282"; # will remain empty (implicit port 80) unless it's specifically been assigned
440
441 foreach my $line (@lines) {
442 if($line =~ m/^url=(.*)$/) {
443 $url = $1;
444 } elsif($line =~ m/^enterlib=(.*)$/) {
445 $enterlib = $1;
446 } elsif($line =~ m/^portnumber=(.*)$/) {
447 $portnumber = $1;
448 }
449 }
450
451 if(!$url) {
452 return undef;
453 }
454 elsif($url eq "URL_pending") { # library is not running
455 # do not process url=URL_pending in the file, since for server.exe
456 # this just means the Enter Library button hasn't been pressed yet
457 $url = undef;
458 }
459 else {
460 # In the case of server.exe, need to do extra work to get the proper URL
461 # But first, need to know whether we're indeed dealing with server.exe:
462
463 # compare the URL's domain to the full URL
464 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
465 my $uri = URI->new( $url );
466 my $host = $uri->host;
467 #print STDERR "@@@@@ host: $host\n";
468 if($url =~ m/https?:\/\/$host(\/)?$/) {
469 #if($url !~ m/https?:\/\/$host:$portnumber(\/)?/ || $url =~ m/https?:\/\/$host(\/)?$/) {
470 # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
471 # If the domain with http:// prefix is completely the same as the URL, assume server.exe
472 # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
473 $url = $url.":".$portnumber.$enterlib;
474 } # else, apache web server
475
476 }
477 }
478 } elsif($gs_mode eq "gs3") {
479 # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
480 # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
481
482 # Or, run the new target get-default-servlet-url
483 # the output can look like:
484 #
485 # Buildfile: build.xml
486 # [echo] os.name: Windows Vista
487 #
488 # get-default-servlet-url:
489 # [echo] http://localhost:8383/greenstone3/library
490 # BUILD SUCCESSFUL
491 # Total time: 0 seconds
492
493 #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
494 # - see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
495
496 # The get-default-servlet-url ant target can be run from anywhere by specifying the
497 # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
498 # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
499 # to run the activate.pl script in the first place
500
501 my $full_build_xml = &FileUtils::javaFilenameConcatenate($ENV{'GSDL3SRCHOME'},"build.xml");
502 my $perl_command = "ant -buildfile \"$full_build_xml\" get-default-servlet-url";
503
504 if (open(PIN, "$perl_command |")) {
505 while (defined (my $perl_output_line = <PIN>)) {
506 if($perl_output_line =~ m@(https?):\/\/(\S*)@) { # grab all the non-whitespace chars
507 $url="$1://".$2; # preserve the http protocol #$url="http://".$1;
508 }
509 }
510 close(PIN);
511
512 if(defined $lib_name) { # url won't be undef
513 # replace the servlet_name portion of the url found, with the given library_name
514 $url =~ s@/[^/]*$@/$lib_name@;
515 }
516 } else {
517 $self->print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
518 }
519 }
520
521 # either the url is still undef or it is now set
522 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
523 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
524
525 $self->{'library_url'} = $url;
526 return $url;
527}
528
529
530sub do_deactivate {
531 my $self = shift(@_);
532
533 # 1. Get library URL
534
535 # For web servers that are external to a Greenstone installation,
536 # the user can pass in their web server's library URL.
537 # For web servers included with GS (like tomcat for GS3 and server.exe
538 # and apache for GS2), we work out the library URL:
539
540 # Can't do $self->{'library_url'}, because it may not yet be set
541 my $library_url = $self->get_library_URL(); # returns undef if no valid server URL
542
543 if(!defined $library_url) { # undef if no valid server URL
544 return; # can't do any deactivation without a valid server URL
545 }
546
547 my $is_persistent_server = $self->{'is_persistent_server'};
548 my $qualified_collection = $self->{'qualified_collection'};
549
550 # CollectionManager's installCollection phase in GLI
551 # 2. Ping the library URL, and if it's a persistent server and running, release the collection
552
553 $self->print_msg("Pinging $library_url\n");
554 if ($self->ping_library()) { # server running
555
556 # server is running, so release the collection if
557 # the server is persistent and the collection is active
558
559 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
560 if (!defined $is_persistent_server) {
561 $self->print_msg("Checking if Greenstone server is persistent\n");
562 $is_persistent_server = $self->is_persistent();
563 $self->{'is_persistent_server'} = $is_persistent_server;
564 }
565
566 if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
567
568 $self->print_msg("Checking if the collection $qualified_collection is already active\n");
569 my $collection_active = $self->ping_library_collection();
570
571 if ($collection_active) {
572 $self->print_msg("De-activating collection $qualified_collection\n");
573 $self->deactivate_collection();
574 }
575 else {
576 $self->print_msg("Collection is not active => No need to deactivate\n");
577 }
578 }
579 else {
580 $self->print_msg("Server is not persistent => No need to deactivate collection\n");
581 }
582 }
583 else {
584 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
585 }
586
587 return $is_persistent_server;
588}
589
590sub do_activate {
591 my $self = shift @_;
592
593 my $library_url = $self->get_library_URL(); # Can't do $self->{'library_url'}; as it may not be set yet
594
595 if(!defined $library_url) { # undef if no valid server URL
596 return; # nothing to activate if without valid server URL
597 }
598
599 my $is_persistent_server = $self->{'is_persistent_server'};
600 my $qualified_collection = $self->{'qualified_collection'};
601
602 $self->print_msg("Pinging $library_url\n");
603 if ($self->ping_library()) { # server running
604
605 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
606 if (!defined $is_persistent_server) {
607 $self->print_msg("Checking if Greenstone server is persistent\n");
608 $is_persistent_server = $self->is_persistent();
609 $self->{'is_persistent_server'} = $is_persistent_server;
610 }
611
612 if ($is_persistent_server) { # persistent server, so can try activating collection
613
614 $self->print_msg("Checking if the collection $qualified_collection is not already active\n");
615
616 # Since we could have deactivated the collection at this point,
617 # it is likely that it is not yet active. When pinging the collection
618 # a "ping did not succeed" message is expected, therefore tell the ping
619 # to proceed silently
620 my $silent = 1;
621 my $collection_active = $self->ping_library_collection($silent);
622
623 if (!$collection_active) {
624 $self->print_msg(" Collection is not active.\n");
625 $self->print_msg("Activating collection $qualified_collection\n");
626 $self->activate_collection();
627
628 # unless an error occurred, the collection should now be active:
629 $collection_active = $self->ping_library_collection(); # not silent if ping did not succeed
630 if(!$collection_active) {
631 $self->print_msg("ERROR: collection $qualified_collection did not get activated\n");
632 }
633 }
634 else {
635 $self->print_msg("Collection is already active => No need to activate\n");
636 }
637 }
638 else {
639 $self->print_msg("Server is not persistent => No need to activate collection\n");
640 }
641 }
642 else {
643 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
644 }
645
646 return $is_persistent_server;
647}
648
649
650#########################################################
651### UNUSED METHODS - CAN BE HANDY
652
653
654# This method uses the perl libraries we're advised to use in place of wget for pinging and retrieving web
655# pages. The problem is that not all perl installations may support these libraries. So we now use the new
656# config() method further above, which uses the wget included in Greenstone binary installations.
657# If the library imports at page top conflict, comment out those imports and move the methods config_old(),
658# is_URL_active() and pingHost() out to a temporary file.
659#
660# If for some reason you can't use wget, then rename the config() method to config_old(), and rename the
661# method below to config() and things should work as before.
662sub config_old {
663 my $self = shift(@_);
664 my ($command, $check_message_against_regex, $expected_error_code, $silent) = @_;
665
666 my $library_url = $self->get_library_URL(); #$self->{'library_url'};
667
668
669 # Gatherer.java's configGS3Server doesn't use the site variable
670 # so we don't have to either
671
672 # for GS2, getting the HTTP status isn't enough, we need to read the output
673 # since this is what CollectionManager.config() stipulates.
674 # Using LWP::UserAgent::get($url) for this
675
676 if(!defined $library_url) {
677 return 0;
678 }
679 else {
680 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
681 # Need to set this, else it takes I don't know how long to timeout
682 # http://www.perlmonks.org/?node_id=618534
683
684 # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
685 # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
686
687 my $response_obj = $ua->get($library_url.$command);
688
689 # $response_obj->content stores the content and $response_obj->code the HTTP response code
690 my $response_code = $response_obj->code();
691
692 if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
693 $self->print_msg("*** Command $library_url$command\n", 3);
694 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
695
696 # check the page content is as expected
697 my $response_content = $response_obj->content;
698 my $resultstr = $response_content;
699 $resultstr =~ s@.*gs_content\"\>@@s;
700 $resultstr =~ s@</div>.*@@s;
701
702 if($resultstr =~ m/$check_message_against_regex/) {#if($response_content =~ m/$check_message_against_regex/) {
703 $self->print_msg(" Response as expected.\n", 3);
704 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
705 return 1;
706 } else {
707 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
708 # don't print out the "ping did not succeed" response, but print out any other messages
709
710 # So we only suppress the ping col "did not succeed" response if we're in silent mode
711 # But if any message other than ping "did not succeed" is returned, we always print it
712 if($resultstr !~ m/did not succeed/ || !$silent) {#if($response_content !~ m/did not succeed/ || !$silent) {
713 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
714 $self->print_msg("*** Got message:\n$response_content.\n", 4);
715 $self->print_msg("*** Got result:\n$resultstr\n", 3);
716 }
717 return 0; # ping on a collection may "not succeed."
718 }
719 }
720 elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
721 # check the page content is as expected
722 if(defined $expected_error_code && $response_code == $expected_error_code) {
723 $self->print_msg(" Response status $response_code as expected.\n", 3);
724 } else {
725 $self->print_msg("*** Command $library_url$command\n");
726 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
727 }
728 return 0; # return false, since the response_code was an error, expected or not
729 }
730 else {
731 $self->print_msg("*** Command $library_url$command\n");
732 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
733 return 0;
734 }
735 }
736}
737
738# This method is now unused. Using ping_library instead to send the ping action to a
739# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
740# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
741# and http://www.perlmonks.org/?node_id=618534
742sub is_URL_active {
743 my $url = shift(@_); # gs3 or gs2 URL
744
745 my $status = 0;
746 if(defined $url) {
747 $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
748 # Need to set this, else it takes I don't know how long to timeout
749 # http://www.perlmonks.org/?node_id=618534
750
751 $status = LWP::Simple::head($url); # returns empty list of headers if it fails
752 # LWP::Simple::get($url) is more intensive, so don't need to do that
753 #print STDERR "**** $url is alive.\n" if $status;
754 }
755 return $status;
756}
757
758# Pinging seems to always return true, so this method doesn't work
759sub pingHost {
760 my $url = shift(@_); # gs3 or gs2 URL
761
762 my $status = 0;
763 if(defined $url) {
764 # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
765 # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
766
767 #my $host = $url;
768 #$host =~ s@^https?:\/\/(www.)?@@;
769 #$host =~ s@\/.*@@;
770 #print STDERR "**** HOST: $host\n";
771
772 # More robust way
773 # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
774 my $uri = URI->new( $url );
775 my $host = $uri->host;
776
777 # Ping the host. http://perldoc.perl.org/Net/Ping.html
778 my $p = Net::Ping->new();
779 $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
780 print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
781 $p->close();
782 }
783 # return whether pinging was a success or failure
784 return $status;
785}
786
7871;
Note: See TracBrowser for help on using the repository browser.