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

Last change on this file since 15894 was 15894, checked in by mdewsnip, 16 years ago

Added "use strict" to the files missing it.

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