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

Last change on this file since 15582 was 15582, checked in by ak19, 16 years ago

Dr Bainbridge added code to make sure that on Windows other perl programs are launched with perl -S flag

File size: 9.3 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 $ENV{'PATH'} .= ":$fedora_client_bin";
14}
15
16use util;
17
18sub run_cmd_old
19{
20 my ($cmd,$verbosity,$tolerate_error) = @_;
21
22 if (($verbosity == 0)
23 || (defined $tolerate_error && ($tolerate_error eq "tolerate_error"))) {
24 $cmd .= " > /dev/null"; # Too Unix specific?
25 }
26
27 if ($verbosity >= 2) {
28 print "Runing command:\n";
29 print "$cmd\n";
30 }
31
32 my $status = system($cmd);
33
34 if ($verbosity >= 2) {
35 print "Exit status = ", $status/256, "\n";
36 }
37
38 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error")) {
39 if ($status>0) {
40 print STDERR "Error executing:\n$cmd\n";
41 print STDERR "$!\n";
42 }
43 }
44
45 return $status;
46}
47
48
49sub run_cmd
50{
51 my ($prog,$arguments,$verbosity,$tolerate_error) = @_;
52
53 my $script_ext = ($ENV{'GSDLOS'} =~ m/^windows/) ? ".bat" : ".sh";
54
55 if ($prog =~ m/^fedora-/) {
56 $prog .= $script_ext;
57 }
58 if (($prog =~ m/.pl$/i) && ($ENV{'GSDLOS'} =~ m/^windows/)) {
59 $prog ="perl -S $prog";
60 }
61
62
63 my $cmd = "$prog $arguments";
64
65### print "*** cmd = $cmd\n";
66
67 if (open(CMD,"$cmd 2>&1 |"))
68 {
69 my $result = "";
70 my $line;
71 while (defined ($line = <CMD>))
72 {
73 $result .= $line;
74
75 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
76 {
77 print $line;
78 }
79
80
81 }
82
83 close(CMD);
84
85 $cmd_status = $?;
86
87 if ($cmd_status == 0) {
88 # Check for any lines in result begining 'Error:'
89
90 if ($result =~ m/^Error\s*:/m) {
91 # Fedora script generated an error, but did not exit
92 # with an error status => artificially raise one
93
94 $cmd_status = -1;
95 }
96 }
97
98 if ($cmd_status != 0) {
99
100 if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
101 {
102 print STDERR "Error: processing command failed. Exit status $cmd_status\n";
103
104 if ($verbosity >= 2) {
105 print STDERR " Command was: $cmd\n";
106 }
107 if ($verbosity >= 3) {
108 print STDERR "result: $result\n";
109 }
110
111 }
112 }
113 }
114 else
115 {
116 print STDERR "Error: failed to execute $cmd\n";
117 }
118
119
120 return $cmd_status;
121}
122
123
124sub run_datastore_info
125{
126 my ($pid,$options) = @_;
127
128 my $verbosity = $options->{'verbosity'};
129
130 my $hostname = $options->{'hostname'};
131 my $port = $options->{'port'};
132 my $username = $options->{'username'};
133 my $password = $options->{'password'};
134 my $protocol = $options->{'protocol'};
135
136 my $prog = "fedora-dsinfo";
137 my $arguments = "$hostname $port $username $password $pid $protocol";
138 my $status = run_cmd($prog,$arguments,$verbosity,"tolerate_error");
139
140 return $status;
141}
142
143sub run_purge
144{
145 my ($pid,$options) = @_;
146
147 my $verbosity = $options->{'verbosity'};
148
149 my $hostname = $options->{'hostname'};
150 my $port = $options->{'port'};
151 my $username = $options->{'username'};
152 my $password = $options->{'password'};
153 my $protocol = $options->{'protocol'};
154
155 my $server = "$hostname:$port";
156
157 my $prog = "fedora-purge";
158 my $arguments = "$server $username $password $pid $protocol";
159 $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";
160
161 my $status = run_cmd($prog,$arguments,$verbosity);
162
163 return $status;
164}
165
166sub run_ingest
167{
168 my ($docmets_filename,$options) = @_;
169
170 my $verbosity = $options->{'verbosity'};
171
172 my $hostname = $options->{'hostname'};
173 my $port = $options->{'port'};
174 my $username = $options->{'username'};
175 my $password = $options->{'password'};
176 my $protocol = $options->{'protocol'};
177
178 my $server = "$hostname:$port";
179
180 my $prog = "fedora-ingest";
181
182 my $type = undef;
183
184 if ($ENV{'FEDORA2_HOME'}) {
185 $type = "metslikefedora1";
186 }
187 else {
188 $type = "info:fedora/fedora-system:METSFedoraExt-1.1";
189 }
190
191 my $arguments = "file \"$docmets_filename\" $type $server $username $password $protocol";
192 $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";
193
194 my $status = run_cmd($prog,$arguments,$verbosity);
195
196 return $status;
197}
198
199
200
201sub get_hash_id
202{
203 my ($hash_dir) = @_;
204
205 my $hash_id = undef;
206
207 my $docmets_filename = &util::filename_cat($hash_dir,"docmets.xml");
208
209 if (open(DIN,"<$docmets_filename"))
210 {
211 while (defined (my $line = <DIN>))
212 {
213 if ($line =~ m/<dc:identifier>(.*?)<\/dc:identifier>/)
214 {
215 $hash_id = $1;
216 last;
217 }
218 }
219
220 close(DIN);
221 }
222 else
223 {
224 print STDERR "Warning: Unable to open \"$docmets_filename\"\n";
225 }
226
227 return $hash_id;
228
229}
230
231
232# Subroutine to write the gsdl.xml file in FEDORA_HOME/tomcat/conf/Catalina/<host/localhost>/
233# This xml file will tell Fedora where to find the parent folder of the GS collect dir
234# so that it can obtain the FedoraMETS files for ingestion.
235# It depends on the Fedora server being on the same machine as the Greenstone server that
236# this code is part of.
237sub write_gsdl_xml_file
238{
239 my ($fedora_host, $collect_dir) = @_;
240
241 print STDERR "Ensuring that a correct gsdl.xml file exists on the Fedora server end\n";
242 # The top of this file has already made sure that FEDORA_HOME is set
243
244 # 1. Find out which folder to write to: fedora_host or localhost
245 # whichever contains fedora.xml is the one we want - if none, exit with error value
246 my $fedora_home = $ENV{'FEDORA_HOME'};
247 my $base_path = &util::filename_cat($fedora_home, "tomcat", "conf", "Catalina");
248
249 my $host_path = &util::filename_cat($base_path, $fedora_host);
250 my $xmlFile = &util::filename_cat($host_path, "fedora.xml");
251 if (!-e $xmlFile) {
252 # try seeing if folder localhost contains fedoraXML
253 $host_path = &util::filename_cat($base_path, "localhost");
254 $xmlFile = &util::filename_cat($host_path, "fedora.xml");
255 if(!-e $xmlFile) {
256 # try putting gsdl in this folder, but still print a warning
257 print STDERR "**** $host_path does not contain file fedora.xml. Hoping gsdl.xml belongs there anyway\n";
258 }
259 }
260
261 # 2. Construct the string we are going write to the gsdl.xml file
262 # a. get the parent directory of collect_dir by removinbg the word
263 # "collect" from it and any optional OS-type slash at the end.
264 my $collectParentDir = $collect_dir;
265 $collectParentDir =~ s/collect(\/|\\)?//;
266 #print STDERR "**** collect's parent dir is: $collectParentDir\n";
267
268 # b. Use the collectParentDir to create the contents of gsdl.xml
269 my $gsdlXMLcontents = "<?xml version='1.0' encoding='utf-8'?>\n<Context docBase=\"";
270 $gsdlXMLcontents = $gsdlXMLcontents.$collectParentDir."\" path=\"/gsdl\"></Context>";
271
272 # 3. If there is already a gsdl.xml file in host_path, compare the string we
273 # want to write with what is already in there. If they're the same, we can return
274 $xmlFile = &util::filename_cat($host_path, "gsdl.xml");
275 if(-e $xmlFile) {
276 # such a file exists, so read the contents
277 unless(open(FIN, "<$xmlFile")) {
278 print STDERR "***g2f-import.pl: Unable to open existing $xmlFile for comparing...Recoverable. $!\n";
279 # doesn't matter, we'll just overwrite it then
280 }
281 my $xml_contents;
282 {
283 local $/ = undef; # Read entire file at once
284 $xml_contents = <FIN>; # Now file is read in as one single 'line'
285 }
286 close(FIN); # close the file
287 if($xml_contents eq $gsdlXMLcontents) {
288 print STDERR "The old gsdl.xml file already contains the same.\n";
289 # it already contains what we want, we're done
290 return "gsdl.xml";
291 }
292 }
293
294 # 4. If we're here, the contents of gsdl.xml need to be updated:
295 # a. First stop the fedora server
296 my $stop_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "shutdown.sh");
297 # execute the command
298 $!=0; # does this initialise the return value?
299 if (system($stop_tomcat)!=0) { # to get the actual exit value, divide by 256, but not useful here
300 # possible tomcat was already stopped - it's not the end of the world
301 print STDERR "**** Failed to stop Fedora server. Perhaps it was not running. $!\n";
302 }
303
304 # b. overwrite the file that has outdated contents with the contents we just constructed
305 unless(open(FOUT, ">$xmlFile")) { # create or overwrite gsdl.xml file
306 die "g2f-import.pl: Unable to open $xmlFile for telling Fedora where the collect dir is...ERROR: $!\n";
307 }
308 # write out the updated contents and close the file
309 print FOUT $gsdlXMLcontents;
310 close(FOUT);
311
312 # c. Restart the fedora server
313 my $start_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "startup.sh");
314 $!=0;
315 if (system($start_tomcat)!=0) {
316 print STDERR "Failed to restart the Fedora server... ERROR: $!\n";
317 }
318 # QUESTION:
319 # Starting up the Fedora server takes a long time. How long should we wait before
320 # import continues? g2f-import relies on an up-and-running Fedora server to purge the
321 # collection from it whereas g2f-build.pl needs a ready Fedora server in order to make
322 # it ingest the FedoraMETS.
323 # Let's try waiting 10s for the Fedora server to really be up and running after the
324 # restart so import and build can work without glitches. But how can we check if this
325 # duration is actually sufficient?
326 print STDERR "Fedora server restarted. Waiting 10 seconds to ensure the server is ready...\n";
327 sleep 10;
328
329 # return some indication that things went well
330 return "gsdl.xml";
331}
332
333
3341;
Note: See TracBrowser for help on using the repository browser.