%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2008 Best Practical Solutions, LLC
%#
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<%ONCE>
my %OPTION = (
DatabaseType => {
Section => 'Database', #loc
Widget => '/Widgets/Form/Select',
WidgetArguments => {
Description => 'Type of the database where RT will store its data', #loc
Values => [qw(mysql Pg Oracle SQLite Informix)],
ValuesLabel => {
mysql => 'MySQL', #loc
Pg => 'PostgreSQL', #loc
SQLite => 'SQLite (for experiments and development only)', #loc
Informix => 'Informix (experimental)',
},
},
},
DatabaseHost => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'The domain name of your database server', #loc
},
},
DatabasePort => {
Section => 'Database', #loc
Widget => '/Widgets/Form/Integer',
WidgetArguments => {
Description => 'Port number database server listen to', #loc
Default => 1,
DefaultLabel => 'Leave empty to use default value of the RDBMS', #loc
},
},
DatabaseName => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'Name of the database', #loc
},
},
DatabaseUser => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'The name of the user RT will use to connect to the DB', #loc
},
},
DatabasePassword => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'Password of the above user RT will use to connect to the DB', #loc
},
},
DatabaseAdmin => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'The name of the database admin we will use to create and initialize the DB', #loc
},
},
DatabaseAdminPassword => {
Section => 'Database', #loc
Widget => '/Widgets/Form/String',
WidgetArguments => {
Description => 'Password of the database admin', #loc
},
},
);
my @ORDERED_OPTIONS = qw(
DatabaseType
DatabaseHost DatabasePort DatabaseName
DatabaseAdmin DatabaseAdminPassword
DatabaseUser DatabasePassword
);
my $dump_config = sub {
my %data = @_;
my $res = '';
foreach my $option ( keys %OPTION ) {
next unless exists $data{ $option } && defined $data{ $option };
my $value = $data{ $option };
$res .= "Set(\$$option, '$value');\n";
}
$res .= "\n1;# don't delete this line\n";
return $res;
};
my $get_dbh = sub {
my ($dsn, $user, $pass) = @_;
unless ( $user ) {
my $user = RT->Config->Get('DatabaseUser');
my $password = RT->Config->Get('DatabasePassword');
}
my $dbh = DBI->connect(
$dsn, $user, $pass,
{ RaiseError => 0, PrintError => 0 },
);
unless ( $dbh ) {
return (undef, "Failed to connect to $dsn as user '$user': ". $DBI::errstr);
}
return $dbh;
};
%ONCE>
Hello, looks like it's new intallation of RT.
We're going to bla-bla... intstall RT.
% unless ( $DefinedOptions{'DatabaseHost'} ) {
The domain name or IP address of your database server.
% if ( $config{'DatabaseType'} eq 'mysql' ) {
If it's on localhost then leave it blank for slightly enhanced performance.
% }
<& $OPTION{'DatabaseHost'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabaseHost'}->{'WidgetArguments'} },
Name => 'DatabaseHost',
CurrentValue => scalar RT->Config->Get('DatabaseHost'),
&>
% } else {
RT will be using database on server <% $config{ 'DatabaseHost' } %>.
% }
% }
% if ( $CurrentOption eq 'DatabasePort' || $DefinedOptions{'DatabasePort'} ) {
Port
% unless ( $DefinedOptions{'DatabasePort'} ) {
Port number your database server listen to. If you don't know value then most
probably it listen to default port and you can leave this option empty.
<& $OPTION{'DatabasePort'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabasePort'}->{'WidgetArguments'} },
Name => 'DatabasePort',
CurrentValue => scalar RT->Config->Get('DatabasePort'),
&>
% } elsif ( $config{'DatabasePort'} ) {
RT will be connecting to port number <% $config{ 'DatabasePort' } %>.
% } else {
RT will be connecting to default port.
% }
% }
% if ( $CurrentOption eq 'DatabaseName' || $DefinedOptions{'DatabaseName'} ) {
Name
% unless ( $DefinedOptions{'DatabaseName'} ) {
% if ( $config{'DatabaseType'} eq 'SQLite' ) {
Absolute path to the database.
% } else {
Name of the database.
% }
<& $OPTION{'DatabaseName'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabaseName'}->{'WidgetArguments'} },
Name => 'DatabaseName',
CurrentValue => scalar RT->Config->Get('DatabaseName'),
&>
% } else {
'<% $config{ 'DatabaseName' } %>' will be used as the database.
% }
% }
% if ( $CurrentOption eq 'DatabaseUser' || $DefinedOptions{'DatabaseUser'} ) {
Credentials
% unless ( $DefinedOptions{'DatabaseUser'} ) {
Please enter credentials RT is going to use to connect to the DB.
This account has nothing to do with the web UI and any users' accounts.
This user will be created in your database server and granted with rights
to access RT DB and update it.
<& $OPTION{'DatabaseUser'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabaseUser'}->{'WidgetArguments'} },
Name => 'DatabaseUser',
CurrentValue => scalar RT->Config->Get('DatabaseUser'),
&>
<& $OPTION{'DatabasePassword'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabasePassword'}->{'WidgetArguments'} },
Name => 'DatabasePassword',
CurrentValue => scalar RT->Config->Get('DatabasePassword'),
&>
% } else {
RT will connect to the database using username '<% $config{ 'DatabaseUser' } %>'
with password '<% $config{ 'DatabasePassword' } %>'.
% }
% }
% if ( $CurrentOption eq 'DatabaseAdmin' || $DefinedOptions{'DatabaseAdmin'} ) {
Adminstrator credentials
% unless ( $DefinedOptions{'DatabaseAdmin'} ) {
Please enter credentials of administrator that can create RT's database on
the RDBMS server. This credentials will be used only to create the DB.
<& $OPTION{'DatabaseAdmin'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabaseAdmin'}->{'WidgetArguments'} },
Name => 'DatabaseAdmin',
CurrentValue => scalar RT->Config->Get('DatabaseAdmin'),
&>
<& $OPTION{'DatabaseAdminPassword'}->{'Widget'},
Default => 0,
%{ $OPTION{'DatabaseAdminPassword'}->{'WidgetArguments'} },
Name => 'DatabaseAdminPassword',
CurrentValue => scalar RT->Config->Get('DatabaseAdminPassword'),
&>
% } else {
RT will connect to the RDBMS server using administrator account '<% $config{ 'DatabaseAdmin' } %>'
with password '<% $config{ 'DatabaseAdminPassword' } %>'.
% }
% }
% if ( $CurrentOption ) {
<& /Elements/Submit, Name => 'NextWizardStep', Label => loc('Next') &>
% } else {
Create
Please, review your settings again.
<& /Elements/Submit, Name => 'DatabaseCreate', Label => loc('Create database') &>
% }
<%FLAGS>
inherit => undef
%FLAGS>
<%ARGS>
@DefinedOptions => ()
%ARGS>
<%INIT>
my %DefinedOptions = map { $_ => 1 } @DefinedOptions;
# process form data
my %config = ();
foreach my $option ( @DefinedOptions ) {
my $meta = $OPTION{ $option };
my $value = $m->comp( $meta->{'Widget'} .':Process',
Arguments => \%ARGS,
Default => 0,
%{ $meta->{'WidgetArguments'} },
Name => $option,
CurrentValue => scalar RT->Config->Get( $option ),
);
$config{ $option } = $value;
$config{ $option } = RT->Config->Get( $option )
if !defined $value && $meta->{'WidgetArguments'}{'Default'};
}
# checks
my %CheckErrors;
if ( $DefinedOptions{'DatabaseType'} ) {
local $@;
unless ( eval "require DBD::". $config{'DatabaseType'} ) {
$CheckErrors{'DatabaseType'} = "Couldn't load driver for ". $config{'DatabaseType'} .". Error: $@";
delete $DefinedOptions{'DatabaseType'};
}
}
my $ActionError;
if ( $ARGS{'DatabaseCreate'} ) { {
warn($dump_config->(%config));
{
open my $fh, '>', '/tmp/SiteConfig';
print $fh $dump_config->(%config);
close $fh;
}
RT->Config->LoadConfig( File => '/tmp/SiteConfig' );
require RT::Handle;
my ($dba_user, $dba_pass) = @config{'DatabaseAdmin', 'DatabaseAdminPassword'};
my ($dbh, $status, $msg);
($dbh, $msg) = $get_dbh->( RT::Handle->SystemDSN, $dba_user, $dba_pass );
unless ( $dbh ) {
$ActionError = "Couldn't connect to the DB using system DSN and admin credentials: $msg";
delete $DefinedOptions{$_} foreach qw(DatabaseHost DatabasePort DatabaseAdmin DatabaseAdminPassword);
last;
}
($status, $msg) = RT::Handle->CreateDatabase( $dbh );
unless ( $status ) {
$ActionError = "Couldn't create DB: $msg";
last;
}
($dbh, $msg) = $get_dbh->( RT::Handle->DSN, $dba_user, $dba_pass );
unless ( $dbh ) {
$ActionError = "Couldn't connect to the new RT DB using admin credentials: $msg";
delete $DefinedOptions{$_} foreach qw(DatabaseHost DatabasePort DatabaseAdmin DatabaseAdminPassword);
last;
}
($status, $msg) = RT::Handle->InsertSchema( $dbh );
unless ( $status ) {
$ActionError = "Couldn't insert database schema: $msg";
last;
}
($status, $msg) = RT::Handle->InsertACL( $dbh );
unless ( $status ) {
$ActionError = "Couldn't setup access control: $msg";
last;
}
RT::ConnectToDatabase();
RT::InitLogging();
RT::InitSystemObjects();
($status, $msg) = $RT::Handle->InsertInitialData;
$RT::Handle = new RT::Handle;
RT::Init();
($status, $msg) = $RT::Handle->InsertData( $RT::EtcPath . "/initialdata" );
} }
my @RequiredOptions = @ORDERED_OPTIONS;
if ( $DefinedOptions{'DatabaseType'} && $config{'DatabaseType'} eq 'SQLite' ) {
@RequiredOptions = qw(DatabaseType DatabaseName);
}
if ( $DefinedOptions{'DatabaseType'} && $config{'DatabaseType'} eq 'mysql' && $DefinedOptions{'DatabaseHost'} ) {
@RequiredOptions = grep $_ ne 'DatabaseRTHost', @RequiredOptions
if !$config{'DatabaseHost'} || $config{'DatabaseHost'} eq 'localhost';
}
my %RequiredOptions = map { $_ => 1 } @RequiredOptions;
my $CurrentOption = (grep !$DefinedOptions{ $_ }, @RequiredOptions)[0];
%INIT>