Advertisement
Guest User

Untitled

a guest
Dec 31st, 2011
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 13.33 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # cpanel - installruby                            Copyright(c) 2011 cPanel, Inc.
  3. #                                                           All rights Reserved.
  4. # copyright@cpanel.net                                         http://cpanel.net
  5. # This code is subject to the cpanel license. Unauthorized copying is prohibited
  6.  
  7. BEGIN { unshift @INC, '/usr/local/cpanel'; }
  8.  
  9. use Cpanel::FileUtils        ();
  10. use Cpanel::FindBin          ();
  11. use Cpanel::Logger           ();
  12. use Cpanel::OSSys            ();
  13. use Cpanel::SafeRun          ();
  14. use Cpanel::SysPkgs          ();
  15. use Cpanel::Tar              ();
  16. use Cpanel::Update           ();
  17. use Cpanel::Version::Compare ();
  18.  
  19. # Allow disabling of ruby
  20. Cpanel::Update::init_UP_update('ruby');
  21.  
  22. my %install = (
  23.     'ruby'     => '1.8.7-p334',
  24.     'rubygems' => '1.3.7',
  25. );
  26.  
  27. my $force      = @ARGV && grep( /^--force$/,      @ARGV ) ? 1 : 0;
  28. my $just_rails = @ARGV && grep( /^--just-rails$/, @ARGV ) ? 1 : 0;
  29. my $src_dir    = '/usr/local/cpanel/src/3rdparty/ruby';
  30. my $system     = Cpanel::OSSys::get_system();
  31. my $prefix   = $system eq 'freebsd'     ? '/usr/local'          : '/usr';
  32. my $gem_bin  = -x $prefix . '/bin/gem'  ? $prefix . '/bin/gem'  : '/usr/bin/gem';
  33. my $ruby_bin = -x $prefix . '/bin/ruby' ? $prefix . '/bin/ruby' : '/usr/bin/ruby';
  34. my $ruby_version_file = '/var/cpanel/version/ruby';
  35.  
  36. my %archive_types = (
  37.     '.tar.gz'  => '-z',
  38.     '.tar.bz2' => '-j',
  39.     '.tgz'     => '-z',
  40. );
  41.  
  42. my $exit_code = system '/usr/local/cpanel/scripts/checkccompiler';
  43. if ($exit_code) {
  44.     print "C compiler appears to be broken.  Ruby cannot be installed until this is resolved!\n";
  45.     exit 1;
  46. }
  47.  
  48. my $logger = Cpanel::Logger->new();
  49.  
  50. system '/usr/local/cpanel/scripts/installsqlite3', '--source';
  51.  
  52. if ( !$just_rails ) {
  53.     my $needs_gem = -x $gem_bin && !$force ? 0 : 1;
  54.     if ( !$needs_gem ) {
  55.  
  56.         # Gem 0.8.5+ can self update, cPanel started with 1.1
  57.         my $gem_version = `$gem_bin -v`;
  58.         if ( !$gem_version ) {
  59.             $needs_gem = 1;
  60.         }
  61.         else {
  62.             chomp $gem_version;
  63.             if ( $gem_version =~ m/^0/ ) {
  64.                 $needs_gem = 1;
  65.             }
  66.         }
  67.     }
  68.  
  69.     my $needs_ruby = ( !-e $ruby_version_file || $force ) ? 1 : 0;
  70.  
  71.     my $ruby_version = `$ruby_bin -v`;
  72.     chomp $ruby_version;
  73.  
  74.     # check version in file
  75.     if ( !$needs_ruby ) {
  76.         if ( !$ruby_version ) {
  77.             $needs_ruby = 1;
  78.         }
  79.         else {
  80.             if ( open my $vers_fh, '<', $ruby_version_file ) {
  81.                 my $vers = readline $vers_fh;
  82.                 close $vers_fh;
  83.                 chomp $vers;
  84.                 if ( !$vers ) {
  85.                     $needs_ruby = 1;
  86.                 }
  87.                 elsif ( $vers ne $install{'ruby'} ) {
  88.                     my ( $new_version_number,       $new_patch_level )       = split( /-/, $install{'ruby'} );
  89.                     my ( $installed_version_number, $installed_patch_level ) = split( /-/, $vers );
  90.  
  91.                     if ( $new_version_number > $installed_version_number ) {
  92.                         $needs_ruby = 1;
  93.                     }
  94.                     elsif ( $new_version_number eq $installed_version_number ) {
  95.                         my ($new_patch)       = $new_patch_level       =~ m/(\d+)/;
  96.                         my ($installed_patch) = $installed_patch_level =~ m/(\d+)/;
  97.  
  98.                         if ($new_patch) {    # if we distribute a patched version
  99.                             if ( !$installed_patch ) {    # they don't have a patched version
  100.                                 $needs_ruby = 1;
  101.                             }
  102.                             elsif ( $new_patch > $installed_patch ) {    # our patch is greater than theirs
  103.                                 $needs_ruby = 1;
  104.                             }
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.             else {
  110.                 $logger->warn("Failed to check version file: $!");
  111.                 $needs_ruby = 1;
  112.             }
  113.         }
  114.  
  115.     }
  116.  
  117.     if ( $system eq 'freebsd' ) {
  118.         my @install = ('ruby18');
  119.         if ($needs_gem) {
  120.             push @install, 'ruby-gems';
  121.         }
  122.  
  123.         my $syspkgs = Cpanel::SysPkgs->new();
  124.         if ( !$syspkgs ) { die print "Could not create SysPkgs object\n"; }
  125.         $syspkgs->ensure( 'pkglist' => \@install );
  126.     }
  127.     else {
  128.         my $tarcfg = Cpanel::Tar::load_tarcfg();
  129.  
  130.         my @install;
  131.         if ($needs_ruby) {
  132.             @install = ('ruby');
  133.         }
  134.         if ($needs_gem) {
  135.             push @install, 'rubygems';
  136.         }
  137.  
  138.       INSTALL:
  139.         foreach my $app (@install) {
  140.  
  141.             my $basedir = '/home/cp' . $app . 'build';
  142.             eval {
  143.                 system 'rm', '-rf', $basedir;
  144.                 system 'mkdir', $basedir;
  145.                 chdir $basedir or $logger->die("Unable to chdir $basedir: $!");
  146.  
  147.                 print "Installing $app version: $install{$app}\n";
  148.  
  149.                 my $app_source_base = $app . '-' . $install{$app};
  150.  
  151.                 my $app_source;
  152.                 my $decompression_flag;
  153.                 foreach my $archive_extension ( keys %archive_types ) {
  154.                     if ( -e $src_dir . '/' . $app_source_base . $archive_extension ) {
  155.                         $app_source         = $app_source_base . $archive_extension;
  156.                         $decompression_flag = $archive_types{$archive_extension};
  157.                         last;
  158.                     }
  159.                 }
  160.  
  161.                 if ( !$app_source ) {
  162.                     $logger->warn("Unable to locate source for $app");
  163.                     die;
  164.                 }
  165.  
  166.                 system $tarcfg->{'bin'}, '-x', '-p', $tarcfg->{'no_same_owner'}, $decompression_flag, '-C', $basedir, '-f', $src_dir . '/' . $app_source;
  167.  
  168.                 if ( !-d $basedir . '/' . $app_source_base ) {
  169.                     $logger->warn("Failed to decompress source for $app");
  170.                     die;
  171.                 }
  172.  
  173.                 print "Using Dir: $basedir/$app_source_base\n";
  174.  
  175.                 if ( !chdir( $basedir . '/' . $app_source_base ) ) {
  176.                     $logger->warn("Unable to chdir to $basedir/$app_source_base failed.");
  177.                     die;
  178.                 }
  179.  
  180.                 if ( $app ne 'ruby' ) {
  181.                     print "Setting up $app ...\n";
  182.                     system $ruby_bin, 'setup.rb';
  183.                 }
  184.                 else {
  185.                     if ($ruby_version) {
  186.                         print "\nUpdating $ruby_version with $install{$app}\n\n";
  187.                     }
  188.  
  189.                     # Rubygems installation says you should do this manually????
  190.                     if ( $app eq 'rubygems' && -e $gem_bin ) {
  191.                         unlink $gem_bin;
  192.                     }
  193.  
  194.                     unlink $ruby_version_file;
  195.                     my @CONF = ( './configure', '--prefix=' . $prefix, '--enable-shared' );
  196.                     system @CONF;
  197.                     system 'gmake';
  198.                     system 'gmake', 'install';
  199.  
  200.                     if ( open my $vers_fh, '>', $ruby_version_file ) {
  201.                         print {$vers_fh} $install{$app};
  202.                         close $vers_fh;
  203.                     }
  204.                     else {
  205.                         $logger->warn("Unable to update version file $ruby_version_file: $!");
  206.                         die;
  207.                     }
  208.                 }
  209.             };
  210.             print "Removing source directory $basedir\n";
  211.             chdir '/';
  212.             system 'rm', '-rf', $basedir;
  213.         }
  214.     }
  215. }
  216.  
  217. $gem_bin = -x '/usr/local/bin/gem' ? '/usr/local/bin/gem' : '/usr/bin/gem';
  218. if ( !-x $gem_bin ) {
  219.     $logger->warn("gem installation failed. Unable to locate gem binary.");
  220. }
  221. else {
  222.     print "gem system update ...\n";
  223.     system $prefix . '/bin/gem', 'update', '--system';
  224.     print "gem system update complete.\n";
  225. }
  226.  
  227. my $rails_bin = Cpanel::FindBin::findbin('rails');
  228.  
  229. if ( -x $rails_bin ) {
  230.     my $rails_version = Cpanel::SafeRun::saferun( 'rails', '--version' );
  231.     chomp($rails_version);
  232.     ($rails_version) = ( split( ' ', $rails_version, 2 ) )[1];
  233.  
  234.     if ( Cpanel::Version::Compare::compare( $rails_version, '>=', '3.0.0' ) ) {
  235.         my $warning_msg = "\n" . 'Rails 3 has been detected on your system. Rails 3 is not supported at this time.';
  236.         if ( -t STDIN ) {
  237.             print $warning_msg . ' Would you like to remove it and install a supported version of rails? ';
  238.  
  239.             my $choice;
  240.             while ( !defined $choice ) {
  241.                 $choice = <STDIN>;
  242.                 chomp($choice);
  243.  
  244.                 if ( $choice =~ /^y(?:es)?$/i ) {
  245.                     $choice = 1;
  246.                 }
  247.                 elsif ( $choice =~ /^n(?:o)?$/ ) {
  248.                     $choice = 0;
  249.                 }
  250.                 else {
  251.                     $choice = undef;
  252.                 }
  253.             }
  254.  
  255.             if ($choice) {
  256.                 uninstallrails3();
  257.             }
  258.             else {
  259.                 exit 1;
  260.             }
  261.         }
  262.         else {
  263.             print $warning_msg . ' Uninstalling rails 3.' . "\n";
  264.             uninstallrails3();
  265.         }
  266.     }
  267. }
  268.  
  269. my @rails        = ( 'rails',                                     '-v=2.3.14' );
  270. my @install      = ( 'mongrel',                                   'sqlite3' );
  271. my @sqlite3_args = ( '--with-sqlite3-include=/usr/local/include', '--with-sqlite3-lib=/usr/local/lib' );
  272.  
  273. my $hasperlexpect = 0;
  274. eval {
  275.     require IO::Tty;
  276.     require Expect;
  277.     $hasperlexpect = 1;
  278. };
  279.  
  280. print "\nInstalling rails...\n\n";
  281. Cpanel::SafeRun::saferunnoerrordynamic( $gem_bin, 'install', @rails, );
  282.  
  283. if ($hasperlexpect) {
  284.     if ( my $pid = fork() ) {
  285.         waitpid( $pid, 0 );
  286.     }
  287.     else {
  288.         local $SIG{'ALRM'} = sub { die "Timeout\n"; };
  289.         alarm( 20 * 60 );
  290.  
  291.         my $exp               = Expect->spawn( $gem_bin, 'install', @install, '--', @sqlite3_args ) or die "Cannot spawn gem installer: $!";
  292.         my $expect_pid        = $exp->pid();
  293.         my $mongrel_number    = 0;
  294.         my $fastthread_number = 0;
  295.         while ( waitpid( $expect_pid, 1 ) != -1 ) {
  296.             $exp->expect(
  297.                 300,
  298.                 [ 'Select which gem', sub { return Expect::exp_continue() } ],
  299.                 [
  300.                     qr/^\s*\d\.\smongrel\s.*/,
  301.                     sub {
  302.                         if ( $exp->match =~ /\(ruby\)/ ) {
  303.                             if ( !$mongrel_number ) {
  304.                                 ($mongrel_number) = $exp->match =~ /\s*(\d+)/;
  305.                             }
  306.                         }
  307.                         else {
  308.                             return Expect::exp_continue();
  309.                         }
  310.                       }
  311.                 ],
  312.                 [
  313.                     qr/^\s*\d\.\sfastthread\s.*/,
  314.                     sub {
  315.                         if ( $exp->match =~ /\(ruby\)/ ) {
  316.                             if ( !$fastthread_number ) {
  317.                                 ($fastthread_number) = $exp->match =~ /\s*(\d+)/;
  318.                             }
  319.                         }
  320.                         else {
  321.                             return Expect::exp_continue();
  322.                         }
  323.                       }
  324.                 ],
  325.                 [
  326.                     '>',
  327.                     sub {
  328.                         my $number = 0;
  329.                         if ($mongrel_number) {
  330.                             $number         = $mongrel_number;
  331.                             $mongrel_number = 0;
  332.                         }
  333.                         elsif ($fastthread_number) {
  334.                             $number            = $fastthread_number;
  335.                             $fastthread_number = 0;
  336.                         }
  337.                         $exp->send( $number . "\r" );
  338.                       }
  339.                 ],
  340.                 [ 'timeout', sub { $exp->send("\r"); } ],
  341.             );
  342.         }
  343.         $exp->soft_close();
  344.         exit;
  345.     }
  346. }
  347. else {
  348.     foreach my $app (@install) {
  349.         system $gem_bin, 'install', $app, '--include-dependencies';
  350.     }
  351. }
  352.  
  353. if ( !-e '/etc/init.d/ror' && !-e '/usr/local/etc/rc.d/ror.sh' ) {
  354.     system('/usr/local/cpanel/bin/ror_setup');
  355. }
  356.  
  357. print "Ruby - & - Rails Installed\n";
  358.  
  359. system '/usr/local/cpanel/scripts/magicloader';
  360. system '/usr/local/cpanel/bin/updatemongrel';
  361.  
  362. exit;
  363.  
  364. sub uninstallrails3 {
  365.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'rails' );
  366.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'actionmailer' );
  367.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'activemodel' );
  368.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'actionpack' );
  369.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'activerecord' );
  370.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'activeresource' );
  371.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'activesupport' );
  372.     Cpanel::SafeRun::saferunnoerrordynamic( 'gem', 'uninstall', '-I', '-x', '-v', '>= 3.0.0', 'railties' );
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement