-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathww_install.pl
2315 lines (2099 loc) · 77.3 KB
/
ww_install.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
###############################################################################
#WeBWorK Installation Script
#
#Goals:
#(1) interactively install webwork on any machine on which the prerequisites are installed
#(2) do as much as possible for the user, finding paths, writing config files, etc.
#(3) never use anything other than core perl modules, webwork modules, webwork prerequisite modules
#(4) eventually add options for --nointeractive, --with-svn, other?
#
#How it works
#(1) check if running as root
#(2) Have you downloaded webwork already?
#--if so, where is webwork2/, pg/, NationalProblemLibrary/?
#--if not, do you want me to get the software for you via svn?
#(3) check prerequisites, using this opportunity to populate %externalPrograms hash, and gather
#environment information: $server_userID, $server_groupID, hostname?, timezone?
#(4) Initially ask user minimum set of config questions:
#-Directory root PREFIX
#--accept standard webwork layout below PREFIX? (later)
#-$server_root_url = ""; # e.g. http://webwork.yourschool.edu (default from hostname lookup in (2))
#-$server_userID = ""; # e.g. www-data (default from httpd.conf lookup in (2))
#-$server_groupID = ""; # e.g. wwdata (default from httpd.conf lookup in (2))
#-$mail{smtpServer} = 'mail.yourschool.edu';
#-$mail{smtpSender} = '[email protected]';
#-$mail{smtpTimeout} = 30;
#-database root password
#-$database_dsn = "dbi:mysql:webwork";
#-$database_username = "webworkWrite";
#-$database_password = "";
#-$siteDefaults{timezone} = "America/New_York";
#(5) Put software in correct locations
#(6) use gathered information to write initial global.conf file, webwork.apache2-config,database.conf,
#wwapache2ctl,
#(7) check and fix filesystem permissions in webwork2/ tree
#(8) Create initial database user, initial mysql tables
#(9) Create admin course
#(10) append include statement to httpd.conf to pick up webwork.apache2-config
#(11) restart apache, check for errors
#(12) Do some testing!
#############################################################################
use strict;
use warnings;
use lib 'lib';
use Config;
use File::Path qw(make_path);
use File::Spec;
use File::Copy;
#use File::CheckTree;
use Cwd;
use IPC::Cmd qw(can_run run);
use Sys::Hostname;
use User::pwent;
use List::Util qw(max);
use DateTime::TimeZone; #non-core!
use DBI;
use Term::UI;
use Term::ReadLine;
use Term::ReadPassword #to be found in lib/
#use Term::ReadKey;
#use Data::Dumper;
###############################################################################################
# Create a new Term::Readline object for interactivity
#Don't worry people with spurious warnings.
###############################################################################################
$Term::UI::VERBOSE = 0;
my $term = Term::ReadLine->new('');
#########################################################################################
#
# Defaults - each of these values is passed as a default to some config question
#
########################################################################################
use constant WEBWORK2_REPO => 'https://github.com/openwebwork/webwork2.git';
use constant PG_REPO => 'https://github.com/openwebwork/pg.git';
use constant OPL_REPO =>
'https://github.com/openwebwork/webwork-open-problem-library.git';
use constant MATHJAX_REPO => 'https://github.com/mathjax/MathJax.git';
use constant WW_PREFIX => '/opt/webwork/';
use constant ROOT_URL => 'http://localhost';
use constant WW_URL => '/webwork2';
use constant SMTP_SERVER => 'localhost';
use constant SMTP_SENDER => 'webwork@localhost';
use constant WW_DB => 'webwork';
use constant WWDB_USER => 'webworkWrite';
#######################################################################################
#
# Constants that control behavior of the script
#
# ####################################################################################
use constant IPC_CMD_TIMEOUT =>
6000; #Sets maximum time system commands will be allowed to run
use constant IPC_CMD_VERBOSE => 1; #Controls whether all output of a command
#should be printed to STDOUT/STDERR
#########################################################################################
#
# Prerequisites - keep in sync with webwork2/bin/check_modules.pl
# - right now we die if these aren't present, but later we'll offer
# to intall missing prereqs
#
########################################################################################
my @apacheBinaries = qw(
apache2
apachectl
);
my @applicationsList = qw(
mv
cp
rm
mkdir
tar
gzip
latex
pdflatex
dvipng
mysql
giftopnm
ppmtopgm
pnmtops
pnmtopng
pngtopnm
lwp-request
mysql
mysqldump
svn
git
);
my @apache1ModulesList = qw(
Apache
Apache::Constants
Apache::Cookie
Apache::Log
Apache::Request
);
my @apache2ModulesList = qw(
Apache2::Request
Apache2::Cookie
Apache2::ServerRec
Apache2::ServerUtil
);
my @modulesList = qw(
Benchmark
Carp
CGI
Data::Dumper
Data::UUID
Date::Format
Date::Parse
DateTime
DBD::mysql
DBI
Digest::MD5
Email::Address
Errno
Exception::Class
File::Copy
File::Find
File::Find::Rule
File::Path
File::Spec
File::stat
File::Temp
GD
Getopt::Long
Getopt::Std
HTML::Entities
HTML::Scrubber
HTML::Tagset
HTML::Template
IO::File
Iterator
Iterator::Util
JSON
Locale::Maketext::Lexicon
Locale::Maketext::Simple
Mail::Sender
MIME::Base64
Net::IP
Net::LDAPS
Net::OAuth
Net::SMTP
Opcode
PadWalker
PHP::Serialization
Pod::Usage
Pod::WSDL
Safe
Scalar::Util
SOAP::Lite
Socket
SQL::Abstract
String::ShellQuote
Text::CSV
Text::Wrap
Tie::IxHash
Time::HiRes
Time::Zone
URI::Escape
UUID::Tiny
XML::Parser
XML::Parser::EasyTree
XML::Writer
XMLRPC::Lite
);
####################################################################################################
#
# Platform specific data - these data structures are to help with identifying our platform and
# eventually will be used for specifying prerequiste packages, likely locations of binaries we can't find
# with can_run(), and doing other platform specific processing. Such as
# (1) Disabling SELinux on RH/Fedora
# Others....
# ##################################################################################################
my %release_files = (
'gentoo-release' => 'gentoo',
'fedora-release' => 'fedora',
'centos-release' => 'centos',
'enterprise-release' => 'oracle enterprise linux',
'turbolinux-release' => 'turbolinux',
'mandrake-release' => 'mandrake',
'mandrakelinux-release' => 'mandrakelinux',
'debian_version' => 'debian',
'debian_release' => 'debian',
'SuSE-release' => 'suse',
'knoppix-version' => 'knoppix',
'yellowdog-release' => 'yellowdog',
'slackware-version' => 'slackware',
'slackware-release' => 'slackware',
'redflag-release' => 'redflag',
'redhat-release' => 'redhat',
'redhat_version' => 'redhat',
'conectiva-release' => 'conectiva',
'immunix-release' => 'immunix',
'tinysofa-release' => 'tinysofa',
'trustix-release' => 'trustix',
'adamantix_version' => 'adamantix',
'yoper-release' => 'yoper',
'arch-release' => 'arch',
'libranet_version' => 'libranet',
'va-release' => 'va-linux',
'pardus-release' => 'pardus',
);
my %version_match = (
'gentoo' => 'Gentoo Base System release (.*)',
'debian' => '(.+)',
'suse' => 'VERSION = (.*)',
'fedora' => 'Fedora(?: Core)? release (\d+) \(',
'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
'redhat' => 'Red Hat(?: Enterprise)? Linux(?: Server)? release (.*) \(',
'oracle enterprise linux' => 'Enterprise Linux Server release (.+) \(',
'slackware' => '^Slackware (.+)$',
'pardus' => '^Pardus (.+)$',
'centos' => '^CentOS(?: Linux)? release (.+)(?:\s\(Final\))',
'scientific' => '^Scientific Linux release (.+) \(',
);
#Apache 2.2 locations for various operating systems
#From http://wiki.apache.org/httpd/DistrosDefaultLayout
#Note that the above url may not contain current information
#double checking it with the docs for your favorite distro would
#be helpful
my $apache22Layouts = {
httpd22 => { #Apache 2.2 default layout
MPMDir => 'server/mpm/prefork',
ServerRoot => '/usr/local/apache2',
DocumentRoot => '/usr/local/apache2/htdocs',
ConfigFile => '/usr/local/apache2/conf/httpd.conf',
OtherConfig => '/usr/local/apache2/conf/extra',
SSLConfig => '/usr/local/apache2/conf/extra/httpd-ssl.conf',
ErrorLog => '/usr/local/apache2/logs/error_log',
AccessLog => '/usr/local/apache2/logs/access_log',
ctl => '/usr/local/apache2/bin/apachectl',
User => '',
Group => '',
},
debian => { #Checked 7.1 (mostly)
MPMDir => 'server/mpm/prefork',
ServerRoot => '/etc/apache2',
DocumentRoot => '/var/www',
ConfigFile => '/etc/apache2/apache2.conf',
OtherConfig => '/etc/apache2/conf.d',
SSLConfig => '',
Modules => '/etc/apache2/mods_enabled',
ErrorLog => '/var/log/apache2/error.log',
AccessLog => '/var/log/access.log',
Binary => '/usr/sbin/apache2ctl',
User => 'www-data',
Group => 'www-data',
},
ubuntu => { #Checked 12.04
MPMDir => 'server/mpm/prefork',
ServerRoot => '/etc/apache2',
DocumentRoot => '/var/www',
ConfigFile => '/etc/apache2/apache2.conf',
OtherConfig => '/etc/apache2/conf.d',
SSLConfig => '',
Modules => '/etc/apache2/mods_enabled',
ErrorLog => '/var/log/apache2/error.log',
AccessLog => '/var/log/access.log',
Binary => '/usr/sbin/apache2ctl',
User => 'www-data',
Group => 'www-data',
},
rhel => { #And Fedora Core, CentOS...checked Fedora 17, CentOS 6
MPMDir => 'server/mpm/prefork',
ServerRoot => '/etc/httpd',
DocumentRoot => '/var/www/html',
ConfigFile => '/etc/httpd/conf/httpd.conf',
OtherConfig => '/etc/httpd/conf.d',
SSLConfig => '',
Modules => '/etc/httpd/modules', #symlink
ErrorLog => '/var/log/httpd/error_log',
AccessLog => '/var/log/httpd/access_log',
Binary => '/usr/sbin/apachectl',
User => 'apache',
Group => 'apache',
},
freebsd => { #Checked on freebsd 8.2
MPMDir => '',
ServerRoot => '/usr/local',
DocumentRoot => '/usr/local/www/apache22/data',
ConfigFile => '/usr/local/etc/apache22/httpd.conf',
OtherConfig => '/usr/local/etc/apache22/extra',
SSLConfig => '/usr/local/etc/apache22/extra/httpd-ssl.conf',
Modules => '',
ErrorLog => '/var/log/httpd-error.log',
AccessLog => '/var/log/httpd-access.log',
Binary => '/usr/sbin/apachectl',
User => 'www',
Group => 'www',
},
osx => { #Checked on OSX 10.7
MPMDir => 'server/mpm/prefork',
ServerRoot => '/usr',
DocumentRoot => '/Library/WebServer/Documents',
ConfigFile => '/etc/apache2/httpd.conf',
OtherConfig => '/etc/apache2/extra',
SSLConfig => '/etc/apache2/extra/httpd-ssl.conf',
Modules => '/usr/libexec/apache2',
ErrorLog => '/var/log/apache2/error_log',
AccessLog => '/var/log/apache2/access_log',
Binary => '/usr/sbin/apachectl',
User => '_www',
Group => '_www',
},
suse => {
MPMDir => '',
ServerRoot => '/srv/www',
DocumentRoot => '/srv/www/htdocs',
ConfigFile => '/etc/apache2/httpd.conf',
OtherConfig => '/etc/sysconfig/apache2',
SSLConfig => '/etc/apache2/ssl-global.conf',
ErrorLog => '/var/log/apache2/httpd-error.log',
AccessLog => '/var/log/apache2/httpd-access.log',
Binary => '/usr/sbin/apachectl',
User => 'wwwrun',
Group => 'www',
},
};
my %linux = (
'DISTRIB_ID' => '',
'DISTRIB_RELEASE' => '',
'DISTRIB_CODENAME' => '',
'DISTRIB_DESCRIPTION' => '',
'release_file' => '',
'pattern' => ''
);
sub get_os {
my $os;
if ( $^O eq "darwin" ) {
$os->{type} = "unix";
$os->{name} = "darwin";
chomp( $os->{version} = `sw_vers -productVersion` );
chomp( $os->{arch} = `uname -p` );
print
"Great, you're on Mac running OS X $$os{version} on $$os{arch} hardware.\n";
} elsif ( $^O eq "freebsd" ) {
$os->{type} = "unix";
$os->{name} = "freebsd";
chomp( $os->{version} = `uname -r` );
chomp( $os->{arch} = `uname -p` );
print
"I see - rocking it on FreeBSD $$os{version} on $$os{arch} hardware.\n";
} elsif ( $^O eq "linux" )
{ #Now we're going to have to look more closely to get specific distro
$os->{type} = "linux";
$os->{name} = distribution_name();
$os->{version} = distribution_version();
chomp( $os->{arch} = `uname -p` );
print "Linux, yay! That's my favorite. I see you're running $$os{name} "
. "version $$os{version} on $$os{arch} hardware. This will be easy.\n";
} elsif ( $^O eq "MSWin32" ) {
print
"Installing WeBWorK on Windows is, afaik, currently impossible.\n";
die "If you get it working, please let us know. Good luck!";
} else {
$os->{type} = $^O;
$os->{name} = distribution_name();
$os->{version} = distribution_version();
chomp( $os->{arch} = `uname -p` );
print
"I see you're running $$os{name} version $$os{version} on $$os{arch} hardware. "
. "This script mostly finds the information it needs at run time. But, there are a few "
. "places where we have hard coded OS specific details into data structures built into the "
. "script. We don't have anything for your OS. The script will very likely still work. Either "
. "way, it would be very helpful if you could send a report to webwork\@maa.org. We can help you "
. "get it working if it doesn't work, and if it does we would like to add it to the list of supported "
. "systems.\n";
}
return $os;
}
#Subroutines to find linux distribution and version
sub distribution_name {
my $release_files_directory = '/etc';
my $standard_release_file = 'lsb-release';
my $distro = get_lsb_info();
if ($distro) {
return $distro if ($distro);
}
foreach (qw(enterprise-release fedora-release)) {
if ( -f "$release_files_directory/$_"
&& !-l "$release_files_directory/$_" )
{
if ( -f "$release_files_directory/$_"
&& !-l "$release_files_directory/$_" )
{
$linux{'DISTRIB_ID'} = $release_files{$_};
$linux{'release_file'} = $_;
return $linux{'DISTRIB_ID'};
}
}
}
foreach ( keys %release_files ) {
if ( -f "$release_files_directory/$_"
&& !-l "$release_files_directory/$_" )
{
if ( -f "$release_files_directory/$_"
&& !-l "$release_files_directory/$_" )
{
if ( $release_files{$_} eq 'redhat' ) {
foreach my $rhel_deriv ( 'centos', 'scientific', ) {
$linux{'pattern'} = $version_match{$rhel_deriv};
$linux{'release_file'} = 'redhat-release';
my $file_info = get_file_info();
if ($file_info) {
$linux{'DISTRIB_ID'} = $rhel_deriv;
$linux{'release_file'} = $_;
return $linux{'DISTRIB_ID'};
}
}
$linux{'pattern'} = '';
}
$linux{'release_file'} = $_;
$linux{'DISTRIB_ID'} = $release_files{$_};
return $linux{'DISTRIB_ID'};
}
}
}
undef;
}
sub distribution_version {
my $release_files_directory = '/etc';
my $standard_release_file = 'lsb-release';
my $release;
return $release if ( $release = get_lsb_info('DISTRIB_RELEASE') );
if ( !$linux{'DISTRIB_ID'} ) {
distribution_name() or die 'No version because no distro.';
}
$linux{'pattern'} = $version_match{ $linux{'DISTRIB_ID'} };
$release = get_file_info();
$linux{'DISTRIB_RELEASE'} = $release;
return $release;
}
sub get_lsb_info {
my $field = shift || 'DISTRIB_ID';
my $release_files_directory = '/etc';
my $standard_release_file = 'lsb-release';
my $tmp = $linux{'release_file'};
if ( -r "$release_files_directory/" . $standard_release_file ) {
$linux{'release_file'} = $standard_release_file;
$linux{'pattern'} = $field . '=(.+)';
my $info = get_file_info();
if ($info) {
$linux{$field} = $info;
return $info;
}
}
$linux{'release_file'} = $tmp;
$linux{'pattern'} = '';
undef;
}
sub get_file_info {
my $release_files_directory = '/etc';
my $standard_release_file = 'lsb-release';
open my $fh, '<', "$release_files_directory/" . $linux{'release_file'}
or die 'Cannot open file: '
. $release_files_directory . '/'
. $linux{'release_file'};
my $info = '';
local $_;
while (<$fh>) {
chomp $_;
($info) = $_ =~ m/$linux{'pattern'}/;
return "\L$info" if $info;
}
undef;
}
#End of linux-finding subroutines.
sub get_existing_users {
my $envir = shift;
my $passwd_file = $envir->{passwd_file};
my $users;
open( my $in, '<', $passwd_file );
while (<$in>) {
push @$users, ( split( ':', $_ ) )[0];
}
close($in);
return $users;
}
sub get_existing_groups {
my $envir = shift;
my $group_file = $envir->{group_file};
my $groups;
open( my $in, '<', $group_file );
while (<$in>) {
push @$groups, ( split( ':', $_ ) )[0];
}
return $groups;
}
sub user_exists {
my ( $envir, $user ) = @_;
my %users = map { $_ => 1 } @{ $envir->{existing_users} };
return 1 if $users{$user};
}
sub group_exists {
my ( $envir, $group ) = @_;
my %groups = map { $_ => 1 } @{ $envir->{existing_groups} };
return 1 if $groups{$group};
}
############################################################################################
#
# Script Util Subroutines: The script is based on Term::Readline to interact with user
#
###########################################################################################
#For confirming answers
sub confirm_answer {
my $answer = shift;
my $confirm = $term->get_reply(
print_me => "Ok, you entered: $answer. Please confirm.",
prompt => "Well? ",
choices => [ "Looks good.", "Change my answer.", "Quit." ],
default => "Looks good."
);
if ( $confirm eq "Quit." ) {
die "Exiting...";
} elsif ( $confirm eq "Change my answer." ) {
return 0;
} else {
return 1;
}
}
sub get_reply {
my ($print_me,$prompt,$choices,$default) = @_;
my $answer = $term->get_reply(
print_me => $print_me,
prompt => $prompt,
choices => $choices,
default => $default,
);
my $confirmed = confirm_answer($answer);
if($confirmed) {
return $answer;
} else {
get_reply($print_me,$prompt,$choices,$default);
}
}
#####################################################
#
# Check if user is ready to install
#
# ###################################################
sub get_ready {
my $print_me = <<EOF;
Welcome to the WeBWorK. This installation script will ask you a few questions and then attempt to install
WeBWorK on your system. To complete the installation
(a) You must be connected to the internet.
(b) You must have administrative privliges on this machine, and
(c) The mysql server must be running, and you should have already gone through the process of setting up the
root mysql account and securing your mysql server. If you haven't done this or aren't sure if it has been done
then exit this script and do
'sudo service mysqld start' to start mysql, and then
'mysql_secure_installation' to secure the server and set the root password.
Once you know the root mysql password then you can come back to this script and install webwork.
EOF
my $ready = $term->ask_yn(
print_me => $print_me,
prompt => 'Ready to install WeBWorK?',
default => 'y',
);
die "Come back soon!" unless $ready;
}
####################################################################
#
# Check if the user is root
#
# We probably need to be root. The effective user id of the user running the script
# is held in the perl special variable $>. In particular,
# if $> = 0 user is root, works with sudo too.
####################################################################
sub check_root {
if ( $> == 0 ) {
print "Running as root....\n";
return 1;
} else {
#my $term = Term::ReadLine->new('');
my $print_me = <<EOF;
IMPORTANT: I see that you are not running this script as root. Some elevated system
privliges are needed to install WeBWorK. If you run the script without sufficient privilges then
it will fail in ways that might be hard to track down.
EOF
my $prefix = $term->ask_yn(
print_me => $print_me,
prompt =>
'Are you sure you\'re running the script with the privilges you\'ll need to complete the installation?',
default => 'n',
);
}
}
sub get_wwadmin_user {
my $envir = shift;
my $print_me = <<END;
###########################################################################
#
# The first decision is whether or not to create a webwork admin user.
# The purpose ofdoing this would be to allow the webwork admin user to
# edit the webwork system code (e.g. for updates) while restricting
# that user from having write access to other files outside of his
# home directory. If the system administrator(s) and webwork
# application maintainer(s) are the same then there is no need to
# create this user.
#
# If the maintainer of webwork (the webwork administrator) is not the
# system administrator for the server, then you as a user with root
# access must decide how to give the webwork admin sufficient privileges
# to maintain webwork. One approach, depending on your OS, is to give
# that user root access by either addiing that user to the wheel group
# or the sudoers file. This gives the webwork administrator complete
# access to the system, but if that's fine with you then it's fine with me too.
#
# The approach offered here is to create a webwork admin user and group.
# All of the webwork code will be owned by this user. The only webwork
# files that will be writable by other users are those that must be
# writable by the webserver. For those files and directories we will
# create a new webwork data group to which we will add the
# webwork admin user and the webserver.
#
# If you choose not to create this user, the webwork code will be owned
# by root. We will still give you the opportunity to create the webwork
# data group. Members of this group will be able to do limited webwork
# maintenance tasks, such as creating courses from the command line and
# managing logs, but will not be able edit webwork system files or other
# files that are writable by the webserver.
#
##########################################################################
END
my $answer = undef;
my $confirmed = undef;
my $exists = undef;
my $ww_admin = undef;
my $prompt = "Shall I create a webwork admin user?";
# $answer = $term->get_reply(
# print_me => $print_me,
# prompt => $prompt,
# choices => [
# "Yes, let's do it",
# "No, the root user will administer webwork",
# "No, a separate webwork admin user already exists"
# ],
# default => "Yes, let's do it",
# );
$answer = "no";
#has this been confirmed?
#$confirmed = confirm_answer($answer);
if ( $answer eq "Yes, let's do it" && $confirmed ) {
$ww_admin = create_wwadmin_user($envir);
return $ww_admin if $ww_admin;
get_wwadmin_user($envir); #Try again if not
} else
{
print
"Sounds good. We will not create a separate webwork admin user. The root user will administer webwork.\n";
return "root";
}
}
sub create_wwadmin_user {
my $envir = shift;
my $wwadmin = $term->get_reply(
print_me => "You chose to create a webwork admin user.",
prompt => "Please enter a username for the webwork admin user.",
default => "wwadmin",
);
my $exists_already = user_exists( $envir, $wwadmin );
if ($exists_already) {
print "Sorry, that user already exists. Try again.\n";
get_wwadmin_user($envir);
} else {
my $wwadmin_pw = $term->get_reply(
prompt =>
"Please enter an initial password for the webwork admin user.",
default => "wwadmin",
);
my $wwadmin_shell = $term->get_reply(
prompt =>
"Please enter a default shell for the webwork admin user.",
default => $ENV{SHELL},
);
my $confirm = $term->ask_yn(
prompt =>
"Shall I create a webwork admin user with username $wwadmin, initial password"
. " $wwadmin_pw and default shell $wwadmin_shell?",
default => 'y',
);
if ($confirm) {
#useradd -s /usr/bin/bash -c "WeBWorK Administrator" -p $wwadmin_pw $wwadmin
#TODO: FreeBSD, MacOSX don't have useradd!!!
my $full_path = can_run("useradd");
my $cmd = [
$full_path, '-s',
$wwadmin_shell, '-c',
"WeBWorK Administrator", '-p',
$wwadmin_pw, $wwadmin
];
my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf )
= run(
command => $cmd,
verbose => IPC_CMD_VERBOSE,
timeout => IPC_CMD_TIMEOUT
);
if ($success) {
print
"Created webwork admin user $wwadmin with initial password $wwadmin_pw and default shell $wwadmin_shell.\n";
return $wwadmin;
} else {
print "There was an error creating $wwadmin: $error_message \n";
get_wwadmin_user($envir);
}
} else {
print "Let's try again.\n";
get_wwadmin_user($envir);
}
}
}
sub get_wwdata_group {
my ( $envir, $apache, $wwadmin ) = @_;
my $print_me = <<END;
#############################################################################
# Certain data directories need to be writable by the webserver. These
# are the webwork2/DATA, webwork2/htdocs/tmp, webwork2/logs, webwork2/tmp,
# and the courses/ directories.
#
# It can convenient to give WeBWorK system administrators access to these
# directories as well, so they can permform administrative tasks such as
# removing temporary files, creating and editing courses from the command
# line, managing logs, and so on.
#
# While it can be convenient to allow the WeBWorK administrator(s) to
# manipulate the webwork files writable by the webserver, we may not want
# to give him or her rights to manipulate other files writable by the
# webserver.
#
# We can accomplish this by adding a new webwork data group to the system
# containing any WeBWorK administrators and also containing the web server.
# We will then recursively give the directories listed above permissions
# g+sw and files in those directories g+w.
#
# If you choose not to create the webwork data group, the webwork
# directories listed above will be put into the same group as the
# webserver. We will then recursively give those directories permissions
# g+sw and the files in those directories permissions g+w.
#
###########################################################################
END
my $answer = undef;
my $confirmed = undef;
my $exists = undef;
my $group = undef;
my $prompt = "Shall I create a webwork data group? ";
# $answer = $term->get_reply(
# print_me => $print_me,
# prompt => $prompt,
# choices => [
# "Yes, let's do it",
# "No, we'll just use the webserver's group",
# "No, a separate webwork data group already exists"
# ],
# default => "Yes, let's do it",
# );
#
$answer = "no";
#has this been confirmed?
#$confirmed = confirm_answer($answer);
if ( $answer eq "Yes, let's do it" && $confirmed ) {
$group = create_wwdata_group( $envir, $apache, $wwadmin );
return $group if $group;
get_wwdata_group( $envir, $apache, $wwadmin ); #Try again if not
} else
{
print
"Sounds good. We will not create a separate webwork data group. Instead we'lljust use the webserver's group.\n";
return $apache->{group};
}
}
#Create webwork data group and add webserver and wwadmin (if user is not root)
sub create_wwdata_group {
my ( $envir, $apache, $wwadmin ) = @_;
my $group = $term->get_reply(
print_me => "You chose to create a webwork data group.",
prompt => "What would you like to call the group?",
default => "wwdata",
);
#does this group exist?
my $already_exists = group_exists( $envir, $group );
if ($already_exists) {
print "Oops - that group already exists. Let's try this again.\n";
get_wwdata_group( $envir, $apache, $wwadmin ); #Try again if not
} else {
#group doesn't exist so now confirm answer
my $confirmed = confirm_answer($group);
copy( "/etc/group", "/etc/group.bak" );
open( my $in, "<", "/etc/group.bak" )
or die "Can't open /etc/group.bak for reading: $!";
open( my $out, ">", "/etc/group" )
or die "Can't open /etc/group for writing: $!";
my @gids;
while (<$in>) {
push @gids, ( split( ':', $_ ) )[2];
print $out $_;
}
my $new_gid = max(@gids) + 1;
if ( $wwadmin eq 'root' ) {
print $out "$group:*:$new_gid:" . $apache->{user} . "\n";
} else {
print $out "$group:*:$new_gid:" . $apache->{user} . ",$wwadmin\n";
}
return $group;
}
}
##############################################################
#
# Adjust file owernship and permissions
#
#############################################################
#change_grp($server_groupid, $webwork_courses_dir, "$webwork_dir/DATA", "$webwork_dir/htdocs/tmp", "$webwork_dir/logs", "$webwork_dir/tmp");
#change_permissions($server_groupid, "$webwork_courses_dir", "$webwork_dir/DATA", "$webwork_dir/htdocs/tmp", "$webwork_dir/logs", "$webwork_dir/tmp");
#chgrp -R wwdata DATA ../courses htdocs/tmp logs tmp
# chmod -R g+w DATA ../courses htdocs/tmp logs tmp
# find DATA/ ../courses/ htdocs/tmp logs/ tmp/ -type d -a ! -name CVS -exec chmod g+s {}
sub change_owner {
my $owner = shift;
my @dirs = @_;
my $full_path = can_run('chown');
my $cmd = [ $full_path, '-R', $owner, @dirs ];
my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = run(
command => $cmd,
verbose => IPC_CMD_VERBOSE,
timeout => IPC_CMD_TIMEOUT
);
if ($success) {
print "Changed ownership of @dirs and below to $owner.\n";
} else {
warn
"There was an error changing ownership of @dirs to $owner: $error_message.\n";
}
}
# chmod -R g+w DATA ../courses htdocs/tmp logs tmp
# find DATA/ ../courses/ htdocs/tmp logs/ tmp/ -type d -a ! -name CVS -exec chmod g+s {}
sub change_data_dir_permissions {
my $gid = shift;
my @dirs = @_;
# my ( $gid, $courses, $data, $htdocs_tmp, $logs, $tmp ) = @_;
my $chmod = can_run('chmod');
my $cmd =
[ $chmod, '-R', 'g+w', @dirs ];
my (
$chmod_success, $chmod_error_message, $chmod_full_buf,
$chmod_stdout_buf, $chmod_stderr_buf
)
= run(
command => $cmd,
verbose => IPC_CMD_VERBOSE,
timeout => IPC_CMD_TIMEOUT
);
if ($chmod_success) {
print
"Made the directories \n ". join("\n ", @dirs) . "\n group writable.\n";
} else {
warn
"Could not make the directories group writable: $chmod_error_message\n";
}
my $find = can_run('find');
$cmd = [
$find, @dirs,
'-type', 'd', '-and', '!', '(', '-name',
'".git"', '-prune', ')', '-exec', $chmod, 'g+s',
'{}', ';'
];
my (
$find_success, $find_error_message, $find_full_buf,
$find_stdout_buf, $find_stderr_buf
)
= run(
command => $cmd,
verbose => IPC_CMD_VERBOSE,
timeout => IPC_CMD_TIMEOUT
);
if ($find_success) {
print
"Added group sticky bit to \n ". join("\n ", @dirs) . "\n and subdirectories (except .git's).\n";
} else {
warn "Error. Could not add sticky bit: $find_error_message\n";
}
}
####################################################################
#
# Environment Data
#
# What use is this information?
# - any reason to get the hostname?
# - maybe warn against perl versions that are too old; version specific perl bugs?
# - maybe process timezone separately?
####################################################################