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