#!/usr/bin/perl ### confvarnish # # AUTHOR: Scott Sullivan (ssullivan@liquidweb.com) # # BUGS: ssullivan@liquidweb.com, mshooltz@liquidweb.com # # GENERAL: # See the POD (perldoc /path/to/confvarnish.pl) or --help ### use Term::ANSIColor; use Scalar::Util qw(looks_like_number); use LWP::UserAgent; use POSIX qw(strftime); use strict; my $version = '0.1'; my @contact_email = ('ssullivan@liquidweb.com,','mshooltz@liquidweb.com'); $|=1; ### Set buffering. sub help { print "\n"; print color 'yellow'; print "--install apache_port varnish_port mangmt_port "; print color 'blue'; print "=> "; print color 'reset'; print "This moves Apache to 'apache_port', installs varnish on 'varnish_port', and sets up the management interface on 'mangmt_port'.\n"; print color 'yellow'; print "--remove "; print color 'blue'; print "=> "; print color 'reset'; print "This removes varnish from the system and places Apache back on port 80.\n"; } sub exception_msg { print color 'red'; print "An unexpected error has occurred please email: @contact_email \n"; print color 'reset'; } sub preChecks { if ( $< != 0 ) { print color 'red'; print "ERROR: You must be root to run this.\n"; print color 'reset'; exit(1); } system("which rpm &> /dev/null"); if ( $? != 0 ) { print color 'red'; print "ERROR: rpm not found in path! \n"; exception_msg(); print color 'reset'; exit(1); } system("which sed &> /dev/null"); if ( $? != 0 ) { print color 'red'; print "ERROR: sed not found in path! \n"; exception_msg(); print color 'reset'; exit(1); } system("which grep &> /dev/null"); if ( $? != 0 ) { print color 'red'; print "ERROR: sed not found in path! \n"; exception_msg(); print color 'reset'; exit(1); } my $cpanelVerify = `/sbin/chkconfig --list | grep cpanel`; if ( $cpanelVerify !~/3:on/) { print color 'red'; print "ERROR: cPanel not found (needs to be enabled in chkconfig run level 3).\n"; print color 'reset'; exit(1); } } sub getOSInfo { my $versionChkFile = '/etc/redhat-release'; if ( ! -e $versionChkFile ) { print color 'red'; print "ERROR: $versionChkFile doesn't exist; $0 only supports CentOS/RHEL 5 presently.\n"; print color 'reset'; exit(1); } my $contents = `cat $versionChkFile`; my @columns = split /\s+/, $contents; my @majorVer = split /\./, $columns[2]; if ( $majorVer[0] != '5' ) { print color 'red'; print "ERROR: $0 currently only supports CentOS/RHEL 5.\n"; exit(1); } return $majorVer[0]; } sub chkURL { die "FATAL: wrong args passed to chkURL()!\n", exception_msg() if (scalar(@_) != 1); my $return; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get("$_[0]"); if ($response->is_success) { $return = 'success'; } else { $return = 'fail'; } return $return; } sub do_install { die "FATAL: wrong args passed to do_install()!\n", exception_msg() if (scalar(@_) != 3); ### Arg0: apache_port ### Arg1: varnish_port ### Arg2: mangmt_port my $url = 'http://repo.varnish-cache.org/redhat/el5/noarch/varnish-release-2.1-2.noarch.rpm'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Ensuring mirror is up... "; my $urlCheck = chkURL("$url"); if ( $urlCheck eq 'fail' ) { print color 'red'; print "ERROR\n"; print "ERROR: Unable to retrieve from $url \n"; exception_msg(); print "\n"; print color 'reset'; exit(1); } print color 'green'; print "OK\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Installing varnish RPM's... "; my $varnishRepoChk = `rpm -qa|grep -i varnish-release`; if ( $varnishRepoChk !~ m/varnish-release/ ) { system("rpm --nosignature -i http://repo.varnish-cache.org/redhat/el5/noarch/varnish-release-2.1-2.noarch.rpm >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'rpm --nosignature -i http://repo.varnish-cache.org/redhat/el5/noarch/varnish-release-2.1-2.noarch.rpm' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } } system("yum install -y varnish >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'yum install -y varnish' returned non-zero; got $? ; Review /root/confvarnish.log\n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Setting Varnish listen port to $_[1]... "; system("sed -i '/VARNISH_LISTEN_PORT=/cVARNISH_LISTEN_PORT=$_[1]' /etc/sysconfig/varnish >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: ' sed -i '/VARNISH_LISTEN_PORT=/cVARNISH_LISTEN_PORT=$_[1] /etc/sysconfig/varnish' returned non-zero; got $? ; Review /root/confvarnish.log\n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Setting Varnish management port to $_[2]... "; system("sed -i '/VARNISH_ADMIN_LISTEN_PORT=/cVARNISH_ADMIN_LISTEN_PORT=$_[2]' /etc/sysconfig/varnish >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: ' sed -i '/VARNISH_ADMIN_LISTEN_PORT=/cVARNISH_ADMIN_LISTEN_PORT=$_[2]' /etc/sysconfig/varnish ' returned non-zero; got $? ; Review /root/confvarnish.log\n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Setting Varnish backend port to Apache port ($_[0])... "; system("sed -i '/.port = /c.port = \"$_[0]\";' /etc/varnish/default.vcl >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: ' sed -i '/.port = /c.port = \"$_[0]\";' /etc/varnish/default.vcl ' returned non-zero; got $? ; Review /root/confvarnish.log\n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Changing Apache port in cPanel configuration to $_[0]... "; my $apachePortChk = `grep apache_port /var/cpanel/cpanel.config`; if ( $apachePortChk !~ m/apache_port/ ) { system("echo 'apache_port=0.0.0.0:80' >> /var/cpanel/cpanel.config"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'echo 'apache_port=0.0.0.0:80' >> /var/cpanel/cpanel.config' returned non-zero; got $? \n"; exception_msg(); print color 'reset'; exit(1); } } system("sed -i '/apache_port=0.0.0.0/capache_port=0.0.0.0:$_[0]' /var/cpanel/cpanel.config >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'sed -i '/apache_port=0.0.0.0/capache_port=0.0.0.0:$_[0]' /var/cpanel/cpanel.config' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } system("/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Backing up httpd.conf and rebuilding Apache configuration... "; system("cp -Rp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.conf.confvarnish.$^T >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'cp -Rp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.conf$^T' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } system("/scripts/rebuildhttpdconf >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/scripts/rebuildhttpdconf' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Restarting Apache... "; print color 'green'; print "Done\n"; print color 'reset'; system("killall -9 httpd >> /root/confvarnish.log"); system("/etc/init.d/httpd stop >> /root/confvarnish.log"); system("/etc/init.d/httpd startssl >> /root/confvarnish.log"); print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Starting Varnish... "; system("/etc/init.d/varnish restart >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/etc/init.d/varnish restart' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Starting Varnish logging system... "; system("/etc/init.d/varnishlog restart >> /root/confvarnish.log"); system("/etc/init.d/varnishncsa restart >> /root/confvarnish.log"); print color 'green'; print "Done\n"; print color 'reset'; } sub do_remove { my @isInstalled = `rpm -qa |grep -i varnish`; my $isInstalledSize = scalar (@isInstalled); if ( $isInstalledSize == 0 ) { print color 'yellow'; print "NOTICE: Varnish is already removed (I don't see varnish RPM's installed on the system); skipping removal.\n"; print color 'reset'; exit; } print color 'yellow'; print "NOTICE: This will entirely remove Varnish from the system and place Apache on port 80. Proceeding in 15 seconds... ctrl+c to abort!\n"; print color 'reset'; sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1); print ". ";sleep(1); print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1); print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ". ";sleep(1);print ".\n"; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Removing Varnish RPM's... "; system("rpm -qa |grep -i varnish|xargs rpm -e &> /dev/null"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'rpm -qa |grep -i varnish|xargs rpm -e' returned non-zero; got $? \n"; exception_msg(); print color 'reset'; exit(1); } system("rm -rf /etc/varnish/ >> /root/confvarnish.log"); system("rm -f /etc/sysconfig/varnish.rpmsave >> /root/confvarnish.log"); system("rm -rf /var/log/varnish >> /root/confvarnish.log"); print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Changing Apache port in cPanel configuration to 80... "; system("sed -i '/apache_port=0.0.0.0/capache_port=0.0.0.0:80' /var/cpanel/cpanel.config >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'sed -i '/apache_port=0.0.0.0/capache_port=0.0.0.0:80' /var/cpanel/cpanel.config' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } system("/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Backing up httpd.conf and rebuilding Apache configuration... "; system("cp -Rp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.conf.confvarnish.$^T >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: 'cp -Rp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.conf$^T' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } system("/scripts/rebuildhttpdconf >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/scripts/rebuildhttpdconf' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; print color 'yellow'; print "[ ",POSIX::strftime("%m/%d/%Y %H:%M:%S ", localtime) ,"] "; print color 'reset'; print "Restarting Apache... "; system("/etc/init.d/httpd restart >> /root/confvarnish.log"); if ( $? != 0 ) { print color 'red'; print "ERROR: '/etc/init.d/httpd restart' returned non-zero; got $? ; Review /root/confvarnish.log \n"; exception_msg(); print color 'reset'; exit(1); } print color 'green'; print "Done\n"; print color 'reset'; } #### ### Main() #### print color 'green'; print "-confvarnish $version\n"; print color 'reset'; my $centRelease = getOSInfo(); preChecks(); if ( $ARGV[0] eq '--help' ) { help(); } elsif ( $ARGV[0] eq '--install' ) { if ( scalar(@ARGV) != 4 ) { print "Install usage: ./$0 --install apache_port varnish_port mangmt_port\n"; exit(1); } if ( ! looks_like_number($ARGV[1]) ) { print "$ARGV[1] is not numeric.\n"; exit(1); } elsif ( !looks_like_number($ARGV[2]) ) { print "$ARGV[2] is not numeric.\n"; exit(1); } elsif ( !looks_like_number($ARGV[3]) ) { print "$ARGV[3] is not numeric.\n"; exit(1); } else { do_install("$ARGV[1]","$ARGV[2]","$ARGV[3]"); } } elsif ( $ARGV[0] eq '--remove' ) { do_remove(); } else { print "Option '$ARGV[0]' not recognized; please specify a valid option (do you need --help?).\n"; } =head1 TITLE confvarnish.pl - Automatically install and configure varnish web cache. =head1 SUMMARY Installs or removes varnish cache system on CentOS/RHEL 5 systems with cPanel. For specific commands, see the USAGE section. =head1 USAGE ./confvarnish.pl --install apache_port varnish_port mangmt_port => This moves Apache to 'apache_port', installs varnish on 'varnish_port', and sets up the management interface on 'mangmt_port'. ./confvarnish.pl --remove => This removes varnish from the system and places Apache back on port 80. =head1 AUTHOR Scott Sullivan (ssullivan@liquidweb.com) =cut