Skip to content

Commit

Permalink
Item13023: rewrite for refactoredBootstrap
Browse files Browse the repository at this point in the history
Calls Load::bootstrapConfig instead of duplicating all the core code.
  • Loading branch information
gac410 committed Sep 11, 2014
1 parent ea13703 commit 6c06711
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 215 deletions.
2 changes: 1 addition & 1 deletion data/System/TestBootstrapPlugin.txt
Expand Up @@ -2,7 +2,7 @@
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
---+!! Empty Plugin
---+!! Test Bootstrap Plugin

%SHORTDESCRIPTION%

Expand Down
245 changes: 31 additions & 214 deletions lib/Foswiki/Plugins/TestBootstrapPlugin.pm
Expand Up @@ -48,225 +48,32 @@ sub initPlugin {
return 1;
}

sub _workOutOS {
unless ( ( $boot_cfg{DetailedOS} = $^O ) ) {
require Config;
$boot_cfg{DetailedOS} = $Config::Config{'osname'};
}
$boot_cfg{OS} = 'UNIX';
if ( $boot_cfg{DetailedOS} =~ /darwin/i ) { # MacOS X
$boot_cfg{OS} = 'UNIX';
}
elsif ( $boot_cfg{DetailedOS} =~ /Win/i ) {
$boot_cfg{OS} = 'WINDOWS';
}
elsif ( $boot_cfg{DetailedOS} =~ /vms/i ) {
$boot_cfg{OS} = 'VMS';
}
elsif ( $boot_cfg{DetailedOS} =~ /bsdos/i ) {
$boot_cfg{OS} = 'UNIX';
}
elsif ( $boot_cfg{DetailedOS} =~ /dos/i ) {
$boot_cfg{OS} = 'DOS';
}
elsif ( $boot_cfg{DetailedOS} =~ /^MacOS$/i ) { # MacOS 9 or earlier
$boot_cfg{OS} = 'MACINTOSH';
}
elsif ( $boot_cfg{DetailedOS} =~ /os2/i ) {
$boot_cfg{OS} = 'OS2';
}
}

