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

Last change on this file since 30530 was 30530, checked in by ak19, 8 years ago

Cosmetic change. Removed comment I'd copied in from another file and which doesn't apply here.

File size: 27.1 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 with single quotes
136 my $wgetCommand = $command;
137 $wgetCommand =~ s@&@'&'@g;
138
139 my $wget_file_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
140
141 # https://www.gnu.org/software/wget/manual/wget.html
142 # output-document set to - (STDOUT), so page is streamed to STDOUT
143 # timeout: 5 seconds, tries: 1
144 # wget sends status information and response code to STDERR, so redirect it to STDOUT
145 # Searching for "perl backtick operator redirect stderr to stdout":
146 # http://www.perlmonks.org/?node=How%20can%20I%20capture%20STDERR%20from%20an%20external%20command%3F
147 $wgetCommand = "\"$wget_file_path\" --output-document=- -T 5 -t 1 $library_url$wgetCommand 2>&1";
148 #$wgetCommand = "\"$wget_file_path\" --spider -T 5 -t 1 $library_url$wgetCommand 2>&1"; # won't save page
149 my $response_content = `$wgetCommand`;
150 my $response_code = undef;
151 my @lines = split( /\n/, $response_content );
152 foreach my $line (@lines) {
153 #print STDERR "@@@@ LINE: $line\n";
154 if($line =~ m@failed: Connection timed out.$@) {
155 $response_code = "failed: Connection timed out.";
156 last; # break keyword in perl = last
157 }
158 elsif($line =~ m@failed: Connection refused.$@) {
159 $response_code = "failed: Connection refused.";
160 last; # break keyword in perl = last
161 }
162 elsif($line =~ m@HTTP request sent, @) {
163 $response_code = $line;
164 $response_code =~ s@[^\d]*(.*)$@$1@;
165 last;
166 }
167 }
168
169 if($command =~ m@ping@ && $response_code =~ m@failed: Connection refused@) {
170 # server not running
171 $self->print_msg("*** Server not running. $library_url$command\n", 3);
172 return 0;
173 }
174 if($response_code && $response_code eq "200 OK") {
175 $self->print_msg("*** Command $library_url$command\n", 3);
176 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
177
178 # check the page content is as expected
179 my $resultstr = $response_content;
180 $resultstr =~ s@.*gs_content\"\>@@s;
181 $resultstr =~ s@</div>.*@@s;
182 if($resultstr =~ m/$check_message_against_regex/) {
183 $self->print_msg(" Response as expected.\n", 3);
184 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
185 return 1;
186 } else {
187 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
188 # don't print out the "ping did not succeed" response, but print out any other messages
189
190 # So we only suppress the ping col "did not succeed" response if we're in silent mode
191 # But if any message other than ping "did not succeed" is returned, we always print it
192 if($resultstr !~ m/did not succeed/ || !$silent) {
193 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
194 $self->print_msg("*** Got message:\n$response_content.\n", 4);
195 $self->print_msg("*** Got result:\n$resultstr\n", 3);
196 }
197 return 0; # ping on a collection may "not succeed."
198 }
199 }
200 elsif($response_code && $response_code =~ m@^(4|5)\d\d@) { # client side errors start with 4xx, server side with 5xx
201 # check the page content is as expected
202 if(defined $expected_error_code && $response_code =~ m@^$expected_error_code@) {
203 $self->print_msg(" Response status $response_code as expected.\n", 3);
204 } else {
205 $self->print_msg("*** Command $library_url$command\n");
206 $self->print_msg("*** Unexpected error type 1. HTTP Response Status: $response_code - Failed.\n");
207 }
208 return 0; # return false, since the response_code was an error, expected or not
209 }
210 else { # also if response_code is still undefined, as can happen with connection timing out
211 $self->print_msg("*** Command $library_url$command\n");
212 if(defined $response_code) {
213 $self->print_msg("*** Unexpected error type 2. HTTP Response Status: $response_code - Failed.\n");
214 } else {
215 $self->print_msg("*** Unexpected error type 3. Failed:\n\n$response_content\n\n");
216 }
217 return 0;
218 }
219 #print STDERR "********** WgetCommand: $wgetCommand\n\n";
220 #print STDERR "********** Response_content:\n$response_content\n\n";
221 #print STDERR "********** Response_CODE: $response_code\n";
222
223 }
224}
225
226sub deactivate_collection {
227 my $self = shift(@_);
228
229 my $gs_mode = $self->{'gs_mode'};
230 my $qualified_collection = $self->{'qualified_collection'};
231
232 if($gs_mode eq "gs2") {
233 my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
234 my $check_message_against_regex = q/configured release-collection/;
235 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
236 }
237 elsif ($gs_mode eq "gs3") {
238 my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
239 my $check_message_against_regex = "collection: $qualified_collection deactivated";
240 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
241 }
242}
243
244sub activate_collection {
245 my $self = shift(@_);
246
247 my $gs_mode = $self->{'gs_mode'};
248 my $qualified_collection = $self->{'qualified_collection'};
249
250 if($gs_mode eq "gs2") {
251 my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
252 my $check_message_against_regex = q/configured add-collection/;
253 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
254 }
255 elsif ($gs_mode eq "gs3") {
256 my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
257 my $check_message_against_regex = "collection: $qualified_collection activated";
258 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
259 }
260}
261
262sub ping {
263 my $self = shift(@_);
264 my $command = shift(@_);
265 my $silent = shift(@_);
266
267 # If the GS server is not running, we *expect* to see a "500" status code.
268 # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
269 # When pinging an inactive collection, it will say it did "not succeed". This is
270 # a message of interest to return.
271 my $check_responsemsg_against_regex = q/(succeeded)/;
272 my $expected_error_code = 500;
273
274 $self->print_msg("*** COMMAND WAS: |$command|***\n", 4);
275
276 return $self->config($command, $check_responsemsg_against_regex, $expected_error_code, $silent);
277}
278
279# send a pingaction to the GS library. General server-level ping.
280sub ping_library {
281 my $self = shift(@_);
282
283 my $gs_mode = $self->{'gs_mode'};
284
285 my $command = "";
286 if($gs_mode eq "gs2") {
287 $command = "?a=ping";
288 }
289 elsif ($gs_mode eq "gs3") {
290 $command = "?a=s&sa=ping";
291 }
292 return $self->ping($command);
293}
294
295
296# send a pingaction to a collection in GS library to check if it's active
297sub ping_library_collection {
298 my $self = shift(@_);
299 my $silent = shift(@_);
300
301 my $gs_mode = $self->{'gs_mode'};
302 my $qualified_collection = $self->{'qualified_collection'};
303
304 my $command = "";
305 if($gs_mode eq "gs2") {
306 $command = "?a=ping&c=$qualified_collection";
307 }
308 elsif ($gs_mode eq "gs3") {
309 $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";
310 }
311 return $self->ping($command, $silent);
312}
313
314# return true if server is persistent, by calling is-persistent on library_url
315# this is only for GS2, since the GS3 server is always persistent
316sub is_persistent {
317 my $self = shift(@_);
318
319 if($self->{'gs_mode'} eq "gs3") { # GS3 server is always persistent
320 return 1;
321 }
322
323 my $command = "?a=is-persistent";
324 my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
325 return $self->config($command, $check_responsemsg_against_regex);
326}
327
328sub set_library_URL {
329 my $self = shift(@_);
330 my $library_url = shift(@_);
331 $self->{'library_url'} = $library_url;
332}
333
334sub get_library_URL {
335 my $self = shift(@_);
336
337 # For web servers that are external to a Greenstone installation,
338 # the user can pass in their web server's library URL.
339 if($self->{'library_url'}) {
340 return $self->{'library_url'};
341 }
342
343 # For web servers included with GS (like tomcat for GS3 and server.exe
344 # and apache for GS2), we work out the library URL:
345 my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
346 $gs_mode = $self->{'gs_mode'};
347 $lib_name = $self->{'library_name'};
348
349 # If we get here, we are dealing with a server included with GS.
350 # For GS3, we ask ant for the library URL.
351 # For GS2, we derive the URL from the llssite.cfg file.
352
353 my $url = undef;
354
355 if($gs_mode eq "gs2") {
356 my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
357
358 if(-f $llssite_cfg) {
359 # check llssite.cfg for line with url property
360 # for server.exe also need to use portnumber and enterlib properties
361
362 # Read in the entire contents of the file in one hit
363 if (!open (FIN, $llssite_cfg)) {
364 $self->print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
365 return undef;
366 }
367
368 my $contents;
369 sysread(FIN, $contents, -s FIN);
370 close(FIN);
371
372 my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
373 my $enterlib = "";
374 my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
375
376 foreach my $line (@lines) {
377 if($line =~ m/^url=(.*)$/) {
378 $url = $1;
379 } elsif($line =~ m/^enterlib=(.*)$/) {
380 $enterlib = $1;
381 } elsif($line =~ m/^portnumber=(.*)$/) {
382 $portnumber = $1;
383 }
384 }
385
386 if(!$url) {
387 return undef;
388 }
389 elsif($url eq "URL_pending") { # library is not running
390 # do not process url=URL_pending in the file, since for server.exe
391 # this just means the Enter Library button hasn't been pressed yet
392 $url = undef;
393 }
394 else {
395 # In the case of server.exe, need to do extra work to get the proper URL
396 # But first, need to know whether we're indeed dealing with server.exe:
397
398 # compare the URL's domain to the full URL
399 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
400 my $uri = URI->new( $url );
401 my $host = $uri->host;
402 #print STDERR "@@@@@ host: $host\n";
403 if($url =~ m/http:\/\/$host(\/)?$/) {
404 #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
405 # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
406 # If the domain with http:// prefix is completely the same as the URL, assume server.exe
407 # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
408 $url = $url.":".$portnumber.$enterlib;
409 } # else, apache web server
410
411 }
412 }
413 } elsif($gs_mode eq "gs3") {
414 # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
415 # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
416
417 # Or, run the new target get-default-servlet-url
418 # the output can look like:
419 #
420 # Buildfile: build.xml
421 # [echo] os.name: Windows Vista
422 #
423 # get-default-servlet-url:
424 # [echo] http://localhost:8383/greenstone3/library
425 # BUILD SUCCESSFUL
426 # Total time: 0 seconds
427
428 #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
429 # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
430
431 # The get-default-servlet-url ant target can be run from anywhere by specifying the
432 # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
433 # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
434 # to run the activate.pl script in the first place
435 my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
436
437 if (open(PIN, "$perl_command |")) {
438 while (defined (my $perl_output_line = <PIN>)) {
439 if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
440 $url="http://".$1;
441 }
442 }
443 close(PIN);
444 } else {
445 $self->print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
446 }
447 if(defined $lib_name) {
448 # replace the servlet_name portion of the url found, with the given library_name
449 $url =~ s@/[^/]*$@/$lib_name@;
450 }
451 }
452
453 # either the url is still undef or it is now set
454 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
455 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
456
457 $self->{'library_url'} = $url;
458 return $url;
459}
460
461
462sub do_deactivate {
463 my $self = shift(@_);
464
465 # 1. Get library URL
466
467 # For web servers that are external to a Greenstone installation,
468 # the user can pass in their web server's library URL.
469 # For web servers included with GS (like tomcat for GS3 and server.exe
470 # and apache for GS2), we work out the library URL:
471
472 # Can't do $self->{'library_url'}, because it may not yet be set
473 my $library_url = $self->get_library_URL(); # returns undef if no valid server URL
474
475 if(!defined $library_url) { # undef if no valid server URL
476 return; # can't do any deactivation without a valid server URL
477 }
478
479 my $is_persistent_server = $self->{'is_persistent_server'};
480 my $qualified_collection = $self->{'qualified_collection'};
481
482 # CollectionManager's installCollection phase in GLI
483 # 2. Ping the library URL, and if it's a persistent server and running, release the collection
484
485 $self->print_msg("Pinging $library_url\n");
486 if ($self->ping_library()) { # server running
487
488 # server is running, so release the collection if
489 # the server is persistent and the collection is active
490
491 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
492 if (!defined $is_persistent_server) {
493 $self->print_msg("Checking if Greenstone server is persistent\n");
494 $is_persistent_server = $self->is_persistent();
495 $self->{'is_persistent_server'} = $is_persistent_server;
496 }
497
498 if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
499
500 $self->print_msg("Checking if the collection $qualified_collection is already active\n");
501 my $collection_active = $self->ping_library_collection();
502
503 if ($collection_active) {
504 $self->print_msg("De-activating collection $qualified_collection\n");
505 $self->deactivate_collection();
506 }
507 else {
508 $self->print_msg("Collection is not active => No need to deactivate\n");
509 }
510 }
511 else {
512 $self->print_msg("Server is not persistent => No need to deactivate collection\n");
513 }
514 }
515 else {
516 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
517 }
518
519 return $is_persistent_server;
520}
521
522sub do_activate {
523 my $self = shift @_;
524
525 my $library_url = $self->get_library_URL(); # Can't do $self->{'library_url'}; as it may not be set yet
526
527 if(!defined $library_url) { # undef if no valid server URL
528 return; # nothing to activate if without valid server URL
529 }
530
531 my $is_persistent_server = $self->{'is_persistent_server'};
532 my $qualified_collection = $self->{'qualified_collection'};
533
534 $self->print_msg("Pinging $library_url\n");
535 if ($self->ping_library()) { # server running
536
537 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
538 if (!defined $is_persistent_server) {
539 $self->print_msg("Checking if Greenstone server is persistent\n");
540 $is_persistent_server = $self->is_persistent();
541 $self->{'is_persistent_server'} = $is_persistent_server;
542 }
543
544 if ($is_persistent_server) { # persistent server, so can try activating collection
545
546 $self->print_msg("Checking if the collection $qualified_collection is not already active\n");
547
548 # Since we could have deactivated the collection at this point,
549 # it is likely that it is not yet active. When pinging the collection
550 # a "ping did not succeed" message is expected, therefore tell the ping
551 # to proceed silently
552 my $silent = 1;
553 my $collection_active = $self->ping_library_collection($silent);
554
555 if (!$collection_active) {
556 $self->print_msg(" Collection is not active.\n");
557 $self->print_msg("Activating collection $qualified_collection\n");
558 $self->activate_collection();
559
560 # unless an error occurred, the collection should now be active:
561 $collection_active = $self->ping_library_collection(); # not silent if ping did not succeed
562 if(!$collection_active) {
563 $self->print_msg("ERROR: collection $qualified_collection did not get activated\n");
564 }
565 }
566 else {
567 $self->print_msg("Collection is already active => No need to activate\n");
568 }
569 }
570 else {
571 $self->print_msg("Server is not persistent => No need to activate collection\n");
572 }
573 }
574 else {
575 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
576 }
577
578 return $is_persistent_server;
579}
580
581
582#########################################################
583### UNUSED METHODS - CAN BE HANDY
584
585
586# This method uses the perl libraries we're advised to use in place of wget for pinging and retrieving web
587# pages. The problem is that not all perl installations may support these libraries. So we now use the new
588# config() method further above, which uses the wget included in Greenstone binary installations.
589# If the library imports at page top conflict, comment out those imports and move the methods config_old(),
590# is_URL_active() and pingHost() out to a temporary file.
591#
592# If for some reason you can't use wget, then rename the config() method to config_old(), and rename the
593# method below to config() and things should work as before.
594sub config_old {
595 my $self = shift(@_);
596 my ($command, $check_message_against_regex, $expected_error_code, $silent) = @_;
597
598 my $library_url = $self->get_library_URL(); #$self->{'library_url'};
599
600
601 # Gatherer.java's configGS3Server doesn't use the site variable
602 # so we don't have to either
603
604 # for GS2, getting the HTTP status isn't enough, we need to read the output
605 # since this is what CollectionManager.config() stipulates.
606 # Using LWP::UserAgent::get($url) for this
607
608 if(!defined $library_url) {
609 return 0;
610 }
611 else {
612 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
613 # Need to set this, else it takes I don't know how long to timeout
614 # http://www.perlmonks.org/?node_id=618534
615
616 # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
617 # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
618
619 my $response_obj = $ua->get($library_url.$command);
620
621 # $response_obj->content stores the content and $response_obj->code the HTTP response code
622 my $response_code = $response_obj->code();
623
624 if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
625 $self->print_msg("*** Command $library_url$command\n", 3);
626 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
627
628 # check the page content is as expected
629 my $response_content = $response_obj->content;
630 my $resultstr = $response_content;
631 $resultstr =~ s@.*gs_content\"\>@@s;
632 $resultstr =~ s@</div>.*@@s;
633
634 if($resultstr =~ m/$check_message_against_regex/) {#if($response_content =~ m/$check_message_against_regex/) {
635 $self->print_msg(" Response as expected.\n", 3);
636 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
637 return 1;
638 } else {
639 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
640 # don't print out the "ping did not succeed" response, but print out any other messages
641
642 # So we only suppress the ping col "did not succeed" response if we're in silent mode
643 # But if any message other than ping "did not succeed" is returned, we always print it
644 if($resultstr !~ m/did not succeed/ || !$silent) {#if($response_content !~ m/did not succeed/ || !$silent) {
645 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
646 $self->print_msg("*** Got message:\n$response_content.\n", 4);
647 $self->print_msg("*** Got result:\n$resultstr\n", 3);
648 }
649 return 0; # ping on a collection may "not succeed."
650 }
651 }
652 elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
653 # check the page content is as expected
654 if(defined $expected_error_code && $response_code == $expected_error_code) {
655 $self->print_msg(" Response status $response_code as expected.\n", 3);
656 } else {
657 $self->print_msg("*** Command $library_url$command\n");
658 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
659 }
660 return 0; # return false, since the response_code was an error, expected or not
661 }
662 else {
663 $self->print_msg("*** Command $library_url$command\n");
664 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
665 return 0;
666 }
667 }
668}
669
670# This method is now unused. Using ping_library instead to send the ping action to a
671# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
672# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
673# and http://www.perlmonks.org/?node_id=618534
674sub is_URL_active {
675 my $url = shift(@_); # gs3 or gs2 URL
676
677 my $status = 0;
678 if(defined $url) {
679 $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
680 # Need to set this, else it takes I don't know how long to timeout
681 # http://www.perlmonks.org/?node_id=618534
682
683 $status = LWP::Simple::head($url); # returns empty list of headers if it fails
684 # LWP::Simple::get($url) is more intensive, so don't need to do that
685 #print STDERR "**** $url is alive.\n" if $status;
686 }
687 return $status;
688}
689
690# Pinging seems to always return true, so this method doesn't work
691sub pingHost {
692 my $url = shift(@_); # gs3 or gs2 URL
693
694 my $status = 0;
695 if(defined $url) {
696 # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
697 # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
698
699 #my $host = $url;
700 #$host =~ s@^http:\/\/(www.)?@@;
701 #$host =~ s@\/.*@@;
702 #print STDERR "**** HOST: $host\n";
703
704 # More robust way
705 # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
706 my $uri = URI->new( $url );
707 my $host = $uri->host;
708
709 # Ping the host. http://perldoc.perl.org/Net/Ping.html
710 my $p = Net::Ping->new();
711 $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
712 print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
713 $p->close();
714 }
715 # return whether pinging was a success or failure
716 return $status;
717}
718
7191;
Note: See TracBrowser for help on using the repository browser.