source: gsdl/trunk/perllib/g2futil.pm@ 16395

Last change on this file since 16395 was 16395, checked in by ak19, 16 years ago
  1. For Fedora CATALINA_HOME ought to be its own tomcat, but When Greenstone 3 is remote (but not when it's local, for some reason) CATALINA_HOME is set to GS3's tomcat. This causes the fedora restart to fail after gsdl.xml was created. 2. Other fixes.
File size: 12.1 KB
RevLine 
[14968]1package g2futil;
2
3
4BEGIN
5{
6 if (!defined $ENV{'FEDORA_HOME'}) {
[15605]7 print STDERR "Error: Environment variable FEDORA_HOME not set.\n";
[14968]8 exit 1;
9 }
10
11 my $fedora_home = $ENV{'FEDORA_HOME'};
12 my $fedora_client_bin = &util::filename_cat($fedora_home,"client","bin");
[15605]13 if($ENV{'GSDLOS'} =~ m/^windows/) {
14 $ENV{'PATH'} .= ";$fedora_client_bin";
15 } else {
16 $ENV{'PATH'} .= ":$fedora_client_bin";
17 }
[14968]18}
19
[15894]20use strict;
[14968]21use util;
22
23sub run_cmd_old
24{
25 my ($cmd,$verbosity,$tolerate_error) = @_;
26
27 if (($verbosity == 0)
28 || (defined $tolerate_error && ($tolerate_error eq "tolerate_error"))) {
29 $cmd .= " > /dev/null"; # Too Unix specific?
30 }
31
32 if ($verbosity >= 2) {
33 print "Runing command:\n";
34 print "$cmd\n";
35 }
36
37 my $status = system($cmd);
38
39 if ($verbosity >= 2) {
40 print "Exit status = ", $status/256, "\n";
41 }
42
43 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error")) {
44 if ($status>0) {
45 print STDERR "Error executing:\n$cmd\n";
46 print STDERR "$!\n";
47 }
48 }
49
50 return $status;
51}
52
53
54sub run_cmd
55{
56 my ($prog,$arguments,$verbosity,$tolerate_error) = @_;
57
[15979]58 my $cmd_status = undef;
59
[14968]60 my $script_ext = ($ENV{'GSDLOS'} =~ m/^windows/) ? ".bat" : ".sh";
61
62 if ($prog =~ m/^fedora-/) {
63 $prog .= $script_ext;
64 }
[15582]65 if (($prog =~ m/.pl$/i) && ($ENV{'GSDLOS'} =~ m/^windows/)) {
66 $prog ="perl -S $prog";
67 }
68
[14968]69 my $cmd = "$prog $arguments";
70
[15656]71### print "*** cmd = $cmd\n";
[14968]72
73 if (open(CMD,"$cmd 2>&1 |"))
74 {
75 my $result = "";
76 my $line;
77 while (defined ($line = <CMD>))
78 {
79 $result .= $line;
80
81 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
82 {
83 print $line;
84 }
85
86
87 }
88
89 close(CMD);
90
91 $cmd_status = $?;
92
93 if ($cmd_status == 0) {
94 # Check for any lines in result begining 'Error:'
95
96 if ($result =~ m/^Error\s*:/m) {
97 # Fedora script generated an error, but did not exit
98 # with an error status => artificially raise one
99
100 $cmd_status = -1;
101 }
102 }
103
104 if ($cmd_status != 0) {
105
106 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
107 {
108 print STDERR "Error: processing command failed. Exit status $cmd_status\n";
109
110 if ($verbosity >= 2) {
111 print STDERR " Command was: $cmd\n";
112 }
113 if ($verbosity >= 3) {
114 print STDERR "result: $result\n";
115 }
116
117 }
118 }
119 }
120 else
121 {
122 print STDERR "Error: failed to execute $cmd\n";
123 }
124
125
126 return $cmd_status;
127}
128
129
130sub run_datastore_info
131{
132 my ($pid,$options) = @_;
133
134 my $verbosity = $options->{'verbosity'};
135
136 my $hostname = $options->{'hostname'};
137 my $port = $options->{'port'};
138 my $username = $options->{'username'};
139 my $password = $options->{'password'};
140 my $protocol = $options->{'protocol'};
141
142 my $prog = "fedora-dsinfo";
143 my $arguments = "$hostname $port $username $password $pid $protocol";
144 my $status = run_cmd($prog,$arguments,$verbosity,"tolerate_error");
145
146 return $status;
147}
148
149sub run_purge
150{
151 my ($pid,$options) = @_;
152
153 my $verbosity = $options->{'verbosity'};
154
155 my $hostname = $options->{'hostname'};
156 my $port = $options->{'port'};
157 my $username = $options->{'username'};
158 my $password = $options->{'password'};
159 my $protocol = $options->{'protocol'};
160
161 my $server = "$hostname:$port";
162
163 my $prog = "fedora-purge";
164 my $arguments = "$server $username $password $pid $protocol";
165 $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";
166
167 my $status = run_cmd($prog,$arguments,$verbosity);
168
169 return $status;
170}
171
172sub run_ingest
173{
174 my ($docmets_filename,$options) = @_;
175
176 my $verbosity = $options->{'verbosity'};
177
178 my $hostname = $options->{'hostname'};
179 my $port = $options->{'port'};
180 my $username = $options->{'username'};
181 my $password = $options->{'password'};
182 my $protocol = $options->{'protocol'};
183
184 my $server = "$hostname:$port";
185
186 my $prog = "fedora-ingest";
187
[15015]188 my $type = undef;
189
190 if ($ENV{'FEDORA2_HOME'}) {
191 $type = "metslikefedora1";
192 }
193 else {
194 $type = "info:fedora/fedora-system:METSFedoraExt-1.1";
195 }
[14968]196
197 my $arguments = "file \"$docmets_filename\" $type $server $username $password $protocol";
198 $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";
199
200 my $status = run_cmd($prog,$arguments,$verbosity);
201
202 return $status;
203}
204
205
[16102]206sub rec_get_all_hash_dirs
207{
208 my ($full_dir,$all_dirs) = @_;
[14968]209
[16102]210 if (opendir(DIR, $full_dir)) {
211 my @sub_dirs = grep { ($_ !~ /^\./) && (-d &util::filename_cat($full_dir,$_)) } readdir(DIR);
212 closedir DIR;
213
214 my @hash_dirs = grep { $_ =~ m/\.dir$/ } @sub_dirs;
215 my @rec_dirs = grep { $_ !~ m/\.dir$/ } @sub_dirs;
216
217 foreach my $hd (@hash_dirs) {
218 my $full_hash_dir = &util::filename_cat($full_dir,$hd);
219 push(@$all_dirs,$full_hash_dir);
220 }
221
222 foreach my $rd (@rec_dirs) {
223 my $full_rec_dir = &util::filename_cat($full_dir,$rd);
224 rec_get_all_hash_dirs($full_rec_dir,$all_dirs);
225 }
226 }
227}
228
229sub get_all_hash_dirs
230{
231 my ($start_dir,$maxdocs) = @_;
232
233 my @all_dirs = ();
234 rec_get_all_hash_dirs($start_dir,\@all_dirs);
235
236 if ((defined $maxdocs) && ($maxdocs ne "")) {
237 my @maxdoc_dirs = ();
238 for (my $i=0; $i<$maxdocs; $i++) {
239 push(@maxdoc_dirs,shift(@all_dirs));
240 }
241 @all_dirs = @maxdoc_dirs;
242 }
243
244 return @all_dirs;
245}
246
[14968]247sub get_hash_id
248{
249 my ($hash_dir) = @_;
250
251 my $hash_id = undef;
252
253 my $docmets_filename = &util::filename_cat($hash_dir,"docmets.xml");
254
255 if (open(DIN,"<$docmets_filename"))
256 {
257 while (defined (my $line = <DIN>))
258 {
259 if ($line =~ m/<dc:identifier>(.*?)<\/dc:identifier>/)
260 {
261 $hash_id = $1;
262 last;
263 }
264 }
265
266 close(DIN);
267 }
268 else
269 {
270 print STDERR "Warning: Unable to open \"$docmets_filename\"\n";
271 }
272
273 return $hash_id;
274
275}
276
277
[15395]278# Subroutine to write the gsdl.xml file in FEDORA_HOME/tomcat/conf/Catalina/<host/localhost>/
279# This xml file will tell Fedora where to find the parent folder of the GS collect dir
280# so that it can obtain the FedoraMETS files for ingestion.
281# It depends on the Fedora server being on the same machine as the Greenstone server that
282# this code is part of.
283sub write_gsdl_xml_file
284{
[15605]285 my ($fedora_host, $collect_dir, $options) = @_;
286 my $verbosity = $options->{'verbosity'};
[15656]287 my $hostname = $options->{'hostname'};
288 my $port = $options->{'port'};
289 my $protocol = $options->{'protocol'};
[15605]290
[15395]291 print STDERR "Ensuring that a correct gsdl.xml file exists on the Fedora server end\n";
[16395]292 # The top of this file has already made sure that FEDORA_HOME is set, but for GS3
293 # CATALINA_HOME is set to GS' own tomcat. Since we'll be working with fedora, we need
294 # to temporarily set CATALINA_HOME to fedora's tomcat:
295 my $gs_catalina_home = $ENV{'CATALINA_HOME'};
296 $ENV{'CATALINA_HOME'} = &util::filename_cat($ENV{'FEDORA_HOME'}, "tomcat");
[14968]297
[15395]298 # 1. Find out which folder to write to: fedora_host or localhost
[15656]299 # whichever contains fedora.xml is the one we want (if none, exit with error value?)
[15395]300 my $fedora_home = $ENV{'FEDORA_HOME'};
301 my $base_path = &util::filename_cat($fedora_home, "tomcat", "conf", "Catalina");
[14968]302
[15395]303 my $host_path = &util::filename_cat($base_path, $fedora_host);
304 my $xmlFile = &util::filename_cat($host_path, "fedora.xml");
305 if (!-e $xmlFile) {
[15656]306 # check if the folder localhost contains fedoraXML
[15395]307 $host_path = &util::filename_cat($base_path, "localhost");
308 $xmlFile = &util::filename_cat($host_path, "fedora.xml");
309 if(!-e $xmlFile) {
310 # try putting gsdl in this folder, but still print a warning
[16395]311 print STDERR "$host_path does not contain file fedora.xml. Hoping gsdl.xml belongs there anyway\n";
[15395]312 }
313 }
314
315 # 2. Construct the string we are going write to the gsdl.xml file
316 # a. get the parent directory of collect_dir by removinbg the word
[15656]317 # "collect" from it and any optional OS-type slash at the end.
318 # (Path slash direction does not matter here.)
[15395]319 my $collectParentDir = $collect_dir;
320 $collectParentDir =~ s/collect(\/|\\)?//;
[15656]321
[15395]322 # b. Use the collectParentDir to create the contents of gsdl.xml
323 my $gsdlXMLcontents = "<?xml version='1.0' encoding='utf-8'?>\n<Context docBase=\"";
324 $gsdlXMLcontents = $gsdlXMLcontents.$collectParentDir."\" path=\"/gsdl\"></Context>";
325
326 # 3. If there is already a gsdl.xml file in host_path, compare the string we
327 # want to write with what is already in there. If they're the same, we can return
328 $xmlFile = &util::filename_cat($host_path, "gsdl.xml");
329 if(-e $xmlFile) {
330 # such a file exists, so read the contents
331 unless(open(FIN, "<$xmlFile")) {
[16363]332 print STDERR "g2f-import.pl: Unable to open existing $xmlFile for comparing...Recoverable. $!\n";
[15395]333 # doesn't matter, we'll just overwrite it then
334 }
335 my $xml_contents;
336 {
337 local $/ = undef; # Read entire file at once
338 $xml_contents = <FIN>; # Now file is read in as one single 'line'
339 }
340 close(FIN); # close the file
341 if($xml_contents eq $gsdlXMLcontents) {
[16363]342 print STDERR "Fedora links to the FLI import folder through gsdl.xml.\n";
[15395]343 # it already contains what we want, we're done
344 return "gsdl.xml";
345 }
346 }
347
348 # 4. If we're here, the contents of gsdl.xml need to be updated:
349 # a. First stop the fedora server
[15605]350 my $script_ext = ($ENV{'GSDLOS'} =~ m/^windows/) ? ".bat" : ".sh";
351 my $stop_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "shutdown".$script_ext);
[15395]352 # execute the command
[16395]353 $! = 0; # does this initialise the return value?
[15605]354 my $status = system($stop_tomcat);
355 if ($status!=0) { # to get the actual exit value, divide by 256, but not useful here
[15395]356 # possible tomcat was already stopped - it's not the end of the world
[16363]357 print STDERR "Failed to stop Fedora server. Perhaps it was not running. $!\n";
[15605]358 print "Exit status = ", $status/256, "\n";
[15395]359 }
360
361 # b. overwrite the file that has outdated contents with the contents we just constructed
362 unless(open(FOUT, ">$xmlFile")) { # create or overwrite gsdl.xml file
363 die "g2f-import.pl: Unable to open $xmlFile for telling Fedora where the collect dir is...ERROR: $!\n";
364 }
365 # write out the updated contents and close the file
366 print FOUT $gsdlXMLcontents;
367 close(FOUT);
368
369 # c. Restart the fedora server
[15605]370 my $start_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "startup".$script_ext);
[16395]371 $! = 0;
[15605]372 $status = system($start_tomcat);
373 if ($status!=0) {
[15395]374 print STDERR "Failed to restart the Fedora server... ERROR: $!\n";
[15605]375 print "Exit status = ", $status/256, "\n";
[15395]376 }
[16395]377
378 # reset CATALINA_HOME to GS' Tomcat:
379 $ENV{'CATALINA_HOME'} = $gs_catalina_home;
[15605]380
[15656]381 # Starting up the Fedora server takes a long time. We need to wait for the server to be
382 # ready before import can continue, because g2f-import relies on an up-and-running Fedora
383 # server to purge the collection from it while g2f-build.pl needs a ready Fedora server
384 # in order to make it ingest the FedoraMETS. Sleeping is not sufficient (#sleep 10;) since
385 # the subsequent steps depend on a proper server restart.
386 # Dr Bainbridge's suggestion: test the server is ready with a call to wget.
387
388 # Wget tries to retrieve the fedora search page (protocol://host:port/fedora/search)
389 # 20 times, waiting 3 seconds between each failed attempt. If it ultimately fails, we
390 # print a message to the user.
391 # The wget --spider option makes it check that the page is merely there rather than
392 # downloading it (see http://www.gnu.org/software/wget/manual/wget.html#Download-Options)
393 # -q is for quiet, --tries for the number of retries, --waitretry is the number of seconds
394 # between each attempt. Usually wget returns the contents of the page, but in our case it
395 # will return 0 for success since we are not downloading.
[15395]396
[15656]397 print STDERR "Fedora server restarted. Waiting for it to become ready...\n";
[16395]398 #print STDERR "****$protocol://$hostname:$port/fedora/search\n";
[15656]399
[16395]400 $! = 0;
[15656]401 my $fedoraServerReady = system("wget -q --spider --waitretry=3 --tries=20 $protocol://$hostname:$port/fedora/search");
402 if($fedoraServerReady != 0) {
403 print STDERR "Fedora server is still not ready... ERROR: $!\n";
[16395]404 print "Exit status = ", $fedoraServerReady/256, "\n";
[15656]405 die "Exiting....\n";
406 }
[16395]407
[15395]408 # return some indication that things went well
409 return "gsdl.xml";
410}
411
412
[14968]4131;
Note: See TracBrowser for help on using the repository browser.