All pastes #766123 Raw Edit

http release

public text v1 · immutable
·published 2007-11-08 17:06 UTC
rendered paste body
#!/usr/bin/perl

###############################
# Apache HTTPd Install Script #
# By Jorge Schrauwen 2007     #
###############################

# todo
# - svn checkout -> fetch_svn
# - don't download source tarball if allready present (do make clean)
# - module manager
#   - db with modules and location
#   - apxs version detect if version manager on

# require
use Cwd;
use File::Path;
use Term::ANSIColor;
use Term::Menu;
use WWW::Mechanize;
use HTML::TreeBuilder;

# configuration
my $srcDir 		= getcwd() . '/';	# set default source path
my $instDir 		= '/server/httpd';	# set default install location
my $bVersionManage 	= 0;			# disable/enable source version mangement (not for trunk);


# variables
my $srcType = 1;
my $cfgStartOptions = 0;
my $cfgOptions = './configure';	

# functions
sub header(){
	system('clear');
	print "#" x 35 . "\n";
	print "# Apache HTTPd Installer          #\n";
	print "# By Jorge Schrauwen 2007         #\n";
	print "#" x 35 . "\n\n";	
}

sub main_menu(){
	header();
	my $prompt = new Term::Menu(
		beforetext	=>	'Please select the kind of source you want to use:',
		aftertext		=>	'Prompt: ',
	);
	my $answer = $prompt->menu(
		release		=>	['Release', '1'],
		tag_role		=>	['Tag & Role', '2'],
		svntrunk		=>	['SVN Trunk', '3'],
		doexit		=>	['Exit', '0']
	);
	if(defined($answer)){
		if( $answer eq 'release'  )	{ $srcType = 1; fetch_source(); };
		if( $answer eq 'tag_role' )	{ $srcType = 2; fetch_source(); };
		if( $answer eq 'svntrunk' )	{ $srcType = 3; print "SVN Not Implemented!\n"; };
		if( $answer eq 'doexit'   )	{ exit; }
	}
}

sub fetch_source(){
	header();
	# get latest version
	@url = ( 'http://apache.belnet.be/httpd/', 'http://httpd.apache.org/dev/dist/' );
	my $oMech = WWW::Mechanize->new( 'agent' => 'HTTPD Installer/1.1', 'autocheck' => '1' );
	$oMech->get(@url[$srcType-1]);
	
	my $pageIndex = HTML::TreeBuilder->new_from_content($oMech->content());
	my @links = $pageIndex->find_by_tag_name('a');
	
	my $sLatestVersion = '';
	my $i = 1;
	while( length( $sLatestVersion ) == 0 ) {
		my $node = @links[length(@links)-$i]->as_text();
		# check if its the good node
		if( substr ( $node, 0, 5) eq "httpd" ) {
			if( substr ( $node, -6) eq "tar.gz" ) {
				$sLatestVersion = substr( $node, 0, -7 );
			}
		}
		$i++;
	}
	print "Do you wish to compile $sLatestVersion? (Y/n): ";
	my $answer = <>;
	chomp( $answer );
	if( $answer eq 'n' ) { exit; }
	
	# get aditional info
	pre_req_info(); 
	
	# download the source tarball
	header();
	print "Downloading $sLatestVersion.tar.gz...\n";
	system( "wget -c @url[$srcType-1]$sLatestVersion.tar.gz" );
	
	# extract source and remove tarball
	header();
	print "Extracting source...\n";
	if( $bVersionManage ) { mkpath( substr( $sLatestVersion, 6 ) ); }
	system( "tar xvf $sLatestVersion.tar.gz" );
	system( "rm $sLatestVersion.tar.gz" );
	if( $bVersionManage ) {
		system( "mv $sLatestVersion " . substr( $sLatestVersion, 6 ) . "/httpd" );
		chdir(substr( $sLatestVersion, 6 ) . "/httpd");
	} else {
		chdir($sLatestVersion);
	}
	configure();
}

