Requirements.pm 22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
16
#                 Marc Schumann <wurblzap@gmail.com>
17 18 19 20 21 22 23 24 25 26 27

package Bugzilla::Install::Requirements;

# NOTE: This package MUST NOT "use" any Bugzilla modules other than
# Bugzilla::Constants, anywhere. We may "use" standard perl modules.
#
# Subroutines may "require" and "import" from modules, but they
# MUST NOT "use."

use strict;

28
use List::Util qw(max);
29
use POSIX ();
30
use Safe;
31

32 33 34
use base qw(Exporter);
our @EXPORT = qw(
    REQUIRED_MODULES
35
    OPTIONAL_MODULES
36

37
    check_requirements
38
    check_graphviz
39
    display_version_and_os
40
    have_vers
41
    vers_cmp
42 43 44 45
    install_command
);

use Bugzilla::Constants;
46

47 48 49
# The below two constants are subroutines so that they can implement
# a hook. Other than that they are actually constants.

50 51 52 53
# "package" is the perl package we're checking for. "module" is the name
# of the actual module we load with "require" to see if the package is
# installed or not. "version" is the version we need, or 0 if we'll accept
# any version.
54 55 56 57
#
# "blacklist" is an arrayref of regular expressions that describe versions that
# are 'blacklisted'--that is, even if the version is high enough, Bugzilla
# will refuse to say that it's OK to run with that version.
58 59
sub REQUIRED_MODULES {
    my @modules = (
60
    {
61 62
        package => 'CGI',
        module  => 'CGI',
63 64 65
        version => '2.93'
    },
    {
66 67
        package => 'TimeDate',
        module  => 'Date::Format',
68 69 70
        version => '2.21'
    },
    {
71 72
        package => 'DBI',
        module  => 'DBI',
73
        version => '1.41'
74 75
    },
    {
76 77
        package => 'PathTools',
        module  => 'File::Spec',
78 79 80
        version => '0.84'
    },
    {
81 82
        package => 'Template-Toolkit',
        module  => 'Template',
83
        version => '2.12'
84 85
    },
    {
86 87
        package => 'MIME-Base64',
        module  => 'MIME::Base64',
88 89 90
        version => '3.01'
    },
    {
91
        package => 'MIME-tools',
92
        # MIME::Parser is packaged as MIME::Tools on ActiveState Perl
93
        module  => ON_WINDOWS ? 'MIME::Tools' : 'MIME::Parser',
94 95
        version => '5.406'
    },
96 97 98 99 100 101 102 103 104 105 106
    {
        package => 'Email-Send',
        module  => 'Email::Send',
        version => ON_WINDOWS ? '2.16' : '2.00'
    },
    {
        # This will pull in Email::MIME for us, also. 
        package => 'Email-MIME-Modifier',
        module  => 'Email::MIME::Modifier',
        version => 0
    },
107 108 109 110 111 112
    );

    my $all_modules = _get_extension_requirements(
        'REQUIRED_MODULES', \@modules);
    return $all_modules;
};
113

