]>
Commit | Line | Data |
---|---|---|
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 }}} | |
49 | use warnings; | |
50 | use strict; | |
51 | ||
52 | # fix lib paths, some may be relative | |
53 | BEGIN { | |
54 | die <<EOT if ${^TAINT}; | |
55 | RT does not run under Perl's "taint mode". Remove -T from the command | |
56 | line, or remove the PerlTaintCheck parameter from your mod_perl | |
57 | configuration. | |
58 | EOT | |
59 | ||
60 | require File::Spec; | |
61 | my @libs = ("lib", "local/lib"); | |
62 | my $bin_path; | |
63 | ||
64 | for my $lib (@libs) { | |
65 | unless ( File::Spec->file_name_is_absolute($lib) ) { | |
66 | unless ($bin_path) { | |
67 | if ( File::Spec->file_name_is_absolute(__FILE__) ) { | |
68 | $bin_path = ( File::Spec->splitpath(__FILE__) )[1]; | |
69 | } | |
70 | else { | |
71 | require FindBin; | |
72 | no warnings "once"; | |
73 | $bin_path = $FindBin::Bin; | |
74 | } | |
75 | } | |
76 | $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib ); | |
77 | } | |
78 | unshift @INC, $lib; | |
79 | } | |
80 | ||
81 | } | |
82 | ||
83 | use Getopt::Long; | |
84 | no warnings 'once'; | |
85 | ||
86 | if (grep { m/help/ } @ARGV) { | |
87 | require Pod::Usage; | |
88 | print Pod::Usage::pod2usage( { verbose => 2 } ); | |
89 | exit; | |
90 | } | |
91 | ||
92 | require RT; | |
93 | RT->LoadConfig(); | |
94 | RT->InitLogging(); | |
95 | require Module::Refresh if RT->Config->Get('DevelMode'); | |
96 | ||
97 | require RT::Handle; | |
98 | my ($integrity, $state, $msg) = RT::Handle->CheckIntegrity; | |
99 | ||
100 | unless ( $integrity ) { | |
101 | print STDERR <<EOF; | |
102 | ||
103 | RT couldn't connect to the database where tickets are stored. | |
104 | If this is a new installation of RT, you should visit the URL below | |
105 | to configure RT and initialize your database. | |
106 | ||
107 | If this is an existing RT installation, this may indicate a database | |
108 | connectivity problem. | |
109 | ||
110 | The error RT got back when trying to connect to your database was: | |
111 | ||
112 | $msg | |
113 | ||
114 | EOF | |
115 | ||
116 | require RT::Installer; | |
117 | # don't enter install mode if the file exists but is unwritable | |
118 | if (-e RT::Installer->ConfigFile && !-w _) { | |
119 | die 'Since your configuration exists (' | |
120 | . RT::Installer->ConfigFile | |
121 | . ") but is not writable, I'm refusing to do anything.\n"; | |
122 | } | |
123 | ||
124 | RT->Config->Set( 'LexiconLanguages' => '*' ); | |
125 | RT::I18N->Init; | |
126 | ||
127 | RT->InstallMode(1); | |
128 | } else { | |
129 | RT->Init(); | |
130 | ||
131 | my ($status, $msg) = RT::Handle->CheckCompatibility( $RT::Handle->dbh, 'post'); | |
132 | unless ( $status ) { | |
133 | print STDERR $msg, "\n\n"; | |
134 | exit -1; | |
135 | } | |
136 | } | |
137 | ||
138 | # we must disconnect DB before fork | |
139 | if ($RT::Handle) { | |
140 | $RT::Handle->dbh(undef); | |
141 | undef $RT::Handle; | |
142 | } | |
143 | ||
144 | require RT::Interface::Web::Handler; | |
145 | my $app = RT::Interface::Web::Handler->PSGIApp; | |
146 | ||
147 | if ($ENV{RT_TESTING}) { | |
148 | my $screen_logger = $RT::Logger->remove('screen'); | |
149 | require Log::Dispatch::Perl; | |
150 | $RT::Logger->add( | |
151 | Log::Dispatch::Perl->new( | |
152 | name => 'rttest', | |
153 | min_level => $screen_logger->min_level, | |
154 | action => { | |
155 | error => 'warn', | |
156 | critical => 'warn' | |
157 | } | |
158 | ) | |
159 | ); | |
160 | require Plack::Middleware::Test::StashWarnings; | |
161 | $app = Plack::Middleware::Test::StashWarnings->wrap($app); | |
162 | } | |
163 | ||
164 | # when used as a psgi file | |
165 | if (caller) { | |
166 | return $app; | |
167 | } | |
168 | ||
169 | ||
170 | # load appropriate server | |
171 | ||
172 | require Plack::Runner; | |
173 | ||
174 | my $is_fastcgi = $0 =~ m/fcgi$/; | |
175 | my $r = Plack::Runner->new( $0 =~ /standalone/ ? ( server => 'Standalone' ) : | |
176 | $is_fastcgi ? ( server => 'FCGI' ) | |
177 | : (), | |
178 | env => 'deployment' ); | |
179 | ||
180 | # figure out the port | |
181 | my $port; | |
182 | ||
183 | # handle "rt-server 8888" for back-compat, but complain about it | |
184 | if ($ARGV[0] && $ARGV[0] =~ m/^\d+$/) { | |
185 | warn "Deprecated: please run $0 --port $ARGV[0] instead\n"; | |
186 | unshift @ARGV, '--port'; | |
187 | } | |
188 | ||
189 | my @args = @ARGV; | |
190 | ||
191 | use List::MoreUtils 'last_index'; | |
192 | my $last_index = last_index { $_ eq '--port' } @args; | |
193 | ||
194 | my $explicit_port; | |
195 | ||
196 | if ( $last_index != -1 && $args[$last_index+1] =~ /^\d+$/ ) { | |
197 | $explicit_port = $args[$last_index+1]; | |
198 | $port = $explicit_port; | |
199 | ||
200 | # inform the rest of the system what port we manually chose | |
201 | my $old_app = $app; | |
202 | $app = sub { | |
203 | my $env = shift; | |
204 | ||
205 | $env->{'rt.explicit_port'} = $port; | |
206 | ||
207 | $old_app->($env, @_); | |
208 | }; | |
209 | } | |
210 | else { | |
211 | # default to the configured WebPort and inform Plack::Runner | |
212 | $port = RT->Config->Get('WebPort') || '8080'; | |
213 | push @args, '--port', $port; | |
214 | } | |
215 | ||
216 | push @args, '--server', 'Standalone' if RT->InstallMode; | |
217 | push @args, '--server', 'Starlet' unless $r->{server} || grep { m/--server/ } @args; | |
218 | ||
219 | $r->parse_options(@args); | |
220 | ||
221 | delete $r->{options} if $is_fastcgi; ### mangle_host_port_socket ruins everything | |
222 | ||
223 | unless ($r->{env} eq 'development') { | |
224 | push @{$r->{options}}, server_ready => sub { | |
225 | my($args) = @_; | |
226 | my $name = $args->{server_software} || ref($args); # $args is $server | |
227 | my $host = $args->{host} || 0; | |
228 | my $proto = $args->{proto} || 'http'; | |
229 | print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n"; | |
230 | }; | |
231 | } | |
232 | eval { $r->run($app) }; | |
233 | if (my $err = $@) { | |
234 | handle_startup_error($err); | |
235 | } | |
236 | ||
237 | exit 0; | |
238 | ||
239 | sub handle_startup_error { | |
240 | my $err = shift; | |
241 | if ( $err =~ /listen/ ) { | |
242 | handle_bind_error(); | |
243 | } else { | |
244 | die | |
245 | "Something went wrong while trying to run RT's standalone web server:\n\t" | |
246 | . $err; | |
247 | } | |
248 | } | |
249 | ||
250 | ||
251 | sub handle_bind_error { | |
252 | ||
253 | print STDERR <<EOF; | |
254 | WARNING: RT couldn't start up a web server on port @{[$port]}. | |
255 | This is often the case if the port is already in use or you're running @{[$0]} | |
256 | as someone other than your system's "root" user. You may also specify a | |
257 | temporary port with: $0 --port <port> | |
258 | EOF | |
259 | ||
260 | if ($explicit_port) { | |
261 | print STDERR | |
262 | "Please check your system configuration or choose another port\n\n"; | |
263 | } | |
264 | } | |
265 | ||
266 | __END__ | |
267 | ||
268 | =head1 NAME | |
269 | ||
270 | rt-server - RT standalone server | |
271 | ||
272 | =head1 SYNOPSIS | |
273 | ||
274 | # runs prefork server listening on port 8080, requires Starlet | |
275 | rt-server --port 8080 | |
276 | ||
277 | # runs server listening on port 8080 | |
278 | rt-server --server Standalone --port 8080 | |
279 | # or | |
280 | standalone_httpd --port 8080 | |
281 | ||
282 | # runs other PSGI server on port 8080 | |
283 | rt-server --server Starman --port 8080 |