RT count still running on 3.8.10 - porting to RT4.
[usit-rt.git] / sbin / rt-server.fcgi
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl -w
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
6# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
14# This work is made available to you under the terms of Version 2 of
15# the GNU General Public License. A copy of that license should have
16# been provided with this software, but in any event can be snarfed
17# from www.gnu.org.
18#
19# This work is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27# 02110-1301 or visit their web page on the internet at
28# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
33# (The following paragraph is not intended to limit the rights granted
34# to you to modify and distribute this software under the terms of
35# the GNU General Public License and is only of importance to you if
36# you choose to contribute your changes and enhancements to the
37# community by submitting them to Best Practical Solutions, LLC.)
38#
39# By intentionally submitting any modifications, corrections or
40# derivatives to this work, or any other work intended for use with
41# Request Tracker, to Best Practical Solutions, LLC, you confirm that
42# you are the copyright holder for those contributions and you grant
43# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44# royalty-free, perpetual, license to use, copy, create derivative
45# works based on those contributions, and sublicense and distribute
46# those contributions and any derivatives thereof.
47#
48# END BPS TAGGED BLOCK }}}
49use warnings;
50use strict;
51
52use lib '/www/data/rt/rt-perl/current-perl10/share/perl5';
53use lib '/www/data/rt/rt-perl/current-perl10/lib/perl5';
54use lib '/www/data/rt/rt-perl/current-perl10/lib64/perl5';
55
56# fix lib paths, some may be relative
57BEGIN {
58 die <<EOT if ${^TAINT};
59RT does not run under Perl's "taint mode". Remove -T from the command
60line, or remove the PerlTaintCheck parameter from your mod_perl
61configuration.
62EOT
63
64 require File::Spec;
65 my @libs = ("lib", "local/lib");
66 my $bin_path;
67
68 for my $lib (@libs) {
69 unless ( File::Spec->file_name_is_absolute($lib) ) {
70 unless ($bin_path) {
71 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
72 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
73 }
74 else {
75 require FindBin;
76 no warnings "once";
77 $bin_path = $FindBin::Bin;
78 }
79 }
80 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
81 }
82 unshift @INC, $lib;
83 }
84
85}
86
87use Getopt::Long;
88no warnings 'once';
89
90if (grep { m/help/ } @ARGV) {
91 require Pod::Usage;
92 print Pod::Usage::pod2usage( { verbose => 2 } );
93 exit;
94}
95
96require RT;
97RT->LoadConfig();
98require Module::Refresh if RT->Config->Get('DevelMode');
99
100require RT::Handle;
101my ($integrity, $state, $msg) = RT::Handle->CheckIntegrity;
102
103unless ( $integrity ) {
104 print STDERR <<EOF;
105
106RT couldn't connect to the database where tickets are stored.
107If this is a new installation of RT, you should visit the URL below
108to configure RT and initialize your database.
109
110If this is an existing RT installation, this may indicate a database
111connectivity problem.
112
113The error RT got back when trying to connect to your database was:
114
115$msg
116
117EOF
118
119 require RT::Installer;
120 # don't enter install mode if the file exists but is unwritable
121 if (-e RT::Installer->ConfigFile && !-w _) {
122 die 'Since your configuration exists ('
123 . RT::Installer->ConfigFile
124 . ") but is not writable, I'm refusing to do anything.\n";
125 }
126
127 RT->Config->Set( 'LexiconLanguages' => '*' );
128 RT::I18N->Init;
129
130 RT->InstallMode(1);
131} else {
132 RT->Init();
133
134 my ($status, $msg) = RT::Handle->CheckCompatibility( $RT::Handle->dbh, 'post');
135 unless ( $status ) {
136 print STDERR $msg, "\n\n";
137 exit -1;
138 }
139}
140
141# we must disconnect DB before fork
142if ($RT::Handle) {
143 $RT::Handle->dbh(undef);
144 undef $RT::Handle;
145}
146
147require RT::Interface::Web::Handler;
148my $app = RT::Interface::Web::Handler->PSGIApp;
149
150if ($ENV{RT_TESTING}) {
151 my $screen_logger = $RT::Logger->remove('screen');
152 require Log::Dispatch::Perl;
153 $RT::Logger->add(
154 Log::Dispatch::Perl->new(
155 name => 'rttest',
156 min_level => $screen_logger->min_level,
157 action => {
158 error => 'warn',
159 critical => 'warn'
160 }
161 )
162 );
163 require Plack::Middleware::Test::StashWarnings;
164 $app = Plack::Middleware::Test::StashWarnings->wrap($app);
165}
166
167# when used as a psgi file
168if (caller) {
169 return $app;
170}
171
172
173# load appropriate server
174
175require Plack::Runner;
176
177my $is_fastcgi = $0 =~ m/fcgi$/;
178my $r = Plack::Runner->new( $0 =~ 'standalone' ? ( server => 'Standalone' ) :
179 $is_fastcgi ? ( server => 'FCGI' )
180 : (),
181 env => 'deployment' );
182
183# figure out the port
184my $port;
185
186# handle "rt-server 8888" for back-compat, but complain about it
187if ($ARGV[0] && $ARGV[0] =~ m/^\d+$/) {
188 warn "Deprecated: please run $0 --port $ARGV[0] instead\n";
189 unshift @ARGV, '--port';
190}
191
192my @args = @ARGV;
193
194use List::MoreUtils 'last_index';
195my $last_index = last_index { $_ eq '--port' } @args;
196
197my $explicit_port;
198
199if ( $last_index != -1 && $args[$last_index+1] =~ /^\d+$/ ) {
200 $explicit_port = $args[$last_index+1];
201 $port = $explicit_port;
202
203 # inform the rest of the system what port we manually chose
204 my $old_app = $app;
205 $app = sub {
206 my $env = shift;
207
208 $env->{'rt.explicit_port'} = $port;
209
210 $old_app->($env, @_);
211 };
212}
213else {
214 # default to the configured WebPort and inform Plack::Runner
215 $port = RT->Config->Get('WebPort') || '8080';
216 push @args, '--port', $port;
217}
218
219push @args, '--server', 'Standalone' if RT->InstallMode;
220push @args, '--server', 'Starlet' unless $r->{server} || grep { m/--server/ } @args;
221
222$r->parse_options(@args);
223
224delete $r->{options} if $is_fastcgi; ### mangle_host_port_socket ruins everything
225
226unless ($r->{env} eq 'development') {
227 push @{$r->{options}}, server_ready => sub {
228 my($args) = @_;
229 my $name = $args->{server_software} || ref($args); # $args is $server
230 my $host = $args->{host} || 0;
231 my $proto = $args->{proto} || 'http';
232 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
233 };
234}
235eval { $r->run($app) };
236if (my $err = $@) {
237 handle_startup_error($err);
238}
239
240exit 0;
241
242sub handle_startup_error {
243 my $err = shift;
244 if ( $err =~ /listen/ ) {
245 handle_bind_error();
246 } else {
247 die
248 "Something went wrong while trying to run RT's standalone web server:\n\t"
249 . $err;
250 }
251}
252
253
254sub handle_bind_error {
255
256 print STDERR <<EOF;
257WARNING: RT couldn't start up a web server on port @{[$port]}.
258This is often the case if the port is already in use or you're running @{[$0]}
259as someone other than your system's "root" user. You may also specify a
260temporary port with: $0 --port <port>
261EOF
262
263 if ($explicit_port) {
264 print STDERR
265 "Please check your system configuration or choose another port\n\n";
266 }
267}
268
269__END__
270
271=head1 NAME
272
273rt-server - RT standalone server
274
275=head1 SYNOPSIS
276
277 # runs prefork server listening on port 8080, requires Starlet
278 rt-server --port 8080
279
280 # runs server listening on port 8080
281 rt-server --server Standalone --port 8080
282 # or
283 standalone_httpd --port 8080
284
285 # runs other PSGI server on port 8080
286 rt-server --server Starman --port 8080