sub _BOOTSTRAP {
my ( $session, $params, $topic, $web, $topicObject ) = @_;

my $msg = "<blockquote><verbatim>\n";
my $boot_cfg;
my $resp;

# Try to repair $boot_cfg to a minimal configuration,
# using paths and URLs relative to this request. If URL
# rewriting is happening in the web server this is likely
# to go down in flames, but it gives us the best chance of
# recovering. We need to guess values for all the vars that
# would otherwise default to NOT SET.
eval "require FindBin";
return "Could not load FindBin to support configuration recovery: $@"
if $@;
FindBin::again(); # in case we are under mod_perl or similar
$FindBin::Bin =~ /^(.*)$/;
my $bin = $1;
$FindBin::Script =~ /^(.*)$/;
my $script = $1;
$msg .=
"AUTOCONFIG: Found Bin dir: $bin, Script name: $script using FindBin\n"
if (TRAUTO);

$boot_cfg{ScriptSuffix} = ( fileparse( $script, qr/\.[^.]*/ ) )[2];
print STDERR "AUTOCONFIG: Found SCRIPT SUFFIX $boot_cfg{ScriptSuffix} \n"
if ( TRAUTO && $boot_cfg{ScriptSuffix} );

my $protocol = $ENV{HTTPS} ? 'https' : 'http';
if ( $ENV{HTTP_HOST} ) {
$boot_cfg{DefaultUrlHost} = "$protocol://$ENV{HTTP_HOST}";
$msg .=
"AUTOCONFIG: Set DefaultUrlHost $boot_cfg{DefaultUrlHost} from HTTP_HOST $ENV{HTTP_HOST} \n"
if (TRAUTO);
}
elsif ( $ENV{SERVER_NAME} ) {
$boot_cfg{DefaultUrlHost} = "$protocol://$ENV{SERVER_NAME}";
$msg .=
"AUTOCONFIG: Set DefaultUrlHost $boot_cfg{DefaultUrlHost} from SERVER_NAME $ENV{SERVER_NAME} \n"
if (TRAUTO);
}
else {
# OK, so this is barfilicious. Think of something better.
$boot_cfg{DefaultUrlHost} = "$protocol://localhost";
$msg .=
"AUTOCONFIG: barfilicious: Set DefaultUrlHost $boot_cfg{DefaultUrlHost} \n"
if (TRAUTO);
}

# Examine the CGI path. The 'view' script it typically removed from the
# URL when using "Short URLs. If this BEGIN block is being run by
# 'view', then $boot_cfg{ScriptUrlPaths}{view} will be correctly
# bootstrapped. If run for any other script, it will be set to a
# reasonable though probably incorrect default.
if ( $ENV{SCRIPT_NAME} ) {
$msg .= "AUTOCONFIG: Found SCRIPT $ENV{SCRIPT_NAME} \n"
if (TRAUTO);

if ( $ENV{SCRIPT_NAME} =~ m{^(.*?)/$script(\b|$)} ) {

# Conventional URLs with path and script
$boot_cfg{ScriptUrlPath} = $1;
$boot_cfg{ScriptUrlPaths}{view} =
$1 . '/view' . $boot_cfg{ScriptSuffix};

# This might not work, depending on the websrver config,
# but it's the best we can do
$boot_cfg{PubUrlPath} = "$1/../pub";
}
else {
# Short URLs but with a path
$msg .= "AUTOCONFIG: Found path, but no script. short URLs \n"
if (TRAUTO);
$boot_cfg{ScriptUrlPath} = $ENV{SCRIPT_NAME} . '/bin';
$boot_cfg{ScriptUrlPaths}{view} = $ENV{SCRIPT_NAME};
$boot_cfg{PubUrlPath} = $ENV{SCRIPT_NAME} . '/pub';
}
}
else {
# No script, no path, shortest URLs
$msg .= "AUTOCONFIG: No path, No script, probably shorter URLs \n"
if (TRAUTO);
$boot_cfg{ScriptUrlPaths}{view} = '';
$boot_cfg{ScriptUrlPath} = '/bin';
$boot_cfg{PubUrlPath} = '/pub';
}
{
local *STDERR;
my $log;
open STDERR, '>', \$log;

$msg .= "AUTOCONFIG: Using ScriptUrlPath $boot_cfg{ScriptUrlPath} \n"
if (TRAUTO);
$msg .= "AUTOCONFIG: Using {ScriptUrlPaths}{view} "
. (
( defined $boot_cfg{ScriptUrlPaths}{view} )
? $boot_cfg{ScriptUrlPaths}{view}
: 'undef'
)
. "\n"
if (TRAUTO);
$msg .= "AUTOCONFIG: Using PubUrlPath: $boot_cfg{PubUrlPath} \n"
if (TRAUTO);

my %rel_to_root = (
DataDir => { dir => 'data', required => 0 },
LocalesDir => { dir => 'locale', required => 0 },
PubDir => { dir => 'pub', required => 0 },
ToolsDir => { dir => 'tools', required => 0 },
WorkingDir => {
dir => 'working',
required => 1,
validate_file => 'README'
},
TemplateDir => {
dir => 'templates',
required => 1,
validate_file => 'configure.tmpl'
},
ScriptDir => {
dir => 'bin',
required => 1,
validate_file => 'setlib.cfg'
}
);

# Note that we don't resolve x/../y to y, as this might
# confuse soft links
my $root = File::Spec->catdir( $bin, File::Spec->updir() );
$root =~ s{\\}{/}g;
my $fatal = '';
my $warn = '';
while ( my ( $key, $def ) = each %rel_to_root ) {
$boot_cfg{$key} = File::Spec->rel2abs( $def->{dir}, $root );
$boot_cfg{$key} = abs_path( $boot_cfg{$key} );
( $boot_cfg{$key} ) = $boot_cfg{$key} =~ m/^(.*)$/; # untaint

$msg .= "AUTOCONFIG: $key = $boot_cfg{$key} \n"
if (TRAUTO);

if ( -d $boot_cfg{$key} ) {
if ( $def->{validate_file}
&& !-e "$boot_cfg{$key}/$def->{validate_file}" )
{
$fatal .=
"\n{$key} (guessed $boot_cfg{$key}) $boot_cfg{$key}/$def->{validate_file} not found";
}
}
elsif ( $def->{required} ) {
$fatal .= "\n{$key} (guessed $boot_cfg{$key})";
}
else {
$warn .= "\n{$key} could not be guessed";
}
}
if ($fatal) {
$msg .= <<EPITAPH;
Unable to bootstrap configuration. LocalSite.cfg could not be loaded,
and Foswiki was unable to guess the locations of the following critical
directories: $fatal
EPITAPH
( $boot_cfg, $resp ) = _runBootstrap();
close STDERR;
$msg .= $resp . "\n\n";
$msg .= $log;
$msg .= "</verbatim><noautolink>\n";
}

_workOutOS();

$boot_cfg{isVALID} = 1;
$boot_cfg{isBOOTSTRAPPING} = 1;

# Note: message is not I18N'd because there is no point; there
# is no localisation in a default cfg derived from Foswiki.spec
$msg .= <<BOOTS;
*WARNING !LocalSite.cfg could not be found, or failed to load.* This
Foswiki is running using a bootstrap configuration worked
out by detecting the layout of the installation. Any requests made to this
Foswiki will be treated as requests made by an administrator with full rights
to make changes! You should either:
* correct any permissions problems with an existing !LocalSite.cfg (see the webserver error logs for details), or
* visit [[%SCRIPTURL{configure}%?VIEWPATH=$boot_cfg{ScriptUrlPaths}{view}][configure]] as soon as possible to generate a new one.
BOOTS

require Data::Dumper;

#$msg .= Data::Dumper::Dumper( \%boot_cfg );

$msg .= "</verbatim>\n";
$msg .= "<noautolink>\n";
$msg .= "| *Key* | *Current* | *Bootstrap* |\n";

foreach my $key ( sort keys %boot_cfg ) {
foreach my $key ( sort keys %$boot_cfg ) {
next if ( $key eq 'BOOTSTRAP' );

# SMELL: Not very pretty
if ( ref( $boot_cfg{$key} ) eq 'HASH' ) {
foreach my $k2 ( keys %{ $boot_cfg{$key} } ) {
if ( ref( $boot_cfg->{$key} ) eq 'HASH' ) {
foreach my $k2 ( keys %{ $boot_cfg->{$key} } ) {
my $cur =
( defined $Foswiki::cfg{$key}{$k2} )
? $Foswiki::cfg{$key}{$k2}
Expand All @@ -276,12 +83,12 @@ BOOTS
? $Foswiki::cfg{$key}{$k2}
: '(empty)';
my $boot =
( defined $boot_cfg{$key}{$k2} )
? $boot_cfg{$key}{$k2}
( defined $boot_cfg->{$key}{$k2} )
? $boot_cfg->{$key}{$k2}
: '(undefined)';
$boot =
( length $boot_cfg{$key}{$k2} )
? $boot_cfg{$key}{$k2}
( length $boot_cfg->{$key}{$k2} )
? $boot_cfg->{$key}{$k2}
: '(empty)';
$msg .= "| ={$key}{$k2}= | =$cur= | =$boot= |\n";
}
Expand All @@ -295,14 +102,24 @@ BOOTS
$cur =
( length $Foswiki::cfg{$key} ) ? $Foswiki::cfg{$key} : '(empty)';
my $boot =
( defined $boot_cfg{$key} ) ? $boot_cfg{$key} : '(undefined)';
$boot = ( length $boot_cfg{$key} ) ? $boot_cfg{$key} : '(empty)';
( defined $boot_cfg->{$key} ) ? $boot_cfg->{$key} : '(undefined)';
$boot =
( length $boot_cfg->{$key} ) ? $boot_cfg->{$key} : '(empty)';
$msg .= "| =$key= | =$cur= | =$boot= |\n";
}
}

return "$msg</noautolink></blockquote>\n";

}

sub _runBootstrap {
local %Foswiki::cfg = ( Engine => $Foswiki::cfg{Engine} );
my $msg = Foswiki::Configure::Load::bootstrapConfig(1);

#my %boot_cfg = %Foswiki::cfg;
return ( \%Foswiki::cfg, $msg );
}
1;

__END__
Expand Down

0 comments on commit 6c06711

Please sign in to comment.