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