#!/usr/bin/perl
# this program will compute and write out the appl_dyn.conf file and set up
# symbolic links, according
# to the following data from the environment:
# $ARGV[0] - IP address to put in a Listen directive
# $ENV{_host_name} -> ServerName and UseCanonicalName
# logs_enabled, access_log_filename, error_log_filename ->
# ErrorLog logs/error_log, CustomLog logs/access_log combined
# use_htaccess -> AllowOverride All
# docs_dir, docs_loc -> (sym links setup)
# scripts_dir, scripts_loc -> (sym links setup)
use strict;
use warnings;
my $cfg = '';
# static header
$cfg .= "# !!! Automatically generated file. Do not edit. !!!\n";
# NB: here, we use the fact that the combination of NOT having a host name
# with UseCanonicalName set to On makes no sense in our case, since
# the normal mechanism for divining the canonical host name does not
# work in an AppLogic Appliance. This allows us to have just one property:
# host_name, which automatically enables the canonical name if it is set.
# ServerName & UseCanonicalName
my $n;
if ( $ENV{_host_name} )
{
$n = $ENV{_host_name};
$cfg .= "ServerName $n\n";
$cfg .= "UseCanonicalName On\n";
}
else
{
$cfg .= "UseCanonicalName Off\n";
}
# log files
# The log rotation won't work properly for files on the /mnt/log filesystem,
# unless we set them up properly in the /etc/logrotate.d/ config dir.
$n = '';
$n = $ENV{_logs_enabled} if $ENV{_logs_enabled};
my $elog='';
my $alog='';
if ( $n =~ m/^on$/i )
{
if ( $ENV{_error_log_filename} )
{
$elog = "$ENV{_logs_base_dir}/$ENV{_error_log_filename}";
$elog =~ s{^/+}{}o;
$elog =~ s{/+}{/}o;
die "error_log_filename: $elog is not valid for this property\n"
if ($elog eq '');
$cfg .= "ErrorLog /mnt/log/$elog\n";
}
else
{
$cfg .= "ErrorLog logs/error_log\n";
}
if ( $ENV{_access_log_filename} )
{
$alog = "$ENV{_logs_base_dir}/$ENV{_access_log_filename}";
$alog =~ s{^/+}{}o;
$elog =~ s{/+}{/}o;
die "access_log_filename: $alog is not valid for this property\n"
if ($alog eq '');
$cfg .= "SetEnvIf Request_URI ^/server-status\$ dontlog\nCustomLog /mnt/log/$alog combined env=!dontlog\n";
}
}
sub get_envdir($)
{
my $v = $_[0];
return '' unless defined $ENV{$v};
$v = $ENV{$v};
$v =~ s{^/+}{}o; # strip unnecessary / characters
$v =~ s{/+$}{}o;
die "invalid value for $_[0]:, '..' is not allowed\n" if $v =~ m{\.\.};
return '' if $v eq '';
return '/' . $v;
}
# prepare docs_dir, docs_loc, scripts_dir, scripts_loc
my $docs_dir = get_envdir ('_docs_dir');
my $docs_loc = get_envdir ('_docs_loc');
my $scripts_dir = get_envdir ('_scripts_dir');
my $scripts_loc = get_envdir ('_scripts_loc');
if ( $ENV{_use_htaccess} && $ENV{_use_htaccess} =~ m/^on$/oi )
{
$cfg .= "\n";
$cfg .= " AllowOverride All\n";
$cfg .= "\n";
}
# use_htaccess -> AllowOverride All
# ErrorLog logs/error_log, CustomLog logs/access_log combined
# $symlink_exists = eval { symlink("",""); 1 };
# set up the symlinks - delegate this to 'bash', it is better at this stuff
system (
"bash alias_set.sh /mnt/content$docs_dir /var/www html$docs_loc 1") == 0
or die "failed symlink /var/www/html$docs_loc->/mnt/content$docs_dir\n";
system (
"bash alias_set.sh /mnt/content$scripts_dir /var/www cgi-bin 2") == 0
or die "failed symlink /var/www/cgi-bin /mnt/content$scripts_dir";
# script alias (if specified)
if ( $scripts_loc ne '' )
{
$cfg .= "ScriptAlias $scripts_loc/ \"/var/www/cgi-bin/\"\n";
}
# optional include files
if ( -d "/var/www/html$docs_loc/.apache_conf" )
{
$cfg .= " Include /var/www/html$docs_loc/.apache_conf/*.conf\n"
}
if ( -f "/var/www/html$docs_loc/.htconf" )
{
$cfg .= " Include /var/www/html$docs_loc/.htconf\n"
}
$cfg .= "Listen $ARGV[0]\n" if defined $ARGV[0];
# AddHandler directives, added only if the corresponding property is not empty
if ( $ENV{_cgi_ext} )
{
$cfg .= "AddHandler cgi-script $ENV{_cgi_ext}\n"
}
if ( $ENV{_shared_perl_ext} )
{
$cfg .= "AddHandler perl-script $ENV{_shared_perl_ext}\n"
}
# MaxClients - set according to available system memory
#
#MaxClients 128
#
open H,'<', '/proc/meminfo' or die "appl_dyn.pl: fatal error, /proc not mounted";
my $mem_k=32000; # assume worst-case, if something goes wrong
while ()
{
if ( m/MemTotal:\s*(.*) kB/ )
{
$mem_k = $1;
last;
}
}
close H;
my $max_clients = ($mem_k - 16000) / 2048;
if ( $max_clients < 8) { $max_clients = 8; }
if ( $max_clients > $ENV{_max_connections} )
{ $max_clients = $ENV{_max_connections}; }
$max_clients = int($max_clients);
$cfg .= "\n";
$cfg .= "MaxClients $max_clients\n";
$cfg .= "\n";
print $cfg;
# etc/php.ini: memory_limit=
if (! -f '/etc/php.ini.dist' )
{ rename '/etc/php.ini', '/etc/php.ini.dist'; }
my $max_php_mem = (($mem_k - 16000) - $max_clients*1024) + 1023;
$max_php_mem = int ($max_php_mem / (1024*2));
if ($max_php_mem < 4) { $max_php_mem = 4; }
open H,'<','/etc/php.ini.dist';
$cfg='';
while ()
{
next if m/^\s*;/;
if (! m/^\s*memory_limit/ ) { $cfg = $cfg . $_; next; }
$cfg = $cfg . 'memory_limit = ' . $max_php_mem . "M\n";
}
close H;
open H,'>','/etc/php.ini';
print H $cfg;
close H;
exit 0;