]> git.koha-community.org Git - koha.git/blob - koha.upgrade
Finished modularizing installer.pl
[koha.git] / koha.upgrade
1 #!/usr/bin/perl -w
2
3 #use diagnostics;
4 use strict; # please develop with the strict pragma
5
6
7 if ($<) {
8     print "\n\nYou must run koha.upgrade as root.\n\n";
9     exit;
10 }
11
12 my $input;
13
14 my %configfile;
15
16 open (KC, "/etc/koha.conf");
17 while (<KC>) {
18  chomp;
19  (next) if (/^\s*#/);
20  if (/(.*)\s*=\s*(.*)/) {
21    my $variable=$1;
22    my $value=$2;
23    # Clean up white space at beginning and end
24    $variable=~s/^\s*//g;
25    $variable=~s/\s*$//g;
26    $value=~s/^\s*//g;
27    $value=~s/\s*$//g;
28    $configfile{$variable}=$value;
29  }
30 }
31
32 my $intranetdir=$configfile{'intranetdir'};
33 my $opacdir=$configfile{'opacdir'};
34 my $kohaversion=$configfile{'kohaversion'};
35 my $kohalogdir=$configfile{'kohalogdir'};
36 my $database=$configfile{'database'};
37 my $hostname=$configfile{'hostname'};
38 my $user=$configfile{'user'};
39 my $pass=$configfile{'pass'};
40
41
42 ($kohaversion) || ($kohaversion='unknown version');
43 my $newversion=`cat koha.version`;
44 chomp $newversion;
45 if ($newversion =~ /RC/) {
46     print qq|
47 =====================
48 = RELEASE CANDIDATE =
49 =====================
50
51 WARNING WARNING WARNING WARNING WARNING
52
53 You are about to install Koha version $newversion.  This version of Koha is a
54 release candidate.  It is not intended to be installed on production systems.
55 It is being released so that users can test it before we release a final
56 version.
57
58 |;
59     print "Are you sure you want to install Koha $newversion? (Y/[N]): ";
60
61     my $answer = <STDIN>;
62     chomp $answer;
63
64     if ($answer eq "Y" || $answer eq "y") {
65         print "Great! continuing setup... \n";
66     } else {
67         print qq|
68
69 Watch for announcements of Koha releases on the Koha mailing list or the Koha
70 web site (http://www.koha.org/).
71
72 |;
73         exit;
74     };
75 }
76
77 print qq|
78
79 ================
80 = Koha Upgrade =
81 ================
82
83 You are attempting to upgrade from Koha $kohaversion to $newversion.
84
85 We recommend that you do a complete backup of all your files before upgrading.
86 This upgrade script will make a backup copy of your files for you.
87
88 Would you like to proceed?  ([Y]/N):  
89 |;
90
91 my $answer = <STDIN>;
92 chomp $answer;
93
94 if ($answer eq "Y" || $answer eq "y") {
95         print "Great! continuing upgrade... \n";
96     } else {
97     print qq|
98
99 Aborting.  Please re-run koha.upgrade when you are ready to upgrade Koha.
100 |;
101     exit;
102 };
103
104
105
106 #
107 # Test for Perl and Modules
108 #
109 print qq|
110
111 PERL & MODULES
112 ==============
113
114 |;
115
116 print "\nChecking perl modules ...\n";
117     unless (eval "require 5.004") {
118     die "Sorry, you need at least Perl 5.004\n";
119 }
120
121 my @missing = ();
122 unless (eval {require DBI})               { push @missing,"DBI" };
123 unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
124 unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
125 unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
126 unless (eval {require Net::Z3950})        { 
127     print qq|
128
129 The Net::Z3950 module is missing.  This module is necessary if you want to use
130 Koha's Z39.50 client to download bibliographic records from other libraries.
131 To install this module, you will need the yaz client installed from
132 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
133 command:
134
135 perl -MCPAN -e 'install Net::Z3950'
136
137 Press the <ENTER> key to continue:
138 |;
139     <STDIN>;
140 }
141
142 #
143 # Print out a list of any missing modules
144 #
145 if (@missing > 0) {
146     print "\n\n";
147     print "You are missing some Perl modules which are required by Koha.\n";
148     print "Once these modules have been installed, rerun this installer.\n";
149     print "They can be installed by running (as root) the following:\n";
150     foreach my $module (@missing) {
151         print "   perl -MCPAN -e 'install \"$module\"'\n";
152         exit(1);
153     }} else{
154     print "All modules appear to be installed, continuing...\n";
155 };
156
157
158
159 my $backupdir='/usr/local/koha/backups';
160 print "Please specify a backup directory [$backupdir]: ";  
161
162 $answer = <STDIN>;
163 chomp $answer;
164
165 if ($answer) {
166     $backupdir=$answer;
167 }
168
169 if (! -e $backupdir) {
170    my $result=mkdir ($backupdir, oct(770));
171    if ($result==0) {
172        my @dirs = split(m#/#, $backupdir);
173         my $checkdir='';
174         foreach (@dirs) {
175             $checkdir.="$_/";
176             unless (-e "$checkdir") {
177                 mkdir($checkdir, 0775);
178             }
179         }
180    }
181 }
182
183 chmod 0770, $backupdir;
184
185 # Backup MySql database
186 #
187 #
188 my $mysql;
189 my $mysqldir;
190
191 foreach my $mysql (qw(/usr/local/mysql
192                       /opt/mysql
193                     )) {
194     if ( -d $mysql ) {
195             $mysqldir=$mysql;
196     }
197 }
198 if (!$mysqldir){
199     $mysqldir='/usr';
200 }
201
202
203
204 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
205 $month++;
206 $year+=1900;
207 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
208
209 open (MD, "$mysqldir/bin/mysqldump --user=$user --password=$pass --host=$hostname $database|");
210
211 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
212
213 my $itemcounter=0;
214 my $bibliocounter=0;
215 my $biblioitemcounter=0;
216 my $membercounter=0;
217
218 while (<MD>) {
219     (/insert into items /i) && ($itemcounter++);
220     (/insert into biblioitems /i) && ($biblioitemcounter++);
221     (/insert into biblio /i) && ($bibliocounter++);
222     (/insert into borrowers /i) && ($membercounter++);
223     print BF $_;
224 }
225
226 close BF;
227 close MD;
228
229
230 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
231 chomp $filels;
232 printf qq|
233
234 Backed up:
235
236 %6d biblio entries
237 %6d biblioitems entries
238 %6d items entries
239 %6d borrowers
240
241 $filels
242
243 Does this look right? ([Y]/N):
244 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
245
246 $answer = <STDIN>;
247 chomp $answer;
248
249 if ($answer=~/^n/i) {
250     print qq|
251
252 Aborting.  The database dump is located in:
253
254         $backupdir/Koha.backup_$date
255
256 |;
257     exit;
258 } else {
259         print "Great! continuing upgrade... \n";
260 };
261
262
263
264 if ($opacdir && $intranetdir) {
265     print qq|
266
267 I believe that your old files are located in:
268
269   OPAC:      $opacdir
270   INTRANET:  $intranetdir
271
272
273 Does this look right?  ([Y]/N):
274 |;
275     $answer = <STDIN>;
276     chomp $answer;
277
278     if ($answer =~/n/i) {
279         $intranetdir='';
280         $opacdir='';
281     } else {
282         print "Great! continuing upgrade... \n";
283     }
284 }
285
286
287 if (!$opacdir || !$intranetdir) {
288     $intranetdir='';
289     $opacdir='';
290     while (!$intranetdir) {
291         print "Please specify the location of your INTRANET files: ";  
292
293         $answer = <STDIN>;
294         chomp $answer;
295
296         if ($answer) {
297             $intranetdir=$answer;
298         }
299         if (! -e "$intranetdir/htdocs") {
300             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
301             $intranetdir='';
302         }
303     }
304     while (!$opacdir) {
305         print "Please specify the location of your OPAC files: ";  
306
307         $answer = <STDIN>;
308         chomp $answer;
309
310         if ($answer) {
311             $opacdir=$answer;
312         }
313         if (! -e "$opacdir/htdocs") {
314             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
315             $opacdir='';
316         }
317     }
318 }
319
320
321
322 print "\n\nBacking up old Koha scripts...\n";
323 print     "===============================\n\n";
324
325 mkdir "$backupdir/kohafiles-$date", 0770;
326 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
327 mkdir "$backupdir/kohafiles-$date/opac", 0770;
328
329 my $result=system("cp -R $intranetdir/* $backupdir/kohafiles-$date/intranet/");
330 if ($result) {
331     print "Error encounted when copying $intranetdir to $backupdir/kohafiles-$date/intranet/\n";
332     exit;
333 } else {
334     system("rm -rf $intranetdir/*");
335 }
336 $result=system("cp -R $opacdir/* $backupdir/kohafiles-$date/opac/");
337 if ($result) {
338     print "Error encounted when copying $opacdir to $backupdir/kohafiles-$date/opac/\n";
339     exit;
340 } else {
341     system("rm -rf $opacdir/*");
342 }
343
344    print "Creating $intranetdir/htdocs...\n";
345    mkdir ("$intranetdir/htdocs", oct(750));
346    print "Creating $intranetdir/cgi-bin...\n";
347    mkdir ("$intranetdir/cgi-bin", oct(750));
348    print "Creating $intranetdir/modules...\n";
349    mkdir ("$intranetdir/modules", oct(750));
350    print "Creating $intranetdir/scripts...\n";
351    mkdir ("$intranetdir/scripts", oct(750));
352    chmod (oct(770), "$opacdir");
353    print "Creating $opacdir/htdocs...\n";
354    mkdir ("$opacdir/htdocs", oct(750));
355    print "Creating $opacdir/cgi-bin...\n";
356    mkdir ("$opacdir/cgi-bin", oct(750));
357
358 my $httpduser;
359 my $realhttpdconf;
360
361 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
362                       /usr/local/etc/apache/httpd.conf
363                       /usr/local/etc/apache/apache.conf
364                       /var/www/conf/httpd.conf
365                       /etc/apache/conf/httpd.conf
366                       /etc/apache/conf/apache.conf
367                       /etc/apache-ssl/conf/apache.conf
368                       /etc/httpd/conf/httpd.conf
369                       /etc/httpd/httpd.conf)) {
370    if ( -f $httpdconf ) {
371             $realhttpdconf=$httpdconf;
372             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
373       while (<HTTPDCONF>) {
374          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
375             $httpduser = $1;
376          }
377       }
378       close(HTTPDCONF);
379    }
380 }
381
382 unless ($httpduser) {
383     print qq|
384 I was not able to determine the user that Apache is running as.  This
385 information is necessary in order to set the access privileges correctly on
386 /etc/koha.conf.  This user should be set in one of the Apache configuration
387 files using the "User" directive.
388 |;
389     print "What is your Apache user? ";
390     chomp($input = <STDIN>);
391
392     if ($input) {
393         $httpduser = $input;
394     } else {
395         $httpduser='Undetermined';
396     }
397 }
398
399 print "\n\nINSTALLING KOHA...\n";
400 print "\n\n==================\n";
401 print "Copying internet-html files to $intranetdir/htdocs...\n";
402 system("cp -R intranet-html/* $intranetdir/htdocs/");
403 print "Copying intranet-cgi files to $intranetdir/cgi-bin...\n";
404 system("cp -R intranet-cgi/* $intranetdir/cgi-bin/");
405 print "Copying script files to $intranetdir/scripts...\n";
406 system("cp -R scripts/* $intranetdir/scripts/");
407 print "Copying module files to $intranetdir/modules...\n";
408 system("cp -R modules/* $intranetdir/modules/");
409 print "Copying opac-html files to $opacdir/htdocs...\n";
410 system("cp -R opac-html/* $opacdir/htdocs/");
411 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
412 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
413
414 system("chown -R root.$httpduser $opacdir");
415 system("chown -R root.$httpduser $intranetdir");
416
417 # LAUNCH SCRIPT
418 print "Modifying Z39.50 daemon launch script...\n";
419 my $newfile='';
420 open (L, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh");
421 while (<L>) {
422     if (/^RunAsUser=/) {
423         $newfile.="RunAsUser=$httpduser\n";
424     } elsif (/^KohaZ3950Dir=/) {
425         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
426     } else {
427         $newfile.=$_;
428     }
429 }
430 close L;
431 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh.orig");
432 open L, ">$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
433 print L $newfile;
434 close L;
435
436 unless ($kohalogdir && -e $kohalogdir) {
437     $kohalogdir='/var/log/koha';
438     print "\n\nDirectory for logging by Z39.50 daemon [$kohalogdir]: ";
439     chomp($input = <STDIN>);
440     if ($input) {
441         $kohalogdir=$input;
442     }
443 }
444
445 unless (-e "$kohalogdir") {
446     my $result = mkdir 0770, "$kohalogdir"; 
447     if ($result==0) {
448         my @dirs = split(m#/#, $kohalogdir);
449         my $checkdir='';
450         foreach (@dirs) {
451             $checkdir.="$_/";
452             unless (-e "$checkdir") {
453                 mkdir($checkdir, 0775);
454             }
455         }
456     }
457 }
458
459 # SHELL SCRIPT
460 print "Modifying Z39.50 daemon wrapper script...\n";
461 $newfile='';
462 open (S, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh");
463 while (<S>) {
464     if (/^KohaModuleDir=/) {
465         $newfile.="KohaModuleDir=$intranetdir/modules\n";
466     } elsif (/^KohaZ3950Dir=/) {
467         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
468     } elsif (/^LogDir=/) {
469         $newfile.="LogDir=$kohalogdir\n";
470     } else {
471         $newfile.=$_;
472     }
473 }
474 close S;
475
476 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh.orig");
477 open S, ">$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
478 print S $newfile;
479 close S;
480 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
481 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
482 chmod 0750, "$intranetdir/scripts/z3950daemon/processz3950queue";
483 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
484 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $intranetdir/scripts/z3950daemon/processz3950queue: $!";
485
486
487 open (KC, "/etc/koha.conf");
488 my $kccontents='';
489 my $kc;
490 while (<KC>) {
491     if (/^\s*includes\s*=/) {
492         $kccontents.="includes=$intranetdir/htdocs/includes\n";
493         $kc->{'includes'}=1;
494     } elsif (/^\s*httpduser\s*=/) {
495         $kccontents.="httpduser=$httpduser\n";
496         $kc->{'httpduser'}=1;
497     } elsif (/^\s*kohaversion\s*=/) {
498         $kccontents.="kohaversion=$newversion\n";
499         $kc->{'kohaversion'}=1;
500     } elsif (/^\s*kohalogdir\s*=/) {
501         $kccontents.="kohalogdir=$kohalogdir\n";
502         $kc->{'kohalogdir'}=1;
503     } elsif (/^\s*intranetdir\s*=/) {
504         $kccontents.="intranetdir=$intranetdir\n";
505         $kc->{'intranetdir'}=1;
506     } elsif (/^\s*opacdir\s*=/) {
507         $kccontents.="opacdir=$opacdir\n";
508         $kc->{'opacdir'}=1;
509     } else {
510         $kccontents.="$_";
511     }
512 }
513
514 unless (defined($kc->{'kohaversion'})) {
515     $kccontents.="kohaversion=$newversion\n";
516 }
517 unless (defined($kc->{'includes'})) {
518     $kccontents.="includes=$intranetdir/htdocs/includes\n";
519 }
520 unless (defined($kc->{'httpduser'})) {
521     $kccontents.="httpduser=$httpduser\n";
522 }
523 unless (defined($kc->{'intranetdir'})) {
524     $kccontents.="intranetdir=$intranetdir\n";
525 }
526 unless (defined($kc->{'opacdir'})) {
527     $kccontents.="opacdir=$opacdir\n";
528 }
529 unless (defined($kc->{'kohalogdir'})) {
530     $kccontents.="kohalogdir=$kohalogdir\n";
531 }
532
533
534 system("mv /etc/koha.conf /etc/koha.conf.backup");
535
536 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
537 print KC $kccontents;
538 close KC;
539
540
541 print qq|
542
543 Upgrading Database
544 ==================
545 |;
546 system ("perl -I $intranetdir/modules scripts/updater/updatedatabase");
547
548
549 print qq|
550
551 ==================
552 = Authentication =
553 ==================
554
555 This release of Koha has a new authentication module.  If you are not already
556 using basic authentication on your intranet, you will be required to log in to
557 access some of the features of the intranet.  You can log in using the userid
558 and password from the /etc/koha.conf configuration file at any time.  Use the
559 "Members" module to add passwords for other accounts and set their permissions.
560
561 [NOTE PERMISSIONS ARE NOT COMPLETED AS OF 1.2.3RC1.  Do not give passwords to
562  any patrons unless you want them to have full access to your intranet.]
563 |;
564 print "Press the <ENTER> key to continue: ";
565 <STDIN>;
566
567
568
569 print qq|
570
571 ==========================
572 = Koha Upgrade Completed =
573 ==========================
574 The Koha Upgrade is finished.  If you are upgrading from a version of Koha
575 prior to 1.2.1, it is likely that you will have to modify your Apache
576 configuration to point it to the new files.
577
578 In your INTRANET VirtualHost section you should have:
579   DocumentRoot $intranetdir/htdocs
580   ScriptAlias /cgi-bin/koha/ $intranetdir/cgi-bin/
581   SetEnv PERL5LIB $intranetdir/modules
582
583 In the OPAC VirtualHost section you should have:
584   DocumentRoot $opacdir/htdocs
585   ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
586   SetEnv PERL5LIB $intranetdir/modules
587
588 You may also need to uncomment a "LoadModules env_module ... " line and restart
589 Apache.
590
591 Please report any problems you encounter through http://bugs.koha.org/
592 |;