[25573] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | ###########################################################################
|
---|
| 4 | #
|
---|
| 5 | # activate.pl -- to be called after building a collection to activate it.
|
---|
| 6 | #
|
---|
| 7 | # A component of the Greenstone digital library software
|
---|
| 8 | # from the New Zealand Digital Library Project at the
|
---|
| 9 | # University of Waikato, New Zealand.
|
---|
| 10 | #
|
---|
| 11 | # Copyright (C) 2009 New Zealand Digital Library Project
|
---|
| 12 | #
|
---|
| 13 | # This program is free software; you can redistribute it and/or modify
|
---|
| 14 | # it under the terms of the GNU General Public License as published by
|
---|
| 15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 16 | # (at your option) any later version.
|
---|
| 17 | #
|
---|
| 18 | # This program is distributed in the hope that it will be useful,
|
---|
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 21 | # GNU General Public License for more details.
|
---|
| 22 | #
|
---|
| 23 | # You should have received a copy of the GNU General Public License
|
---|
| 24 | # along with this program; if not, write to the Free Software
|
---|
| 25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 26 | #
|
---|
| 27 | ###########################################################################
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | # This program is designed to support the building process of Greenstone.
|
---|
| 31 | # It deactivates the collection just built, if the web server is running
|
---|
| 32 | # and is a persistent web server (or if the library_URL provided as
|
---|
| 33 | # parameter to this script is of a currently running web server). It then
|
---|
| 34 | # moves building to index, before activating the collection on the GS2 or
|
---|
| 35 | # GS3 web server again if necessary.
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | BEGIN {
|
---|
| 39 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
| 40 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
| 41 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | use strict;
|
---|
| 46 | no strict 'refs'; # allow filehandles to be variables and vice versa
|
---|
| 47 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
| 48 |
|
---|
| 49 | use File::Basename;
|
---|
| 50 | use File::Find;
|
---|
| 51 |
|
---|
| 52 | use HTTP::Response;
|
---|
| 53 | use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
|
---|
| 54 | # don't import LWP::Simple's head function by name since it can conflict with CGI:head())
|
---|
| 55 | #use CGI qw(:standard); # then only CGI.pm defines a head()
|
---|
| 56 | use Net::Ping;
|
---|
| 57 | use URI;
|
---|
| 58 |
|
---|
| 59 | use colcfg;
|
---|
| 60 | use scriptutil;
|
---|
| 61 | use util;
|
---|
[25579] | 62 | #use enum;
|
---|
[25573] | 63 |
|
---|
[25579] | 64 | # enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
|
---|
| 65 | # Unfortunately, not part of perl's core
|
---|
| 66 | #use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
|
---|
[25573] | 67 |
|
---|
[25579] | 68 | # global variables
|
---|
| 69 | #my $default_verbosity = LEVEL_ERROR; # by default we display basic error messages
|
---|
| 70 |
|
---|
| 71 | my $default_verbosity = 2; # by default we display basic error and info messages
|
---|
| 72 |
|
---|
[25573] | 73 | sub print_task_msg {
|
---|
[25579] | 74 | my ($task_msg, $verbosity_setting) = @_;
|
---|
| 75 |
|
---|
| 76 | $verbosity_setting = $default_verbosity unless $verbosity_setting;
|
---|
| 77 | #$verbosity_setting = 1 unless defined $verbosity;
|
---|
| 78 | if($verbosity_setting >= 1) {
|
---|
| 79 | print STDERR "\n";
|
---|
| 80 | print STDERR "************************\n";
|
---|
| 81 | print STDERR "* $task_msg\n";
|
---|
| 82 | print STDERR "************************\n";
|
---|
| 83 | }
|
---|
[25573] | 84 | }
|
---|
| 85 |
|
---|
[25579] | 86 | # Prints messages if the verbosity is right. Does not add new lines.
|
---|
| 87 | sub print_msg {
|
---|
| 88 | my ($msg, $min_verbosity, $verbosity_setting) = @_;
|
---|
| 89 |
|
---|
| 90 | # only display error messages if the current
|
---|
| 91 | # verbosity setting >= the minimum verbosity level
|
---|
| 92 | # needed for that message to be displayed.
|
---|
| 93 |
|
---|
| 94 | $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
|
---|
| 95 | $min_verbosity = 1 unless defined $min_verbosity;
|
---|
| 96 | if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
|
---|
| 97 | print STDERR "$msg";
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[25573] | 101 | # Method to send a command to a GS2 or GS3 library_URL
|
---|
| 102 | # the commands used in this script can be activate, deactivate, ping,
|
---|
| 103 | # and is-persistent (is-persistent only implemented for GS2).
|
---|
| 104 | sub config {
|
---|
[25887] | 105 | my ($library_url, $command, $check_message_against_regex, $site, $expected_error_code) = @_;
|
---|
[25573] | 106 | # Gatherer.java's configGS3Server doesn't use the site variable
|
---|
| 107 | # so we don't have to either
|
---|
| 108 |
|
---|
| 109 | # for GS2, getting the HTTP status isn't enough, we need to read the output
|
---|
| 110 | # since this is what CollectionManager.config() stipulates.
|
---|
| 111 | # Using LWP::UserAgent::get($url) for this
|
---|
| 112 |
|
---|
| 113 | if(!defined $library_url) {
|
---|
| 114 | return 0;
|
---|
| 115 | }
|
---|
| 116 | else {
|
---|
| 117 | $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
|
---|
| 118 | # Need to set this, else it takes I don't know how long to timeout
|
---|
| 119 | # http://www.perlmonks.org/?node_id=618534
|
---|
| 120 |
|
---|
| 121 | # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
|
---|
| 122 | # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
|
---|
| 123 |
|
---|
| 124 | my $response_obj = $ua->get( $library_url.$command);
|
---|
| 125 |
|
---|
| 126 | # $response_obj->content stores the content and $response_obj->code the HTTP response code
|
---|
| 127 | my $response_code = $response_obj->code();
|
---|
| 128 |
|
---|
| 129 | if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
|
---|
[25579] | 130 | &print_msg("*** Command $library_url$command\n", 3);
|
---|
| 131 | &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
|
---|
[25573] | 132 |
|
---|
| 133 | # check the page content is as expected
|
---|
| 134 | my $response_content = $response_obj->content;
|
---|
| 135 | if($response_content =~ m/$check_message_against_regex/) {
|
---|
[25579] | 136 | &print_msg(" Response as expected.\n", 3);
|
---|
[25573] | 137 | return 1;
|
---|
| 138 | } else {
|
---|
[25579] | 139 | &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
|
---|
| 140 | &print_msg("*** Got message:\n$response_content.\n", 4);
|
---|
[25573] | 141 | return 0; # ping on a collection may "not succeed."
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
|
---|
[25887] | 145 | # check the page content is as expected
|
---|
| 146 | if(defined $expected_error_code && $response_code == $expected_error_code) {
|
---|
| 147 | &print_msg(" Response status $response_code as expected.\n", 3);
|
---|
| 148 | } else {
|
---|
[25579] | 149 | &print_msg("*** Command $library_url$command\n");
|
---|
[25887] | 150 | &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
|
---|
| 151 | }
|
---|
| 152 | return 0; # return false, since the response_code was an error, expected or not
|
---|
[25573] | 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | sub deactivate_collection {
|
---|
| 158 | my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
|
---|
| 159 |
|
---|
| 160 | if($gs_mode eq "gs2") {
|
---|
| 161 | my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
|
---|
| 162 | my $check_message_against_regex = q/configured release-collection/;
|
---|
| 163 | config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
|
---|
| 164 | }
|
---|
| 165 | elsif ($gs_mode eq "gs3") {
|
---|
| 166 | my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
|
---|
| 167 | my $check_message_against_regex = "collection: $qualified_collection deactivated";
|
---|
| 168 | config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | sub activate_collection {
|
---|
| 173 | my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
|
---|
| 174 |
|
---|
| 175 | if($gs_mode eq "gs2") {
|
---|
| 176 | my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
|
---|
| 177 | my $check_message_against_regex = q/configured add-collection/;
|
---|
| 178 | config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
|
---|
| 179 | }
|
---|
| 180 | elsif ($gs_mode eq "gs3") {
|
---|
| 181 | my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
|
---|
| 182 | my $check_message_against_regex = "collection: $qualified_collection activated";
|
---|
| 183 | config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[25581] | 187 | sub ping {
|
---|
| 188 | my ($library_url, $command, $gs_mode, $site) = @_;
|
---|
[25887] | 189 |
|
---|
| 190 | # If the GS server is not running, we *expect* to see a "500" status code.
|
---|
| 191 | # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
|
---|
| 192 | # When pinging an inactive collection, it will say it did "not succeed". This is
|
---|
| 193 | # a message of interest to return.
|
---|
| 194 | my $check_responsemsg_against_regex = q/(succeeded)/;
|
---|
| 195 | my $expected_error_code = 500;
|
---|
| 196 | return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code);
|
---|
[25581] | 197 | }
|
---|
| 198 |
|
---|
| 199 | # send a pingaction to the GS library. General server-level ping.
|
---|
[25573] | 200 | sub ping_library {
|
---|
| 201 | my ($library_url, $gs_mode, $site) = @_;
|
---|
| 202 |
|
---|
[25581] | 203 | my $command = "";
|
---|
| 204 | if($gs_mode eq "gs2") {
|
---|
| 205 | $command = "?a=ping";
|
---|
[25573] | 206 | }
|
---|
[25581] | 207 | elsif ($gs_mode eq "gs3") {
|
---|
| 208 | $command = "?a=s&sa=ping";
|
---|
| 209 | }
|
---|
| 210 | return &ping($library_url, $command, $gs_mode, $site);
|
---|
[25573] | 211 | }
|
---|
| 212 |
|
---|
[25581] | 213 |
|
---|
| 214 | # send a pingaction to a collection in GS library to check if it's active
|
---|
| 215 | sub ping_library_collection {
|
---|
| 216 | my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
|
---|
| 217 |
|
---|
| 218 | my $command = "";
|
---|
| 219 | if($gs_mode eq "gs2") {
|
---|
| 220 | $command = "?a=ping&c=$qualified_collection";
|
---|
| 221 | }
|
---|
| 222 | elsif ($gs_mode eq "gs3") {
|
---|
| 223 | $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";
|
---|
| 224 | }
|
---|
| 225 | return &ping($library_url, $command, $gs_mode, $site);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[25573] | 228 | # return true if server is persistent, by calling is-persistent on library_url
|
---|
| 229 | # this is only for GS2, since the GS3 server is always persistent
|
---|
| 230 | sub is_persistent {
|
---|
| 231 | my ($library_url, $gs_mode) = @_;
|
---|
| 232 |
|
---|
| 233 | if($gs_mode eq "gs3") { # GS3 server is always persistent
|
---|
| 234 | return 1;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | my $command = "?a=is-persistent";
|
---|
| 238 | my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
|
---|
| 239 | return config($library_url, $command, $check_responsemsg_against_regex);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | sub get_library_URL {
|
---|
| 243 | my $gs_mode = shift(@_); # gs3 or gs2
|
---|
| 244 |
|
---|
| 245 | # If we get here, we are dealing with a server included with GS.
|
---|
| 246 | # For GS3, we ask ant for the library URL.
|
---|
| 247 | # For GS2, we derive the URL from the llssite.cfg file.
|
---|
| 248 |
|
---|
| 249 | my $url = undef;
|
---|
| 250 |
|
---|
| 251 | if($gs_mode eq "gs2") {
|
---|
| 252 | my $llssite_cfg = &util::filename_cat($ENV{'GSDLHOME'}, "llssite.cfg");
|
---|
| 253 |
|
---|
| 254 | if(-f $llssite_cfg) {
|
---|
| 255 | # check llssite.cfg for line with url property
|
---|
| 256 | # for server.exe also need to use portnumber and enterlib properties
|
---|
| 257 |
|
---|
| 258 | # Read in the entire contents of the file in one hit
|
---|
| 259 | if (!open (FIN, $llssite_cfg)) {
|
---|
[25579] | 260 | &print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
|
---|
[25573] | 261 | return undef;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | my $contents;
|
---|
| 265 | sysread(FIN, $contents, -s FIN);
|
---|
| 266 | close(FIN);
|
---|
| 267 |
|
---|
| 268 | my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
|
---|
| 269 | my $enterlib = "";
|
---|
| 270 | my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
|
---|
| 271 |
|
---|
| 272 | foreach my $line (@lines) {
|
---|
| 273 | if($line =~ m/^url=(.*)$/) {
|
---|
| 274 | $url = $1;
|
---|
| 275 | } elsif($line =~ m/^enterlib=(.*)$/) {
|
---|
| 276 | $enterlib = $1;
|
---|
| 277 | } elsif($line =~ m/^portnumber=(.*)$/) {
|
---|
| 278 | $portnumber = $1;
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | if(!$url) {
|
---|
| 283 | return undef;
|
---|
| 284 | }
|
---|
| 285 | elsif($url eq "URL_pending") { # library is not running
|
---|
| 286 | # do not process url=URL_pending in the file, since for server.exe
|
---|
| 287 | # this just means the Enter Library button hasn't been pressed yet
|
---|
| 288 | $url = undef;
|
---|
| 289 | }
|
---|
| 290 | else {
|
---|
| 291 | # In the case of server.exe, need to do extra work to get the proper URL
|
---|
| 292 | # But first, need to know whether we're indeed dealing with server.exe:
|
---|
| 293 |
|
---|
| 294 | # compare the URL's domain to the full URL
|
---|
| 295 | # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
|
---|
| 296 | my $uri = URI->new( $url );
|
---|
| 297 | my $host = $uri->host;
|
---|
| 298 | #print STDERR "@@@@@ host: $host\n";
|
---|
| 299 | if($url =~ m/http:\/\/$host(\/)?$/) {
|
---|
| 300 | #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
|
---|
| 301 | # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
|
---|
| 302 | # If the domain with http:// prefix is completely the same as the URL, assume server.exe
|
---|
| 303 | # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
|
---|
| 304 | $url = $url.":".$portnumber.$enterlib;
|
---|
| 305 | } # else, apache web server
|
---|
| 306 |
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | } elsif($gs_mode eq "gs3") {
|
---|
| 310 | # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
|
---|
| 311 | # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
|
---|
| 312 |
|
---|
| 313 | # Or, run the new target get-default-servlet-url
|
---|
| 314 | # the output can look like:
|
---|
| 315 | #
|
---|
| 316 | # Buildfile: build.xml
|
---|
| 317 | # [echo] os.name: Windows Vista
|
---|
| 318 | #
|
---|
| 319 | # get-default-servlet-url:
|
---|
| 320 | # [echo] http://localhost:8383/greenstone3/library
|
---|
| 321 | # BUILD SUCCESSFUL
|
---|
| 322 | # Total time: 0 seconds
|
---|
| 323 |
|
---|
| 324 | #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
|
---|
| 325 | # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
|
---|
| 326 |
|
---|
[25675] | 327 | # The get-default-servlet-url ant target can be run from anywhere by specifying the
|
---|
| 328 | # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
|
---|
| 329 | # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
|
---|
| 330 | # to run the activate.pl script in the first place
|
---|
| 331 | my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
|
---|
[25573] | 332 |
|
---|
| 333 | if (open(PIN, "$perl_command |")) {
|
---|
| 334 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
| 335 | if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
|
---|
| 336 | $url="http://".$1;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | close(PIN);
|
---|
| 340 | } else {
|
---|
[25579] | 341 | &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
|
---|
[25573] | 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | # either the url is still undef or it is now set
|
---|
| 346 | #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
|
---|
| 347 | #print STDERR "\n@@@@@ URL still undef\n" if !$url;
|
---|
| 348 | return $url;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | ### UNUSED METHODS TO MOVE TO util.pm?
|
---|
| 352 |
|
---|
| 353 | # This method is now unused. Using ping_library instead to send the ping action to a
|
---|
| 354 | # GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
|
---|
| 355 | # http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
|
---|
| 356 | # and http://www.perlmonks.org/?node_id=618534
|
---|
| 357 | sub is_URL_active {
|
---|
| 358 | my $url = shift(@_); # gs3 or gs2 URL
|
---|
| 359 |
|
---|
| 360 | my $status = 0;
|
---|
| 361 | if(defined $url) {
|
---|
| 362 | $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
|
---|
| 363 | # Need to set this, else it takes I don't know how long to timeout
|
---|
| 364 | # http://www.perlmonks.org/?node_id=618534
|
---|
| 365 |
|
---|
| 366 | $status = LWP::Simple::head($url); # returns empty list of headers if it fails
|
---|
| 367 | # LWP::Simple::get($url) is more intensive, so don't need to do that
|
---|
| 368 | #print STDERR "**** $url is alive.\n" if $status;
|
---|
| 369 | }
|
---|
| 370 | return $status;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | # Pinging seems to always return true, so this method doesn't work
|
---|
| 374 | sub pingHost {
|
---|
| 375 | my $url = shift(@_); # gs3 or gs2 URL
|
---|
| 376 |
|
---|
| 377 | my $status = 0;
|
---|
| 378 | if(defined $url) {
|
---|
| 379 | # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
|
---|
| 380 | # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
|
---|
| 381 |
|
---|
| 382 | #my $host = $url;
|
---|
| 383 | #$host =~ s@^http:\/\/(www.)?@@;
|
---|
| 384 | #$host =~ s@\/.*@@;
|
---|
| 385 | #print STDERR "**** HOST: $host\n";
|
---|
| 386 |
|
---|
| 387 | # More robust way
|
---|
| 388 | # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
|
---|
| 389 | my $uri = URI->new( $url );
|
---|
| 390 | my $host = $uri->host;
|
---|
| 391 |
|
---|
| 392 | # Ping the host. http://perldoc.perl.org/Net/Ping.html
|
---|
| 393 | my $p = Net::Ping->new();
|
---|
| 394 | $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
|
---|
| 395 | print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
|
---|
| 396 | $p->close();
|
---|
| 397 | }
|
---|
| 398 | # return whether pinging was a success or failure
|
---|
| 399 | return $status;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 |
|
---|
| 403 | # Most of the arguments are familiar from the building scripts like buildcol.pl
|
---|
| 404 | # The special optional argument -library_url is for when we're dealing with a web
|
---|
| 405 | # library server such as an apache that's separate from any included with GS2.
|
---|
| 406 | # In such a case, this script's caller should pass in -library_url <URL>.
|
---|
| 407 | #
|
---|
| 408 | # $site argument must be specified in the cmdline for collectionConfig.xml to get
|
---|
| 409 | # generated which makes $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
|
---|
| 410 | sub main
|
---|
| 411 | {
|
---|
| 412 | my ($argc,@argv) = @_;
|
---|
| 413 |
|
---|
| 414 | if (($argc==0) || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
|
---|
[25579] | 415 | my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
|
---|
[25573] | 416 |
|
---|
| 417 |
|
---|
[25579] | 418 | print STDERR "\n";
|
---|
| 419 | print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -removeold -keepold -verbosity v\n";
|
---|
| 420 | print STDERR "\t-library_url URL] <[colgroup/]collection>\n";
|
---|
| 421 | print STDERR "\n";
|
---|
[25573] | 422 |
|
---|
[25579] | 423 | exit(-1);
|
---|
[25573] | 424 | }
|
---|
| 425 |
|
---|
| 426 | # get the collection details
|
---|
| 427 | my $qualified_collection = pop @argv; # qualified collection
|
---|
| 428 |
|
---|
[25796] | 429 | my $collect_dir = undef; #"collect"; # can't be "collect" when only -site is provided for GS3
|
---|
[25573] | 430 | my $build_dir = undef;
|
---|
| 431 | my $index_dir = undef;
|
---|
| 432 | my $site = undef;
|
---|
| 433 |
|
---|
| 434 | my $removeold = 0;
|
---|
| 435 | my $keepold = 0;
|
---|
| 436 |
|
---|
[25579] | 437 | my $library_url = undef; # to be specified on the cmdline if not using a GS-included web server
|
---|
[25573] | 438 |
|
---|
| 439 | while (my $arg = shift @argv) {
|
---|
| 440 | if ($arg eq "-collectdir") {
|
---|
| 441 | $collect_dir = shift @argv;
|
---|
| 442 | }
|
---|
| 443 | elsif ($arg eq "-builddir") {
|
---|
| 444 | $build_dir = shift @argv;
|
---|
| 445 | }
|
---|
| 446 | elsif ($arg eq "-indexdir") {
|
---|
| 447 | $index_dir = shift @argv;
|
---|
| 448 | }
|
---|
| 449 | elsif ($arg eq "-site") {
|
---|
| 450 | $site = shift @argv;
|
---|
| 451 | }
|
---|
| 452 | elsif ($arg eq "-removeold") {
|
---|
| 453 | $removeold = 1;
|
---|
| 454 | }
|
---|
| 455 | elsif ($arg eq "-keepold") {
|
---|
| 456 | $keepold = 1;
|
---|
| 457 | }
|
---|
| 458 | elsif ($arg eq "-library_url") {
|
---|
| 459 | $library_url = shift @argv;
|
---|
| 460 | }
|
---|
[25579] | 461 | elsif ($arg eq "-verbosity") {
|
---|
| 462 | $default_verbosity = shift @argv; # global variable
|
---|
| 463 |
|
---|
| 464 | # ensure we're working with ints not strings (int context not str context), in case verbosity=0
|
---|
| 465 | # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
|
---|
| 466 | $default_verbosity = int($default_verbosity || 0); ### is this the best way?
|
---|
| 467 | }
|
---|
[25573] | 468 | }
|
---|
| 469 |
|
---|
| 470 | # work out the building and index dirs
|
---|
[25579] | 471 | my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
|
---|
| 472 | $build_dir = &util::filename_cat($collection_dir, "building") unless (defined $build_dir);
|
---|
[25573] | 473 | $index_dir = &util::filename_cat($collection_dir, "index") unless (defined $index_dir);
|
---|
| 474 |
|
---|
| 475 | &print_task_msg("Running Collection Activation Stage");
|
---|
| 476 |
|
---|
| 477 | # can't do anything without a build directory with something in it to move into index
|
---|
| 478 | if(!&util::dir_exists($build_dir)) {
|
---|
[25579] | 479 | &print_msg("No building folder at $build_dir to move to index.\n");
|
---|
[25573] | 480 | exit -1;
|
---|
| 481 | } elsif (&util::is_dir_empty($build_dir)) {
|
---|
[25579] | 482 | &print_msg("Nothing in building folder $build_dir to move into index folder.\n");
|
---|
[25573] | 483 | exit -1;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | # get and check the collection name
|
---|
| 487 | if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
|
---|
[25579] | 488 | &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
|
---|
| 489 | exit -1;
|
---|
[25573] | 490 | }
|
---|
| 491 |
|
---|
| 492 | # Read in the collection configuration file.
|
---|
[25887] | 493 | # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get
|
---|
| 494 | # generated and does $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
|
---|
[25573] | 495 | my ($collect_cfg_filename, $gs_mode) = &colcfg::get_collect_cfg_name(STDERR);
|
---|
| 496 | my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
|
---|
| 497 |
|
---|
| 498 | # look for build.cfg/buildConfig.xml
|
---|
| 499 | my $build_cfg_filename ="";
|
---|
| 500 |
|
---|
| 501 | if ($gs_mode eq "gs2") {
|
---|
| 502 | $build_cfg_filename = &util::filename_cat($build_dir,"build.cfg");
|
---|
| 503 | } else {
|
---|
| 504 | $build_cfg_filename = &util::filename_cat($build_dir, "buildConfig.xml");
|
---|
| 505 | # gs_mode is GS3. Set the site now if this was not specified as cmdline argument
|
---|
| 506 | #$site = "localsite" unless defined $site;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[25887] | 509 | # We need to know the buildtype for Solr.
|
---|
| 510 | # Any change of indexers is already detected and handled by the calling code (buildcol or
|
---|
| 511 | # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
|
---|
[25573] | 512 |
|
---|
[25887] | 513 | # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
|
---|
| 514 | my $buildtype;
|
---|
| 515 | if (defined $collectcfg->{'buildtype'}) {
|
---|
| 516 | $buildtype = $collectcfg->{'buildtype'};
|
---|
| 517 | } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
|
---|
| 518 | $buildtype = "mgpp";
|
---|
| 519 | } else {
|
---|
| 520 | $buildtype = "mg"; #mg is the default
|
---|
[25573] | 521 | }
|
---|
| 522 |
|
---|
| 523 | # Now the logic in GLI's CollectionManager.java (processComplete()
|
---|
| 524 | # and installCollection()) and Gatherer.configGS3Server().
|
---|
| 525 |
|
---|
| 526 | # 1. Get library URL
|
---|
| 527 |
|
---|
| 528 | # For web servers that are external to a Greenstone installation,
|
---|
| 529 | # the user can pass in their web server's library URL.
|
---|
| 530 | # For web servers included with GS (like tomcat for GS3 and server.exe
|
---|
| 531 | # and apache for GS2), we work out the library URL:
|
---|
| 532 | if(!$library_url) {
|
---|
[25579] | 533 | $library_url = &get_library_URL($gs_mode); # returns undef if no server is running
|
---|
[25573] | 534 | }
|
---|
| 535 |
|
---|
| 536 | # CollectionManager's installCollection phase in GLI
|
---|
| 537 | # 2. Ping the library URL, and if it's a persistent server and running, release the collection
|
---|
| 538 |
|
---|
| 539 | my $is_persistent_server = undef;
|
---|
[25581] | 540 | if($library_url) { # undef if no valid server URL
|
---|
[25675] | 541 |
|
---|
[25581] | 542 | &print_msg("Pinging $library_url\n");
|
---|
| 543 | if(&ping_library($library_url, $gs_mode, $site)) { # server running
|
---|
| 544 |
|
---|
| 545 | # server is running, so release the collection if
|
---|
| 546 | # the server is persistent and the collection is active
|
---|
[25579] | 547 | &print_msg("Checking if Greenstone server is persistent\n");
|
---|
[25596] | 548 | $is_persistent_server = &is_persistent($library_url, $gs_mode);
|
---|
[25581] | 549 |
|
---|
[25596] | 550 | if($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
|
---|
| 551 |
|
---|
| 552 | &print_msg("Checking if the collection $qualified_collection is already active\n");
|
---|
| 553 | my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
|
---|
| 554 |
|
---|
| 555 | if($collection_active) {
|
---|
[25579] | 556 | &print_msg("De-activating collection $qualified_collection\n");
|
---|
| 557 | &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
|
---|
[25596] | 558 | }
|
---|
[25579] | 559 | }
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
[25581] | 562 |
|
---|
[25573] | 563 | # 3. Do all the moving building to index stuff now
|
---|
| 564 |
|
---|
[25579] | 565 | # If removeold: replace index dir with building dir.
|
---|
| 566 | # If keepold: move building's contents into index, where only duplicates will get deleted.
|
---|
| 567 | # removeold and keepold can't both be on at the same time
|
---|
[25573] | 568 | ($removeold, $keepold) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
|
---|
| 569 | 0, # incremental is irrelevant to what activate.pl does, setting this = 0
|
---|
| 570 | $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building
|
---|
| 571 | $collectcfg);
|
---|
| 572 |
|
---|
| 573 | if($removeold) {
|
---|
[25887] | 574 |
|
---|
[25573] | 575 | if(&util::dir_exists($index_dir)) {
|
---|
| 576 | &print_task_msg("Removing \"index\"");
|
---|
| 577 | &util::rm_r($index_dir);
|
---|
| 578 |
|
---|
| 579 | # Wait for a couple of seconds, just for luck
|
---|
| 580 | sleep 2;
|
---|
| 581 |
|
---|
| 582 | if (&util::dir_exists($index_dir)) {
|
---|
[25579] | 583 | &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
|
---|
[25573] | 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | # if remote GS server: gliserver.pl would call activate.pl to activate
|
---|
| 588 | # the collection at this point since activate.pl lives on the server side
|
---|
| 589 |
|
---|
| 590 | # Move the building directory to become the new index directory
|
---|
| 591 | &print_task_msg("Moving \"building\" -> \"index\"");
|
---|
| 592 | &util::mv($build_dir, $index_dir);
|
---|
| 593 | if(&util::dir_exists($build_dir) || !&util::dir_exists($index_dir)) {
|
---|
[25579] | 594 | &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
|
---|
[25573] | 595 | }
|
---|
| 596 | }
|
---|
| 597 | elsif ($keepold) {
|
---|
| 598 | # Copy just the contents of building dir into the index dir, overwriting
|
---|
| 599 | # existing files, but don't replace index with building.
|
---|
| 600 | &print_task_msg("Moving \"building\" -> \"index\"");
|
---|
| 601 | &util::mv_dir_contents($build_dir, $index_dir);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again
|
---|
| 605 |
|
---|
| 606 | # Check for success: if building does not exist OR is empty
|
---|
| 607 | if(!&util::dir_exists($build_dir) || &util::is_dir_empty($build_dir)) {
|
---|
[25581] | 608 |
|
---|
| 609 | if($library_url) { # undef if no valid server URL
|
---|
| 610 |
|
---|
[25579] | 611 | &print_msg("Pinging $library_url\n");
|
---|
[25581] | 612 | if(&ping_library($library_url, $gs_mode, $site)) { # server running
|
---|
| 613 |
|
---|
| 614 | # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
|
---|
[25579] | 615 | if(!defined $is_persistent_server) {
|
---|
| 616 | &print_msg("Checking if Greenstone server is persistent\n");
|
---|
| 617 | $is_persistent_server = &is_persistent($library_url, $gs_mode);
|
---|
[25581] | 618 | }
|
---|
| 619 |
|
---|
| 620 |
|
---|
[25596] | 621 | if($is_persistent_server) { # persistent server, so can try activating collection
|
---|
| 622 |
|
---|
| 623 | &print_msg("Checking if the collection $qualified_collection is not already active\n");
|
---|
| 624 | my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
|
---|
| 625 |
|
---|
| 626 | if(!$collection_active) {
|
---|
[25579] | 627 | &print_msg("Activating collection $qualified_collection\n");
|
---|
| 628 | &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
|
---|
[25676] | 629 |
|
---|
| 630 | # unless an error occurred, the collection should now be active:
|
---|
| 631 | $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
|
---|
| 632 | if(!$collection_active) {
|
---|
| 633 | &print_msg("ERROR: collection $qualified_collection did not get activated\n");
|
---|
| 634 | }
|
---|
[25596] | 635 | }
|
---|
[25579] | 636 | }
|
---|
| 637 | }
|
---|
[25573] | 638 | }
|
---|
| 639 | } else { # installcollection failed
|
---|
| 640 | #CollectionManager.Preview_Ready_Failed
|
---|
[25579] | 641 | &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
|
---|
[25573] | 642 | }
|
---|
[25676] | 643 |
|
---|
| 644 | &print_msg("\n");
|
---|
[25573] | 645 | }
|
---|
| 646 |
|
---|
| 647 | &main(scalar(@ARGV),@ARGV);
|
---|