sub pre_req_info(){
	print "Where do you want to save the source? ($srcDir): ";
	my $newSrcDir = <>;
	chomp( $newSrcDir );
	if( length( $newSrcDir ) > 0 ) { $srcDir = $newSrcDir; }
	if ( !-d $srcDir ) {
		mkpath( $srcDir );
	}
	chdir( $srcDir );
	
	print "Where do you want to install httpd? ($instDir): ";
	my $newInstDir = <>;
	chomp( $newInstDir );
	if( length( $newInstDir ) > 0 ) { $instDir = $newInstDir; }
	$cfgOptions = $cfgOptions . " --prefix=$instDir";
}

sub configure(){
	header();	
	my $prompt = new Term::Menu(
		beforetext	=>	'Configuration Options:',
		aftertext		=>	'Prompt: ',
	);
	my $answer = $prompt->menu(
		auto			=>	['Configure All Modules', '1'],
		manual		=>	['Manual Module Selection', '2'],
	);
	if(defined($answer)){
		if( $answer eq 'auto'  )	{ auto_config(); };
		if( $answer eq 'manual' )	{ manual_config(); };
	}
}

sub auto_config(){
	header();	
	# select MPM
	my $prompt = new Term::Menu(
		beforetext	=>	'Select your MPM:',
		aftertext		=>	'Prompt: ',
	);
	my $mpm = $prompt->menu(
		worker			=>	['Worker', '1'],
		prefork			=>	['Prefork', '2'],
		event			=>	['Event', '3'],
	);
	$cfgOptions = $cfgOptions . " --with-mpm=$mpm";
	$cfgStartOptions = length( $cfgOptions );
	
	# enable almost all modules
	$cfgOptions = $cfgOptions . " --enable-mods-shared=\"all cache proxy authn_alias mem_cache file_cache charset_lite dav_lock disk_cache";
	
	# enable mod_ssl
	print "Enable mod_ssl? (Y/n): ";
	my $answer = <>;
	chomp( $answer );
	if( ( $answer eq 'y' ) or ( $answer eq '' ) ) { $cfgOptions = $cfgOptions . " ssl"; }
	
	# enable ldap support
	print "Enable LDAP support? (y/N): ";
	my $answer = <>;
	chomp( $answer );
	if( $answer eq 'y' ) { 
		$cfgOptions = $cfgOptions . " ldap\" --with-ldap"; 
	} else {
		$cfgOptions = $cfgOptions . "\""; 	
	}
	
	build();
}

sub manual_config(){
	header();	
	# enable mod_ssl
	print "Please enter your own ./configure options (do not include the ./configure):\n";
	my $answer = <>;
	chomp( $answer );
	$cfgOptions = $cfgOptions . " $answer";
	
	build();
}

sub build(){
	header();	
	# confirm compile
	print "Version: $sLatestVersion\n";
	print "Install Directory: $instDir\n";
	print "Configuration Options:\n";
	print substr( $cfgOptions, $cfgStartOptions ) . "\n\n";
	print "Do you want to compile? (Y/n): ";
	my $answer = <>;
	chomp( $answer );
	if( $answer eq 'n' ) { exit; }
	
	# configuring
	header();	
	print "Configuring...\n";
	system( $cfgOptions );
	
	# running make
	header();	
	print "Running make...\n";
	system( 'make' );
	
	# running make install
	header();	
	print "Running make install...\n";
	print "Enter your sudo password to continue or crtl+c to terminate!\n";
	system( 'sudo make install' );
	
	# done
	header();	
	print "File locations:\n";
	print "   apxs       ==> $instDir/bin/apxs\n";
	print "   apachectl  ==> $instDir/bin/apachectl\n";
	print "   httpd.conf ==> $instDir/conf/httpd.cong\n";
	print "\n";
	print "Installation Complete!\n";
}

# init code
main_menu();