source: main/trunk/greenstone2/perllib/activate.pm@ 30520

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

Refactoring activate.pl into activate.pm (class, OOP) and activate.pl. Now buildcolutils.pm uses do_deactivate() from activate.pm.

File size: 22.0 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 activate;
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
35use HTTP::Response;
36use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
37 # don't import LWP::Simple's head function by name since it can conflict with CGI:head())
38#use CGI qw(:standard); # then only CGI.pm defines a head()
39use Net::Ping;
40use URI;
41
42use printusage;
43use parse2;
44
45#use enum;
46# enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
47# Unfortunately, not part of perl's core
48#use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
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 my $self = {'build_dir' => $build_dir,
58 'index_dir' => $index_dir,
59 'collect_dir' => $collect_dir,
60 'site' => $site,
61 'qualified_collection' => $qualified_collection,
62 #'is_persistent_server' => undef,
63 'library_url' => $library_url || $ENV{'GSDL_LIBRARY_URL'} || undef, # to be specified on the cmdline if not using a GS-included web server
64 'library_name' => $library_name,
65 #'gs_mode' => "gs2",
66 'verbosity' => $verbosity || 2
67 };
68
69
70 # Do we need 'listall' support in buildcol? If so, copy code from inexport
71 # later [jmt12]
72
73 if ((defined $site) && ($site ne "")) { # GS3
74 $self->{'gs_mode'} = "gs3";
75 } else {
76 $self->{'gs_mode'} = "gs2";
77 }
78
79 return bless($self, $class);
80}
81
82## TODO: gsprintf to $self->{'out'} in these 2 print functions
83## See buildcolutils.pm new() for setting up $out
84
85sub print_task_msg {
86 my $self = shift(@_);
87 my ($task_msg, $verbosity_setting) = @_;
88
89 $verbosity_setting = $self->{'verbosity'} unless $verbosity_setting;
90 #$verbosity_setting = 1 unless defined $verbosity;
91 if($verbosity_setting >= 1) {
92 print STDERR "\n";
93 print STDERR "************************\n";
94 print STDERR "* $task_msg\n";
95 print STDERR "************************\n";
96 }
97}
98
99# Prints messages if the verbosity is right. Does not add new lines.
100sub print_msg {
101 my $self = shift(@_);
102 my ($msg, $min_verbosity, $verbosity_setting) = @_;
103
104 # only display error messages if the current
105 # verbosity setting >= the minimum verbosity level
106 # needed for that message to be displayed.
107
108 $verbosity_setting = $self->{'verbosity'} unless defined $verbosity_setting;
109 $min_verbosity = 1 unless defined $min_verbosity;
110 if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
111 print STDERR "$msg";
112 }
113}
114
115# Method to send a command to a GS2 or GS3 library_URL
116# the commands used in this script can be activate, deactivate, ping,
117# and is-persistent (is-persistent only implemented for GS2).
118sub config {
119 my $self = shift(@_);
120 my ($command, $check_message_against_regex, $site, $expected_error_code, $silent) = @_;
121
122 my ($library_url);
123 $library_url = $self->{'library_url'};
124 #$site = $self->{'site'};
125
126
127 # Gatherer.java's configGS3Server doesn't use the site variable
128 # so we don't have to either
129
130 # for GS2, getting the HTTP status isn't enough, we need to read the output
131 # since this is what CollectionManager.config() stipulates.
132 # Using LWP::UserAgent::get($url) for this
133
134 if(!defined $library_url) {
135 return 0;
136 }
137 else {
138 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
139 # Need to set this, else it takes I don't know how long to timeout
140 # http://www.perlmonks.org/?node_id=618534
141
142 # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
143 # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
144
145 my $response_obj = $ua->get($library_url.$command);
146
147 # $response_obj->content stores the content and $response_obj->code the HTTP response code
148 my $response_code = $response_obj->code();
149
150 if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
151 $self->print_msg("*** Command $library_url$command\n", 3);
152 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
153
154 # check the page content is as expected
155 my $response_content = $response_obj->content;
156 my $resultstr = $response_content;
157 $resultstr =~ s@.*gs_content\"\>@@s;
158 $resultstr =~ s@</div>.*@@s;
159
160 if($response_content =~ m/$check_message_against_regex/) {
161 $self->print_msg(" Response as expected.\n", 3);
162 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
163 return 1;
164 } else {
165 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
166 # don't print out the "ping did not succeed" response, but print out any other messages
167
168 # So we only suppress the ping col "did not succeed" response if we're in silent mode
169 # But if any message other than ping "did not succeed" is returned, we always print it
170 if($response_content !~ m/did not succeed/ || !$silent) {
171 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
172 $self->print_msg("*** Got message:\n$response_content.\n", 4);
173 $self->print_msg("*** Got result:\n$resultstr\n", 3);
174 }
175 return 0; # ping on a collection may "not succeed."
176 }
177 }
178 elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
179 # check the page content is as expected
180 if(defined $expected_error_code && $response_code == $expected_error_code) {
181 $self->print_msg(" Response status $response_code as expected.\n", 3);
182 } else {
183 $self->print_msg("*** Command $library_url$command\n");
184 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
185 }
186 return 0; # return false, since the response_code was an error, expected or not
187 }
188 else {
189 $self->print_msg("*** Command $library_url$command\n");
190 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
191 return 0;
192 }
193 }
194}
195
196sub deactivate_collection {
197 my $self = shift(@_);
198
199 my ($library_url, $gs_mode, $qualified_collection, $site);
200 $library_url = $self->{'library_url'};
201 $gs_mode = $self->{'gs_mode'};
202 $qualified_collection = $self->{'qualified_collection'};
203 $site = $self->{'site'};
204
205 if($gs_mode eq "gs2") {
206 my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
207 my $check_message_against_regex = q/configured release-collection/;
208 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
209 }
210 elsif ($gs_mode eq "gs3") {
211 my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
212 my $check_message_against_regex = "collection: $qualified_collection deactivated";
213 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
214 }
215}
216
217sub activate_collection {
218 my $self = shift(@_);
219
220 my ($library_url, $gs_mode, $qualified_collection, $site);
221 $library_url = $self->{'library_url'};
222 $gs_mode = $self->{'gs_mode'};
223 $qualified_collection = $self->{'qualified_collection'};
224 $site = $self->{'site'};
225
226 if($gs_mode eq "gs2") {
227 my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
228 my $check_message_against_regex = q/configured add-collection/;
229 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
230 }
231 elsif ($gs_mode eq "gs3") {
232 my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
233 my $check_message_against_regex = "collection: $qualified_collection activated";
234 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
235 }
236}
237
238sub ping {
239 my $self = shift(@_);
240 my $command = shift(@_);
241 my $silent = shift(@_);
242
243 my ($library_url, $gs_mode, $site);
244 $library_url = $self->{'library_url'};
245 $gs_mode = $self->{'gs_mode'};
246 $site = $self->{'site'};
247
248 # If the GS server is not running, we *expect* to see a "500" status code.
249 # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
250 # When pinging an inactive collection, it will say it did "not succeed". This is
251 # a message of interest to return.
252 my $check_responsemsg_against_regex = q/(succeeded)/;
253 my $expected_error_code = 500;
254
255 $self->print_msg("*** COMMAND WAS: |$command|***\n", 4);
256
257 return $self->config($command, $check_responsemsg_against_regex, $site, $expected_error_code, $silent);
258}
259
260# send a pingaction to the GS library. General server-level ping.
261sub ping_library {
262 my $self = shift(@_);
263
264 my ($library_url, $gs_mode, $site);
265 $library_url = $self->{'library_url'};
266 $gs_mode = $self->{'gs_mode'};
267 $site = $self->{'site'};
268
269 my $command = "";
270 if($gs_mode eq "gs2") {
271 $command = "?a=ping";
272 }
273 elsif ($gs_mode eq "gs3") {
274 $command = "?a=s&sa=ping";
275 }
276 return $self->ping($command);
277}
278
279
280# send a pingaction to a collection in GS library to check if it's active
281sub ping_library_collection {
282 my $self = shift(@_);
283 my $silent = shift(@_);
284
285 my ($library_url, $gs_mode, $qualified_collection, $site);
286 $library_url = $self->{'library_url'};
287 $gs_mode = $self->{'gs_mode'};
288 $qualified_collection = $self->{'qualified_collection'};
289 $site = $self->{'site'};
290 #$silent = $self->{'silent'};
291
292 my $command = "";
293 if($gs_mode eq "gs2") {
294 $command = "?a=ping&c=$qualified_collection";
295 }
296 elsif ($gs_mode eq "gs3") {
297 $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";
298 }
299 return $self->ping($command, $silent);
300}
301
302# return true if server is persistent, by calling is-persistent on library_url
303# this is only for GS2, since the GS3 server is always persistent
304sub is_persistent {
305 my $self = shift(@_);
306
307 my ($library_url, $gs_mode);
308 $library_url = $self->{'library_url'};
309 $gs_mode = $self->{'gs_mode'};
310
311 if($gs_mode eq "gs3") { # GS3 server is always persistent
312 return 1;
313 }
314
315 my $command = "?a=is-persistent";
316 my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
317 return $self->config($command, $check_responsemsg_against_regex);
318}
319
320sub set_library_URL {
321 my $self = shift(@_);
322 my $library_url = shift(@_);
323 $self->{'library_url'} = $library_url;
324}
325
326sub get_library_URL {
327 my $self = shift(@_);
328
329 # For web servers that are external to a Greenstone installation,
330 # the user can pass in their web server's library URL.
331 if($self->{'library_url'}) {
332 return $self->{'library_url'};
333 }
334
335 # For web servers included with GS (like tomcat for GS3 and server.exe
336 # and apache for GS2), we work out the library URL:
337 my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
338 $gs_mode = $self->{'gs_mode'};
339 $lib_name = $self->{'library_name'};
340
341 # If we get here, we are dealing with a server included with GS.
342 # For GS3, we ask ant for the library URL.
343 # For GS2, we derive the URL from the llssite.cfg file.
344
345 my $url = undef;
346
347 if($gs_mode eq "gs2") {
348 my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
349
350 if(-f $llssite_cfg) {
351 # check llssite.cfg for line with url property
352 # for server.exe also need to use portnumber and enterlib properties
353
354 # Read in the entire contents of the file in one hit
355 if (!open (FIN, $llssite_cfg)) {
356 $self->print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
357 return undef;
358 }
359
360 my $contents;
361 sysread(FIN, $contents, -s FIN);
362 close(FIN);
363
364 my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
365 my $enterlib = "";
366 my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
367
368 foreach my $line (@lines) {
369 if($line =~ m/^url=(.*)$/) {
370 $url = $1;
371 } elsif($line =~ m/^enterlib=(.*)$/) {
372 $enterlib = $1;
373 } elsif($line =~ m/^portnumber=(.*)$/) {
374 $portnumber = $1;
375 }
376 }
377
378 if(!$url) {
379 return undef;
380 }
381 elsif($url eq "URL_pending") { # library is not running
382 # do not process url=URL_pending in the file, since for server.exe
383 # this just means the Enter Library button hasn't been pressed yet
384 $url = undef;
385 }
386 else {
387 # In the case of server.exe, need to do extra work to get the proper URL
388 # But first, need to know whether we're indeed dealing with server.exe:
389
390 # compare the URL's domain to the full URL
391 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
392 my $uri = URI->new( $url );
393 my $host = $uri->host;
394 #print STDERR "@@@@@ host: $host\n";
395 if($url =~ m/http:\/\/$host(\/)?$/) {
396 #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
397 # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
398 # If the domain with http:// prefix is completely the same as the URL, assume server.exe
399 # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
400 $url = $url.":".$portnumber.$enterlib;
401 } # else, apache web server
402
403 }
404 }
405 } elsif($gs_mode eq "gs3") {
406 # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
407 # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
408
409 # Or, run the new target get-default-servlet-url
410 # the output can look like:
411 #
412 # Buildfile: build.xml
413 # [echo] os.name: Windows Vista
414 #
415 # get-default-servlet-url:
416 # [echo] http://localhost:8383/greenstone3/library
417 # BUILD SUCCESSFUL
418 # Total time: 0 seconds
419
420 #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
421 # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
422
423 # The get-default-servlet-url ant target can be run from anywhere by specifying the
424 # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
425 # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
426 # to run the activate.pl script in the first place
427 my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
428
429 if (open(PIN, "$perl_command |")) {
430 while (defined (my $perl_output_line = <PIN>)) {
431 if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
432 $url="http://".$1;
433 }
434 }
435 close(PIN);
436 } else {
437 $self->print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
438 }
439 if(defined $lib_name) {
440 # replace the servlet_name portion of the url found, with the given library_name
441 $url =~ s@/[^/]*$@/$lib_name@;
442 }
443 }
444
445 # either the url is still undef or it is now set
446 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
447 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
448
449 $self->{'library_url'} = $url;
450 return $url;
451}
452
453
454sub do_deactivate {
455 my $self = shift(@_);
456
457 my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
458 $is_persistent_server = $self->{'is_persistent_server'};
459 $library_url = $self->get_library_URL(); #may not yet be set, so can't do $self->{'library_url'};
460 $site = $self->{'site'};
461 $gs_mode = $self->{'gs_mode'};
462 $qualified_collection = $self->{'qualified_collection'};
463
464
465 $self->print_msg("Pinging $library_url\n");
466 if ($self->ping_library()) { # server running
467
468 # server is running, so release the collection if
469 # the server is persistent and the collection is active
470
471 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
472 if (!defined $is_persistent_server) {
473 $self->print_msg("Checking if Greenstone server is persistent\n");
474 $is_persistent_server = $self->is_persistent();
475 $self->{'is_persistent_server'} = $is_persistent_server;
476 }
477
478 if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
479
480 $self->print_msg("Checking if the collection $qualified_collection is already active\n");
481 my $collection_active = $self->ping_library_collection();
482
483 if ($collection_active) {
484 $self->print_msg("De-activating collection $qualified_collection\n");
485 $self->deactivate_collection();
486 }
487 else {
488 $self->print_msg("Collection is not active => No need to deactivate\n");
489 }
490 }
491 else {
492 $self->print_msg("Server is not persistent => No need to deactivate collection\n");
493 }
494 }
495 else {
496 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
497 }
498
499 return $is_persistent_server;
500}
501
502sub do_activate {
503 my $self = shift @_;
504
505 my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
506 $is_persistent_server = $self->{'is_persistent_server'};
507 $library_url = $self->get_library_URL(); #may not yet be set, so can't do $self->{'library_url'};
508 $site = $self->{'site'};
509 $gs_mode = $self->{'gs_mode'};
510 $qualified_collection = $self->{'qualified_collection'};
511
512 $self->print_msg("Pinging $library_url\n");
513 if ($self->ping_library()) { # server running
514
515 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
516 if (!defined $is_persistent_server) {
517 $self->print_msg("Checking if Greenstone server is persistent\n");
518 $is_persistent_server = $self->is_persistent();
519 $self->{'is_persistent_server'} = $is_persistent_server;
520 }
521
522 if ($is_persistent_server) { # persistent server, so can try activating collection
523
524 $self->print_msg("Checking if the collection $qualified_collection is not already active\n");
525
526 # Since we could have deactivated the collection at this point,
527 # it is likely that it is not yet active. When pinging the collection
528 # a "ping did not succeed" message is expected, therefore tell the ping
529 # to proceed silently
530 my $silent = 1;
531 my $collection_active = $self->ping_library_collection($silent);
532
533 if (!$collection_active) {
534 $self->print_msg(" Collection is not active.\n");
535 $self->print_msg("Activating collection $qualified_collection\n");
536 $self->activate_collection();
537
538 # unless an error occurred, the collection should now be active:
539 $collection_active = $self->ping_library_collection(); # not silent if ping did not succeed
540 if(!$collection_active) {
541 $self->print_msg("ERROR: collection $qualified_collection did not get activated\n");
542 }
543 }
544 else {
545 $self->print_msg("Collection is already active => No need to activate\n");
546 }
547 }
548 else {
549 $self->print_msg("Server is not persistent => No need to activate collection\n");
550 }
551 }
552 else {
553 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
554 }
555
556 return $is_persistent_server;
557}
558
559
560#########################################################
561### UNUSED METHODS - CAN BE HANDY
562
563# This method is now unused. Using ping_library instead to send the ping action to a
564# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
565# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
566# and http://www.perlmonks.org/?node_id=618534
567sub is_URL_active {
568 my $url = shift(@_); # gs3 or gs2 URL
569
570 my $status = 0;
571 if(defined $url) {
572 $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
573 # Need to set this, else it takes I don't know how long to timeout
574 # http://www.perlmonks.org/?node_id=618534
575
576 $status = LWP::Simple::head($url); # returns empty list of headers if it fails
577 # LWP::Simple::get($url) is more intensive, so don't need to do that
578 #print STDERR "**** $url is alive.\n" if $status;
579 }
580 return $status;
581}
582
583# Pinging seems to always return true, so this method doesn't work
584sub pingHost {
585 my $url = shift(@_); # gs3 or gs2 URL
586
587 my $status = 0;
588 if(defined $url) {
589 # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
590 # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
591
592 #my $host = $url;
593 #$host =~ s@^http:\/\/(www.)?@@;
594 #$host =~ s@\/.*@@;
595 #print STDERR "**** HOST: $host\n";
596
597 # More robust way
598 # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
599 my $uri = URI->new( $url );
600 my $host = $uri->host;
601
602 # Ping the host. http://perldoc.perl.org/Net/Ping.html
603 my $p = Net::Ping->new();
604 $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
605 print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
606 $p->close();
607 }
608 # return whether pinging was a success or failure
609 return $status;
610}
611
6121;
Note: See TracBrowser for help on using the repository browser.