blib/script/moby-s-install.pl untested

filter: Statementstmt 0.0% 0 / 592 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 198 MC/DCmcdc 0.0% 0 / 113 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 1176 CC 63 SCAR 48.7
CC
63
Cov
0%
config_file_from_template
506.0
BEGIN
56.0
prompt_for_directory
30.0
CRAP 129.9
1 #!/usr/local/bin/perl -w
2 #
3 # Prepare the stage...
4 #
5 # $Id: moby-s-install.pl,v 1.15 2009/04/15 16:38:57 kawas Exp $
6 # Contact: Edward Kawas <edward.kawas@gmail.com>
7 # -----------------------------------------------------------
8  
9 BEGIN {
10 use Getopt::Std;
11 use vars qw/ $opt_h /;
12 getopt;
13  
14 # usage
15 if ($opt_h) {
16 print STDOUT <<'END_OF_USAGE';
17 Preparing the stage for hosting a BioMOBY registry.
18 Usage: moby-s-install.pl [-h]
19  
20 -h .... shows this message ;-)
21
22 Installer script for installing a BioMOBY registry
23
24 This script goes through and edits your apache configuration,
25 mysql settings, and installs cgi scripts needed to host your
26 own registry. It is best to do this as root, because items are
27 copied into priviledged locations (apache directory, etc.).
28
29 Good luck!
30
31  
32 END_OF_USAGE
33 exit(0);
34 }
35  
36 my $errors_found = 0;
37  
38 sub say { print @_, "\n"; }
39  
40 # query is for creating the moby_username and password for local access to dbs
41 sub prepare_query {
42 my $s = join "", @_;
43 return qq|GRANT ALL ON $s TO ?\@'localhost' IDENTIFIED BY ? WITH GRANT OPTION|;
44 }
45 sub check_module {
46 eval "require $_[0]";
47 if ($@) {
48 $errors_found = 1;
49 say "Module $_[0] not installed.";
50 } else {
51 say "OK. Module $_[0] is installed.";
52 }
53 }
54  
55 use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0;
56  
57 say
58 'Welcome, BioMobiers. Preparing stage for installation of your custom registry...';
59 say '------------------------------------------------------';
60  
61 # check needed modules
62 foreach $module (
63 qw (
64 File::Spec
65 SOAP::Lite
66 XML::LibXML
67 IO::Stringy
68 Unicode::String
69 File::HomeDir
70 File::ShareDir
71 Class::Inspector
72 RDF::Core
73 XML::XPath
74 Text::Shellwords
75 HTML::Entities
76 DBI
77 DBD::mysql
78 LS
79 XML::Simple
80 )
81 )
82 {
83 check_module($module);
84 }
85 if (MSWIN) {
86 check_module('Term::ReadLine');
87 {
88 local $^W = 0;
89 $SimplePrompt::Terminal = Term::ReadLine->new('Installation');
90 }
91 } else {
92 check_module('IO::Prompt');
93 require IO::Prompt;
94 import IO::Prompt;
95 # module required for service_tester.pl
96 check_module('IPC::Shareable');
97 # check for async libraries if user wants to ....
98 do {
99 foreach $module (
100 qw / WSRF::Lite
101 XML::DOM
102 DateTime::Format::Epoch
103 DateTime::Format::W3CDTF
104 HTTP::Daemon
105 Crypt::OpenSSL::X509
106 MIME::Base64
107 Digest::SHA1
108 Crypt::OpenSSL::RSA
109 Sys::Hostname::Long
110 HTTP::Daemon::SSL
111 / ) {
112 check_module($module);
113
114 }
115 } if prompt( "Shall we check for the moby-async libraries\n\t(do this only if you plan on creating async moby services)? ", -yn );
116 }
117  
118 if ($errors_found) {
119 say "\nSorry, some needed modules were not found.";
120 say "Please install them and run 'moby-s-install.pl' again.";
121 exit(1);
122 }
123 say;
124 }
125 use File::ShareDir;
126 use File::Spec;
127 use English qw( -no_match_vars );
128 use strict;
129  
130 # different prompt modules used for different OSs
131 # ('pprompt' as 'proxy_prompt')
132 sub pprompt {
133 return prompt(@_) unless MSWIN;
134 return SimplePrompt::prompt(@_);
135 }
136  
137 # $prompt ... a prompt asking for a directory
138 # $prompted_dir ... suggested directory
139 sub prompt_for_directory {
140 my ( $prompt, $prompted_dir ) = @_;
141 while (1) {
142 my $dir = pprompt("$prompt [$prompted_dir] ");
143 $dir =~ s/^\s*//;
144 $dir =~ s/\s*$//;
145 $dir = $prompted_dir unless $dir;
146 return $dir if -d $dir and -w $dir; # okay: writable directory
147 next
148 if ( not -e $dir )
149 and say "'$dir' does not exist. Try again please.";
150  
151 next
152 if ( not -w $dir )
153 and say "'$dir' is not a writable directory. Try again please.";
154 }
155 }
156  
157 sub prompt_for_directory_expand {
158 my ( $prompt, $prompted_dir ) = @_;
159 while (1) {
160 my $dir = pprompt("$prompt [$prompted_dir] ");
161 $dir =~ s/^\s*//;
162 $dir =~ s/\s*$//;
163 $dir = $prompted_dir unless $dir;
164 return $dir if -d $dir and -w $dir; # okay: writable directory
165 $prompted_dir = $dir;
166 next
167 if -e $dir
168 and say "'$dir' is not a writable directory. Try again please.";
169 next
170 unless pprompt( "Directory '$dir' does not exists. Create? ", -yn );
171  
172 # okay, we agreed to create it
173 mkdir( $dir, 0755 ) and return $dir;
174 say "'$dir' not created: $!";
175 }
176 }
177  
178 sub prompt_user_input {
179 my ( $prompt, $default ) = @_;
180 while (1) {
181 my $dir = pprompt("$prompt [$default] ");
182 $dir =~ s/^\s*//;
183 $dir =~ s/\s*$//;
184 $dir = $default unless $dir;
185 return $dir;
186 }
187 }
188  
189 # create a file from a template
190 # - from $file_template to $file,
191 # - $file_desc used in messages,
192 # - hashref $filters tells what to change in template
193 sub file_from_template {
194 my ( $file, $file_template, $file_desc, $filters ) = @_;
195 eval {
196 open FILETEMPL, "<$file_template"
197 or die "Cannot read template file '$file_template': $!\n";
198 open FILEOUT, ">$file"
199 or die "Cannot open '$file' for writing: $!\n";
200 while (<FILETEMPL>) {
201 foreach my $token ( keys %$filters ) {
202 if ( $^O eq 'MSWin32' ) {
203 $filters->{$token} =~ s|\\|\/|;
204 }
205 s/\Q$token\E/$filters->{$token}/ge;
206 }
207 print FILEOUT
208 or die "Cannot write into '$file': $!\n";
209 }
210 close FILEOUT;
211 close FILETEMPL
212 or die "Cannot close '$file': $!\n";
213 };
214 if ($@) {
215 say "ERROR: $file_desc was (probably) not created.\n$@";
216 } else {
217 say
218 "\n$file_desc created: '$file'\n\tPlease ensure that the file is executable!\n";
219 }
220  
221 # try to make file executable
222 eval {
223 my $mode = 0655;
224 chmod $mode, "$file";
225 };
226 }
227  
228 # create a config file from a template
229 # - from $file_template to $file,
230 # - $file_desc used in messages,
231 # - hashref $filters tells what to change in template
232 # - hashref $dbnames maps db sections to user specified names
233 sub config_file_from_template {
234 my ( $file, $file_template, $file_desc, $filters, $dbnames, $resourceURLs,
235 $LSIDS )
236 = @_;
237 my $current_section = "";
238 eval {
239  
240 #check if FILETEMPL and FILEOUT are the same if so, create a temp file
241 my $same_resource = $file eq $file_template;
242 my ( $fh, $filename ) = tempfile( UNLINK => 1 ) if $same_resource;
243  
244 open FILETEMPL, "<$file_template"
245 or die "Cannot read template file '$file_template': $!\n";
246  
247 (
248 open FILEOUT, ">$file"
249 or die "Cannot open '$file' for writing: $!\n"
250 )
251 unless $same_resource;
252  
253 while (<FILETEMPL>) {
254 if (/^\[(.*)\]\s*$/) {
255 $current_section = $1;
256 chomp($current_section);
257 }
258 foreach my $token ( keys %$filters ) {
259 if ( $^O eq 'MSWin32' ) {
260 $filters->{$token} =~ s|\\|\/|;
261 }
262 if (/\Q$token\E\s*\=/) {
263 s/^(\Q$token\E\s*\=\s*)(.*)$/$1 . $filters->{$token}/eg;
264 }
265 }
266 if (/dbname\s*\=/) {
267 s/^(dbname\s*\=\s*)(.*)$/$1 . $dbnames->{$current_section}/eg
268 if exists $dbnames->{$current_section}
269 and defined $dbnames->{$current_section};
270 }
271 if (/resourceURL\s*\=/) {
272 s/^(resourceURL\s*\=\s*)(.*)$/$1 . $resourceURLs->{$current_section}/eg
273 if exists $resourceURLs->{$current_section}
274 and defined $resourceURLs->{$current_section};
275 }
276 if (/allResources\s*\=/) {
277 s/^(allResources\s*\=\s*)(.*)$/$1 . $resourceURLs->{'ALL'}/eg
278 if exists $resourceURLs->{'ALL'}
279 and defined $resourceURLs->{'ALL'};
280 }
281 if (/lsid_authority\s*\=/) {
282 s/^(lsid_authority\s*\=\s*)(.*)$/$1 . $LSIDS->{$current_section}->{'AUTHORITY'}/eg
283 if exists $LSIDS->{$current_section}->{'AUTHORITY'}
284 and defined $LSIDS->{$current_section}->{'AUTHORITY'};
285 }
286 if (/lsid_namespace\s*\=/) {
287 s/^(lsid_namespace\s*\=\s*)(.*)$/$1 . $LSIDS->{$current_section}->{'NAMESPACE'}/eg
288 if exists $LSIDS->{$current_section}->{'NAMESPACE'}
289 and defined $LSIDS->{$current_section}->{'NAMESPACE'};
290 }
291 ( print FILEOUT $_ or die "Cannot write into '$file': $!\n" )
292 unless $same_resource;
293 ( print $fh $_ or die "Cannot write into '$filename': $!\n" )
294 if $same_resource;
295  
296 }
297 close FILETEMPL or die "Cannot close '$file_template': $!\n";
298 close FILEOUT unless $same_resource;
299 close $fh if defined $fh;
300  
301 do {
302 open FILEOUT, ">$file"
303 or die "Cannot open '$file' for writing: $!\n";
304 open $fh, "<$filename"
305 or die "Cannot read temp file '$filename':$!\n";
306 while (<$fh>) {
307 print FILEOUT or die "Cannot write into '$file': $!\n";
308 }
309 close FILEOUT;
310 } if $same_resource;
311  
312 };
313 if ($@) {
314 say "ERROR: $file_desc was (probably) not created.\n$@";
315 } else {
316 say "\n$file_desc created: '$file'\n";
317 }
318 }
319  
320 sub search_config_file {
321 my ( $file, $filters ) = @_;
322 my $current_section = "";
323 eval {
324 open FILETEMPL, "<$file"
325 or die "Cannot read file '$file': $!\n";
326  
327 while (<FILETEMPL>) {
328 if (/^\[(.*)\]\s*$/) {
329 $current_section = $1;
330 chomp($current_section);
331 next;
332 }
333 next unless defined $filters->{$current_section};
334 for my $key ( keys %{ $filters->{$current_section} } ) {
335 if (/\Q$key\E\s*\=/) {
336 $filters->{$current_section}->{$key} = 1;
337 }
338 }
339 }
340 close FILETEMPL or die "Cannot close '$file': $!\n";
341 };
342 return $filters;
343 }
344  
345 sub add_missing_keys_to_config_file {
346 my ( $file, $filters ) = @_;
347 my $current_section = "";
348 eval {
349 open FILETEMPL, "<$file"
350 or die "Cannot read file '$file': $!\n";
351 my ( $fh, $filename ) = tempfile( UNLINK => 1 );
352  
353 while (<FILETEMPL>) {
354 if (/^\[(.*)\]\s*$/) {
355 $current_section = $1;
356 chomp($current_section);
357 print $fh $_;
358 next;
359 }
360 ( print $fh $_ and next )
361 unless defined $filters->{$current_section};
362 for my $key ( keys %{ $filters->{$current_section} } ) {
363 if ( $filters->{$current_section}->{$key} == 0 ) {
364 print $fh "$key = some_fake_value\n";
365 delete $filters->{$current_section}->{$key};
366 }
367 }
368 print $fh $_;
369 }
370 close FILETEMPL or die "Cannot close '$file': $!\n";
371 close $fh;
372  
373 #now copy temp file to existing one
374 open FILEOUT, ">$file"
375 or die "Cannot open '$file' for writing: $!\n";
376 open $fh, "<$filename" or die "Cannot read temp file '$filename':$!\n";
377 while (<$fh>) {
378 print FILEOUT or die "Cannot write into '$file': $!\n";
379 }
380 close FILEOUT;
381 close $fh;
382 };
383 }
384  
385 sub prompt_moby_config_info {
386 my $username =
387 prompt_user_input( "What is your root mysql username?", "root" );
388 my $password =
389 prompt_user_input( "What is your root mysql users' password?", "" );
390 $password = "" unless $password;
391 my $url = prompt_user_input( "What is the mysql url?", "localhost" );
392 my $port = prompt_user_input( "What is the mysql port #?", "3306" );
393  
394 # prompt for a new user/password combo so that people cant determine the root password from MOBY::Config
395 my $moby_username = prompt_user_input( "What username (I will create it if it doesn't exist)\n would you like to access your local db?", "moby_user" );
396 my $moby_password = prompt_user_input( "Please provide a password for '$moby_username': ", "" );
397 $moby_password = "" unless $moby_password;
398 #db names
399 say
400 "\nPrompting for mysql table names. Use default names unless you know what you are doing!";
401 my $m_object =
402 prompt_user_input( "Table name for object ontology: ", "mobyobject" );
403 my $m_relationship =
404 prompt_user_input( "Table name for the relationship ontology: ",
405 "mobyrelationship" );
406 my $m_service =
407 prompt_user_input( "Table name for the service type ontology: ",
408 "mobyservice" );
409 my $m_namespace =
410 prompt_user_input( "Table name for the namespace ontology: ",
411 "mobynamespace" );
412 my $m_central = prompt_user_input( "Table name for the service ontology: ",
413 "mobycentral" );
414  
415 my $msg = <<EOF;
416
417 You have provided the following values:
418  
419 mySQL Details
420 username : $username
421 password : $password
422 URL : $url
423 port : $port
424 registry db username : $moby_username
425 registry db password : $moby_password
426
427 Tables:
428 Object ontology : $m_object
429 Relationshipt ontology : $m_relationship
430 Service ontology : $m_central
431 Namespace ontology : $m_namespace
432 Service type ontology : $m_service
433  
434 EOF
435 say($msg);
436  
437 return (
438 $username, $password, $url,
439 $port, $m_object, $m_relationship,
440 $m_service, $m_namespace, $m_central,
441 $moby_username, $moby_password
442 );
443 }
444  
445 sub fill_out_moby_conf {
446 my (
447 $file, $username, $password, $url,
448 $port, $m_object, $m_relationship, $m_service,
449 $m_namespace, $m_central
450 )
451 = @_;
452  
453 # load & fill the template file
454  
455 config_file_from_template(
456 $file,
457 File::ShareDir::dist_file(
458 'MOBY', 'config/mobycentral.config.template'
459 ),
460 'MOBY/SQL Configuration file',
461  
462 # mysql settings
463 {
464 'username' => $username,
465 'password' => $password,
466 'url' => $url,
467 'port' => $port,
468 },
469  
470 # db section mappings
471 {
472 'mobycentral' => $m_central,
473 'mobyobject' => $m_object,
474 'mobynamespace' => $m_namespace,
475 'mobyservice' => $m_service,
476 'mobyrelationship' => $m_relationship,
477 },
478 {},
479 {}
480 );
481  
482 # since the file contains the root DB password
483 # make it only readable by root
484 chmod 0644, $file;
485 die "Couldn't make MOBY config file readable only by root: $!"
486 if $?;
487  
488 # finished!
489 return;
490 }
491  
492 sub check_root {
493 # assume that windows has no security preventing the copying of files
494 return if MSWIN;
495 unless (getpwuid($<) eq 'root') {
496 print STDOUT <<EOT;
497  
498 Hmmm - you are not installing this as root. If you indicate any
499 system directories for the installation, such as '/usr/local', then
500 you do not have permission to install files there. If so, you should
501 cancel now with ^C, su to root, and restart.
502  
503 If you will not specify any system directories, you can proceed.
504  
505 EOT
506  
507 print STDOUT "Should I proceed? [n] ";
508 my $tmp = <STDIN>; $tmp =~ s/\s//g;
509 exit() unless $tmp =~ /y/i;
510 } else {
511 print STDOUT <<EOT;
512  
513 Take care! you are installing this as ** root **. Please take the normal
514 precautions that you would ordinarily take when installing software as root.
515 In particular, be careful with ownership, paths, and environment variables.
516  
517 EOT
518  
519 }
520 }
521  
522 # --- main ---
523 no warnings 'once';
524  
525 use Tie::File;
526 use MOBY::dbConfig;
527 use MOBY::Client::Central;
528 use File::Temp qw/ tempfile /;
529 use DBI;
530  
531 # give a waring if we are not running with su privelege
532 check_root();
533  
534 say "Installing a mobycentral registry ...\n";
535  
536 my $answer;
537  
538 my $apache_base = "";
539 my $apache_conf = "";
540 my $apache_cgi = "";
541 my $perl_exec = "/usr/bin/perl";
542  
543 # make sure perl exists
544 if (not -x $perl_exec) {
545 $perl_exec = '';
546 }
547  
548 # check for debian
549 if (-e "/etc/debian_version") {
550 $apache_base = "/var/lib";
551 $apache_conf = "/etc/apache2";
552 $apache_cgi = "/usr/lib/cgi-bin";
553 }
554  
555 my (
556 $username, $password, $url,
557 $port, $m_object, $m_relationship,
558 $m_service, $m_namespace, $m_central,
559 $moby_username, $moby_password
560 );
561  
562 say "We are going to be placing files into your apache setup, in the cgi-bin/ directory and conf/ directory";
563  
564 unless ($apache_base) {
565 say <<EOT;
566  
567 I am not able to determing defaults for your apache setup, perhaps you have a
568 custom install. Custom installs of have a base directory,
569 e.g. '/usr/local/apache' under which all the other directories are placed.
570  
571 EOT
572  
573 $apache_base = prompt_for_directory(
574 "What is the base installation path of apache?",
575 $apache_base );
576  
577 }
578  
579 $apache_conf ||= "$apache_base/conf";
580 $apache_conf = prompt_for_directory(
581 "What is the path of the apache 'conf' directory?",
582 $apache_conf );
583  
584 $apache_cgi ||= "$apache_base/cgi-bin";
585 $apache_cgi = prompt_for_directory(
586 "What is the path of the apache 'cgi-bin' directory?",
587 $apache_cgi );
588  
589 $perl_exec = prompt_user_input(
590 "What is the path to your perl executable?",
591 $perl_exec );
592 $perl_exec = "$perl_exec -w";
593  
594 say <<EOT;
595  
596 using $apache_base as base directory ...
597 using $apache_conf as conf directory ...
598 using $apache_cgi as cgi directory ...
599 using '#!$perl_exec' as header for perl cgi scripts ...
600  
601 EOT
602  
603 my $apache_info = <<EOT;
604 To run MOBY Central we will need to configure your MySQL DB, and install three
605 default variables into your apache webserver. If you are using Apache2 we will
606 place a config file in your conf.d/ directory, otherwise if you are still using
607 Apache1.3 we will need to modify your httpd.conf file.
608  
609 You will be prompted before any changes are made.
610  
611 EOT
612  
613 $answer = pprompt( "$apache_info\nShall we begin setting up apache? [n] ",
614 -ynd => 'n' );
615  
616 if ($answer eq 'y') {
617  
618 say "Configuring your MOBY/SQL config file ...";
619  
620 # mobycentral.config exists
621 if ( -e "$apache_conf/mobycentral.config" ) {
622 do {
623  
624 # copy mobycentral.config to conf directory
625 (
626 $username, $password, $url,
627 $port, $m_object, $m_relationship,
628 $m_service, $m_namespace, $m_central,
629 $moby_username, $moby_password
630 )
631 = prompt_moby_config_info;
632  
633 # TODO - collect the values and do this at the end
634 # fill out mobycentral.config
635 fill_out_moby_conf("$apache_conf/mobycentral.config",
636 $moby_username, $moby_password, $url,
637 $port, $m_object, $m_relationship,
638 $m_service, $m_namespace, $m_central
639 );
640  
641 }
642 if pprompt(
643 "Would you like to overwrite the file '$apache_conf/mobycentral.config'? [n] ",
644 -ynd => 'n'
645 ) eq 'y';
646 } else {
647 say " Installing the file '$apache_conf/mobycentral.config' ...";
648  
649 # fill out mobycentral.config
650 ( $username, $password, $url,
651 $port, $m_object, $m_relationship,
652 $m_service, $m_namespace, $m_central,
653 $moby_username, $moby_password
654 ) = prompt_moby_config_info;
655  
656 # TODO - collect the values and do this at the end
657 fill_out_moby_conf("$apache_conf/mobycentral.config",
658 $moby_username, $moby_password, $url,
659 $port, $m_object, $m_relationship,
660 $m_service, $m_namespace, $m_central
661 );
662 }
663  
664 #install MOBY-Central.pl and OntologyServer.cgi
665 say " Installing the MOBY-Central.pl & OntologyServer.cgi ...";
666 file_from_template(
667 "$apache_cgi/MOBY-Central.pl",
668 File::ShareDir::dist_file('MOBY', 'cgi/MOBY-Central.pl'),
669 'MOBY-Central Dispatch file',
670 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
671 );
672 file_from_template(
673 "$apache_cgi/OntologyServer.cgi",
674 File::ShareDir::dist_file('MOBY', 'cgi/OntologyServer.cgi'),
675 'MOBY-Central Ontology Server file',
676 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
677 );
678  
679 #install the moby-admin module
680 file_from_template(
681 "$apache_cgi/MOBY-Admin.pl",
682 File::ShareDir::dist_file('MOBY', 'cgi/MOBY-Admin.pl'),
683 'MOBY-Admin Dispatch file',
684 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
685 );
686  
687 #
688 # Configure Apache - check for Apache2 first
689 #
690  
691 say <<EOT;
692 I would like to add the following lines to your httpd.conf file:
693  
694 SetEnv MOBY_CENTRAL_CONFIG "$apache_conf/mobycentral.config"
695
696 SetEnv MOBY_URI "http://localhost/MOBY/Central"
697
698 SetEnv MOBY_SERVER "http://localhost/cgi-bin/MOBY-Central.pl"
699
700 SetEnv MOBY_ONTOLOGYSERVER "http://localhost/cgi-bin/OntologyServer.cgi"
701  
702 Of course, those lines may not be exactly right, so I will give you a chance to modify them!
703  
704 EOT
705  
706 # pass in the value w/o the SETENV part ... we will add that ourselves
707 my $env1 = "$apache_conf/mobycentral.config";
708 my $env2 = "http://localhost/MOBY/Central";
709 my $env3 = "http://localhost/cgi-bin/MOBY-Central.pl";
710 my $env4 = "http://localhost/cgi-bin/OntologyServer.cgi";
711  
712 say <<EOT;
713 Please review the following ensuring that [at least!] the domain name is correct!
714 EOT
715  
716 $env2 = prompt_user_input( "Enter a value for MOBY_URI: ", $env2 );
717 $env3 = prompt_user_input( "Enter a value for MOBY_SERVER: ", $env3 );
718 $env4 = prompt_user_input( "Enter a value for MOBY_ONTOLOGYSERVER: ",
719 $env4 );
720  
721  
722 if ( -d "$apache_conf/conf.d" ) {
723 say "Found Apache2 setup...";
724  
725 my $conf_file = "$apache_conf/conf.d/moby.conf";
726  
727 $answer = prompt("Going to create your Apache2 MOBY configuration at: $conf_file. Shall I proceed? [y] ", -ynd => 'y');
728  
729 if ($answer eq 'y') {
730 open( DAT, ">$conf_file" )
731 or die("Could not open $conf_file for reading!");
732  
733 print DAT <<EOT;
734 # Values added by moby-s-install.pl
735 SetEnv MOBY_CENTRAL_CONFIG "$env1"
736 SetEnv MOBY_URI "$env2"
737 SetEnv MOBY_SERVER "$env3"
738 SetEnv MOBY_ONTOLOGYSERVER "$env4"
739 EOT
740  
741 }
742 } elsif (not -e "$apache_conf/httpd.conf" ) {
743 die "Could not detect Apache2 setup ($apache_conf/conf.d/) or Apache1.3 ($apache_conf/httpd.conf). Please restart and enter the correct info";
744 } else {
745  
746 # configure httpd.conf create backup of httpd.conf and append the values
747 # to the begining of the file
748  
749 $answer = pprompt( "May I edit your httpd.conf file? [y] ",
750 -ynd => 'y' );
751 if ($answer eq 'y') {
752 open( DAT, "< $apache_conf/httpd.conf" )
753 || die("Could not open file for reading!");
754 my @raw_data = <DAT>;
755 close(DAT);
756  
757 open( DAT, "> $apache_conf/httpd.conf_backup" . time() )
758 || die("Trouble creating a backup of httpd.conf!\n$@");
759 print DAT @raw_data;
760 close(DAT);
761  
762 my %added = (
763 'DIRTY' => 0,
764 'MOBY_CENTRAL_CONFIG' => 0,
765 'MOBY_URI' => 0,
766 'MOBY_SERVER' => 0,
767 'MOBY_ONTOLOGYSERVER' => 0,
768 );
769 tie @raw_data, 'Tie::File', "$apache_conf/httpd.conf" or die "$@";
770  
771 for (@raw_data) {
772  
773 if (
774 /SETENV\s*(MOBY_CENTRAL_CONFIG|MOBY_URI|MOBY_SERVER|MOBY_ONTOLOGYSERVER)\s*.*/gi
775 )
776 {
777 $added{'DIRTY'}++;
778 if ( $1 eq "MOBY_CENTRAL_CONFIG" ) {
779 $_ = "SetEnv MOBY_CENTRAL_CONFIG \"$env1\"";
780 $added{$1} = 1;
781 } elsif ( $1 eq "MOBY_URI" ) {
782 $_ = "SetEnv MOBY_URI \"$env2\"";
783 $added{$1} = 1;
784 } elsif ( $1 eq "MOBY_SERVER" ) {
785 $_ = "SetEnv MOBY_SERVER \"$env3\"";
786 $added{$1} = 1;
787 } else {
788 $_ = "SetEnv MOBY_ONTOLOGYSERVER \"$env4\"";
789 $added{$1} = 1;
790 }
791 }
792 }
793 untie @raw_data;
794  
795 do {
796 open( DAT, ">> $apache_conf/httpd.conf" )
797 || die("Trouble updating httpd.conf!\n$@");
798 print DAT "\n# Values added by moby-s-install.pl\n"
799 if $added{'DIRTY'} == 0;
800 print DAT "SetEnv MOBY_CENTRAL_CONFIG \"$env1\"\n"
801 if $added{'MOBY_CENTRAL_CONFIG'} == 0;
802 print DAT "SetEnv MOBY_URI \"$env2\"\n"
803 if $added{'MOBY_URI'} == 0;
804 print DAT "SetEnv MOBY_SERVER \"$env3\"\n"
805 if $added{'MOBY_SERVER'} == 0;
806 print DAT "SetEnv MOBY_ONTOLOGYSERVER \"$env4\"\n"
807 if $added{'MOBY_ONTOLOGYSERVER'} == 0;
808 close(DAT);
809 } unless $added{'DIRTY'} == 4;
810  
811 say <<EOT;
812 Please do not forget to add the following ENV variables to your profile
813 so that they are always available when calling client BioMOBY API methods
814 from scripts!
815 MOBY_CENTRAL_CONFIG = "$env1"
816 MOBY_URI = "$env2"
817 MOBY_SERVER = "$env3"
818 MOBY_ONTOLOGYSERVER = "$env4"
819  
820 EOT
821 }
822 }
823 }
824  
825 $answer = pprompt( "Would you like to set up mySQL? [n] ", -ynd => 'n' );
826 if ($answer eq 'y') {
827  
828 my $ready_to_go = 0;
829 my $sql_error = 0;
830  
831 #check to see if we can call mysql ... if not, then die!
832 my $mysql_installed = (`mysql --version 2>&1` =~ m/^mysql\s+Ver\s+.*$/);
833 my $mysql_started = (`mysql -e "SHOW VARIABLES LIKE 'version'" 2>&1` !~ m/^ERROR 200.*Can't connect to .*$/);
834 print "mysql is installed ...\n"
835 if $mysql_installed;
836 print "mysql is started ...\n"
837 if $mysql_started;
838  
839 unless ($mysql_installed and $mysql_started) {
840 die <<ERROR;
841 MySQL does not seem to be accessible ...
842 Please ensure that the 'mysql' client program is in your path,
843 and that the msqyl-server is started and try again.
844  
845 ERROR
846 }
847  
848 # have the values been set already?
849 $ready_to_go = 1
850 if $username
851 and $url
852 and $port
853 and $m_object
854 and $m_relationship
855 and $m_service
856 and $m_namespace
857 and $m_central,
858 and $moby_username,
859 and ($moby_password || $moby_password eq "");
860  
861 # check to see if mobycentral.config has been created in the conf
862 # directory first -> if so, parse it
863  
864 if ( -e "$apache_conf/mobycentral.config"
865 && !( -d "$apache_conf/mobycentral.config" )
866 && !$ready_to_go )
867 {
868 open IN, "$apache_conf/mobycentral.config"
869 or die
870 "can't open MOBY Configuration file '$apache_conf/mobycentral.config' for unknown reasons: $!\n";
871  
872 my @sections = split /(\[\s*\S+\s*\][^\[]*)/s, join "", <IN>;
873 my %db_sections = ();
874 foreach my $section (@sections) {
875 my $dbConfig = MOBY::dbConfig->new( section => $section );
876 next unless $dbConfig;
877 my $dbname = $dbConfig->section_title;
878 next unless $dbname;
879 $db_sections{$dbname} = $dbConfig;
880 }
881  
882 $moby_username = $db_sections{mobycentral}->{username};
883 $moby_password = $db_sections{mobycentral}->{password} || "";
884 $url = $db_sections{mobycentral}->{url};
885 $port = $db_sections{mobycentral}->{port};
886 $m_object = $db_sections{mobyobject}->{dbname};
887 $m_relationship = $db_sections{mobyrelationship}->{dbname};
888 $m_service = $db_sections{mobyservice}->{dbname};
889 $m_namespace = $db_sections{mobynamespace}->{dbname};
890 $m_central = $db_sections{mobycentral}->{dbname};
891  
892 $ready_to_go = 1;
893 }
894  
895 # if the values havent been set, then prompt for them
896 unless ($ready_to_go) {
897  
898 say " Installing the file '$apache_conf/mobycentral.config' ...";
899  
900 # fill out mobycentral.config
901 ($username, $password, $url,
902 $port, $m_object, $m_relationship,
903 $m_service, $m_namespace, $m_central,
904 $moby_username, $moby_password
905 ) = prompt_moby_config_info;
906  
907 fill_out_moby_conf(
908 "$apache_conf/mobycentral.config",
909 $moby_username, $moby_password, $url,
910 $port, $m_object, $m_relationship,
911 $m_service, $m_namespace, $m_central
912 );
913 }
914  
915 # make sure that we have a root password/username
916 unless ($username and $password) {
917 $username =
918 prompt_user_input( "What is your root mysql username?", "root" );
919 $password =
920 prompt_user_input( "What is your root mysql users' password?", "" );
921 }
922 # now start creating the tables
923 say " creating the tables to use for the registry ...";
924 my %dbsections = (
925 'mobycentral' => $m_central,
926 'mobyobject' => $m_object,
927 'mobyservice' => $m_service,
928 'mobynamespace' => $m_namespace,
929 'mobyrelationship' => $m_relationship
930 );
931  
932 my $clone = 0;
933 my $central;
934 $answer = pprompt( "Would you like to clone a mobycentral registry? [n] ",
935 -ynd => 'n' );
936 if ($answer eq 'y') {
937 $clone = 1;
938 my %registries = (
939 default => {
940 url => "http://moby.ucalgary.ca/moby/MOBY-Central.pl",
941 uri => "http://moby.ucalgary.ca/MOBY/Central"
942 },
943 testing => {
944 url => "http://bioinfo.icapture.ubc.ca/cgi-bin/mobycentral/MOBY-Central.pl",
945 uri => "http://bioinfo.icapture.ubc.ca/MOBY/Central"
946 },
947 IRRI => {
948 url => "http://cropwiki.irri.org/cgi-bin/MOBY-Central.pl",
949 uri => "http://cropwiki.irri.org/MOBY/Central"
950 },
951  
952 # localhost => {
953 # url=>"http://localhost/cgi-bin/MOBY-Central.pl",
954 # uri=>"http://localhost/MOBY/Central"
955 # },
956 );
957 my $registry = pprompt( "What registry to use? [b] ",
958 -d => 'b',
959 -m => [ sort keys %registries ],
960 );
961  
962 $central = MOBY::Client::Central->new(
963 Registries => {
964 mobycentral => {
965 URL => $registries{$registry}{url},
966 URI => $registries{$registry}{uri}
967 }
968 }
969 );
970 }
971 my $error = 0;
972 if ($clone) {
973 say "Getting db dumps ...";
974 my ($mobycentral, $mobyobject, $mobyservice,
975 $mobynamespace, $mobyrelationship
976 ) = $central->MOBY::Client::Central::DUMP();
977 my $drh = DBI->install_driver("mysql");
978 # used for creating the user/pass for the registry
979 my $dbh = DBI->connect("DBI:mysql:mysql:localhost:3306", "$username", "$password", { RaiseError => 1, AutoCommit => 1 });
980 my ( $fh, $filename ) = tempfile( UNLINK => 1 );
981 say "Processing dump for service instances ...";
982 print $fh $mobycentral;
983 eval {
984 $drh->func( 'dropdb', $dbsections{mobycentral},
985 $url, $username, $password, 'admin' );
986 };
987 eval {
988 $drh->func( 'createdb', $dbsections{mobycentral},
989 $url, $username, $password, 'admin' );
990 };
991 eval {
992 my $db = $dbsections{mobycentral} . ".*";
993 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
994 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
995 };
996 die $! if $!;
997 system( "mysql -h $url -P $port -u $username --password=$password "
998 . $dbsections{mobycentral}
999 . "<$filename" ) == 0
1000 or ( say "Error populating service instance ontology ...\n$!"
1001 and $error++ );
1002  
1003 ( $fh, $filename ) = tempfile( UNLINK => 1 );
1004 say "Processing dump for the objects ontology ...";
1005 print $fh $mobyobject;
1006 eval {
1007 $drh->func( 'dropdb', $dbsections{mobyobject}, $url, $username,
1008 $password, 'admin' );
1009 };
1010 eval {
1011 $drh->func( 'createdb', $dbsections{mobyobject}, $url,
1012 $username, $password, 'admin' );
1013 };
1014 eval {
1015 my $db = $dbsections{mobyobject} . ".*";
1016 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
1017 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
1018 };
1019 system( "mysql -h $url -P $port -u $username --password=$password "
1020 . $dbsections{mobyobject}
1021 . "<$filename" ) == 0
1022 or
1023 ( say "Error populating objects ontology ...\n$!" and $error++ );
1024  
1025 ( $fh, $filename ) = tempfile( UNLINK => 1 );
1026 say "Processing dump for service types ...";
1027 print $fh $mobyservice;
1028 eval {
1029 $drh->func( 'dropdb', $dbsections{mobyservice},
1030 $url, $username, $password, 'admin' );
1031 };
1032 eval {
1033 $drh->func( 'createdb', $dbsections{mobyservice},
1034 $url, $username, $password, 'admin' );
1035 };
1036 eval {
1037 my $db = $dbsections{mobyservice} . ".*";
1038 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
1039 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
1040 };
1041 system( "mysql -h $url -P $port -u $username --password=$password "
1042 . $dbsections{mobyservice}
1043 . "<$filename" ) == 0
1044 or ( say "Error populating service types ontology ...\n$!"
1045 and $error++ );
1046  
1047 ( $fh, $filename ) = tempfile( UNLINK => 1 );
1048 say "Processing dump for the namespace ontology ...";
1049 print $fh $mobynamespace;
1050 eval {
1051 $drh->func( 'dropdb', $dbsections{mobynamespace},
1052 $url, $username, $password, 'admin' );
1053 };
1054 eval {
1055 $drh->func( 'createdb', $dbsections{mobynamespace},
1056 $url, $username, $password, 'admin' );
1057 };
1058 eval {
1059 my $db = $dbsections{mobynamespace} . ".*";
1060 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
1061 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
1062 };
1063 system( "mysql -h $url -P $port -u $username --password=$password "
1064 . $dbsections{mobynamespace}
1065 . "<$filename" ) == 0
1066 or ( say "Error populating namespace ontology ...\n$!"
1067 and $error++ );
1068  
1069 ( $fh, $filename ) = tempfile( UNLINK => 1 );
1070 say "Processing dump for the relationships ontology ...";
1071 print $fh $mobyrelationship;
1072 eval {
1073 $drh->func( 'dropdb', $dbsections{mobyrelationship},
1074 $url, $username, $password, 'admin' );
1075 };
1076 eval {
1077 $drh->func( 'createdb', $dbsections{mobyrelationship},
1078 $url, $username, $password, 'admin' );
1079 };
1080 eval {
1081 my $db = $dbsections{mobyrelationship} . ".*";
1082 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
1083 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
1084 };
1085 system( "mysql -h $url -P $port -u $username --password=$password "
1086 . $dbsections{mobyrelationship}
1087 . "<$filename" ) == 0
1088 or ( say "Error populating relationships ontology ...\n$!"
1089 and $error++ );
1090  
1091 } else {
1092  
1093 # no clone, so create minimalist databases
1094 my $drop_db = 0;
1095  
1096 #ask for permission on dropping data from db ...
1097 $answer = pprompt(
1098 "Shall I drop all pre-existing databases used by BioMOBY? [n] ",
1099 -ynd => 'n'
1100 );
1101 if ($answer eq 'y') {
1102 $drop_db = 1;
1103 }
1104  
1105 #process each db
1106 foreach my $section ( keys %dbsections ) {
1107 my $sqlfilepath = File::ShareDir::dist_file( 'MOBY',
1108 "db/schema/$section.mysql" );
1109 my $drh = DBI->install_driver("mysql");
1110 # used for creating the user/pass for the registry
1111 my $dbh = DBI->connect("DBI:mysql:mysql:localhost:3306", "root", "root", { RaiseError => 1, AutoCommit => 1 });
1112 # drop the db
1113 eval {
1114 $drh->func( 'dropdb', $dbsections{$section}, $url,
1115 $username, $password, 'admin' );
1116 } if $drop_db;
1117
1118 # create the db
1119 eval {
1120 $drh->func( 'createdb', $dbsections{$section}, $url,
1121 $username, $password, 'admin' );
1122 };
1123  
1124 # give moby_username permission to access it
1125 eval {
1126 my $db = $dbsections{$section} . ".*";
1127 $dbh->do(prepare_query($db),undef,$moby_username,$moby_password)
1128 || say ("Could not give '$moby_username' access to $db - Error:\n$DBI::errstr");
1129 };
1130 #create the tables in the db
1131 do {
1132 say "\n\tProblem creating tables in the db: $section: $!";
1133 }
1134 unless system(
1135 "mysql -h $url -P $port -u $username --password=$password "
1136 . $dbsections{$section}
1137 . "<$sqlfilepath" ) == 0;
1138 say "\tProcessing of $section completed ... ";
1139 }
1140 say "populating the tables with basic data ...";
1141 %dbsections = (
1142 'mobyobject' => $m_object,
1143 'mobyservice' => $m_service,
1144 'mobyrelationship' => $m_relationship
1145 );
1146 foreach my $section ( keys %dbsections ) {
1147 my $sqlfilepath =
1148 File::ShareDir::dist_file( 'MOBY', "db/data/$section.data" );
1149 system(
1150 "mysql -h $url -P $port -u $username --password=$password "
1151 . $dbsections{$section}
1152 . "<$sqlfilepath" ) == 0
1153 or (say "\n\tProblem populating the db: $section: $!" and $error++);
1154 say "\tPopulation processing for db $section completed ...";
1155 }
1156 }
1157  
1158 say "Set up of mySQL complete!" if $error == 0;
1159 say
1160 "There were some problems encountered. Please correct the errors and re-run this script!"
1161 if $error > 0;
1162 }
1163  
1164 $answer = pprompt( "Would you like to install the RESOURCES script? [y] ",
1165 -ynd => 'y' );
1166 if ($answer eq 'y') {
1167 my $exists = 0;
1168  
1169 # install the script, confirm if it exists
1170 if (-e "$apache_cgi/RESOURCES") {
1171 $exists = 1;
1172 $answer = pprompt( "The RESOURCES script already exists, overwrite? [n] ",
1173 -ynd => 'n' );
1174 if ($answer eq 'y') {
1175 $exists = 0;
1176 }
1177 }
1178  
1179 my $rdf_cache_location =
1180 prompt_for_directory_expand(
1181 "Where would you like to store the RDF cache?",
1182 "$apache_base/moby_cache" );
1183  
1184 say
1185 "Please make sure that you make that directory read/writable by your web server!\n";
1186  
1187 # copy the file
1188 if ($exists == 0) {
1189 file_from_template(
1190 "$apache_cgi/RESOURCES",
1191 File::ShareDir::dist_file( 'MOBY', 'cgi/RESOURCES' ),
1192 'RESOURCES script',
1193 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1194 )
1195 }
1196  
1197 # update mobycentral.config file to reflect the location of the script
1198 $answer = pprompt(
1199 "Shall we update mobycentral.config to reflect the installation of this script? [y] ",
1200 -ynd => 'y');
1201 if ($answer eq 'y') {
1202  
1203 # confirm server name, etc then change the values
1204 my $url = "http://localhost/cgi-bin";
1205 $url = prompt_user_input(
1206 "Please enter the correct url to your cgi-bin directory: ",
1207 "$url" );
1208  
1209 # make sure that the key resourceURL exists ...
1210 my $search = search_config_file(
1211 "$apache_conf/mobycentral.config",
1212 {
1213 'mobycentral' => {
1214 'resourceURL' => 0,
1215 'allResources' => 0,
1216 'rdf_cache' => 0,
1217 },
1218 'mobyobject' => { 'resourceURL' => 0 },
1219 'mobyservice' => { 'resourceURL' => 0 },
1220 'mobynamespace' => { 'resourceURL' => 0 },
1221 }
1222 );
1223 add_missing_keys_to_config_file( "$apache_conf/mobycentral.config",
1224 $search );
1225  
1226 # copy the information
1227 config_file_from_template(
1228 "$apache_conf/mobycentral.config",
1229 "$apache_conf/mobycentral.config",
1230 'MOBY/SQL Configuration file',
1231  
1232 # mysql settings
1233 { 'rdf_cache' => "$rdf_cache_location" },
1234  
1235 # db section mappings
1236 {},
1237  
1238 # resource urls
1239 {
1240 'ALL' => "$url/RESOURCES/MOBY-S/FULL",
1241 'mobycentral' => "$url/RESOURCES/MOBY-S/ServiceInstances",
1242 'mobyobject' => "$url/RESOURCES/MOBY-S/Objects",
1243 'mobynamespace' => "$url/RESOURCES/MOBY-S/Namespaces",
1244 'mobyservice' => "$url/RESOURCES/MOBY-S/Services",
1245 },
1246  
1247 # lsid info
1248 {}
1249 );
1250 }
1251 }
1252  
1253 $answer = pprompt( "Would you like to install the LSID authority script? [y] ",
1254 -ynd => 'y' );
1255 if ($answer eq 'y') {
1256  
1257 # install the script, confirm if it exists
1258 my $exists = 0;
1259 if (-e "$apache_cgi/authority.pl") {
1260 $exists = 1;
1261 $answer = pprompt( "The authority script already exists, overwrite? [n] ",
1262 -ynd => 'n' );
1263 if ($answer eq 'y') {
1264 $exists = 0;
1265 }
1266 }
1267  
1268 file_from_template(
1269 "$apache_cgi/authority.pl",
1270 File::ShareDir::dist_file( 'MOBY', 'cgi/authority.pl' ),
1271 'MOBY-Central LSID authority server file',
1272 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1273 )
1274 if $exists == 0;
1275  
1276 # update mobycentral.config file to reflect the particulars of the script
1277 $answer = pprompt(
1278 "Shall we update mobycentral.config to reflect the installation of this script? [y] ",
1279 -ynd => 'y');
1280 if ($answer eq 'y') {
1281  
1282 #ask for namespace/authority information
1283  
1284 # make sure that the key lsid_namespace & lsid_authority exists ...
1285 my $search = search_config_file(
1286 "$apache_conf/mobycentral.config",
1287 {
1288 'mobycentral' => {
1289 'lsid_namespace' => 0,
1290 'lsid_authority' => 0,
1291 },
1292 'mobyobject' => {
1293 'lsid_namespace' => 0,
1294 'lsid_authority' => 0,
1295 },
1296 'mobyservice' => {
1297 'lsid_namespace' => 0,
1298 'lsid_authority' => 0,
1299 },
1300 'mobynamespace' => {
1301 'lsid_namespace' => 0,
1302 'lsid_authority' => 0,
1303 },
1304 'mobyrelationship' => {
1305 'lsid_namespace' => 0,
1306 'lsid_authority' => 0,
1307 },
1308 }
1309 );
1310 add_missing_keys_to_config_file( "$apache_conf/mobycentral.config",
1311 $search );
1312  
1313 # copy the information
1314 config_file_from_template(
1315 "$apache_conf/mobycentral.config",
1316 "$apache_conf/mobycentral.config",
1317 'MOBY/SQL Configuration file',
1318  
1319 # mysql settings
1320 {},
1321  
1322 # db section mappings
1323 {},
1324  
1325 # resource urls
1326 {},
1327  
1328 # lsid info
1329 {
1330 'mobycentral' => {
1331 'NAMESPACE' => prompt_user_input(
1332 "Please enter an LSID namespace for the services ontology: ",
1333 "serviceinstance"
1334 ),
1335 'AUTHORITY' => prompt_user_input(
1336 "Please enter the LSID authority for services: ",
1337 "biomoby.org"
1338 ),
1339 },
1340 'mobyobject' => {
1341 'NAMESPACE' => prompt_user_input(
1342 "Please enter an LSID namespace for the datatype ontology: ",
1343 "objectclass"
1344 ),
1345 'AUTHORITY' => prompt_user_input(
1346 "Please enter the LSID authority for datatypes: ",
1347 "biomoby.org"
1348 ),
1349 },
1350 'mobyservice' => {
1351 'NAMESPACE' => prompt_user_input(
1352 "Please enter an LSID namespace for the service types ontology: ",
1353 "servicetype"
1354 ),
1355 'AUTHORITY' => prompt_user_input(
1356 "Please enter the LSID authority for service types: ",
1357 "biomoby.org"
1358 ),
1359 },
1360 'mobynamespace' => {
1361 'NAMESPACE' => prompt_user_input(
1362 "Please enter an LSID namespace for the namespaces ontology: ",
1363 "namespacetype"
1364 ),
1365 'AUTHORITY' => prompt_user_input(
1366 "Please enter the LSID authority for namespaces: ",
1367 "biomoby.org"
1368 ),
1369 },
1370 'mobyrelationship' => {
1371 'NAMESPACE' => prompt_user_input(
1372 "Please enter an LSID namespace for the relationship ontology: ",
1373 "relationshiptype"
1374 ),
1375 'AUTHORITY' => prompt_user_input(
1376 "Please enter the LSID authority for relationships: ",
1377 "biomoby.org"
1378 ),
1379 },
1380 }
1381 );
1382 }
1383
1384 # tell the user to make sure that apache serves the script from
1385 # http://domain.com/authority
1386  
1387 say <<EOT;
1388 Please ensure that you set up apache to serve the script from:
1389  
1390 'http://your.domain/authority'
1391  
1392 so that the script will work properly!
1393 EOT
1394 }
1395  
1396 $answer = pprompt(
1397 "Would you like to auxillary scripts? These include the service pinger, unit tester, a test page for the rdf agent, an RDF generator page, etc? [y] ",
1398 -ynd => 'y'
1399 );
1400 if ($answer eq 'y') {
1401  
1402 # prompt for a location for the service_tester_path
1403 my $service_tester_path =
1404 prompt_for_directory_expand(
1405 "Where would you like to place the service pinger script?",
1406 "$apache_base/moby_tester" );
1407 say
1408 'Please make sure that you make that directory read/writable by your web server!\n';
1409  
1410 # make sure that the key service_tester_path exists for the config file
1411 my $search = search_config_file(
1412 "$apache_conf/mobycentral.config",
1413 { 'mobycentral' => { 'service_tester_path' => 0, } }
1414 );
1415 add_missing_keys_to_config_file( "$apache_conf/mobycentral.config",
1416 $search );
1417  
1418 # copy the information
1419 config_file_from_template(
1420 "$apache_conf/mobycentral.config",
1421 "$apache_conf/mobycentral.config",
1422 'MOBY/SQL Configuration file',
1423  
1424 # mysql settings
1425 { 'service_tester_path' => "$service_tester_path" },
1426  
1427 # db section mappings
1428 {},
1429  
1430 # resource urls
1431 {},
1432  
1433 # lsid info
1434 {}
1435 );
1436  
1437 file_from_template(
1438 "$service_tester_path/service_tester.pl",
1439 File::ShareDir::dist_file(
1440 'MOBY', 'cgi/service_tester.pl'
1441 ),
1442 'MOBY-Central service tester script',
1443 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1444 );
1445 say
1446 'Please don\'t forget to place the service pinger on a cron!';
1447  
1448 # copy the service unit tester script
1449 file_from_template(
1450 "$service_tester_path/service_unit_tester.pl",
1451 File::ShareDir::dist_file(
1452 'MOBY', 'cgi/service_unit_tester.pl'
1453 ),
1454 'MOBY-Central service unit tester script',
1455 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1456 );
1457 say
1458 'Please don\'t forget to place the service unit tester on a cron!';
1459  
1460 #copy the other scripts now
1461 file_from_template(
1462 "$apache_cgi/AgentRDFValidator",
1463 File::ShareDir::dist_file(
1464 'MOBY', 'cgi/AgentRDFValidator'
1465 ),
1466 'The RDF agent validator page',
1467 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1468 );
1469 file_from_template(
1470 "$apache_cgi/GenerateRDF.cgi",
1471 File::ShareDir::dist_file(
1472 'MOBY', 'cgi/GenerateRDF.cgi'
1473 ),
1474 'MOBY-Central service instance RDF generating form',
1475 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1476 );
1477 file_from_template(
1478 "$apache_cgi/Moby",
1479 File::ShareDir::dist_file( 'MOBY', 'cgi/Moby' ),
1480 'MOBY-Central test page for auxillary scripts',
1481 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1482 );
1483 file_from_template(
1484 "$apache_cgi/BioMobyWSDL",
1485 File::ShareDir::dist_file( 'MOBY', 'cgi/BioMobyWSDL' ),
1486 'MOBY-Central RESTful WSDL generator',
1487 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1488 );
1489 file_from_template(
1490 "$apache_cgi/ServicePingerValidator",
1491 File::ShareDir::dist_file(
1492 'MOBY', 'cgi/ServicePingerValidator'
1493 ),
1494 'MOBY-Central service invocation test form',
1495 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1496 );
1497 file_from_template(
1498 "$apache_cgi/ValidateService",
1499 File::ShareDir::dist_file(
1500 'MOBY', 'cgi/ValidateService'
1501 ),
1502 'MOBY-Central service tester information page',
1503 { '#!/usr/bin/perl -w' => "#!$perl_exec", }
1504 );
1505 }
1506 #
1507  
1508 say
1509 "Please remember to set up the RDF agent! Just restart apache and your registry has been set up!\n\nDone.";
1510  
1511 package SimplePrompt;
1512  
1513 use vars qw/ $Terminal /;
1514  
1515 sub prompt {
1516 my ( $msg, $flags, $others ) = @_;
1517  
1518 # simple prompt
1519 return get_input($msg)
1520 unless $flags;
1521  
1522 $flags =~ s/^-//o; # ignore leading dash
1523  
1524 # 'waiting for yes/no' prompt, possibly with a default value
1525 if ( $flags =~ /^yn(d)?/i ) {
1526 return yes_no( $msg, $others );
1527 }
1528  
1529 # prompt with a menu of possible answers
1530 if ( $flags =~ /^m/i ) {
1531 return menu( $msg, $others );
1532 }
1533  
1534 # default: again a simple prompt
1535 return get_input($msg);
1536 }
1537  
1538 sub yes_no {
1539 my ( $msg, $default_answer ) = @_;
1540 while (1) {
1541 my $answer = get_input($msg);
1542 return $default_answer if $default_answer and $answer =~ /^\s*$/o;
1543 return 'y' if $answer =~ /^(1|y|yes|ano)$/;
1544 return 'n' if $answer =~ /^(0|n|no|ne)$/;
1545 }
1546 }
1547  
1548 sub get_input {
1549 my ($msg) = @_;
1550 local $^W = 0;
1551 my $line = $Terminal->readline($msg);
1552 chomp $line; # remove newline
1553 $line =~ s/^\s*//;
1554 $line =~ s/\s*$//; # trim whitespaces
1555 $Terminal->addhistory($line) if $line;
1556 return $line;
1557 }
1558  
1559 sub menu {
1560 my ( $msg, $ra_menu ) = @_;
1561 my @data = @$ra_menu;
1562  
1563 my $count = @data;
1564  
1565 # die "Too many -menu items" if $count > 26;
1566 # die "Too few -menu items" if $count < 1;
1567  
1568 my $max_char = chr( ord('a') + $count - 1 );
1569 my $menu = '';
1570  
1571 my $next = 'a';
1572 foreach my $item (@data) {
1573 $menu .= ' ' . $next++ . '.' . $item . "\n";
1574 }
1575 while (1) {
1576 print STDOUT $msg . "\n$menu";
1577 my $answer = get_input(">");
1578  
1579 # blank and escape answer accepted as undef
1580 return undef if $answer =~ /^\s*$/o;
1581 return undef
1582 if length $answer == 1 && $answer eq "\e";
1583  
1584 # invalid answer not accepted
1585 if ( length $answer > 1 || ( $answer lt 'a' || $answer gt $max_char ) )
1586 {
1587 print STDOUT "(Please enter a-$max_char)\n";
1588 next;
1589 }
1590  
1591 # valid answer
1592 return $data[ ord($answer) - ord('a') ];
1593 }
1594 }
1595  
1596 __END__