114 115
sub OPTIONAL_MODULES {
    my @modules = (
116
    {
117 118 119 120
        package => 'GD',
        module  => 'GD',
        version => '1.20',
        feature => 'Graphical Reports, New Charts, Old Charts'
121
    },
122
    {
123
        package => 'Template-GD',
124 125
        # This module tells us whether or not Template-GD is installed
        # on Template-Toolkits after 2.14, and still works with 2.14 and lower.
126 127 128
        module  => 'Template::Plugin::GD::Image',
        version => 0,
        feature => 'Graphical Reports'
129
    },
130
    {
131 132 133 134
        package => 'Chart',
        module  => 'Chart::Base',
        version => '1.0',
        feature => 'New Charts, Old Charts'
135 136
    },
    {
137 138 139 140
        package => 'GDGraph',
        module  => 'GD::Graph',
        version => 0,
        feature => 'Graphical Reports'
141 142
    },
    { 
143 144 145 146
        package => 'GDTextUtil',
        module  => 'GD::Text',
        version => 0,
        feature => 'Graphical Reports'
147 148
    },
    {
149 150 151 152
        package => 'XML-Twig',
        module  => 'XML::Twig',
        version => 0,
        feature => 'Move Bugs Between Installations'
153 154
    },
    {
155 156 157 158
        package => 'libwww-perl',
        module  => 'LWP::UserAgent',
        version => 0,
        feature => 'Automatic Update Notifications'
159 160
    },
    {
161 162 163 164
        package => 'PatchReader',
        module  => 'PatchReader',
        version => '0.9.4',
        feature => 'Patch Viewer'
165 166
    },
    {
167 168 169 170
        package => 'PerlMagick',
        module  => 'Image::Magick',
        version => 0,
        feature => 'Optionally Convert BMP Attachments to PNGs'
171 172
    },
    {
173 174 175 176
        package => 'perl-ldap',
        module  => 'Net::LDAP',
        version => 0,
        feature => 'LDAP Authentication'
177
    },
178
    {
179 180 181 182
        package => 'SOAP-Lite',
        module  => 'SOAP::Lite',
        version => 0,
        feature => 'XML-RPC Interface'
183
    },
184
    {
185 186 187 188 189
        # We need the 'utf8_mode' method of HTML::Parser, for HTML::Scrubber.
        package => 'HTML-Parser',
        module  => 'HTML::Parser',
        version => '3.40',
        feature => 'More HTML in Product/Group Descriptions'
190 191
    },
    {
192 193 194 195
        package => 'HTML-Scrubber',
        module  => 'HTML::Scrubber',
        version => 0,
        feature => 'More HTML in Product/Group Descriptions'
196
    },
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    # Inbound Email
    {
        package => 'Email-MIME-Attachment-Stripper',
        module  => 'Email::MIME::Attachment::Stripper',
        version => 0,
        feature => 'Inbound Email'
    },
    {
        package => 'Email-Reply',
        module  => 'Email::Reply',
        version => 0,
        feature => 'Inbound Email'
    },

    # mod_perl
213
    {
214 215 216 217
        package => 'mod_perl',
        module  => 'mod_perl2',
        version => '1.999022',
        feature => 'mod_perl'
218 219 220 221 222
    },
    # Even very new releases of perl (5.8.5) don't come with this version,
    # so I didn't want to make it a general requirement just for
    # running under mod_cgi.
    {
223 224 225 226
        package => 'CGI',
        module  => 'CGI',
        version => '3.11',
        feature => 'mod_perl'
227 228
    },
    {
229 230 231 232
        package => 'Apache-DBI',
        module  => 'Apache::DBI',
        version => '0.96',
        feature => 'mod_perl'
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
    );

    my $all_modules = _get_extension_requirements(
        'OPTIONAL_MODULES', \@modules);
    return $all_modules;
};

# This implements the install-requirements hook described in Bugzilla::Hook.
sub _get_extension_requirements {
    my ($function, $base_modules) = @_;
    my @all_modules;
    # get a list of all extensions
    my @extensions = glob(bz_locations()->{'extensionsdir'} . "/*");
    foreach my $extension (@extensions) {
        my $file = "$extension/code/install-requirements.pl";
        if (-e $file) {
            my $safe = new Safe;
            # This is a very liberal Safe.
            $safe->permit(qw(:browse require entereval caller));
            $safe->rdo($file);
            if ($@) {
                warn $@;
                next;
            }
            my $modules = eval { &{$safe->varglob($function)}($base_modules) };
            next unless $modules;
            push(@all_modules, @$modules);
        }
    }

    unshift(@all_modules, @$base_modules);
    return \@all_modules;
};
267

268 269 270 271 272
sub check_requirements {
    my ($output) = @_;

    print "\nChecking perl modules...\n" if $output;
    my $root = ROOT_USER;
273
    my %missing = _check_missing(REQUIRED_MODULES, $output);
274

275
    print "\nChecking available perl DBD modules...\n" if $output;
276 277 278
    my $have_one_dbd = 0;
    my $db_modules = DB_MODULE;
    foreach my $db (keys %$db_modules) {
279 280
        my $dbd = $db_modules->{$db}->{dbd};
        $have_one_dbd = 1 if have_vers($dbd, $output);
281 282 283
    }

    print "\nThe following Perl modules are optional:\n" if $output;
284
    my %missing_optional = _check_missing(OPTIONAL_MODULES, $output);
285

286 287 288 289
    # If we're running on Windows, reset the input line terminator so that
    # console input works properly - loading CGI tends to mess it up
    $/ = "\015\012" if ON_WINDOWS;

290 291 292 293 294 295 296 297 298
    my $pass = !scalar(keys %missing) && $have_one_dbd;
    return {
        pass     => $pass,
        one_dbd  => $have_one_dbd,
        missing  => \%missing,
        optional => \%missing_optional,
        any_missing => !$pass || scalar(keys %missing_optional),
    };
}
299

300 301 302
# A helper for check_requirements
sub _check_missing {
    my ($modules, $output) = @_;
303

304 305 306 307
    my %missing;
    foreach my $module (@$modules) {
        unless (have_vers($module, $output)) {
            $missing{$module->{package}} = $module;
308
        }
309
    }
310

311 312
    return %missing;
}
313

314 315
sub print_module_instructions {
    my ($check_results, $output) = @_;
316

317 318 319 320 321 322 323 324 325 326 327 328
    # We only print these notes if we have to.
    if ((!$output && %{$check_results->{missing}})
        || ($output && $check_results->{any_missing}))
    {
        print "\n* NOTE: You must run any commands listed below as "
              . ROOT_USER . ".\n\n";

        if (ON_WINDOWS) {
            print <<EOT;
***********************************************************************
* Note For Windows Users                                              *
***********************************************************************
329 330
* In order to install the modules listed below, you first have to run * 
* the following command as an Administrator:                          *
331
*                                                                     *
332
*   ppm repo add theory58S http://theoryx5.uwinnipeg.ca/ppms          *
333
*                                                                     *
334
* Then you have to do (also as an Administrator):                     *
335
*                                                                     *
336 337 338 339
*   ppm repo up theory58S                                             *
*                                                                     *
* Do that last command over and over until you see "theory58S" at the *
* top of the displayed list.                                          *
340 341
***********************************************************************
EOT
342 343 344
        }
    }

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    # Required Modules
    if (my %missing = %{$check_results->{missing}}) {
        print <<EOT;
***********************************************************************
* REQUIRED MODULES                                                    *
***********************************************************************
* Bugzilla requires you to install some Perl modules which are either *
* missing from your system, or the version on your system is too old. *
*                                                                     *
* The latest versions of each module can be installed by running the  *
* commands below.                                                     *
***********************************************************************
EOT

        print "COMMANDS:\n\n";
        foreach my $package (keys %missing) {
            my $command = install_command($missing{$package});
            print "    $command\n";
363 364 365 366
        }
        print "\n";
    }

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    if (!$check_results->{one_dbd}) {
        print <<EOT;
***********************************************************************
* DATABASE ACCESS                                                     *
***********************************************************************
* In order to access your database, Bugzilla requires that the        *
* correct "DBD" module be installed for the database that you are     *
* running.                                                            *
*                                                                     *
* Pick and run the correct command below for the database that you    *
* plan to use with Bugzilla.                                          *
***********************************************************************
COMMANDS:

EOT

        my %db_modules = %{DB_MODULE()};
        foreach my $db (keys %db_modules) {
385
            my $command = install_command($db_modules{$db}->{dbd});
386 387
            printf "%10s: \%s\n", $db_modules{$db}->{name}, $command;
            print ' ' x 12 . "Minimum version required: "
388
                  . $db_modules{$db}->{dbd}->{version} . "\n";
389 390 391 392
        }
        print "\n";
    }

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
    return unless $output;

    if (my %missing = %{$check_results->{optional}}) {
        print <<EOT;
**********************************************************************
* OPTIONAL MODULES                                                   *
**********************************************************************
* Certain Perl modules are not required by Bugzilla, but by          *
* installing the latest version you gain access to additional        *
* features.                                                          *
*                                                                    *
* The optional modules you do not have installed are listed below,   *
* with the name of the feature they enable. If you want to install   *
* one of these modules, just run the appropriate command in the      *
* "COMMANDS TO INSTALL" section.                                     *
**********************************************************************

EOT
        # We want to sort them so that they are ordered by feature.
        my @missing_names = sort {$missing{$a}->{feature} 
                                  cmp $missing{$b}->{feature}} (keys %missing);

        # Now we have to determine how large the table cols will be.
        my $longest_name = max(map(length($_), @missing_names));

        # The first column header is at least 11 characters long.
        $longest_name = 11 if $longest_name < 11;

        # The table is 71 characters long. There are seven mandatory
        # characters (* and space) in the string. So, we have a total
        # of 64 characters to work with.
        my $remaining_space = 64 - $longest_name;
        print '*' x 71 . "\n";
        printf "* \%${longest_name}s * %-${remaining_space}s *\n",
               'MODULE NAME', 'ENABLES FEATURE(S)';
        print '*' x 71 . "\n";
        foreach my $name (@missing_names) {
            printf "* \%${longest_name}s * %-${remaining_space}s *\n",
                   $name, $missing{$name}->{feature};
        }
        print '*' x 71 . "\n";
434

435 436 437 438 439 440
        print "COMMANDS TO INSTALL:\n\n";
        foreach my $module (@missing_names) {
            my $command = install_command($missing{$module});
            printf "%15s: $command\n", $module;
        }
    }
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
sub check_graphviz {
    my ($output) = @_;

    return 1 if (Bugzilla->params->{'webdotbase'} =~ /^https?:/);

    printf("Checking for %15s %-9s ", "GraphViz", "(any)") if $output;

    my $return = 0;
    if(-x Bugzilla->params->{'webdotbase'}) {
        print "ok: found\n" if $output;
        $return = 1;
    } else {
        print "not a valid executable: " . Bugzilla->params->{'webdotbase'} . "\n";
    }

    my $webdotdir = bz_locations()->{'webdotdir'};
    # Check .htaccess allows access to generated images
    if (-e "$webdotdir/.htaccess") {
        my $htaccess = new IO::File("$webdotdir/.htaccess", 'r') 
            || die "$webdotdir/.htaccess: " . $!;
        if (!grep(/png/, $htaccess->getlines)) {
            print "Dependency graph images are not accessible.\n";
            print "delete $webdotdir/.htaccess and re-run checksetup.pl to fix.\n";
        }
        $htaccess->close;
    }

    return $return;
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486
sub display_version_and_os {
    # Display version information
    printf "\n* This is Bugzilla " . BUGZILLA_VERSION . " on perl %vd\n",
           $^V;
    my @os_details = POSIX::uname;
    # 0 is the name of the OS, 2 is the major version,
    my $os_name = $os_details[0] . ' ' . $os_details[2];
    if (ON_WINDOWS) {
        require Win32;
        $os_name = Win32::GetOSName();
    }
    # 3 is the minor version.
    print "* Running on $os_name $os_details[3]\n"
}
487

488
# This was originally clipped from the libnet Makefile.PL, adapted here to
489
# use the below vers_cmp routine for accurate version checking.
490
sub have_vers {
491 492 493 494 495 496 497 498 499
    my ($params, $output) = @_;
    my $module  = $params->{module};
    my $package = $params->{package};
    if (!$package) {
        $package = $module;
        $package =~ s/::/-/g;
    }
    my $wanted  = $params->{version};

500 501
    my ($msg, $vnum, $vstr);
    no strict 'refs';
502
    printf("Checking for %15s %-9s ", $package, !$wanted?'(any)':"(v$wanted)") 
503
        if $output;
504

505
    eval "require $module;";
506

507 508
    # VERSION is provided by UNIVERSAL::
    $vnum = eval { $module->VERSION } || -1;
509 510 511 512

    # CGI's versioning scheme went 2.75, 2.751, 2.752, 2.753, 2.76
    # That breaks the standard version tests, so we need to manually correct
    # the version
513
    if ($module eq 'CGI' && $vnum =~ /(2\.7\d)(\d+)/) {
514 515 516 517 518 519 520 521 522 523 524 525 526 527
        $vnum = $1 . "." . $2;
    }

    if ($vnum eq "-1") { # string compare just in case it's non-numeric
        $vstr = "not found";
    }
    elsif (vers_cmp($vnum,"0") > -1) {
        $vstr = "found v$vnum";
    }
    else {
        $vstr = "found unknown version";
    }

    my $vok = (vers_cmp($vnum,$wanted) > -1);
528 529 530 531 532 533 534 535 536
    my $blacklisted;
    if ($vok && $params->{blacklist}) {
        $blacklisted = grep($vnum =~ /$_/, @{$params->{blacklist}});
        $vok = 0 if $blacklisted;
    }

    my $ok = $vok ? "ok:" : "";
    my $black_string = $blacklisted ? "(blacklisted)" : "";
    print "$ok $vstr $black_string\n" if $output;
537 538 539 540 541 542
    return $vok ? 1 : 0;
}

# This is taken straight from Sort::Versions 1.5, which is not included
# with perl by default.
sub vers_cmp {
543 544 545
    my ($a, $b) = @_;

    # Remove leading zeroes - Bug 344661
546 547
    $a =~ s/^0*(\d.+)/$1/;
    $b =~ s/^0*(\d.+)/$1/;
548 549 550

    my @A = ($a =~ /([-.]|\d+|[^-.\d]+)/g);
    my @B = ($b =~ /([-.]|\d+|[^-.\d]+)/g);
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

    my ($A, $B);
    while (@A and @B) {
        $A = shift @A;
        $B = shift @B;
        if ($A eq '-' and $B eq '-') {
            next;
        } elsif ( $A eq '-' ) {
            return -1;
        } elsif ( $B eq '-') {
            return 1;
        } elsif ($A eq '.' and $B eq '.') {
            next;
        } elsif ( $A eq '.' ) {
            return -1;
        } elsif ( $B eq '.' ) {
            return 1;
        } elsif ($A =~ /^\d+$/ and $B =~ /^\d+$/) {
            if ($A =~ /^0/ || $B =~ /^0/) {
                return $A cmp $B if $A cmp $B;
            } else {
                return $A <=> $B if $A <=> $B;
            }
        } else {
            $A = uc $A;
            $B = uc $B;
            return $A cmp $B if $A cmp $B;
        }
    }
    @A <=> @B;
}

sub install_command {
    my $module = shift;
585 586
    my ($command, $package);

587
    if (ON_WINDOWS) {
588 589 590 591 592 593 594 595
        $command = 'ppm install %s';
        $package = $module->{package};
    }
    else {
        $command = "$^X -MCPAN -e 'install \%s'";
        # Non-Windows installations need to use module names, because
        # CPAN doesn't understand package names.
        $package = $module->{module};
596
    }
597
    return sprintf $command, $package;
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
}


1;

__END__

=head1 NAME

Bugzilla::Install::Requirements - Functions and variables dealing
  with Bugzilla's perl-module requirements.

=head1 DESCRIPTION

This module is used primarily by C<checksetup.pl> to determine whether
or not all of Bugzilla's prerequisites are installed. (That is, all the
perl modules it requires.)

=head1 CONSTANTS

=over 4

=item C<REQUIRED_MODULES>

An arrayref of hashrefs that describes the perl modules required by 
Bugzilla. The hashes have two keys, C<name> and C<version>, which
represent the name of the module and the version that we require.

=back

=head1 SUBROUTINES

=over 4

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
=item C<check_requirements($output)>

 Description: This checks what optional or required perl modules
              are installed, like C<checksetup.pl> does.

 Params:      C<$output> - C<true> if you want the function to print
                           out information about what it's doing,
                           and the versions of everything installed.
                           If you don't pass the minimum requirements,
                           the will always print out something, 
                           regardless of this parameter.

 Returns:    A hashref containing three values:
             C<pass> - Whether or not we have all the mandatory 
                       requirements.
             C<missing> - A hash showing which mandatory requirements
                          are missing. The key is the module name,
                          and the value is the version we require.
             C<optional> - Which optional modules are installed and
                           up-to-date enough for Bugzilla.

653 654 655 656 657 658 659 660 661 662
=item C<check_graphviz($output)>

Description: Checks if the graphviz binary specified in the 
  C<webdotbase> parameter is a valid binary, or a valid URL.

Params:      C<$output> - C<$true> if you want the function to
                 print out information about what it's doing.

Returns:     C<1> if the check was successful, C<0> otherwise.

663 664 665 666 667 668 669 670 671 672 673 674
=item C<vers_cmp($a, $b)>

 Description: This is a comparison function, like you would use in
              C<sort>, except that it compares two version numbers.
              It's actually identical to versioncmp from 
              L<Sort::Versions>.

 Params:      c<$a> and C<$b> are versions you want to compare.

 Returns:     -1 if $a is less than $b, 0 if they are equal, and
              1 if $a is greater than $b.

675
=item C<have_vers($module, $output)>
676 677 678 679 680 681

 Description: Tells you whether or not you have the appropriate
              version of the module requested. It also prints
              out a message to the user explaining the check
              and the result.

682 683
 Params:      C<$module> - A hashref, in the format of an item from 
                           L</REQUIRED_MODULES>.
684 685 686
              C<$output> - Set to true if you want this function to
                           print information to STDOUT about what it's
                           doing.
687 688 689 690 691 692 693 694 695 696

 Returns:   C<1> if you have the module installed and you have the
            appropriate version. C<0> otherwise.

=item C<install_command($module)>

 Description: Prints out the appropriate command to install the
              module specified, depending on whether you're
              on Windows or Linux.

697 698
 Params:      C<$module> - A hashref, in the format of an item from
                           L</REQUIRED_MODULES>.
699 700 701 702

 Returns:     nothing

=back