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

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

For FLI: Removed gsdl.xml file creation from java file Gatherer.java and moved the functionality into Perl (g2futil.pm, called by g2f-import.pl). Now it should work with a remote GS3 server. Tested when FLI is running on GS3 as well as GS2

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