File: //scripts.20110531.215904.25158/upcp_11.30_prep
#!/usr/bin/perl
# cpanel - scripts/upcp_11.30_prep Copyright(c) 2011 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use Socket;
use strict;
print "\n\n\tEnsuring cPanel server is ready to run 11.30\n\n";
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;
my $time_stamp = sprintf( "%04d%02d%02d.%02d%02d%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
my $old_scripts_dir = "/scripts.$time_stamp.$$";
foreach my $dir (qw{/scripts/perl588installer /scripts/perl589installer}) {
next if ( !-e $dir );
print "Detected temp directory $dir. Cleaning up to prevent /scripts upgrade issues\n";
print "/bin/rm -rf $dir\n";
system( '/bin/rm', '-rf', $dir );
}
if ( -e $old_scripts_dir ) {
print "$old_scripts_dir already exists : $!\nThe update script has detected that $old_scripts_dir currently exists, indicating that this update script has already run.\n";
exit(1);
}
if ( !chdir('/usr/local/cpanel') ) {
print "Could not change directory to /usr/local/cpanel : $!\n";
exit(1);
}
if ( !-l '/scripts' ) {
# Check to see if number of files under /scripts exceeds threshold limit of 5000
my $ttl_cnt = get_file_count_recursively("/scripts");
if ( $ttl_cnt > 5000 ) {
print "\nTotal number of files under /scripts is $ttl_cnt, more than expected.\nUpdate will not proceed, please contact support immediately for assistance.\n\n";
exit(1);
}
else {
print "Found $ttl_cnt files under /scripts, continuing..\n";
}
# Check to see if disk space used by files under /scripts exceeds threshold limit of 100MB
if ( open( my $du, '-|', 'du', '-shk', '/scripts/' ) ) {
while (<$du>) {
if (m/^(\d+)\s+(.*)/) {
if ( $2 eq '/scripts/' and $1 > 102400 ) { # 102400 KB = 100MB
print "Total disk space being used in /scripts is $1 K which exceeds the cPanel defined threshold of 100MB.\nUpdate will not proceed, please contact support immediately for assistance.\n\n";
exit(1);
}
}
}
close($du);
}
}
my @excludes;
my $exclude_file = '/etc/cpanelsync.exclude';
my @existing_syncfiles = (
'/usr/local/cpanel/src/.cpanelsync',
'/usr/local/cpanel/bin/.cpanelsync',
'/usr/local/cpanel/whost mgr/.cpanelsync',
'/usr/local/cpanel/java-sys/.cpanelsync',
'/usr/local/cpanel/cgi-sys/.cpanelsync',
'/usr/local/cpanel/3rdparty/.cpanelsync',
'/usr/local/cpanel/Cpanel/.cpanelsync',
'/usr/local/cpanel/perl/.cpanelsync',
'/usr/local/cpanel/base/3rdparty/.cpanelsync',
'/usr/local/cpanel/.cpanelsync',
'/scripts/.cpanelsync',
);
# Sanitize the /etc/cpanelsync.exclude file:
# Check that no paths start with /scripts, if so, rewrite them to /u/l/c/scripts/$file
if ( -e $exclude_file && !-z _ ) {
if ( open my $cpsync_fh, '<', $exclude_file ) {
while ( my $line = readline $cpsync_fh ) {
chomp $line;
$line =~ s{^/scripts/}{/usr/local/cpanel/scripts/};
push @excludes, $line;
}
close $cpsync_fh;
}
else {
print "Unable to open $exclude_file : $!\n";
exit(1);
}
}
else {
print "$exclude_file is empty or does not exist, ignoring.\n";
}
# Rebuild the cpanelsync.exclude
if (@excludes) {
print "Rebuilding $exclude_file\n";
if ( open my $cpsync_fh, '>', $exclude_file ) {
foreach ( sort @excludes ) {
print {$cpsync_fh} "$_\n";
}
close $cpsync_fh;
}
else {
print "Unable to write to $exclude_file : $!\n";
exit(1);
}
}
print "Moving /scripts to /usr/local/cpanel/scripts and symlinking back\n";
# copy the scripts over preserve permissions and such assuming /scripts is a directory
if ( -d '/scripts' ) {
print "Relocating /scripts to /usr/local/cpanel/scripts\n";
chdir('/');
system('tar -cf - scripts | (cd /usr/local/cpanel && tar -xf -)');
}
# move /scripts out of the way
if ( -e '/scripts' ) {
rename( '/scripts', $old_scripts_dir );
}
symlink( '/usr/local/cpanel/scripts', '/scripts' ); # symlink the new directory back to the original
# Get system type with OS name and arch to produce the new .cpanelsync files.
my $arch;
if ( length( pack( 'l!', 1000 ) ) * 8 == 64 ) {
$arch = 'x86_64';
}
else {
$arch = 'i386';
}
my $system = lc($^O) . '-' . $arch;
print "System Arch: $system\n";
open( my $dummy_sync_fh, '>>', '/usr/local/cpanel/.cpanelsync_binaries__forward_slash__' . $system );
close($dummy_sync_fh) if ($dummy_sync_fh);
# Combine old cpanelsync files into cpanelsync_cpanel for updatenow to handle, remove old cpanelsync files
if ( open( my $cpanelsync_cpanel_fh, '>>', '/usr/local/cpanel/.cpanelsync_cpanel' ) ) {
foreach my $oldsyncfile (@existing_syncfiles) {
if ( open( my $fh, '<', $oldsyncfile ) ) {
my @path = split( /\//, $oldsyncfile );
my $just_filename = pop(@path);
my $basedir = join( '/', @path );
$basedir =~ s{^/usr/local/cpanel}{};
$basedir = './' . $basedir;
$basedir =~ s{//}{/}g;
while (<$fh>) {
chomp;
if (m/^(.)===(.*)===(.*)/) {
my $type = $1;
my $file = $2;
my $etc = $3;
next if ( $file eq '.' );
$file =~ s{^\.}{$basedir};
$file =~ s{/+}{/};
print {$cpanelsync_cpanel_fh} "$type===$file===$etc\n";
}
}
close($fh);
unlink($oldsyncfile);
unlink( $oldsyncfile . '.md5s' );
unlink( $oldsyncfile . '.new' );
}
}
close($cpanelsync_cpanel_fh);
}
# Rename the old cpanelsync files to the new names
rename( '/usr/local/cpanel/base/frontend/x3mail/.cpanelsync', '/usr/local/cpanel/base/frontend/x3mail/.cpanelsync_x3mail' ) if ( !-e '/usr/local/cpanel/base/frontend/x3mail/.cpanelsync_x3mail' ) or print "Could not rename /usr/local/cpanel/base/frontend/x3mail/.cpanelsync /usr/local/cpanel/base/frontend/x3mail/.cpanelsync_x3mail\n";
rename( '/usr/local/cpanel/base/frontend/x3/.cpanelsync', '/usr/local/cpanel/base/frontend/x3/.cpanelsync_x3' ) if ( !-e '/usr/local/cpanel/base/frontend/x3/.cpanelsync_x3' ) or print "Could not rename /usr/local/cpanel/base/frontend/x3/.cpanelsync /usr/local/cpanel/base/frontend/x3/.cpanelsync_x3\n";
# Exit 0 if all is well for /scripts/upcp to run
exit(0);
###########################################################################
sub get_file_count_recursively {
my ( $dir, $internal_count_ref ) = @_;
my $count = 0;
$internal_count_ref ||= \$count;
my @items = glob( $dir . "/*" );
foreach my $path (@items) {
${$internal_count_ref}++;
if ( !-l $path && -d $path ) {
get_file_count_recursively( $path, $internal_count_ref );
}
}
return ${$internal_count_ref};
}